Advertisement
thecplusplusguy

OpenGL tutorial 8

Jun 23rd, 2012
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. //you need test.obj file, exported from Blender with normal vectors no texture coorinates (without material)
  4. #include <SDL/SDL.h>
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7. #include <cstdlib>
  8. #include <vector>
  9. #include <string>
  10. #include <algorithm>
  11. #include <fstream>
  12. #include <cstdio>
  13. #include <iostream>
  14.  
  15. struct coordinate{
  16.     float x,y,z;
  17.     coordinate(float a, float b, float c) : x(a),y(b),z(c) {};
  18. };
  19.  
  20. struct face{
  21.     int facenum;
  22.     bool four;
  23.     int faces[4];
  24.     face(int facen, int f1, int f2, int f3) : facenum(facen) {
  25.         faces[0] = f1;
  26.         faces[1] = f2;
  27.         faces[2] = f3;
  28.         four = false;
  29.     }
  30.     face(int facen, int f1, int f2, int f3, int f4) : facenum(facen) {
  31.         faces[0] = f1;
  32.         faces[1] = f2;
  33.         faces[2] = f3;
  34.         faces[3] = f4;
  35.         four = true;
  36.     }
  37. };
  38.  
  39. float angle = 0.0;
  40.  
  41. int loadObject(const char* filename)
  42. {
  43.     std::vector<std::string*> coord;
  44.     std::vector<coordinate*> vertex;
  45.     std::vector<face*> faces;
  46.     std::vector<coordinate*> normals;
  47.     std::ifstream in(filename);
  48.     if(!in.is_open())
  49.     {
  50.         std::cout << "Not Opened" << std::endl;
  51.         return -1;
  52.     }
  53.     char buf[256];
  54.     while(!in.eof())
  55.     {
  56.         in.getline(buf, 256);
  57.         coord.push_back(new std::string(buf));
  58.     }
  59.  
  60.     for(int i = 0; i < coord.size(); i += 1)
  61.     {
  62.         if((*coord[i])[0] == '#')
  63.             continue;
  64.         else if((*coord[i])[0] == 'v' && (*coord[i])[1] == ' ')
  65.         {
  66.             float tmpx, tmpy, tmpz;
  67.             sscanf(coord[i]->c_str(), "v %f %f %f",&tmpx,&tmpy,&tmpz);
  68.             vertex.push_back(new coordinate(tmpx,tmpy,tmpz));
  69.         }
  70.         else if((*coord[i])[0]=='v' && (*coord[i])[1]=='n')
  71.         {
  72.             float tmpx,tmpy,tmpz;
  73.             sscanf(coord[i]->c_str(), "vn %f %f %f",&tmpx,&tmpy,&tmpz);
  74.             normals.push_back(new coordinate(tmpx,tmpy,tmpz));
  75.         }
  76.         else if((*coord[i])[0] == 'f')
  77.         {
  78.             int a,b,c,d,e;
  79.             if(count(coord[i]->begin(), coord[i]->end(), ' ') == 3)
  80.             {
  81.                 sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
  82.                 faces.push_back(new face(b,a,c,d));
  83.             }
  84.             else
  85.             {
  86.                 sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);
  87.                 faces.push_back(new face(b,a,c,d,e));
  88.             }
  89.         }
  90.     }
  91.     // draw
  92.     int num;
  93.     num = glGenLists(1);
  94.     glNewList(num, GL_COMPILE);
  95.     for(int i = 0; i < faces.size(); i += 1)
  96.     {
  97.         if(faces[i]->four)
  98.         {
  99.             glBegin(GL_QUADS);
  100.                 glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
  101.                 glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
  102.                 glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
  103.                 glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
  104.                 glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
  105.             glEnd();
  106.         }
  107.         else
  108.         {
  109.             glBegin(GL_TRIANGLES);
  110.                 glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
  111.                 glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
  112.                 glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
  113.                 glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
  114.             glEnd();
  115.         }
  116.     }
  117.  
  118.     glEndList();
  119.  
  120.     for(int i = 0; i < coord.size(); i += 1)
  121.         delete coord[i];
  122.     for(int i = 0; i < faces.size(); i += 1)
  123.         delete faces[i];
  124.     for(int i = 0; i < normals.size(); i += 1)
  125.         delete normals[i];
  126.     for(int i = 0; i < vertex.size(); i += 1)
  127.         delete vertex[i];
  128.  
  129.     return num;
  130. }
  131.  
  132. int cube;
  133.  
  134. void init()
  135. {
  136.     glClearColor(0.0,0.0,0.0,1.0);  //background color and alpha
  137.     glMatrixMode(GL_PROJECTION);
  138.     glLoadIdentity();
  139.     gluPerspective(45,640.0/480.0,1.0,500.0);
  140.     glMatrixMode(GL_MODELVIEW);
  141.     glEnable(GL_DEPTH_TEST);
  142.     cube = loadObject("test.obj");
  143.     glEnable(GL_LIGHTING);
  144.     glEnable(GL_LIGHT0);
  145.     float col[] = {1.0, 1.0, 1.0, 1.0};
  146.     glLightfv(GL_LIGHT0, GL_DIFFUSE, col);
  147. }
  148.  
  149. void display()
  150. {
  151.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  152.     glLoadIdentity();
  153.     float pos[] = {-1.0, 1.0, -2.0, 1.0};
  154.     glLightfv(GL_LIGHT0, GL_POSITION, pos);
  155.     glTranslatef(0.0,0.0,-5.0);
  156.     glRotatef(angle,0.0,1.0,0.0);   // angle, x-axis, y-axis, z-axis
  157.     glCallList(cube);  
  158. }
  159.  
  160. int main(int argc, char** argv)
  161. {
  162.     SDL_Init(SDL_INIT_EVERYTHING);
  163.     SDL_Surface *screen;
  164.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_OPENGL);
  165. //  screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE|SDL_OPENGL|SDL_FULLSCREEN);
  166.     bool running = true;
  167.     const int FPS = 30;
  168.     Uint32 start;
  169.     SDL_Event event;
  170.     init();
  171.     while(running) {
  172.         start = SDL_GetTicks();
  173.         while(SDL_PollEvent(&event)) {
  174.             switch(event.type) {
  175.                 case SDL_QUIT:
  176.                     running = false;
  177.                     break;
  178.                 case SDL_KEYDOWN:
  179.                     switch(event.key.keysym.sym)
  180.                     {
  181.                         case SDLK_ESCAPE:
  182.                             running = false;
  183.                             break;
  184.                     }
  185.                     break;
  186.             }
  187.         }
  188.  
  189.         display();
  190.         SDL_GL_SwapBuffers();
  191.         angle += 0.5;
  192.         if(angle > 360)
  193.             angle -= 360;
  194.         if(1000/FPS > SDL_GetTicks()-start)
  195.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  196.     }
  197.     SDL_Quit();
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement