Advertisement
thecplusplusguy

OpenGL tutorial 9 functions.cpp

Jun 23rd, 2012
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. #include "functions.h"
  4.  
  5. coordinate::coordinate(float a, float b, float c) {
  6.     x = a;
  7.     y = b;
  8.     z = c;
  9. }
  10.  
  11. face::face(int facen, int f1, int f2, int f3) {
  12.     facenum = facen;
  13.     faces[0] = f1;
  14.     faces[1] = f2;
  15.     faces[2] = f3;
  16.     four = false;
  17. }
  18.  
  19. face::face(int facen, int f1, int f2, int f3, int f4) {
  20.     facenum = facen;
  21.     faces[0] = f1;
  22.     faces[1] = f2;
  23.     faces[2] = f3;
  24.     faces[3] = f4;
  25.     four = true;
  26. }
  27.  
  28. int loadObject(const char* filename)
  29. {
  30.     std::vector<std::string*> coord;
  31.     std::vector<coordinate*> vertex;
  32.     std::vector<face*> faces;
  33.     std::vector<coordinate*> normals;
  34.     std::ifstream in(filename);
  35.     if(!in.is_open())
  36.     {
  37.         std::cout << "Not Opened" << std::endl;
  38.         return -1;
  39.     }
  40.     char buf[256];
  41.     while(!in.eof())
  42.     {
  43.         in.getline(buf, 256);
  44.         coord.push_back(new std::string(buf));
  45.     }
  46.  
  47.     for(int i = 0; i < coord.size(); i += 1)
  48.     {
  49.         if((*coord[i])[0] == '#')
  50.             continue;
  51.         else if((*coord[i])[0] == 'v' && (*coord[i])[1] == ' ')
  52.         {
  53.             float tmpx, tmpy, tmpz;
  54.             sscanf(coord[i]->c_str(), "v %f %f %f",&tmpx,&tmpy,&tmpz);
  55.             vertex.push_back(new coordinate(tmpx,tmpy,tmpz));
  56.         }
  57.         else if((*coord[i])[0]=='v' && (*coord[i])[1]=='n')
  58.         {
  59.             float tmpx,tmpy,tmpz;
  60.             sscanf(coord[i]->c_str(), "vn %f %f %f",&tmpx,&tmpy,&tmpz);
  61.             normals.push_back(new coordinate(tmpx,tmpy,tmpz));
  62.         }
  63.         else if((*coord[i])[0] == 'f')
  64.         {
  65.             int a,b,c,d,e;
  66.             if(count(coord[i]->begin(), coord[i]->end(), ' ') == 3)
  67.             {
  68.                 sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
  69.                 faces.push_back(new face(b,a,c,d));
  70.             }
  71.             else
  72.             {
  73.                 sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);
  74.                 faces.push_back(new face(b,a,c,d,e));
  75.             }
  76.         }
  77.     }
  78.     // draw
  79.     int num;
  80.     num = glGenLists(1);
  81.     glNewList(num, GL_COMPILE);
  82.     for(int i = 0; i < faces.size(); i += 1)
  83.     {
  84.         if(faces[i]->four)
  85.         {
  86.             glBegin(GL_QUADS);
  87.                 glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
  88.                 glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
  89.                 glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
  90.                 glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
  91.                 glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
  92.             glEnd();
  93.         }
  94.         else
  95.         {
  96.             glBegin(GL_TRIANGLES);
  97.                 glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
  98.                 glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
  99.                 glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
  100.                 glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
  101.             glEnd();
  102.         }
  103.     }
  104.  
  105.     glEndList();
  106.  
  107.     for(int i = 0; i < coord.size(); i += 1)
  108.         delete coord[i];
  109.     for(int i = 0; i < faces.size(); i += 1)
  110.         delete faces[i];
  111.     for(int i = 0; i < normals.size(); i += 1)
  112.         delete normals[i];
  113.     for(int i = 0; i < vertex.size(); i += 1)
  114.         delete vertex[i];
  115.  
  116.     return num;
  117. }
  118.  
  119. void drawCube(float size)
  120. {
  121.     glBegin(GL_QUADS);
  122.         // front face
  123.         glColor3f(1.0,0.0,0.0);
  124.         glVertex3f(size/2,size/2,size/2);
  125.         glVertex3f(-size/2,size/2,size/2);
  126.         glVertex3f(-size/2,-size/2,size/2);
  127.         glVertex3f(size/2,-size/2,size/2);
  128.         // left face
  129.         glColor3f(0.0,1.0,0.0);
  130.         glVertex3f(-size/2,size/2,size/2);
  131.         glVertex3f(-size/2,-size/2,size/2);
  132.         glVertex3f(-size/2,-size/2,-size/2);
  133.         glVertex3f(-size/2,size/2,-size/2);
  134.         // back face
  135.         glColor3f(0.0,0.0,1.0);
  136.         glVertex3f(size/2,size/2,-size/2);
  137.         glVertex3f(-size/2,size/2,-size/2);
  138.         glVertex3f(-size/2,-size/2,-size/2);
  139.         glVertex3f(size/2,-size/2,-size/2);
  140.         // right face
  141.         glColor3f(1.0,1.0,0.0);
  142.         glVertex3f(size/2,size/2,size/2);
  143.         glVertex3f(size/2,-size/2,size/2);
  144.         glVertex3f(size/2,-size/2,-size/2);
  145.         glVertex3f(size/2,size/2,-size/2);
  146.         // top face
  147.         glColor3f(1.0,0.0,1.0);
  148.         glVertex3f(size/2,size/2,size/2);
  149.         glVertex3f(-size/2,size/2,size/2);
  150.         glVertex3f(-size/2,size/2,-size/2);
  151.         glVertex3f(size/2,size/2,-size/2);
  152.         // bottom face
  153.         glColor3f(0.0,1.0,1.0);
  154.         glVertex3f(size/2,-size/2,size/2);
  155.         glVertex3f(-size/2,-size/2,size/2);
  156.         glVertex3f(-size/2,-size/2,-size/2);
  157.         glVertex3f(size/2,-size/2,-size/2);
  158.     glEnd();
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement