Guest User

OpenGL Texture using SDL and FreeImage

a guest
Aug 20th, 2012
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1.  
  2. #include <SDL2/SDL.h>
  3. #include <SDL2/SDL_opengl.h>
  4. #include <FreeImage.h>
  5.  
  6. #include <stdio.h>
  7.  
  8. int main( int argc, char *argv[] )
  9. {
  10.     SDL_Init( SDL_INIT_EVERYTHING );
  11.     FreeImage_Initialise();
  12.  
  13.     SDL_Window *window = NULL;
  14.     unsigned int windowWidth = 640;
  15.     unsigned int windowHeight = 480;
  16.     SDL_GLContext context = NULL;
  17.  
  18.     window = SDL_CreateWindow( "Test",
  19.                    SDL_WINDOWPOS_UNDEFINED,
  20.                    SDL_WINDOWPOS_UNDEFINED,
  21.                    windowWidth,
  22.                    windowHeight,
  23.                    SDL_WINDOW_OPENGL );
  24.     context = SDL_GL_CreateContext( window );
  25.  
  26.     glMatrixMode( GL_PROJECTION );
  27.     glLoadIdentity();
  28.     glOrtho( 0, windowWidth, windowHeight, 0, -1, 1 );
  29.     glMatrixMode( GL_MODELVIEW );
  30.     glLoadIdentity();
  31.  
  32.     glEnable( GL_TEXTURE_2D );
  33.     glEnable( GL_DEPTH_TEST );
  34.  
  35.     const char *filename = "rockstar.jpg";
  36.     FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
  37.     fif = FreeImage_GetFileType( filename, 0 );
  38.     if ( fif == FIF_UNKNOWN )
  39.         fprintf( stderr, "Couldn't get the file type!" );
  40.     FIBITMAP *bitmap = FreeImage_Load( fif, filename );
  41.     if ( !bitmap )
  42.         fprintf( stderr, "Couldn't load the image!" );
  43.  
  44.     unsigned int imageWidth = 0;
  45.     unsigned int imageHeight = 0;
  46.     BYTE *imageBits = 0;
  47.  
  48.     imageWidth = FreeImage_GetWidth(bitmap);
  49.     imageHeight = FreeImage_GetHeight(bitmap);
  50.     imageBits = FreeImage_GetBits(bitmap);
  51.     fprintf( stdout, "%u %u %p\n", imageWidth, imageHeight, imageBits );
  52.  
  53.     GLuint texId;
  54.     glGenTextures( 1, &texId );
  55.     glBindTexture( GL_TEXTURE_2D, texId );
  56.     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth,
  57.                 imageHeight, 0, GL_RGB,
  58.                 GL_UNSIGNED_BYTE, imageBits );
  59.  
  60.     if ( bitmap )
  61.         FreeImage_Unload( bitmap );
  62.  
  63.     bool running = true;
  64.     while ( running ) {
  65.         SDL_Event event;
  66.         while ( SDL_PollEvent(&event) )
  67.             if ( event.type == SDL_QUIT )
  68.                 running = false;
  69.         glBindTexture( GL_TEXTURE_2D, texId );
  70.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  71.         glBegin( GL_QUADS );
  72.         glTexCoord2f( 0.0f, 1.0f ); glVertex2f( 0.0f, 0.0f );
  73.         glTexCoord2f( 1.0f, 1.0f ); glVertex2f( imageWidth, 0.0f );
  74.         glTexCoord2f( 1.0f, 0.0f ); glVertex2f( imageWidth, imageHeight );
  75.         glTexCoord2f( 0.0f, 0.0f ); glVertex2f( 0.0f, imageHeight );
  76.         glEnd();
  77.  
  78.         SDL_GL_SwapWindow( window );
  79.         SDL_Delay( 16 );
  80.     }
  81.  
  82.     SDL_GL_DeleteContext( context );
  83.     SDL_DestroyWindow( window );
  84.  
  85.     FreeImage_DeInitialise();
  86.     SDL_Quit();
  87. }
Add Comment
Please, Sign In to add comment