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;
- //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 (int,int,int);
- Vertex();
- ~Vertex();
- double getX(void);
- double getY(void);
- double getZ(void);
- };
- Vertex::Vertex(int nx, int ny, int 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
- {
- Vertex vertices[3];
- } Triangle;
- vector < vector<Vertex> > globalTriangles;
- //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)
- {
- sscanf (line.c_str(), "%c %f %f %f",&ch,&x,&y,&z);
- //cout << "vt x y z " << " x: "<< x << "y: " << y << "z: " << z << endl;
- }
- else if (type.compare("vn") == 0)
- {
- sscanf (line.c_str(), "%c %f %f %f",&ch,&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;
- string tempString;
- temp = sscanf (line.c_str(), "%c %i/%i/%i",&ch,&vnum,&tnum,&nnum);
- //cout << "LIDO F: " << temp << endl;
- if (temp == 4)
- {
- 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);
- // cout << "formato f v1/t1/n1" << endl;
- Vertex v1 = globalVertices.at(vnum);
- Vertex v2 = globalVertices.at(v2num);
- Vertex v3 = globalVertices.at(v3num);
- globalTriangles[nTriangles].push_back(v1);
- globalTriangles[nTriangles].push_back(v2);
- globalTriangles[nTriangles].push_back(v3);
- nTriangles++;
- }
- else
- {
- temp = sscanf (line.c_str(), "%c %i//%i",&ch,&vnum,&nnum);
- if (temp == 3)
- {
- //cout << "formato f v1//n1" << endl;
- temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&nnum,&v2num,&n2num, &v3num,&n3num);
- Vertex v1 = globalVertices.at(vnum);
- Vertex v2 = globalVertices.at(v2num);
- Vertex v3 = globalVertices.at(v3num);
- globalTriangles[nTriangles].push_back(v1);
- globalTriangles[nTriangles].push_back(v2);
- globalTriangles[nTriangles].push_back(v3);
- nTriangles++;
- }
- else
- {
- temp = sscanf (line.c_str(), "%c %i/%i %i/%i %i/%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num);
- if (temp == 3)
- {
- Vertex v1 = globalVertices.at(vnum);
- Vertex v2 = globalVertices.at(v2num);
- Vertex v3 = globalVertices.at(v3num);
- globalTriangles[nTriangles].push_back(v1);
- globalTriangles[nTriangles].push_back(v2);
- globalTriangles[nTriangles].push_back(v3);
- nTriangles++;
- // cout << "formato f v1/t1" << 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 (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
- //glOrtho(-25,25,-2,2,0.1,100);
- glMatrixMode(GL_MODELVIEW);
- }
- void drawCar()
- {
- 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();
- glBegin(GL_LINES);
- glColor3f(1.0f, 0.0f, 0.0f);
- cout << globalTriangles.size() << endl;
- for (int i = 0; i < globalVertices.size(); i++) {
- glVertex3f(globalVertices[i].getX(), globalVertices[i].getY(), globalVertices[i].getZ());
- }
- glEnd();
- drawCar();
- glutSwapBuffers(); //swap the buffers
- }
- int main(int argc,char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
- glutInitWindowSize(800,600);
- glutInitWindowPosition(20,20);
- glutCreateWindow("ObjLoader");
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutIdleFunc(display);
- loadObj("./casa/casa.obj");//replace porsche.obj with radar.obj or any other .obj to display it
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement