SHOW:
|
|
- or go back to the newest paste.
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 | - | class Vertex { |
30 | + | |
31 | // double x; //num de vertices | |
32 | - | public: |
32 | + | |
33 | - | Vertex (int,int,int); |
33 | + | |
34 | // double faces; //qtd faces | |
35 | // | |
36 | //} | |
37 | //Vertex; | |
38 | ||
39 | class Vertex | |
40 | { | |
41 | - | Vertex::Vertex(int nx, int ny, int nz) { |
41 | + | |
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 | - | Vertex::Vertex(void){ |
52 | + | |
53 | x = nx; | |
54 | y = ny; | |
55 | z = nz; | |
56 | - | double Vertex::getX(void){ |
56 | + | |
57 | ||
58 | Vertex::~Vertex(void) | |
59 | { | |
60 | - | double Vertex::getY(void){ |
60 | + | |
61 | } | |
62 | ||
63 | Vertex::Vertex(void) | |
64 | - | double Vertex::getZ(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 | - | int globalTriangles[680000][3]; |
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 | - | type = line.substr(0,2); |
115 | + | |
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 | - | int temp, vnum, tnum, nnum, v2num, t2num, n2num, v3num, t3num, n3num; |
142 | + | |
143 | { | |
144 | - | temp = sscanf (line.c_str(), "%c %i/%i/%i",&ch,&vnum,&tnum,&nnum); |
144 | + | |
145 | - | //cout << "LIDO F: " << temp << endl; |
145 | + | |
146 | - | if (temp == 4) |
146 | + | |
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); |
148 | + | |
149 | - | // cout << "formato f v1/t1/n1" << endl; |
149 | + | |
150 | - | // Vertex v1 = globalVertices.at(vnum); |
150 | + | |
151 | - | // Vertex v2 = globalVertices.at(v2num); |
151 | + | |
152 | - | // Vertex v3 = globalVertices.at(v3num); |
152 | + | |
153 | - | // globalTriangles[nTriangles].push_back(v1); |
153 | + | |
154 | - | // globalTriangles[nTriangles].push_back(v2); |
154 | + | |
155 | - | // globalTriangles[nTriangles].push_back(v3); |
155 | + | |
156 | - | // nTriangles++; |
156 | + | |
157 | - | globalTriangles[nTriangles][0] = vnum; |
157 | + | int temp, vnum, tnum, nnum, v2num, t2num, n2num, v3num, t3num, n3num, v4num, t4num; |
158 | - | globalTriangles[nTriangles][1] = v2num; |
158 | + | |
159 | - | globalTriangles[nTriangles][2] = v3num; |
159 | + | temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num); |
160 | - | nTriangles++; |
160 | + | if (temp == 7) |
161 | { | |
162 | globalTriangles[nTriangles][0] = 3; | |
163 | globalTriangles[nTriangles][1] = vnum-1; | |
164 | - | temp = sscanf (line.c_str(), "%c %i//%i",&ch,&vnum,&nnum); |
164 | + | globalTriangles[nTriangles][2] = v2num-1; |
165 | - | if (temp == 3) |
165 | + | globalTriangles[nTriangles][3] = v3num-1; |
166 | - | { |
166 | + | nTriangles++; |
167 | - | //cout << "formato f v1//n1" << endl; |
167 | + | |
168 | - | temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&nnum,&v2num,&n2num, &v3num,&n3num); |
168 | + | |
169 | - | // Vertex v1 = globalVertices.at(vnum); |
169 | + | |
170 | - | // Vertex v2 = globalVertices.at(v2num); |
170 | + | temp = sscanf (line.c_str(), "%c %i//%i %i//%i %i//%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num, &v4num, &t3num); |
171 | - | // Vertex v3 = globalVertices.at(v3num); |
171 | + | globalTriangles[nTriangles][0] = 4; |
172 | - | // globalTriangles[nTriangles].push_back(v1); |
172 | + | globalTriangles[nTriangles][1] = vnum-1; |
173 | - | // globalTriangles[nTriangles].push_back(v2); |
173 | + | globalTriangles[nTriangles][2] = v2num-1; |
174 | - | // globalTriangles[nTriangles].push_back(v3); |
174 | + | globalTriangles[nTriangles][3] = v3num-1; |
175 | - | // nTriangles++; |
175 | + | globalTriangles[nTriangles][4] = v4num-1; |
176 | - | globalTriangles[nTriangles][0] = vnum; |
176 | + | nTriangles++; |
177 | - | globalTriangles[nTriangles][1] = v2num; |
177 | + | |
178 | - | globalTriangles[nTriangles][2] = v3num; |
178 | + | //cout << "LIDO F: " << temp << " " << ch << "v1: " << vnum << "/ " << tnum << "v2: "<< v2num << "/ " << t2num << "v3: "<< v3num << "/ " << t3num << endl; |
179 | - | nTriangles++; |
179 | + | |
180 | - | } |
180 | + | |
181 | - | else |
181 | + | |
182 | - | { |
182 | + | |
183 | - | temp = sscanf (line.c_str(), "%c %i/%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num); |
183 | + | |
184 | - | if (temp == 3) |
184 | + | |
185 | - | { |
185 | + | |
186 | - | temp = sscanf (line.c_str(), "%c %i/%i/%i",&ch,&vnum,&tnum, &v2num, &t2num, &v3num, &t3num); |
186 | + | |
187 | - | // Vertex v1 = globalVertices.at(vnum); |
187 | + | |
188 | - | // Vertex v2 = globalVertices.at(v2num); |
188 | + | |
189 | - | // Vertex v3 = globalVertices.at(v3num); |
189 | + | |
190 | - | // globalTriangles[nTriangles].push_back(v1); |
190 | + | |
191 | - | // globalTriangles[nTriangles].push_back(v2); |
191 | + | |
192 | - | // globalTriangles[nTriangles].push_back(v3); |
192 | + | |
193 | - | // nTriangles++; |
193 | + | |
194 | - | // cout << "formato f v1/t1" << endl; |
194 | + | |
195 | - | globalTriangles[nTriangles][0] = vnum; |
195 | + | |
196 | - | globalTriangles[nTriangles][1] = v2num; |
196 | + | |
197 | - | globalTriangles[nTriangles][2] = v3num; |
197 | + | |
198 | - | nTriangles++; |
198 | + | gluPerspective (135, (GLfloat)w / (GLfloat)h, 0.1, 10000.0); |
199 | - | } |
199 | + | |
200 | - | } |
200 | + | |
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 | - | gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0); |
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 | - | glPushMatrix(); |
226 | + | |
227 | - | glTranslatef(0,-40.00,-105); |
227 | + | // if (globalTriangles[i][0] == 3) { |
228 | - | glColor3f(1.0,0.23,0.27); |
228 | + | // glBegin(GL_TRIANGLES); |
229 | - | glScalef(0.1,0.1,0.1); |
229 | + | // Vertex v0 = globalVertices[globalTriangles[i][1]]; |
230 | - | //glRotatef(carrot,0,1,0); |
230 | + | // Vertex v1 = globalVertices[globalTriangles[i][2]]; |
231 | - | glCallList(car); |
231 | + | // Vertex v2 = globalVertices[globalTriangles[i][3]]; |
232 | - | glPopMatrix(); |
232 | + | |
233 | - | carrot=carrot+0.6; |
233 | + | // glVertex3f(v0.getX(), v0.getY(), v0.getZ()); |
234 | - | if(carrot>360)carrot=carrot-360; |
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 | - | glColor3f(1.0f, 0.0f, 0.0f); |
243 | + | // Vertex v3 = globalVertices[globalTriangles[i][4]]; |
244 | // glBegin(GL_QUADS); | |
245 | - | for (int i = 0; i < nTriangles; i++) { |
245 | + | // glVertex3f(v0.getX(), v0.getY(), v0.getZ()); |
246 | // glVertex3f(v1.getX(), v1.getY(), v1.getZ()); | |
247 | - | Vertex v0 = globalVertices[globalTriangles[i][0]]; |
247 | + | // glVertex3f(v2.getX(), v2.getY(), v2.getZ()); |
248 | - | Vertex v1 = globalVertices[globalTriangles[i][1]]; |
248 | + | // glVertex3f(v3.getX(), v3.getY(), v3.getZ()); |
249 | - | Vertex v2 = globalVertices[globalTriangles[i][2]]; |
249 | + | // glEnd(); |
250 | - | glBegin(GL_TRIANGLES); |
250 | + | // } |
251 | - | glVertex3f(v0.getX(), v0.getY(), v0.getZ()); |
251 | + | |
252 | - | glVertex3f(v1.getX(), v1.getY(), v1.getZ()); |
252 | + | Vertex v0 = globalVertices[i]; |
253 | - | glVertex3f(v2.getX(), v2.getY(), v2.getZ()); |
253 | + | Vertex v1 = globalVertices[i]; |
254 | - | glEnd(); |
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 | - | glutInitWindowSize(800,600); |
266 | + | glTranslatef(0,-40.00,-105); |
267 | - | glutInitWindowPosition(20,20); |
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 | - | loadObj("./paredesSimples.obj");//replace porsche.obj with radar.obj or any other .obj to display it |
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 | } |