Advertisement
thecplusplusguy

OpenGL tutorial 7

Jun 23rd, 2012
5,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. //you need brick.bmp in format R5G6B5
  4. #include <SDL/SDL.h>
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7.  
  8. void drawCube(float size)
  9. {
  10.     float difamb[] = {1.0,0.5,0.3,1.0};
  11.     glBegin(GL_QUADS);
  12.         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, difamb);
  13.         // front face
  14.         glNormal3f(0.0,0.0,1.0);
  15. //      glColor3f(1.0,0.0,0.0);
  16.         glVertex3f(size/2,size/2,size/2);
  17.         glVertex3f(-size/2,size/2,size/2);
  18.         glVertex3f(-size/2,-size/2,size/2);
  19.         glVertex3f(size/2,-size/2,size/2);
  20.         // left face
  21.         glNormal3f(-1.0,0.0,0.0);
  22. //      glColor3f(0.0,1.0,0.0);
  23.         glVertex3f(-size/2,size/2,size/2);
  24.         glVertex3f(-size/2,-size/2,size/2);
  25.         glVertex3f(-size/2,-size/2,-size/2);
  26.         glVertex3f(-size/2,size/2,-size/2);
  27.         // back face
  28.         glNormal3f(0.0,0.0,-1.0);
  29. //      glColor3f(0.0,0.0,1.0);
  30.         glVertex3f(size/2,size/2,-size/2);
  31.         glVertex3f(-size/2,size/2,-size/2);
  32.         glVertex3f(-size/2,-size/2,-size/2);
  33.         glVertex3f(size/2,-size/2,-size/2);
  34.         // right face
  35.         glNormal3f(1.0,0.0,0.0);
  36. //      glColor3f(1.0,1.0,0.0);
  37.         glVertex3f(size/2,size/2,size/2);
  38.         glVertex3f(size/2,-size/2,size/2);
  39.         glVertex3f(size/2,-size/2,-size/2);
  40.         glVertex3f(size/2,size/2,-size/2);
  41.         // top face
  42.         glNormal3f(0.0,1.0,0.0);
  43. //      glColor3f(1.0,0.0,1.0);
  44.         glVertex3f(size/2,size/2,size/2);
  45.         glVertex3f(-size/2,size/2,size/2);
  46.         glVertex3f(-size/2,size/2,-size/2);
  47.         glVertex3f(size/2,size/2,-size/2);
  48.         // bottom face
  49.         glNormal3f(0.0,-1.0,0.0);
  50. //      glColor3f(0.0,1.0,1.0);
  51.         glVertex3f(size/2,-size/2,size/2);
  52.         glVertex3f(-size/2,-size/2,size/2);
  53.         glVertex3f(-size/2,-size/2,-size/2);
  54.         glVertex3f(size/2,-size/2,-size/2);
  55.     glEnd();
  56. }
  57.  
  58. float angle = 0.0;
  59. const int triangle = 1;
  60.  
  61. unsigned int loadTexture(const char* filename)
  62. {
  63.     SDL_Surface* img = SDL_LoadBMP(filename);
  64.     unsigned int id;
  65.     glGenTextures(1, &id);
  66.     glBindTexture(GL_TEXTURE_2D, id);
  67.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, img->pixels);
  68.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  69.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  70.     SDL_FreeSurface(img);
  71.     return id;
  72. }
  73.  
  74. unsigned int tex;
  75.  
  76. void init()
  77. {
  78.     glClearColor(0.0,0.0,0.0,1.0);  //background color and alpha
  79.     glMatrixMode(GL_PROJECTION);
  80.     glLoadIdentity();
  81.     gluPerspective(45,640.0/480.0,1.0,500.0);
  82.     glMatrixMode(GL_MODELVIEW);
  83.     glEnable(GL_DEPTH_TEST);
  84.     glEnable(GL_TEXTURE_2D);
  85.     tex = loadTexture("brick.bmp");
  86. }
  87.  
  88. void display()
  89. {
  90.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  91.     glLoadIdentity();
  92.     glTranslatef(0.0,0.0,-5.0);
  93.     glRotatef(angle,1.0,1.0,1.0);   // angle, x-axis, y-axis, z-axis
  94.     glBindTexture(GL_TEXTURE_2D, tex);
  95.     glBegin(GL_QUADS);
  96.         glTexCoord2f(0.0,2.0);
  97.         glVertex3f(-2.0,2.0,0.0);
  98.         glTexCoord2f(0.0,0.0);
  99.         glVertex3f(-2.0,-2.0,0.0);
  100.         glTexCoord2f(2.0,0.0);
  101.         glVertex3f(2.0,-2.0,0.0);
  102.         glTexCoord2f(2.0,2.0);
  103.         glVertex3f(2.0,2.0,0.0);
  104.     glEnd();
  105. }
  106.  
  107. int main(int argc, char** argv)
  108. {
  109.     SDL_Init(SDL_INIT_EVERYTHING);
  110.     SDL_Surface *screen;
  111.     screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE|SDL_OPENGL|SDL_FULLSCREEN);
  112. //  screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_FULLSCREEN);
  113.     bool running = true;
  114.     const int FPS = 30;
  115.     Uint32 start;
  116.     SDL_Event event;
  117.     init();
  118.     while(running) {
  119.         start = SDL_GetTicks();
  120.         while(SDL_PollEvent(&event)) {
  121.             switch(event.type) {
  122.                 case SDL_QUIT:
  123.                     running = false;
  124.                     break;
  125.                 case SDL_KEYDOWN:
  126.                     switch(event.key.keysym.sym)
  127.                     {
  128.                         case SDLK_ESCAPE:
  129.                             running = false;
  130.                             break;
  131.                     }
  132.                     break;
  133.             }
  134.         }
  135.  
  136.         display();
  137.         SDL_GL_SwapBuffers();
  138.         angle += 0.5;
  139.         if(angle > 360)
  140.             angle -= 360;
  141.         if(1000/FPS > SDL_GetTicks()-start)
  142.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  143.     }
  144.     SDL_Quit();
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement