Advertisement
thecplusplusguy

OpenGL texture loading example

Sep 24th, 2012
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //multiple texture loading
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_image.h>
  6. #include <GL/gl.h>
  7. #include <GL/glu.h>
  8.  
  9. unsigned int loadTexture(const char* name)
  10. {
  11.     SDL_Surface* img=IMG_Load(name);
  12.     SDL_PixelFormat form={NULL,32,4,0,0,0,0,8,8,8,8,0xff000000,0x00ff0000,0x0000ff00,0x000000ff,0,255};
  13. //  std::cout << img->w << " " << img->h << std::endl;
  14.     SDL_Surface* img2=SDL_ConvertSurface(img,&form,SDL_SWSURFACE);
  15.     unsigned int texture;
  16.     glGenTextures(1,&texture);
  17.     glBindTexture(GL_TEXTURE_2D,texture);
  18.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
  19.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  20.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  21.     std::cout << glGetError() << std::endl;
  22.     SDL_FreeSurface(img);
  23.     SDL_FreeSurface(img2);
  24.     return texture;
  25. }
  26. unsigned int tex[2];
  27. int curTex=0;
  28. void init()
  29. {
  30.     glEnable(GL_TEXTURE_2D);
  31.     glClearColor(0,0,1,1);
  32.     glMatrixMode(GL_PROJECTION);
  33.         glLoadIdentity();
  34.         gluPerspective(50,640.0/480.0,0.1,1000);
  35.     glMatrixMode(GL_MODELVIEW);
  36.     tex[0]=loadTexture("img.png");
  37.     tex[1]=loadTexture("img2.png");
  38.  
  39. }
  40.  
  41.  
  42. void display()
  43. {
  44.     glClear(GL_COLOR_BUFFER_BIT);
  45.     glLoadIdentity();
  46.     glRotatef(180,0,0,1);
  47.     glBindTexture(GL_TEXTURE_2D,tex[curTex]);
  48.     glBegin(GL_QUADS);
  49.         glTexCoord2f(0,1);
  50.         glVertex3f(-2,2,-4);
  51.         glTexCoord2f(0,0);
  52.         glVertex3f(-2,-2,-4);
  53.         glTexCoord2f(1,0);
  54.         glVertex3f(2,-2,-4);
  55.         glTexCoord2f(1,1);
  56.         glVertex3f(2,2,-4);
  57.     glEnd();
  58. }
  59.  
  60. int main()
  61. {
  62.     SDL_Init(SDL_INIT_EVERYTHING);
  63.     SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  64.     bool running=true;
  65.     SDL_Event event;
  66.     Uint32 start;
  67.     init();
  68.     while(running)
  69.     {
  70.         start=SDL_GetTicks();
  71.         while(SDL_PollEvent(&event))
  72.         {
  73.             switch(event.type)
  74.             {
  75.                 case SDL_QUIT:
  76.                     running=false;
  77.                     break;
  78.                 case SDL_KEYDOWN:
  79.                     switch(event.key.keysym.sym)
  80.                     {
  81.                         case SDLK_ESCAPE:
  82.                             running=false;
  83.                             break;
  84.                         case SDLK_SPACE:
  85.                             curTex=!curTex;
  86.                             break;
  87.                     }
  88.                     break;
  89.                 case SDL_MOUSEBUTTONDOWN:
  90.                     break;
  91.             }
  92.         }
  93.         display();
  94.         //SDL_BlitSurface(img2,NULL,screen,NULL);
  95.         //SDL_Flip(screen);
  96.         SDL_GL_SwapBuffers();
  97.         if(1000.0/30>(SDL_GetTicks()-start))
  98.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  99.     }
  100.     SDL_Quit();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement