Advertisement
DimasDark

Untitled

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