genpfault

SDL Test

Sep 27th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. // for http://stackoverflow.com/questions/19011990/odd-happenings-of-opengl-texturing
  2. #include <GL/glew.h>
  3. #include <SDL/SDL.h>
  4. #include <string>
  5.  
  6. GLuint loadTexture( std::string filepath )
  7. {
  8.     SDL_Surface* image = SDL_LoadBMP( filepath.c_str() );
  9.     if( image == NULL )
  10.         return 0;
  11.  
  12.     GLuint tex_id = 0;
  13.     glGenTextures( 1, &tex_id );
  14.     glBindTexture( GL_TEXTURE_2D, tex_id );
  15.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  16.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  17.     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
  18.     glTexImage2D
  19.         (
  20.         GL_TEXTURE_2D, 0,
  21.         GL_RGB, image->w, image->h, 0,
  22.         GL_BGR, GL_UNSIGNED_BYTE, image->pixels
  23.         );
  24.     SDL_FreeSurface( image );
  25.     glBindTexture( GL_TEXTURE_2D, 0 );
  26.  
  27.     return tex_id;
  28. }
  29.  
  30. int main( int argc, char** argv )
  31. {
  32.     SDL_Init( SDL_INIT_EVERYTHING );
  33.     SDL_Surface* disp = SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
  34.     glewInit();
  35.  
  36.     GLuint tex = loadTexture( "image.bmp" );
  37.  
  38.     if( GL_TRUE != glIsTexture( tex ) )
  39.         return -1;
  40.  
  41.     bool running = true;
  42.     while( running )
  43.     {
  44.         SDL_Event ev;
  45.         while( SDL_PollEvent( &ev ) )
  46.         {
  47.             if( ev.type == SDL_QUIT )    
  48.                 running = false;
  49.         }
  50.  
  51.         glMatrixMode( GL_PROJECTION );
  52.         glLoadIdentity();
  53.         double w = disp->w;
  54.         double h = disp->h;
  55.         double ar = w / h;
  56.         glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1 );
  57.  
  58.         glMatrixMode( GL_MODELVIEW );
  59.         glLoadIdentity();
  60.  
  61.         glEnable( GL_TEXTURE_2D );
  62.         glBindTexture( GL_TEXTURE_2D, tex );
  63.  
  64.         glColor3ub( 0, 255, 0 );
  65.  
  66.         glBegin( GL_QUADS );
  67.         glTexCoord2i( 0, 0 );
  68.         glVertex2i( -1, -1 );
  69.         glTexCoord2i( 1, 0 );
  70.         glVertex2i(  1, -1 );
  71.         glTexCoord2i( 1, 1 );
  72.         glVertex2i(  1,  1 );
  73.         glTexCoord2i( 0, 1 );
  74.         glVertex2i( -1,  1 );
  75.         glEnd();
  76.  
  77.         SDL_GL_SwapBuffers();
  78.     }
  79.  
  80.     SDL_Quit();
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment