Advertisement
DimasDark

p3test

Aug 21st, 2014
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  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.     Vertex vertices[3];
  74.  
  75. } Triangle;
  76.  
  77. vector < vector<Vertex> > globalTriangles;
  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.                     sscanf (line.c_str(), "%c %f %f %f",&ch,&x,&y,&z);
  130.                     //cout << "vt x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
  131.                 }
  132.                 else if (type.compare("vn") == 0)
  133.                 {
  134.                     sscanf (line.c_str(), "%c %f %f %f",&ch,&x,&y,&z);
  135.                     //cout << "vn x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
  136.                 }
  137.                 else if (type.compare("f ") == 0)
  138.                 {
  139.                     //cout << "entrou em f" << endl;
  140.                     int temp, vnum, tnum, nnum, v2num, t2num, n2num, v3num, t3num, n3num;
  141.                     string tempString;
  142.                     temp = sscanf (line.c_str(), "%c %i/%i/%i",&ch,&vnum,&tnum,&nnum);
  143.                     //cout << "LIDO F: " << temp << endl;
  144.                     if (temp == 4)
  145.                     {
  146.                         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);
  147.                        // cout << "formato f v1/t1/n1" << endl;
  148.                         Vertex v1 = globalVertices.at(vnum);
  149.                         Vertex v2 = globalVertices.at(v2num);
  150.                         Vertex v3 = globalVertices.at(v3num);
  151.                         globalTriangles[nTriangles].push_back(v1);
  152.                         globalTriangles[nTriangles].push_back(v2);
  153.                         globalTriangles[nTriangles].push_back(v3);
  154.                         nTriangles++;
  155.                     }
  156.                     else
  157.                     {
  158.                         temp = sscanf (line.c_str(), "%c %i//%i",&ch,&vnum,&nnum);
  159.                         if (temp == 3)
  160.                         {
  161.                             //cout << "formato f v1//n1" << endl;
  162.                              temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&nnum,&v2num,&n2num, &v3num,&n3num);
  163.                             Vertex v1 = globalVertices.at(vnum);
  164.                             Vertex v2 = globalVertices.at(v2num);
  165.                             Vertex v3 = globalVertices.at(v3num);
  166.                             globalTriangles[nTriangles].push_back(v1);
  167.                             globalTriangles[nTriangles].push_back(v2);
  168.                             globalTriangles[nTriangles].push_back(v3);
  169.                             nTriangles++;
  170.                         }
  171.                         else
  172.                         {
  173.                             temp = sscanf (line.c_str(), "%c %i/%i %i/%i %i/%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num);
  174.                             if (temp == 3)
  175.                             {
  176.                                 Vertex v1 = globalVertices.at(vnum);
  177.                                 Vertex v2 = globalVertices.at(v2num);
  178.                                 Vertex v3 = globalVertices.at(v3num);
  179.                                 globalTriangles[nTriangles].push_back(v1);
  180.                                 globalTriangles[nTriangles].push_back(v2);
  181.                                 globalTriangles[nTriangles].push_back(v3);
  182.                                 nTriangles++;
  183.                                // cout << "formato f v1/t1" << endl;
  184.                             }
  185.                         }
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.     }
  191. //   glEnd();
  192. //   glEndList();
  193. //   fclose(fp);
  194. }
  195.  
  196.  
  197. //.obj loader code ends here
  198. void reshape(int w,int h)
  199. {
  200.     glViewport(0,0,w,h);
  201.     glMatrixMode(GL_PROJECTION);
  202.     glLoadIdentity();
  203.     gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
  204.     //glOrtho(-25,25,-2,2,0.1,100);
  205.     glMatrixMode(GL_MODELVIEW);
  206. }
  207.  
  208.  
  209. void drawCar()
  210. {
  211.     glPushMatrix();
  212.     glTranslatef(0,-40.00,-105);
  213.     glColor3f(1.0,0.23,0.27);
  214.     glScalef(0.1,0.1,0.1);
  215.     //glRotatef(carrot,0,1,0);
  216.     glCallList(car);
  217.     glPopMatrix();
  218.     carrot=carrot+0.6;
  219.     if(carrot>360)carrot=carrot-360;
  220. }
  221.  
  222. void display(void)
  223. {
  224.     glClearColor (0.0,0.0,0.0,1.0);
  225.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  226.     glLoadIdentity();
  227.     glBegin(GL_LINES);
  228.     glColor3f(1.0f, 0.0f, 0.0f);
  229.     cout << globalTriangles.size() << endl;
  230.     for (int i = 0; i < globalVertices.size(); i++) {
  231.         glVertex3f(globalVertices[i].getX(), globalVertices[i].getY(), globalVertices[i].getZ());
  232.     }
  233.     glEnd();
  234.     drawCar();
  235.     glutSwapBuffers(); //swap the buffers
  236. }
  237.  
  238.  
  239. int main(int argc,char **argv)
  240. {
  241.     glutInit(&argc,argv);
  242.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  243.     glutInitWindowSize(800,600);
  244.     glutInitWindowPosition(20,20);
  245.     glutCreateWindow("ObjLoader");
  246.     glutReshapeFunc(reshape);
  247.     glutDisplayFunc(display);
  248.     glutIdleFunc(display);
  249.     loadObj("./casa/casa.obj");//replace porsche.obj with radar.obj or any other .obj to display it
  250.     glutMainLoop();
  251.     return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement