1 | #include "GeoMeshView.h"
|
---|
2 |
|
---|
3 | using namespace Geometry;
|
---|
4 | using namespace std;
|
---|
5 |
|
---|
6 | //---------------------------------------------------------------------------
|
---|
7 | // Hanle events.
|
---|
8 | //---------------------------------------------------------------------------
|
---|
9 | int GeoMeshView::handle(int event)
|
---|
10 | {
|
---|
11 | int key;
|
---|
12 | int button;
|
---|
13 | int x;
|
---|
14 | int y;
|
---|
15 |
|
---|
16 | if (geoMesh != NULL)
|
---|
17 | {
|
---|
18 | draw();
|
---|
19 | }
|
---|
20 |
|
---|
21 | switch(event)
|
---|
22 | {
|
---|
23 | case fltk::KEY:
|
---|
24 | case fltk::PUSH:
|
---|
25 |
|
---|
26 | button = fltk::event_key();
|
---|
27 | mMouseX = fltk::event_x();
|
---|
28 | mMouseY = fltk::event_y();
|
---|
29 |
|
---|
30 | return 1;
|
---|
31 |
|
---|
32 | case fltk::DRAG:
|
---|
33 |
|
---|
34 | button = fltk::event_state();
|
---|
35 | x = fltk::event_x();
|
---|
36 | y = fltk::event_y();
|
---|
37 |
|
---|
38 | switch (button)
|
---|
39 | {
|
---|
40 | // Button 1 Pushed.
|
---|
41 | case 0x01100000:
|
---|
42 |
|
---|
43 | if (x > mMouseX)
|
---|
44 | {
|
---|
45 | if (mPan)
|
---|
46 | {
|
---|
47 | xshift = xshift + (x - mMouseX);
|
---|
48 | }
|
---|
49 | else
|
---|
50 | {
|
---|
51 | hAng = hAng + (x - mMouseX);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | if (x < mMouseX)
|
---|
57 | {
|
---|
58 | if (mPan)
|
---|
59 | {
|
---|
60 | xshift = xshift - (mMouseX - x);
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | hAng = hAng - (mMouseX - x);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (y > mMouseY)
|
---|
70 | {
|
---|
71 | if (mPan)
|
---|
72 | {
|
---|
73 | yshift = yshift - (y - mMouseY);
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | vAng = vAng + (y - mMouseY);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | if (y < mMouseY)
|
---|
83 | {
|
---|
84 | if (mPan)
|
---|
85 | {
|
---|
86 | yshift = yshift + (mMouseY - y);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | vAng = vAng - (mMouseY - y);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | hAng = float(int(hAng) % 360);
|
---|
96 | vAng = float(int(vAng) % 360);
|
---|
97 |
|
---|
98 | mMouseX = x;
|
---|
99 | mMouseY = y;
|
---|
100 |
|
---|
101 | draw();
|
---|
102 |
|
---|
103 | return 1;
|
---|
104 |
|
---|
105 | case 0x04100000:
|
---|
106 |
|
---|
107 | if (y > mSizeY)
|
---|
108 | {
|
---|
109 | size++;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | if (y < mSizeY)
|
---|
114 | {
|
---|
115 | size--;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | mSizeY = y;
|
---|
120 |
|
---|
121 | draw();
|
---|
122 |
|
---|
123 | return 1;
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | case fltk::MOUSEWHEEL:
|
---|
128 |
|
---|
129 | size = size + fltk::event_dy();
|
---|
130 | draw();
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return fltk::GlWindow::handle(event);
|
---|
135 | }
|
---|
136 |
|
---|
137 | //---------------------------------------------------------------------------
|
---|
138 | // Get the number of frames per second.
|
---|
139 | //---------------------------------------------------------------------------
|
---|
140 | size_t GeoMeshView::getFPS()
|
---|
141 | {
|
---|
142 | return mFPS;
|
---|
143 | }
|
---|
144 |
|
---|
145 | //---------------------------------------------------------------------------
|
---|
146 | // Calculate the number of frames per second.
|
---|
147 | //---------------------------------------------------------------------------
|
---|
148 | void GeoMeshView::calcFPS()
|
---|
149 | {
|
---|
150 | double duration;
|
---|
151 | clock_t finish_time;
|
---|
152 |
|
---|
153 | // Gets the time.
|
---|
154 | finish_time = clock();
|
---|
155 |
|
---|
156 | // Calc the duration respect the start time.
|
---|
157 | duration = (double)((finish_time - mStartTime) / CLOCKS_PER_SEC);
|
---|
158 |
|
---|
159 | // Increments the number of frames per second.
|
---|
160 | mFPS++;
|
---|
161 |
|
---|
162 | // If a second have passed.
|
---|
163 | if (duration >= 1.0)
|
---|
164 | {
|
---|
165 | // Repaint the FPS.
|
---|
166 | mGeoMeshViewUI->refreshFPS(mFPS);
|
---|
167 |
|
---|
168 | mFPS = 0;
|
---|
169 | mStartTime = clock();
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | //---------------------------------------------------------------------------
|
---|
174 | // Enable color strips.
|
---|
175 | //---------------------------------------------------------------------------
|
---|
176 | void GeoMeshView::enableColorStrips()
|
---|
177 | {
|
---|
178 | colorStripsFlag = true;
|
---|
179 | }
|
---|
180 |
|
---|
181 | //---------------------------------------------------------------------------
|
---|
182 | // Disable color strips.
|
---|
183 | //---------------------------------------------------------------------------
|
---|
184 | void GeoMeshView::disableColorStrips()
|
---|
185 | {
|
---|
186 | colorStripsFlag = false;
|
---|
187 | }
|
---|
188 |
|
---|
189 | //---------------------------------------------------------------------------
|
---|
190 | // Gets the color strips state.
|
---|
191 | //---------------------------------------------------------------------------
|
---|
192 | bool GeoMeshView::getColorStrips()
|
---|
193 | {
|
---|
194 | return colorStripsFlag;
|
---|
195 | }
|
---|
196 |
|
---|
197 | //---------------------------------------------------------------------------
|
---|
198 | // JGA
|
---|
199 | // Set the color to the submesh sumbmeshIndex
|
---|
200 | //---------------------------------------------------------------------------
|
---|
201 | void GeoMeshView::setColorSubmeshi(int submeshIndex)
|
---|
202 | {
|
---|
203 | colorsubmeshi = submeshIndex;
|
---|
204 | }
|
---|
205 |
|
---|
206 | //---------------------------------------------------------------------------
|
---|
207 | // Sets the leaves submesh index.
|
---|
208 | //---------------------------------------------------------------------------
|
---|
209 | void GeoMeshView::setLeavesSubmesh(int submeshIndex)
|
---|
210 | {
|
---|
211 | leavesSubmesh = submeshIndex;
|
---|
212 | }
|
---|
213 |
|
---|
214 | //---------------------------------------------------------------------------
|
---|
215 | // Gets the leaves submesh index.
|
---|
216 | //---------------------------------------------------------------------------
|
---|
217 | int GeoMeshView::getLeavesSubmesh()
|
---|
218 | {
|
---|
219 | return leavesSubmesh;
|
---|
220 | }
|
---|
221 |
|
---|
222 | //---------------------------------------------------------------------------
|
---|
223 | // Set the list of strip colors.
|
---|
224 | //---------------------------------------------------------------------------
|
---|
225 | void GeoMeshView::setStripColors()
|
---|
226 | {
|
---|
227 | int total_strips;
|
---|
228 |
|
---|
229 | total_strips = 0;
|
---|
230 |
|
---|
231 | // Delete previous color strips.
|
---|
232 | delete []mStripColors;
|
---|
233 |
|
---|
234 | // Count the total number of strips y the submeshes.
|
---|
235 | for (int i = 0; i < geoMesh->mSubMeshCount; i++)
|
---|
236 | {
|
---|
237 | total_strips = total_strips + geoMesh->mSubMesh[i].mStripCount;
|
---|
238 | }
|
---|
239 |
|
---|
240 | // Initialize the Colors array.
|
---|
241 | mStripColors = new ColorElement[total_strips];
|
---|
242 |
|
---|
243 | // Sets random colors to each strip.
|
---|
244 | for (int i = 0; i < total_strips; i++)
|
---|
245 | {
|
---|
246 | mStripColors[i].r = rand()/((float)RAND_MAX);
|
---|
247 | mStripColors[i].g = rand()/((float)RAND_MAX);
|
---|
248 | mStripColors[i].b = rand()/((float)RAND_MAX);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | //---------------------------------------------------------------------------
|
---|
253 | // Get the bounding box of the object.
|
---|
254 | //---------------------------------------------------------------------------
|
---|
255 | GeometryBounds GeoMeshView::getBoundingBox()
|
---|
256 | {
|
---|
257 | SubMesh *geosubmesh;
|
---|
258 | Index position;
|
---|
259 | Vector3 vector3;
|
---|
260 | GeometryBounds bounds;
|
---|
261 | float x,y,z;
|
---|
262 | float xMin,yMin,zMin;
|
---|
263 | float xMax,yMax,zMax;
|
---|
264 |
|
---|
265 | // For each submesh.
|
---|
266 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
267 | {
|
---|
268 | // Gets the actual submesh.
|
---|
269 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
270 |
|
---|
271 | // For each index of the strip.
|
---|
272 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
273 | {
|
---|
274 | position = geosubmesh->mIndex[index];
|
---|
275 |
|
---|
276 | // Gets the vertex coordinates.
|
---|
277 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
278 |
|
---|
279 | x = vector3[0];
|
---|
280 | y = vector3[1];
|
---|
281 | z = vector3[2];
|
---|
282 |
|
---|
283 | // If is the first iteation.
|
---|
284 | if ((submesh == 0) && (index == 0))
|
---|
285 | {
|
---|
286 | xMin = xMax = x;
|
---|
287 | yMin = yMax = y;
|
---|
288 | zMin = zMax = z;
|
---|
289 | }
|
---|
290 | else
|
---|
291 | {
|
---|
292 | if (x < xMin)
|
---|
293 | {
|
---|
294 | xMin = x;
|
---|
295 | }
|
---|
296 | else if (x > xMax)
|
---|
297 | {
|
---|
298 | xMax = x;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (y < yMin)
|
---|
302 | {
|
---|
303 | yMin = y;
|
---|
304 | }
|
---|
305 | else if (y > yMax)
|
---|
306 | {
|
---|
307 | yMax = y;
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (z < zMin)
|
---|
311 | {
|
---|
312 | zMin = z;
|
---|
313 | }
|
---|
314 | else if (z > zMax)
|
---|
315 | {
|
---|
316 | zMax = z;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | xMed = (xMax + xMin) / 2;
|
---|
323 | yMed = (yMax + yMin) / 2;
|
---|
324 | zMed = (zMax + zMin) / 2;
|
---|
325 |
|
---|
326 |
|
---|
327 | if ((xMax - xMin) > (yMax - yMin))
|
---|
328 | {
|
---|
329 | if ((xMax - xMin) > (zMax - zMin))
|
---|
330 | {
|
---|
331 | mScaleFactor = xMax - xMin;
|
---|
332 | }
|
---|
333 | else
|
---|
334 | {
|
---|
335 | mScaleFactor = zMax - zMin;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | if ((yMax - yMin) > (zMax - zMin))
|
---|
341 | {
|
---|
342 | mScaleFactor = yMax - yMin;
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | mScaleFactor = zMax - zMin;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | // Set the bounds.
|
---|
351 | bounds.minx = xMin;
|
---|
352 | bounds.miny = yMin;
|
---|
353 | bounds.minz = zMin;
|
---|
354 | bounds.maxx = xMax;
|
---|
355 | bounds.maxy = yMax;
|
---|
356 | bounds.maxz = zMax;
|
---|
357 | bounds.radius = mScaleFactor;
|
---|
358 |
|
---|
359 | return bounds;
|
---|
360 | }
|
---|
361 |
|
---|
362 | //---------------------------------------------------------------------------
|
---|
363 | // Constructor.
|
---|
364 | //---------------------------------------------------------------------------
|
---|
365 | GeoMeshView::GeoMeshView( int x,
|
---|
366 | int y,
|
---|
367 | int w,
|
---|
368 | int h,
|
---|
369 | const char *l,
|
---|
370 | GeoMeshViewUI *geoMeshViewUI)
|
---|
371 | : fltk::GlWindow(x,y,w,h,l)
|
---|
372 | {
|
---|
373 | mStripColors = NULL;
|
---|
374 | mSharedPosArray = NULL;
|
---|
375 | mSharedNorArray = NULL;
|
---|
376 | mPosArray = NULL;
|
---|
377 | mNorArray = NULL;
|
---|
378 | mIndexArray = NULL;
|
---|
379 | vAng = 0.0;
|
---|
380 | hAng = 0.0;
|
---|
381 | size = 0.0;
|
---|
382 | mSizeY = 0;
|
---|
383 | xshift = 0.0;
|
---|
384 | yshift = 0.0;
|
---|
385 | geoMesh = 0;
|
---|
386 | mRotate = true;
|
---|
387 | mPan = false;
|
---|
388 | mLodStrip = false;
|
---|
389 | mIdVisualList = 0;
|
---|
390 | mSubMeshCount = 0;
|
---|
391 |
|
---|
392 | // Initialize the model view.
|
---|
393 | deactiveWire();
|
---|
394 | deactiveSolid();
|
---|
395 | activeRotate();
|
---|
396 | deactiveCW();
|
---|
397 | activeCCW();
|
---|
398 |
|
---|
399 | // Init start time for FPS.
|
---|
400 | mStartTime = clock();
|
---|
401 |
|
---|
402 | // Cross Reference.
|
---|
403 | mGeoMeshViewUI = geoMeshViewUI;
|
---|
404 | }
|
---|
405 |
|
---|
406 | // The color used for the edges of the bounding cube.
|
---|
407 | #define CUBECOLOR 255,255,255,255
|
---|
408 |
|
---|
409 | /* Draw a colored cube */
|
---|
410 | #define ALPHA 0.5
|
---|
411 |
|
---|
412 | //---------------------------------------------------------------------------
|
---|
413 | // Sets the Mesh to display.
|
---|
414 | //---------------------------------------------------------------------------
|
---|
415 | void GeoMeshView::setMesh(Mesh *geomesh)
|
---|
416 | {
|
---|
417 | GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
418 |
|
---|
419 | this->geoMesh = geomesh;
|
---|
420 | colorsubmeshi = -1;
|
---|
421 | leavesSubmesh = -1;
|
---|
422 |
|
---|
423 | // Refresh vertices to the vertex array.
|
---|
424 | refreshVertices(geomesh);
|
---|
425 |
|
---|
426 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
427 | }
|
---|
428 |
|
---|
429 | //---------------------------------------------------------------------------
|
---|
430 | // Refresh vertices to the vertex array.
|
---|
431 | //---------------------------------------------------------------------------
|
---|
432 | void GeoMeshView::refreshVertices(Mesh *geomesh)
|
---|
433 | {
|
---|
434 | SubMesh *geosubmesh;
|
---|
435 | VertexBuffer *vertex_buffer;
|
---|
436 |
|
---|
437 | // If the vertex array exists.
|
---|
438 | if (mSubMeshCount > 0)
|
---|
439 | {
|
---|
440 | for (int submesh = 0; submesh < mSubMeshCount; submesh++)
|
---|
441 | {
|
---|
442 | delete [] mPosArray[submesh];
|
---|
443 | delete [] mNorArray[submesh];
|
---|
444 | delete [] mIndexArray[submesh];
|
---|
445 | }
|
---|
446 |
|
---|
447 | delete [] mSharedPosArray;
|
---|
448 | delete [] mSharedNorArray;
|
---|
449 | delete [] mPosArray;
|
---|
450 | delete [] mNorArray;
|
---|
451 | delete [] mIndexArray;
|
---|
452 | }
|
---|
453 |
|
---|
454 | // If there is shared vertex.
|
---|
455 | if (geomesh->mVertexBuffer != NULL)
|
---|
456 | {
|
---|
457 | vertex_buffer = geomesh->mVertexBuffer;
|
---|
458 |
|
---|
459 | // Allocate memory.
|
---|
460 | mSharedPosArray = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
461 | mSharedNorArray = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
462 |
|
---|
463 | for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
|
---|
464 | {
|
---|
465 | mSharedPosArray[3 * vertex] = vertex_buffer->mPosition[vertex].x;
|
---|
466 | mSharedPosArray[(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y;
|
---|
467 | mSharedPosArray[(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z;
|
---|
468 |
|
---|
469 | mSharedNorArray[3 * vertex] = vertex_buffer->mNormal[vertex].x;
|
---|
470 | mSharedNorArray[(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y;
|
---|
471 | mSharedNorArray[(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z;
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | // Build the arrays of vertices and indices.
|
---|
476 | mPosArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
477 | mNorArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
478 | mIndexArray = new GLuint*[geomesh->mSubMeshCount];
|
---|
479 |
|
---|
480 | // For each submesh.
|
---|
481 | for (int submesh = 0; submesh < geomesh->mSubMeshCount; submesh++)
|
---|
482 | {
|
---|
483 | // Gets the actual submesh.
|
---|
484 | geosubmesh = &geomesh->mSubMesh[submesh];
|
---|
485 |
|
---|
486 | // Allocate memory for index array of the actual submesh.
|
---|
487 | mIndexArray[submesh] = new GLuint[geosubmesh->mIndexCount];
|
---|
488 |
|
---|
489 | // Gets the indices of the actual submesh.
|
---|
490 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
491 | {
|
---|
492 | mIndexArray[submesh][index] = geosubmesh->mIndex[index];
|
---|
493 | }
|
---|
494 |
|
---|
495 | // If the submesh is NOT shared vertex.
|
---|
496 | if (!geosubmesh->mSharedVertexBuffer)
|
---|
497 | {
|
---|
498 | vertex_buffer = geosubmesh->mVertexBuffer;
|
---|
499 |
|
---|
500 | // Allocate memory for vertices of the actual submesh.
|
---|
501 | mPosArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
502 | mNorArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
503 |
|
---|
504 | // For each one of the vertex.
|
---|
505 | for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
|
---|
506 | {
|
---|
507 | mPosArray[submesh][3 * vertex] = vertex_buffer->mPosition[vertex].x;
|
---|
508 | mPosArray[submesh][(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y;
|
---|
509 | mPosArray[submesh][(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z;
|
---|
510 |
|
---|
511 | mNorArray[submesh][3 * vertex] = vertex_buffer->mNormal[vertex].x;
|
---|
512 | mNorArray[submesh][(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y;
|
---|
513 | mNorArray[submesh][(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z;
|
---|
514 | }// End for each vertex.
|
---|
515 | }
|
---|
516 | // If the submesh is shared vertex.
|
---|
517 | else
|
---|
518 | {
|
---|
519 | mPosArray[submesh] = NULL;
|
---|
520 | mNorArray[submesh] = NULL;
|
---|
521 | }
|
---|
522 |
|
---|
523 | }// End for each submesh.
|
---|
524 |
|
---|
525 | }
|
---|
526 |
|
---|
527 | //---------------------------------------------------------------------------
|
---|
528 | // Gets the triangle count of the current lod.
|
---|
529 | //---------------------------------------------------------------------------
|
---|
530 | size_t GeoMeshView::getLodStripsTriangleCount()
|
---|
531 | {
|
---|
532 | size_t triangle_count;
|
---|
533 |
|
---|
534 | // Initialize triangle count.
|
---|
535 | triangle_count = 0;
|
---|
536 |
|
---|
537 | // For each strip.
|
---|
538 | for (int strip = 0; strip < lodStripsLib->GetStripCount(); strip++)
|
---|
539 | {
|
---|
540 | triangle_count += lodStripsLib->mStrips[strip].size() - 2;
|
---|
541 | }
|
---|
542 |
|
---|
543 | return triangle_count;
|
---|
544 | }
|
---|
545 |
|
---|
546 | //---------------------------------------------------------------------------
|
---|
547 | // Repaint the Mesh.
|
---|
548 | //---------------------------------------------------------------------------
|
---|
549 | void GeoMeshView::drawGeoMesh(int selectedMesh)
|
---|
550 | {
|
---|
551 | if (geoMesh)
|
---|
552 | {
|
---|
553 | if (mLodStrip)
|
---|
554 | {
|
---|
555 | drawLodStrip();
|
---|
556 | }
|
---|
557 | // Choose the render operation.
|
---|
558 | else if (geoMesh->mType == GEO_TRIANGLE_LIST)
|
---|
559 | {
|
---|
560 | // Paint vertices.
|
---|
561 | drawTriangleList(selectedMesh);
|
---|
562 | }
|
---|
563 | else
|
---|
564 | {
|
---|
565 | // Paint vertices.
|
---|
566 | drawTriangleStrip();
|
---|
567 | }
|
---|
568 | }
|
---|
569 | }// End drawGeoMesh.
|
---|
570 |
|
---|
571 | //---------------------------------------------------------------------------
|
---|
572 | // Paint eah strip separately.
|
---|
573 | //---------------------------------------------------------------------------
|
---|
574 | void GeoMeshView::drawTriangleStrip()
|
---|
575 | {
|
---|
576 | SubMesh *geosubmesh;
|
---|
577 | Index position;
|
---|
578 | Vector3 vector3;
|
---|
579 | float x,y,z;
|
---|
580 | float r,g,b;
|
---|
581 | Index *index;
|
---|
582 | Index *indexBegin;
|
---|
583 | Index *indexEnd;
|
---|
584 | int color_index;
|
---|
585 |
|
---|
586 | color_index = 0;
|
---|
587 |
|
---|
588 | // For each submesh.
|
---|
589 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
590 | {
|
---|
591 | // Gets the actual submesh.
|
---|
592 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
593 |
|
---|
594 | // For each one of the strips.
|
---|
595 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
596 | {
|
---|
597 | // Paint the current strip.
|
---|
598 | glBegin(GL_TRIANGLE_STRIP);
|
---|
599 |
|
---|
600 | // First index of the strip.
|
---|
601 | indexBegin = geosubmesh->mStrip[strip];
|
---|
602 |
|
---|
603 | // If the strips is not the first.
|
---|
604 | //if (strip != 0)
|
---|
605 | //{
|
---|
606 | // indexBegin++;
|
---|
607 | //}
|
---|
608 |
|
---|
609 | // If is the final strip
|
---|
610 | if (strip == (geosubmesh->mStripCount - 1))
|
---|
611 | {
|
---|
612 | // The end of the index array.
|
---|
613 | indexEnd = &geosubmesh->mIndex[geosubmesh->mIndexCount];
|
---|
614 | }
|
---|
615 | else
|
---|
616 | {
|
---|
617 | // The beginning of the next strip.
|
---|
618 | indexEnd = geosubmesh->mStrip[strip + 1];
|
---|
619 |
|
---|
620 | // Remove degenerated
|
---|
621 | //indexEnd--;
|
---|
622 | }
|
---|
623 |
|
---|
624 | int i;
|
---|
625 | i = 0;
|
---|
626 |
|
---|
627 | if (getColorStrips())
|
---|
628 | {
|
---|
629 | // Gets the color of the strip.
|
---|
630 | r = mStripColors[color_index].r;
|
---|
631 | g = mStripColors[color_index].g;
|
---|
632 | b = mStripColors[color_index].b;
|
---|
633 |
|
---|
634 | // Change to the next color.
|
---|
635 | color_index++;
|
---|
636 |
|
---|
637 | // Paint a new color.
|
---|
638 | glColor3f(r,g,b);
|
---|
639 | }
|
---|
640 |
|
---|
641 | // For each index of the strip.
|
---|
642 | for (index = indexBegin; index < indexEnd; index++)
|
---|
643 | {
|
---|
644 | position = indexBegin[i];
|
---|
645 |
|
---|
646 | // Gets the vertex normal.
|
---|
647 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
648 |
|
---|
649 | x = vector3[0];
|
---|
650 | y = vector3[1];
|
---|
651 | z = vector3[2];
|
---|
652 |
|
---|
653 | // Sets the vertex normal.
|
---|
654 | glNormal3f(x,y,z);
|
---|
655 |
|
---|
656 | // Gets the vertex coordinates.
|
---|
657 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
658 |
|
---|
659 | x = vector3[0];
|
---|
660 | y = vector3[1];
|
---|
661 | z = vector3[2];
|
---|
662 |
|
---|
663 |
|
---|
664 | // Sets the vertex position.
|
---|
665 | glVertex3f(x,y,z);
|
---|
666 |
|
---|
667 | // Increments i.
|
---|
668 | i++;
|
---|
669 | }
|
---|
670 |
|
---|
671 | glEnd();
|
---|
672 |
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | }//End drawTriangleStrip.
|
---|
677 |
|
---|
678 | //---------------------------------------------------------------------------
|
---|
679 | // Paint the array of idices.
|
---|
680 | //---------------------------------------------------------------------------
|
---|
681 | void GeoMeshView::drawTriangleList(int selectedMesh)
|
---|
682 | {
|
---|
683 | SubMesh *geosubmesh;
|
---|
684 | Index position;
|
---|
685 | Vector3 vector3;
|
---|
686 | float x,y,z;
|
---|
687 | int idx_offset;
|
---|
688 | int vrt_offset;
|
---|
689 |
|
---|
690 | // Index offset.
|
---|
691 | idx_offset = 0;
|
---|
692 |
|
---|
693 | // Vertex offset.
|
---|
694 | vrt_offset = 0;
|
---|
695 |
|
---|
696 | // For each submesh.
|
---|
697 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
698 | {
|
---|
699 | // Gets the actual submesh.
|
---|
700 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
701 |
|
---|
702 | // If a mesh is selected.
|
---|
703 | if (selectedMesh >= 0)
|
---|
704 | {
|
---|
705 | // If is a tree.
|
---|
706 | if (leavesSubmesh >= 0)
|
---|
707 | {
|
---|
708 | glColor3d(1.0, 0.5, 0.5);
|
---|
709 | GLfloat color_blanco[] = {1.0f,0.5f,0.5f,1.0f};
|
---|
710 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
711 |
|
---|
712 | // For each index of the strip.
|
---|
713 | if (submesh == leavesSubmesh)
|
---|
714 | {
|
---|
715 | glColor3d(0.0,1.0,0.0);
|
---|
716 | GLfloat color_rojo[] = {0.0f,1.0f,0.0f,1.0f};
|
---|
717 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_rojo);
|
---|
718 | }
|
---|
719 | }
|
---|
720 | // If is NOT a tree.
|
---|
721 | else
|
---|
722 | {
|
---|
723 | // Set white color to the object.
|
---|
724 | glColor3d(1.0, 1.0, 1.0);
|
---|
725 | GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
726 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
727 | }
|
---|
728 |
|
---|
729 | // For each index of the strip.
|
---|
730 | if (submesh == selectedMesh)
|
---|
731 | {
|
---|
732 | glColor3d(1.0,0.0,0.0);
|
---|
733 | GLfloat color_rojo[] = {1.0f,0.0f,0.0f,1.0f};
|
---|
734 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_rojo);
|
---|
735 | }
|
---|
736 | }
|
---|
737 | else
|
---|
738 | {
|
---|
739 | // If is a tree.
|
---|
740 | if (leavesSubmesh >= 0)
|
---|
741 | {
|
---|
742 | glColor3d(1.0, 0.5, 0.5);
|
---|
743 | GLfloat color_blanco[] = {1.0f,0.5f,0.5f,1.0f};
|
---|
744 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
745 |
|
---|
746 | // For each index of the strip.
|
---|
747 | if (submesh == leavesSubmesh)
|
---|
748 | {
|
---|
749 | glColor3d(0.0,1.0,0.0);
|
---|
750 | GLfloat color_rojo[] = {0.0f,1.0f,0.0f,1.0f};
|
---|
751 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_rojo);
|
---|
752 | }
|
---|
753 | }
|
---|
754 | }
|
---|
755 |
|
---|
756 | // Enable arrays.
|
---|
757 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
758 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
759 |
|
---|
760 | // If the submesh is shared vertex.
|
---|
761 | if (geosubmesh->mSharedVertexBuffer)
|
---|
762 | {
|
---|
763 | glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray);
|
---|
764 | glNormalPointer(GL_FLOAT, 0, mSharedNorArray);
|
---|
765 | }
|
---|
766 | else
|
---|
767 | {
|
---|
768 | glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]);
|
---|
769 | glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]);
|
---|
770 | }
|
---|
771 |
|
---|
772 | glDrawElements( GL_TRIANGLES,
|
---|
773 | geosubmesh->mIndexCount,
|
---|
774 | GL_UNSIGNED_INT,
|
---|
775 | mIndexArray[submesh]);
|
---|
776 | }
|
---|
777 |
|
---|
778 | }//End drawTriangleList
|
---|
779 |
|
---|
780 | //---------------------------------------------------------------------------
|
---|
781 | // Paint lodstrip object.
|
---|
782 | //---------------------------------------------------------------------------
|
---|
783 | void GeoMeshView::drawLodStrip()
|
---|
784 | {
|
---|
785 | SubMesh *geosubmesh;
|
---|
786 | Index position;
|
---|
787 | Vector3 vector3;
|
---|
788 | float x,y,z;
|
---|
789 | float r,g,b;
|
---|
790 | int color_index;
|
---|
791 | int current_strip;
|
---|
792 |
|
---|
793 | color_index = 0;
|
---|
794 |
|
---|
795 | // Initialize current strip.
|
---|
796 | current_strip = 0;
|
---|
797 |
|
---|
798 | // For each submesh.
|
---|
799 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
800 | {
|
---|
801 | // Gets the actual submesh.
|
---|
802 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
803 |
|
---|
804 | // For each one of the strips.
|
---|
805 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
806 | {
|
---|
807 | // Paint the current strip.
|
---|
808 | glBegin(GL_TRIANGLE_STRIP);
|
---|
809 |
|
---|
810 | if (getColorStrips())
|
---|
811 | {
|
---|
812 | // Gets the color of the strip.
|
---|
813 | r = mStripColors[color_index].r;
|
---|
814 | g = mStripColors[color_index].g;
|
---|
815 | b = mStripColors[color_index].b;
|
---|
816 |
|
---|
817 | // Change to the next color.
|
---|
818 | color_index++;
|
---|
819 |
|
---|
820 | // Paint a new color.
|
---|
821 | glColor3f(r,g,b);
|
---|
822 | }
|
---|
823 |
|
---|
824 | // For each index of the strip.
|
---|
825 | for (int index = 0; index < lodStripsLib->mStrips[current_strip].size(); index++)
|
---|
826 | {
|
---|
827 | position = lodStripsLib->mStrips[current_strip][index];
|
---|
828 |
|
---|
829 | // Gets the vertex normal.
|
---|
830 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
831 |
|
---|
832 | x = vector3[0];
|
---|
833 | y = vector3[1];
|
---|
834 | z = vector3[2];
|
---|
835 |
|
---|
836 | // Sets the vertex normal.
|
---|
837 | glNormal3f(x,y,z);
|
---|
838 |
|
---|
839 | // Gets the vertex coordinates.
|
---|
840 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
841 |
|
---|
842 | x = vector3[0];
|
---|
843 | y = vector3[1];
|
---|
844 | z = vector3[2];
|
---|
845 |
|
---|
846 | // Sets the vertex position.
|
---|
847 | glVertex3f(x,y,z);
|
---|
848 | }
|
---|
849 |
|
---|
850 | // Increments current strip.
|
---|
851 | current_strip++;
|
---|
852 |
|
---|
853 | // A strip is generated.
|
---|
854 | glEnd();
|
---|
855 |
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | }//End drawTriangleStrip.
|
---|
860 |
|
---|
861 | //---------------------------------------------------------------------------
|
---|
862 | // Sets the lodstripslibrary object.
|
---|
863 | //---------------------------------------------------------------------------
|
---|
864 | void GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips)
|
---|
865 | {
|
---|
866 | lodStripsLib = lodstrips;
|
---|
867 | activeLodStrip();
|
---|
868 | }
|
---|
869 |
|
---|
870 | //---------------------------------------------------------------------------
|
---|
871 | // Change de Level of detail of the object.
|
---|
872 | //---------------------------------------------------------------------------
|
---|
873 | void GeoMeshView::GoToLod(unsigned int lod)
|
---|
874 | {
|
---|
875 | lodStripsLib->GoToLod(lod);
|
---|
876 | draw();
|
---|
877 | }
|
---|
878 | //---------------------------------------------------------------------------
|
---|
879 | // Draw the object in the window.
|
---|
880 | //---------------------------------------------------------------------------
|
---|
881 | void GeoMeshView::draw()
|
---|
882 | {
|
---|
883 | glMatrixMode(GL_PROJECTION);
|
---|
884 | glLoadIdentity();
|
---|
885 |
|
---|
886 | glEnable(GL_LIGHTING);
|
---|
887 |
|
---|
888 | // Frustrum.
|
---|
889 | glViewport(0,0,w(),h());
|
---|
890 | gluPerspective(60,1,0.1,1000);
|
---|
891 |
|
---|
892 | glMatrixMode(GL_MODELVIEW);
|
---|
893 | glLoadIdentity();
|
---|
894 |
|
---|
895 | glEnable(GL_BLEND);
|
---|
896 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
897 |
|
---|
898 | GLfloat light_ambient[] = {0.3,0.3,0.3,1.0};
|
---|
899 | GLfloat luzpos[] = {0.0,0.0,1.0,0.0};
|
---|
900 | GLfloat luzcolor[] = {1.0,1.0,1.0,1.0};
|
---|
901 |
|
---|
902 | // glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
|
---|
903 | glEnable(GL_LIGHT0);
|
---|
904 | glLightfv(GL_LIGHT0, GL_POSITION, luzpos);
|
---|
905 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
|
---|
906 | glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor);
|
---|
907 | glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1);
|
---|
908 | glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0);
|
---|
909 | glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
|
---|
910 |
|
---|
911 | glEnable (GL_POLYGON_OFFSET_LINE);
|
---|
912 | glPolygonOffset(-1,-1);
|
---|
913 | glEnable(GL_DEPTH_TEST);
|
---|
914 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
915 |
|
---|
916 | gluLookAt(0,0,mScaleFactor*2+size, 0,0,0, 0,1,0);
|
---|
917 |
|
---|
918 | glTranslatef(xshift,yshift,0);
|
---|
919 | glRotatef(hAng,0,1,0);
|
---|
920 | glRotatef(vAng,1,0,0);
|
---|
921 | glTranslatef(-xMed,-yMed,-zMed);
|
---|
922 |
|
---|
923 | // Set white color to the object.
|
---|
924 | glColor3d(1.0, 1.0, 1.0);
|
---|
925 | GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
926 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
927 |
|
---|
928 | if (mCW)
|
---|
929 | {
|
---|
930 | glFrontFace(GL_CW);
|
---|
931 | glEnable(GL_CULL_FACE);
|
---|
932 | }
|
---|
933 | else
|
---|
934 | {
|
---|
935 | if (mCCW)
|
---|
936 | {
|
---|
937 | glFrontFace(GL_CCW);
|
---|
938 | glEnable(GL_CULL_FACE);
|
---|
939 | }
|
---|
940 | else
|
---|
941 | {
|
---|
942 | glDisable(GL_CULL_FACE);
|
---|
943 | }
|
---|
944 | }
|
---|
945 |
|
---|
946 | if (mSolid)
|
---|
947 | {
|
---|
948 | enableColorStrips();
|
---|
949 | glDisable(GL_LIGHTING);
|
---|
950 | glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
|
---|
951 | drawGeoMesh(colorsubmeshi);
|
---|
952 | }
|
---|
953 | else
|
---|
954 | {
|
---|
955 | glEnable(GL_LIGHTING);
|
---|
956 | }
|
---|
957 |
|
---|
958 | if (mWire)
|
---|
959 | {
|
---|
960 | disableColorStrips();
|
---|
961 | glDisable(GL_LIGHTING);
|
---|
962 | glColor3d(0.0, 0.0, 1.0);
|
---|
963 | glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
|
---|
964 | drawGeoMesh(-1);
|
---|
965 | }
|
---|
966 | else
|
---|
967 | {
|
---|
968 | if (!mSolid)
|
---|
969 | {
|
---|
970 | enableColorStrips();
|
---|
971 | glEnable(GL_LIGHTING);
|
---|
972 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
---|
973 | drawGeoMesh(colorsubmeshi);
|
---|
974 | }
|
---|
975 | }
|
---|
976 |
|
---|
977 | swap_buffers();
|
---|
978 |
|
---|
979 | // Calculate the FPS.
|
---|
980 | calcFPS();
|
---|
981 | }
|
---|
982 |
|
---|