Advertisement
DimasDark

Untitled

Aug 21st, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include<GL/gl.h>
  3. #include<GL/glut.h>
  4. #include<stdio.h>
  5. #include <vector>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12.  
  13. //globals
  14.  
  15.  
  16. GLuint car;
  17. float carrot;
  18. int nTriangles;
  19.  
  20. //typedef struct Vertex
  21. //{
  22. //    double x; //num de vertices
  23. //    double y; //qtd de posições
  24. //    double z; //texturas
  25. //    double faces; //qtd faces
  26. //
  27. //}
  28. //Vertex;
  29.  
  30. class Vertex {
  31.     double x, y,z;
  32.   public:
  33.     Vertex (int,int,int);
  34.     Vertex();
  35.     ~Vertex();
  36.     double getX(void);
  37.     double getY(void);
  38.     double getZ(void);
  39. };
  40.  
  41. Vertex::Vertex(int nx, int ny, int nz) {
  42.     x = nx;
  43.     y = ny;
  44.     z = nz;
  45. }
  46.  
  47. Vertex::~Vertex(void)
  48. {
  49.  
  50. }
  51.  
  52. Vertex::Vertex(void){
  53.  
  54. }
  55.  
  56. double Vertex::getX(void){
  57.     return x;
  58. }
  59.  
  60. double Vertex::getY(void){
  61.     return y;
  62. }
  63.  
  64. double Vertex::getZ(void){
  65.     return z;
  66. }
  67.  
  68.  
  69. vector <Vertex> globalVertices;
  70.  
  71. typedef struct Triangle
  72. {
  73.  
  74.  
  75. } Triangle;
  76.  
  77. int globalTriangles[680000][3];
  78.  
  79. //other functions and main
  80. //.obj loader code
  81.  
  82.  
  83. void loadObj(char *fname)
  84. {
  85. //   FILE *fp;
  86.     int read;
  87.     GLfloat x, y, z;
  88.     int vindex = 0,vtindex = 0,vnindex = 0;
  89.     char ch;
  90.     car=glGenLists(1);
  91.     streampos Position;
  92.     streamoff Offset=0;
  93.     ifstream fp (fname);
  94. //   fstream InputFile(fname,ios::in);
  95. //   fp=f(fname,"r");
  96. //
  97.  
  98.     if (!fp)
  99.     {
  100.         printf("can't open file %s\n", fname);
  101.         exit(1);
  102.     }
  103. //    filebuf * InputBuffer=InputFile.rdbuf();
  104.     glPointSize(2.0);
  105. //    glPushMatrix();
  106. //    glBegin(GL_TRIANGLES);
  107.     nTriangles = 0;
  108.     while(!fp.eof())
  109.     {
  110.         string type;
  111.         string line;
  112.  
  113.         while (getline(fp, line))
  114.     {
  115.         type = line.substr(0,2);
  116.             //cout << "linha: " << line << endl;
  117.  
  118.             if (type != "")
  119.             {
  120.                 if(type.compare("v ") == 0)
  121.                 {
  122.                     sscanf (line.c_str(), "%c %f %f %f",&ch,&x,&y,&z);
  123.                     Vertex v(x,y,z);
  124.                     globalVertices.push_back(v);
  125.                     //cout << "vertex" << " x: "<< x << "y: " << y << "z: " << z << endl;
  126.                 }
  127.                 else if (type.compare("vt") == 0)
  128.                 {
  129.                     char tempc;
  130.                     sscanf (line.c_str(), "%c%c %f %f %f",&ch, &tempc, &x,&y,&z);
  131.                     //cout << "vt x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
  132.                 }
  133.                 else if (type.compare("vn") == 0)
  134.                 {
  135.                     char tempc;
  136.                     sscanf (line.c_str(), "%c%c %f %f %f",&ch,&tempc, &x,&y,&z);
  137.                     //cout << "vn x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
  138.                 }
  139.                 else if (type.compare("f ") == 0)
  140.                 {
  141.                     //cout << "entrou em f" << endl;
  142.                     int temp, vnum, tnum, nnum, v2num, t2num, n2num, v3num, t3num, n3num;
  143.                     string tempString;
  144.                     temp = sscanf (line.c_str(), "%c %i/%i/%i",&ch,&vnum,&tnum,&nnum);
  145.                     //cout << "LIDO F: " << temp << endl;
  146.                     if (temp == 4)
  147.                     {
  148.                         temp = sscanf (line.c_str(), "%c %i/%i/%i %i/%i/%i %i/%i/%i",&ch,&vnum,&tnum,&nnum,&v2num,&t2num,&n2num, &v3num, &t3num, &n3num);
  149.                        // cout << "formato f v1/t1/n1" << endl;
  150. //                        Vertex v1 = globalVertices.at(vnum);
  151. //                        Vertex v2 = globalVertices.at(v2num);
  152. //                        Vertex v3 = globalVertices.at(v3num);
  153. //                        globalTriangles[nTriangles].push_back(v1);
  154. //                        globalTriangles[nTriangles].push_back(v2);
  155. //                        globalTriangles[nTriangles].push_back(v3);
  156. //                        nTriangles++;
  157.                           globalTriangles[nTriangles][0] = vnum;
  158.                           globalTriangles[nTriangles][1] = v2num;
  159.                           globalTriangles[nTriangles][2] = v3num;
  160.                           nTriangles++;
  161.                     }
  162.                     else
  163.                     {
  164.                         temp = sscanf (line.c_str(), "%c %i//%i",&ch,&vnum,&nnum);
  165.                         if (temp == 3)
  166.                         {
  167.                             //cout << "formato f v1//n1" << endl;
  168.                              temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&nnum,&v2num,&n2num, &v3num,&n3num);
  169. //                            Vertex v1 = globalVertices.at(vnum);
  170. //                            Vertex v2 = globalVertices.at(v2num);
  171. //                            Vertex v3 = globalVertices.at(v3num);
  172. //                            globalTriangles[nTriangles].push_back(v1);
  173. //                            globalTriangles[nTriangles].push_back(v2);
  174. //                            globalTriangles[nTriangles].push_back(v3);
  175. //                            nTriangles++;
  176.                             globalTriangles[nTriangles][0] = vnum;
  177.                             globalTriangles[nTriangles][1] = v2num;
  178.                             globalTriangles[nTriangles][2] = v3num;
  179.                             nTriangles++;
  180.                         }
  181.                         else
  182.                         {
  183.                             temp = sscanf (line.c_str(), "%c %i/%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num);
  184.                             if (temp == 3)
  185.                             {
  186.                                 temp = sscanf (line.c_str(), "%c %i/%i/%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num);
  187. //                                Vertex v1 = globalVertices.at(vnum);
  188. //                                Vertex v2 = globalVertices.at(v2num);
  189. //                                Vertex v3 = globalVertices.at(v3num);
  190. //                                globalTriangles[nTriangles].push_back(v1);
  191. //                                globalTriangles[nTriangles].push_back(v2);
  192. //                                globalTriangles[nTriangles].push_back(v3);
  193. //                                nTriangles++;
  194.                                // cout << "formato f v1/t1" << endl;
  195.                             globalTriangles[nTriangles][0] = vnum;
  196.                             globalTriangles[nTriangles][1] = v2num;
  197.                             globalTriangles[nTriangles][2] = v3num;
  198.                             nTriangles++;
  199.                             }
  200.                         }
  201.                     }
  202.                 }
  203.             }
  204.         }
  205.     }
  206. //   glEnd();
  207. //   glEndList();
  208. //   fclose(fp);
  209. }
  210.  
  211.  
  212. //.obj loader code ends here
  213. void reshape(int w,int h)
  214. {
  215.     glViewport(0,0,w,h);
  216.     glMatrixMode(GL_PROJECTION);
  217.     glLoadIdentity();
  218.     gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
  219.     //glOrtho(-25,25,-2,2,0.1,100);
  220.     glMatrixMode(GL_MODELVIEW);
  221. }
  222.  
  223.  
  224. void drawCar()
  225. {
  226.     glPushMatrix();
  227.     glTranslatef(0,-40.00,-105);
  228.     glColor3f(1.0,0.23,0.27);
  229.     glScalef(0.1,0.1,0.1);
  230.     //glRotatef(carrot,0,1,0);
  231.     glCallList(car);
  232.     glPopMatrix();
  233.     carrot=carrot+0.6;
  234.     if(carrot>360)carrot=carrot-360;
  235. }
  236.  
  237. void display(void)
  238. {
  239.     glClearColor (0.0,0.0,0.0,1.0);
  240.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  241.     glLoadIdentity();
  242.  
  243.     glColor3f(1.0f, 0.0f, 0.0f);
  244. //    cout << globalTriangles.size() << endl;
  245.     for (int i = 0; i < nTriangles; i++) {
  246.  
  247.         Vertex v0 = globalVertices[globalTriangles[i][0]];
  248.         Vertex v1 = globalVertices[globalTriangles[i][1]];
  249.         Vertex v2 = globalVertices[globalTriangles[i][2]];
  250.         glBegin(GL_TRIANGLES);
  251.         glVertex3f(v0.getX(), v0.getY(), v0.getZ());
  252.         glVertex3f(v1.getX(), v1.getY(), v1.getZ());
  253.         glVertex3f(v2.getX(), v2.getY(), v2.getZ());
  254.         glEnd();
  255.     }
  256.  
  257.     drawCar();
  258.     glutSwapBuffers(); //swap the buffers
  259. }
  260.  
  261.  
  262. int main(int argc,char **argv)
  263. {
  264.     glutInit(&argc,argv);
  265.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  266.     glutInitWindowSize(800,600);
  267.     glutInitWindowPosition(20,20);
  268.     glutCreateWindow("ObjLoader");
  269.     glutReshapeFunc(reshape);
  270.     glutDisplayFunc(display);
  271.     glutIdleFunc(display);
  272.     loadObj("./paredesSimples.obj");//replace porsche.obj with radar.obj or any other .obj to display it
  273.     glutMainLoop();
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement