Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // for http://stackoverflow.com/questions/19011990/odd-happenings-of-opengl-texturing
- #include <GL/glew.h>
- #include <SDL/SDL.h>
- #include <string>
- GLuint loadTexture( std::string filepath )
- {
- SDL_Surface* image = SDL_LoadBMP( filepath.c_str() );
- if( image == NULL )
- return 0;
- GLuint tex_id = 0;
- glGenTextures( 1, &tex_id );
- glBindTexture( GL_TEXTURE_2D, tex_id );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
- glTexImage2D
- (
- GL_TEXTURE_2D, 0,
- GL_RGB, image->w, image->h, 0,
- GL_BGR, GL_UNSIGNED_BYTE, image->pixels
- );
- SDL_FreeSurface( image );
- glBindTexture( GL_TEXTURE_2D, 0 );
- return tex_id;
- }
- int main( int argc, char** argv )
- {
- SDL_Init( SDL_INIT_EVERYTHING );
- SDL_Surface* disp = SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
- glewInit();
- GLuint tex = loadTexture( "image.bmp" );
- if( GL_TRUE != glIsTexture( tex ) )
- return -1;
- bool running = true;
- while( running )
- {
- SDL_Event ev;
- while( SDL_PollEvent( &ev ) )
- {
- if( ev.type == SDL_QUIT )
- running = false;
- }
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- double w = disp->w;
- double h = disp->h;
- double ar = w / h;
- glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glEnable( GL_TEXTURE_2D );
- glBindTexture( GL_TEXTURE_2D, tex );
- glColor3ub( 0, 255, 0 );
- glBegin( GL_QUADS );
- glTexCoord2i( 0, 0 );
- glVertex2i( -1, -1 );
- glTexCoord2i( 1, 0 );
- glVertex2i( 1, -1 );
- glTexCoord2i( 1, 1 );
- glVertex2i( 1, 1 );
- glTexCoord2i( 0, 1 );
- glVertex2i( -1, 1 );
- glEnd();
- SDL_GL_SwapBuffers();
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment