Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<GL/gl.h>
- #include<GL/glut.h>
- #include<stdio.h>
- #include <vector>
- #include <fstream>
- using namespace std;
- GLfloat X = 0.0f; // Translate screen to x direction (left or right)
- GLfloat Y = 0.0f; // Translate screen to y direction (up or down)
- GLfloat Z = 0.0f; // Translate screen to z direction (zoom in or out)
- GLfloat rotX = 0.0f; // Rotate screen on x axis
- GLfloat rotY = 0.0f; // Rotate screen on y axis
- GLfloat rotZ = 0.0f; // Rotate screen on z axis
- GLfloat rotLx = 0.0f; // Translate screen by using the glulookAt function (left or right)
- GLfloat rotLy = 0.0f; // Translate screen by using the glulookAt function (up or down)
- GLfloat rotLz = 0.0f; // Translate screen by using the glulookAt function (zoom in or out)
- float deltaAngle = 0.0f;
- int xOrigin = -1;
- //globals
- GLuint car;
- float carrot;
- int nTriangles;
- //typedef struct Vertex
- //{
- // double x; //num de vertices
- // double y; //qtd de posições
- // double z; //texturas
- // double faces; //qtd faces
- //
- //}
- //Vertex;
- class Vertex
- {
- double x, y,z;
- public:
- Vertex (double,double,double);
- Vertex();
- ~Vertex();
- double getX(void);
- double getY(void);
- double getZ(void);
- };
- Vertex::Vertex(double nx, double ny, double nz)
- {
- x = nx;
- y = ny;
- z = nz;
- }
- Vertex::~Vertex(void)
- {
- }
- Vertex::Vertex(void)
- {
- }
- double Vertex::getX(void)
- {
- return x;
- }
- double Vertex::getY(void)
- {
- return y;
- }
- double Vertex::getZ(void)
- {
- return z;
- }
- vector <Vertex> globalVertices;
- typedef struct Triangle
- {
- } Triangle;
- int globalTriangles[680000][5];
- //other functions and main
- //.obj loader code
- void loadObj(char *fname)
- {
- // FILE *fp;
- int read;
- GLfloat x, y, z;
- int vindex = 0,vtindex = 0,vnindex = 0;
- char ch;
- car=glGenLists(1);
- streampos Position;
- streamoff Offset=0;
- ifstream fp (fname);
- // fstream InputFile(fname,ios::in);
- // fp=f(fname,"r");
- //
- if (!fp)
- {
- printf("can't open file %s\n", fname);
- exit(1);
- }
- // filebuf * InputBuffer=InputFile.rdbuf();
- glPointSize(2.0);
- // glPushMatrix();
- // glBegin(GL_TRIANGLES);
- nTriangles = 0;
- while(!fp.eof())
- {
- string type;
- string line;
- while (getline(fp, line))
- {
- type = line.substr(0,2);
- //cout << "linha: " << line << endl;
- if (type != "")
- {
- if(type.compare("v ") == 0)
- {
- sscanf (line.c_str(), "%c %f %f %f",&ch,&x,&y,&z);
- Vertex v(x,y,z);
- globalVertices.push_back(v);
- //cout << "vertex" << " x: "<< x << "y: " << y << "z: " << z << endl;
- }
- else if (type.compare("vt") == 0)
- {
- char tempc;
- sscanf (line.c_str(), "%c%c %f %f %f",&ch, &tempc, &x,&y,&z);
- //cout << "vt x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
- }
- else if (type.compare("vn") == 0)
- {
- char tempc;
- sscanf (line.c_str(), "%c%c %f %f %f",&ch,&tempc, &x,&y,&z);
- //cout << "vn x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
- }
- else if (type.compare("f ") == 0)
- {
- //cout << "entrou em f" << endl;
- int temp, vnum, tnum, nnum, v2num, t2num, n2num, v3num, t3num, n3num, v4num, t4num;
- string tempString;
- temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num);
- if (temp == 7)
- {
- globalTriangles[nTriangles][0] = 3;
- globalTriangles[nTriangles][1] = vnum-1;
- globalTriangles[nTriangles][2] = v2num-1;
- globalTriangles[nTriangles][3] = v3num-1;
- nTriangles++;
- }
- else
- {
- temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num, &v4num, &t3num);
- globalTriangles[nTriangles][0] = 4;
- globalTriangles[nTriangles][1] = vnum-1;
- globalTriangles[nTriangles][2] = v2num-1;
- globalTriangles[nTriangles][3] = v3num-1;
- globalTriangles[nTriangles][4] = v4num-1;
- nTriangles++;
- }
- //cout << "LIDO F: " << temp << " " << ch << "v1: " << vnum << "/ " << tnum << "v2: "<< v2num << "/ " << t2num << "v3: "<< v3num << "/ " << t3num << endl;
- }
- }
- }
- }
- // glEnd();
- // glEndList();
- // fclose(fp);
- }
- //.obj loader code ends here
- void reshape(int w,int h)
- {
- glViewport(0,0,w,h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective (135, (GLfloat)w / (GLfloat)h, 0.1, 10000.0);
- //glOrtho(-25,25,-2,2,0.1,100);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt (1.0, 1000.0, 10.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0);
- }
- void drawCar()
- {
- glPointSize(2.0);
- glNewList(car, GL_COMPILE);
- {
- {
- glPushMatrix();
- // cout << globalTriangles.size() << endl;
- glColor3f(1.0f, 0.0f, 0.0f);
- glBegin(GL_POINTS);
- // Vertex v0 = globalVertices[globalTriangles[0][1]];
- // Vertex v1 = globalVertices[globalTriangles[0][2]];
- // Vertex v2 = globalVertices[globalTriangles[0][3]];
- // cout << v0.getX() << " " << " " << v0.getY() << " " << v0.getZ() << endl;
- // cout << v1.getX() << " " << " " << v1.getY() << " " << v1.getZ() << endl;
- // cout << v2.getX() << " " << " " << v2.getY() << " " << v2.getZ() << endl;
- for (int i = 0; i < globalVertices.size(); i++)
- {
- // for (int i = 0; i < nTriangles; i++)
- // {
- // if (globalTriangles[i][0] == 3) {
- // glBegin(GL_TRIANGLES);
- // Vertex v0 = globalVertices[globalTriangles[i][1]];
- // Vertex v1 = globalVertices[globalTriangles[i][2]];
- // Vertex v2 = globalVertices[globalTriangles[i][3]];
- //
- // glVertex3f(v0.getX(), v0.getY(), v0.getZ());
- // glVertex3f(v1.getX(), v1.getY(), v1.getZ());
- // glVertex3f(v2.getX(), v2.getY(), v2.getZ());
- // glEnd();
- // }
- // else if (globalTriangles[i][0] == 4) {
- //
- // Vertex v0 = globalVertices[globalTriangles[i][1]];
- // Vertex v1 = globalVertices[globalTriangles[i][2]];
- // Vertex v2 = globalVertices[globalTriangles[i][3]];
- // Vertex v3 = globalVertices[globalTriangles[i][4]];
- // glBegin(GL_QUADS);
- // glVertex3f(v0.getX(), v0.getY(), v0.getZ());
- // glVertex3f(v1.getX(), v1.getY(), v1.getZ());
- // glVertex3f(v2.getX(), v2.getY(), v2.getZ());
- // glVertex3f(v3.getX(), v3.getY(), v3.getZ());
- // glEnd();
- // }
- Vertex v0 = globalVertices[i];
- Vertex v1 = globalVertices[i];
- Vertex v2 = globalVertices[i];
- glVertex3f(v0.getX(), v0.getY(), v0.getZ());
- glVertex3f(v1.getX(), v1.getY(), v1.getZ());
- glVertex3f(v2.getX(), v2.getY(), v2.getZ());
- }
- glEnd();
- glPopMatrix();
- glEndList();
- glPushMatrix();
- glTranslatef(0,-40.00,-105);
- glColor3f(1.0,0.23,0.27);
- glScalef(0.1,0.1,0.1);
- //glRotatef(carrot,0,1,0);
- glCallList(car);
- glPopMatrix();
- carrot=carrot+0.6;
- if(carrot>360)carrot=carrot-360;
- }
- }
- }
- void display(void)
- {
- glClearColor (0.0,0.0,0.0,1.0);
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- drawCar();
- glutSwapBuffers(); //swap the buffers
- }
- int main(int argc,char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
- glutInitWindowSize(460,300);
- //glutInitWindowPosition(20,20);
- glutCreateWindow("ObjLoader");
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutIdleFunc(display);
- loadObj("./casa/casa.obj");
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement