Advertisement
thecplusplusguy

OpenGL tutorial 22 - mipmap

Jul 30th, 2012
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //mip-map demonstration, press enter, to change the mipmapping
  3. //create an image called chess.jpg (with gimp: create a new image with size 512x512, go to filter/render/pattern/checkerboard, and set the size to 128,then save it)
  4. #include <iostream>
  5. #include <SDL/SDL.h>
  6. #include <SDL/SDL_image.h>  //you need SDL_image to compile this
  7. #include "GLee.h"   //you may also need GLee
  8. #include <GL/gl.h>
  9. #include <GL/glu.h>
  10. #include "camera.h" //the camera class from earlier
  11. #include <vector>
  12.  
  13. camera cam;
  14.  
  15. unsigned int loadTexture(const char* filename,bool generate=false)
  16. {
  17.     unsigned int num;
  18.     glGenTextures(1,&num);
  19.     SDL_Surface* img=IMG_Load(filename);
  20.     if(img==NULL)
  21.     {
  22.         std::cout << "img was not loaded" << std::endl;
  23.         return -1;
  24.     }
  25.     SDL_PixelFormat form={NULL,32,4,0,0,0,0,0,0,0,0,0xff000000,0x00ff0000,0x0000ff00,0x000000ff,0,255};
  26.     SDL_Surface* img2=SDL_ConvertSurface(img,&form,SDL_SWSURFACE);
  27.     if(img2==NULL)
  28.     {
  29.         std::cout << "img2 was not loaded" << std::endl;
  30.         return -1;     
  31.     }
  32.     glBindTexture(GL_TEXTURE_2D,num);      
  33.    
  34.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  35.    
  36.     //opengl 1.4 way:
  37.     //if(generate)
  38.     //  glTexParameteri(GL_TEXTURE_2D,GL_GENERATE_MIPMAP,GL_TRUE);
  39.    
  40.     //openGL 1.1 way:
  41.     if(generate)
  42.         gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,img2->w,img2->h,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
  43.     else
  44.         glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
  45.    
  46.     if(generate)
  47.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  48.     else
  49.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  50.        
  51. //  if(generate)
  52. //      glGenerateMipmap(GL_TEXTURE_2D);    //OpenGL 3.0+ way
  53.        
  54.     SDL_FreeSurface(img);
  55.     SDL_FreeSurface(img2);
  56.     return num;
  57. }
  58.  
  59. unsigned int myImg,myImg2,current;
  60. void init()
  61. {
  62.     glClearColor(0,0,0,1);
  63.     glMatrixMode(GL_PROJECTION);
  64.         glLoadIdentity();
  65.         gluPerspective(50,640.0/480.0,1,1000);
  66.     glMatrixMode(GL_MODELVIEW);
  67.     glEnable(GL_DEPTH_TEST);
  68.     current=myImg=loadTexture("chess.jpg",true);
  69.     myImg2=loadTexture("chess.jpg",false);
  70. }
  71.  
  72. void display()
  73. {
  74.     glLoadIdentity();
  75.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  76.     cam.Control();
  77.     cam.UpdateCamera();
  78.     glEnable(GL_TEXTURE_2D);
  79.     glBindTexture(GL_TEXTURE_2D,current);
  80.     glBegin(GL_QUADS);
  81.         glTexCoord2f(100,0);
  82.         glVertex3f(-200,-3,200);
  83.         glTexCoord2f(0,0);
  84.         glVertex3f(-200,-3,-200);
  85.         glTexCoord2f(0,100);
  86.         glVertex3f(200,-3,-200);
  87.         glTexCoord2f(100,100);
  88.         glVertex3f(200,-3,200);
  89.     glEnd();
  90.    
  91. }
  92.  
  93. int main()
  94. {
  95.     SDL_Init(SDL_INIT_EVERYTHING);
  96.     SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  97.     Uint32 start;
  98.     SDL_Event event;
  99.     bool running=true;
  100.     init();
  101.     bool b=false;
  102.     while(running)
  103.     {
  104.         start=SDL_GetTicks();
  105.         while(SDL_PollEvent(&event))
  106.         {
  107.             switch(event.type)
  108.             {
  109.                 case SDL_QUIT:
  110.                     running=false;
  111.                     break;
  112.                 case SDL_KEYDOWN:
  113.                     switch(event.key.keysym.sym)
  114.                     {
  115.                         case SDLK_ESCAPE:
  116.                             running=false;
  117.                             break;
  118.                         case SDLK_RETURN:
  119.                             if(current==myImg)
  120.                                 current=myImg2;
  121.                             else
  122.                                 current=myImg;
  123.                             break;
  124.                     }
  125.                     break;
  126.                 case SDL_MOUSEBUTTONDOWN:
  127.                     cam.mouseIn(true);
  128.                     break;
  129.                    
  130.             }
  131.         }
  132.         display();
  133.         SDL_GL_SwapBuffers();
  134.         if(1000.0/30>SDL_GetTicks()-start)
  135.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  136.     }
  137.     SDL_Quit();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement