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 | SubMesh *geosubmesh;
|
---|
228 |
|
---|
229 | // Free memory of the strip colors array.
|
---|
230 | for (int submesh = 0; submesh < mStripColorsCount; submesh++)
|
---|
231 | {
|
---|
232 | delete []mStripColors[submesh];
|
---|
233 | }
|
---|
234 |
|
---|
235 | delete []mStripColors;
|
---|
236 |
|
---|
237 | // Initialize the Colors array.
|
---|
238 | mStripColors = new ColorElement*[geoMesh->mSubMeshCount];
|
---|
239 |
|
---|
240 | // Count the total number of strips y the submeshes.
|
---|
241 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
242 | {
|
---|
243 | // Gets the actual submesh.
|
---|
244 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
245 |
|
---|
246 | if (geosubmesh->mType == GEO_TRIANGLE_STRIPS)
|
---|
247 | {
|
---|
248 | mStripColors[submesh] = new ColorElement[geosubmesh->mStripCount];
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | mStripColors[submesh] = NULL;
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | // Sets random colors to each strip.
|
---|
258 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
259 | {
|
---|
260 | // Gets the actual submesh.
|
---|
261 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
262 |
|
---|
263 | if (geosubmesh->mType == GEO_TRIANGLE_STRIPS)
|
---|
264 | {
|
---|
265 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
266 | {
|
---|
267 | mStripColors[submesh][strip].r = rand()/((float)RAND_MAX);
|
---|
268 | mStripColors[submesh][strip].g = rand()/((float)RAND_MAX);
|
---|
269 | mStripColors[submesh][strip].b = rand()/((float)RAND_MAX);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | // Sets the submesh count.
|
---|
275 | mStripColorsCount = geoMesh->mSubMeshCount;
|
---|
276 | }
|
---|
277 |
|
---|
278 | //---------------------------------------------------------------------------
|
---|
279 | // Get the bounding box of the object.
|
---|
280 | //---------------------------------------------------------------------------
|
---|
281 | GeometryBounds GeoMeshView::getBoundingBox()
|
---|
282 | {
|
---|
283 | SubMesh *geosubmesh;
|
---|
284 | Index position;
|
---|
285 | Vector3 vector3;
|
---|
286 | GeometryBounds bounds;
|
---|
287 | float x,y,z;
|
---|
288 | float xMin,yMin,zMin;
|
---|
289 | float xMax,yMax,zMax;
|
---|
290 |
|
---|
291 | // For each submesh.
|
---|
292 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
293 | {
|
---|
294 | // Gets the actual submesh.
|
---|
295 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
296 |
|
---|
297 | // For each index of the strip.
|
---|
298 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
299 | {
|
---|
300 | position = geosubmesh->mIndex[index];
|
---|
301 |
|
---|
302 | // Gets the vertex coordinates.
|
---|
303 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
304 |
|
---|
305 | x = vector3[0];
|
---|
306 | y = vector3[1];
|
---|
307 | z = vector3[2];
|
---|
308 |
|
---|
309 | // If is the first iteation.
|
---|
310 | if ((submesh == 0) && (index == 0))
|
---|
311 | {
|
---|
312 | xMin = xMax = x;
|
---|
313 | yMin = yMax = y;
|
---|
314 | zMin = zMax = z;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | if (x < xMin)
|
---|
319 | {
|
---|
320 | xMin = x;
|
---|
321 | }
|
---|
322 | else if (x > xMax)
|
---|
323 | {
|
---|
324 | xMax = x;
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (y < yMin)
|
---|
328 | {
|
---|
329 | yMin = y;
|
---|
330 | }
|
---|
331 | else if (y > yMax)
|
---|
332 | {
|
---|
333 | yMax = y;
|
---|
334 | }
|
---|
335 |
|
---|
336 | if (z < zMin)
|
---|
337 | {
|
---|
338 | zMin = z;
|
---|
339 | }
|
---|
340 | else if (z > zMax)
|
---|
341 | {
|
---|
342 | zMax = z;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | xMed = (xMax + xMin) / 2;
|
---|
349 | yMed = (yMax + yMin) / 2;
|
---|
350 | zMed = (zMax + zMin) / 2;
|
---|
351 |
|
---|
352 |
|
---|
353 | if ((xMax - xMin) > (yMax - yMin))
|
---|
354 | {
|
---|
355 | if ((xMax - xMin) > (zMax - zMin))
|
---|
356 | {
|
---|
357 | mScaleFactor = xMax - xMin;
|
---|
358 | }
|
---|
359 | else
|
---|
360 | {
|
---|
361 | mScaleFactor = zMax - zMin;
|
---|
362 | }
|
---|
363 | }
|
---|
364 | else
|
---|
365 | {
|
---|
366 | if ((yMax - yMin) > (zMax - zMin))
|
---|
367 | {
|
---|
368 | mScaleFactor = yMax - yMin;
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | mScaleFactor = zMax - zMin;
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | // Set the bounds.
|
---|
377 | bounds.minx = xMin;
|
---|
378 | bounds.miny = yMin;
|
---|
379 | bounds.minz = zMin;
|
---|
380 | bounds.maxx = xMax;
|
---|
381 | bounds.maxy = yMax;
|
---|
382 | bounds.maxz = zMax;
|
---|
383 | bounds.radius = mScaleFactor;
|
---|
384 |
|
---|
385 | return bounds;
|
---|
386 | }
|
---|
387 |
|
---|
388 | //---------------------------------------------------------------------------
|
---|
389 | // Constructor.
|
---|
390 | //---------------------------------------------------------------------------
|
---|
391 | GeoMeshView::GeoMeshView( int x,
|
---|
392 | int y,
|
---|
393 | int w,
|
---|
394 | int h,
|
---|
395 | const char *l,
|
---|
396 | GeoMeshViewUI *geoMeshViewUI)
|
---|
397 | : fltk::GlWindow(x,y,w,h,l)
|
---|
398 | {
|
---|
399 | mStripColors = NULL;
|
---|
400 | mSharedPosArray = NULL;
|
---|
401 | mSharedNorArray = NULL;
|
---|
402 | mPosArray = NULL;
|
---|
403 | mNorArray = NULL;
|
---|
404 | mIndexArray = NULL;
|
---|
405 | vAng = 0.0;
|
---|
406 | hAng = 0.0;
|
---|
407 | size = 0.0;
|
---|
408 | mSizeY = 0;
|
---|
409 | xshift = 0.0;
|
---|
410 | yshift = 0.0;
|
---|
411 | geoMesh = 0;
|
---|
412 | mRotate = true;
|
---|
413 | mPan = false;
|
---|
414 | mLodStrip = false;
|
---|
415 | mIdVisualList = 0;
|
---|
416 | mSubMeshCount = 0;
|
---|
417 | mStripColorsCount = 0;
|
---|
418 |
|
---|
419 | // Initialize the model view.
|
---|
420 | deactiveWire();
|
---|
421 | deactiveSolid();
|
---|
422 | activeRotate();
|
---|
423 | deactiveCW();
|
---|
424 | activeCCW();
|
---|
425 |
|
---|
426 | // Init start time for FPS.
|
---|
427 | mStartTime = clock();
|
---|
428 |
|
---|
429 | // Cross Reference.
|
---|
430 | mGeoMeshViewUI = geoMeshViewUI;
|
---|
431 | }
|
---|
432 |
|
---|
433 | // The color used for the edges of the bounding cube.
|
---|
434 | #define CUBECOLOR 255,255,255,255
|
---|
435 |
|
---|
436 | /* Draw a colored cube */
|
---|
437 | #define ALPHA 0.5
|
---|
438 |
|
---|
439 | //---------------------------------------------------------------------------
|
---|
440 | // Sets the Mesh to display.
|
---|
441 | //---------------------------------------------------------------------------
|
---|
442 | void GeoMeshView::setMesh(Mesh *geomesh)
|
---|
443 | {
|
---|
444 | GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
445 |
|
---|
446 | this->geoMesh = geomesh;
|
---|
447 | colorsubmeshi = -1;
|
---|
448 | leavesSubmesh = -1;
|
---|
449 |
|
---|
450 | // Refresh vertices to the vertex array.
|
---|
451 | refreshVertices(geomesh);
|
---|
452 |
|
---|
453 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
454 | }
|
---|
455 |
|
---|
456 | //---------------------------------------------------------------------------
|
---|
457 | // Refresh vertices to the vertex array.
|
---|
458 | //---------------------------------------------------------------------------
|
---|
459 | void GeoMeshView::refreshVertices(Mesh *geomesh)
|
---|
460 | {
|
---|
461 | SubMesh *geosubmesh;
|
---|
462 | VertexBuffer *vertex_buffer;
|
---|
463 |
|
---|
464 | // If the vertex array exists.
|
---|
465 | if (mSubMeshCount > 0)
|
---|
466 | {
|
---|
467 | for (int submesh = 0; submesh < mSubMeshCount; submesh++)
|
---|
468 | {
|
---|
469 | delete [] mPosArray[submesh];
|
---|
470 | delete [] mNorArray[submesh];
|
---|
471 | delete [] mIndexArray[submesh];
|
---|
472 | }
|
---|
473 |
|
---|
474 | delete [] mSharedPosArray;
|
---|
475 | delete [] mSharedNorArray;
|
---|
476 | delete [] mPosArray;
|
---|
477 | delete [] mNorArray;
|
---|
478 | delete [] mIndexArray;
|
---|
479 | }
|
---|
480 |
|
---|
481 | // If there is shared vertex.
|
---|
482 | if (geomesh->mVertexBuffer != NULL)
|
---|
483 | {
|
---|
484 | vertex_buffer = geomesh->mVertexBuffer;
|
---|
485 |
|
---|
486 | // Allocate memory.
|
---|
487 | mSharedPosArray = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
488 | mSharedNorArray = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
489 |
|
---|
490 | for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
|
---|
491 | {
|
---|
492 | mSharedPosArray[3 * vertex] = vertex_buffer->mPosition[vertex].x;
|
---|
493 | mSharedPosArray[(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y;
|
---|
494 | mSharedPosArray[(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z;
|
---|
495 |
|
---|
496 | mSharedNorArray[3 * vertex] = vertex_buffer->mNormal[vertex].x;
|
---|
497 | mSharedNorArray[(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y;
|
---|
498 | mSharedNorArray[(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z;
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | // Build the arrays of vertices and indices.
|
---|
503 | mPosArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
504 | mNorArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
505 | mIndexArray = new GLuint*[geomesh->mSubMeshCount];
|
---|
506 |
|
---|
507 | // For each submesh.
|
---|
508 | for (int submesh = 0; submesh < geomesh->mSubMeshCount; submesh++)
|
---|
509 | {
|
---|
510 | // Gets the actual submesh.
|
---|
511 | geosubmesh = &geomesh->mSubMesh[submesh];
|
---|
512 |
|
---|
513 | // Allocate memory for index array of the actual submesh.
|
---|
514 | mIndexArray[submesh] = new GLuint[geosubmesh->mIndexCount];
|
---|
515 |
|
---|
516 | // Gets the indices of the actual submesh.
|
---|
517 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
518 | {
|
---|
519 | mIndexArray[submesh][index] = geosubmesh->mIndex[index];
|
---|
520 | }
|
---|
521 |
|
---|
522 | // If the submesh is NOT shared vertex.
|
---|
523 | if (!geosubmesh->mSharedVertexBuffer)
|
---|
524 | {
|
---|
525 | vertex_buffer = geosubmesh->mVertexBuffer;
|
---|
526 |
|
---|
527 | // Allocate memory for vertices of the actual submesh.
|
---|
528 | mPosArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
529 | mNorArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
530 |
|
---|
531 | // For each one of the vertex.
|
---|
532 | for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
|
---|
533 | {
|
---|
534 | mPosArray[submesh][3 * vertex] = vertex_buffer->mPosition[vertex].x;
|
---|
535 | mPosArray[submesh][(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y;
|
---|
536 | mPosArray[submesh][(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z;
|
---|
537 |
|
---|
538 | mNorArray[submesh][3 * vertex] = vertex_buffer->mNormal[vertex].x;
|
---|
539 | mNorArray[submesh][(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y;
|
---|
540 | mNorArray[submesh][(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z;
|
---|
541 | }// End for each vertex.
|
---|
542 | }
|
---|
543 | // If the submesh is shared vertex.
|
---|
544 | else
|
---|
545 | {
|
---|
546 | mPosArray[submesh] = NULL;
|
---|
547 | mNorArray[submesh] = NULL;
|
---|
548 | }
|
---|
549 |
|
---|
550 | }// End for each submesh.
|
---|
551 |
|
---|
552 | mSubMeshCount = geoMesh->mSubMeshCount;
|
---|
553 | }
|
---|
554 |
|
---|
555 | //---------------------------------------------------------------------------
|
---|
556 | // Gets the triangle count of the current lod.
|
---|
557 | //---------------------------------------------------------------------------
|
---|
558 | size_t GeoMeshView::getLodStripsTriangleCount()
|
---|
559 | {
|
---|
560 | size_t triangle_count;
|
---|
561 |
|
---|
562 | // Initialize triangle count.
|
---|
563 | triangle_count = 0;
|
---|
564 |
|
---|
565 | // For each strip.
|
---|
566 | for (int strip = 0; strip < lodStripsLib->GetStripCount(); strip++)
|
---|
567 | {
|
---|
568 | triangle_count += lodStripsLib->mStrips[strip].size() - 2;
|
---|
569 | }
|
---|
570 |
|
---|
571 | return triangle_count;
|
---|
572 | }
|
---|
573 |
|
---|
574 | //---------------------------------------------------------------------------
|
---|
575 | // Repaint the Mesh.
|
---|
576 | //---------------------------------------------------------------------------
|
---|
577 | void GeoMeshView::drawGeoMesh(int selectedMesh)
|
---|
578 | {
|
---|
579 | SubMesh *geosubmesh;
|
---|
580 | bool submesh_selected;
|
---|
581 |
|
---|
582 | if (geoMesh)
|
---|
583 | {
|
---|
584 | if (mLodStrip)
|
---|
585 | {
|
---|
586 | drawLodStrip();
|
---|
587 | }
|
---|
588 | else if (mLodTree)
|
---|
589 | {
|
---|
590 | drawLodTree();
|
---|
591 | }
|
---|
592 | else
|
---|
593 | {
|
---|
594 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
595 | {
|
---|
596 | // Gets the actual submesh.
|
---|
597 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
598 |
|
---|
599 | if (geosubmesh->mType == GEO_TRIANGLE_LIST)
|
---|
600 | {
|
---|
601 | if (selectedMesh == submesh)
|
---|
602 | {
|
---|
603 | submesh_selected = true;
|
---|
604 | }
|
---|
605 | else
|
---|
606 | {
|
---|
607 | submesh_selected = false;
|
---|
608 | }
|
---|
609 |
|
---|
610 | // Draw the actual submesh in triangle list.
|
---|
611 | drawTriangleList(submesh,submesh_selected);
|
---|
612 | }
|
---|
613 | else
|
---|
614 | {
|
---|
615 | // Draw the actual submesh in triangle strip.
|
---|
616 | drawTriangleStrip(submesh);
|
---|
617 | }
|
---|
618 | }
|
---|
619 | }
|
---|
620 | /*
|
---|
621 | // Choose the render operation.
|
---|
622 | else if (geoMesh->mType == GEO_TRIANGLE_LIST)
|
---|
623 | {
|
---|
624 | // Paint vertices.
|
---|
625 | drawTriangleList(selectedMesh);
|
---|
626 | }
|
---|
627 | else
|
---|
628 | {
|
---|
629 | // Paint vertices.
|
---|
630 | drawTriangleStrip();
|
---|
631 | }
|
---|
632 | */
|
---|
633 | }
|
---|
634 | }// End drawGeoMesh.
|
---|
635 |
|
---|
636 | //---------------------------------------------------------------------------
|
---|
637 | // Paint eah strip separately.
|
---|
638 | //---------------------------------------------------------------------------
|
---|
639 | void GeoMeshView::drawTriangleStrip(int submesh)
|
---|
640 | {
|
---|
641 | SubMesh *geosubmesh;
|
---|
642 | Index position;
|
---|
643 | Vector3 vector3;
|
---|
644 | float x,y,z;
|
---|
645 | float r,g,b;
|
---|
646 | Index *index;
|
---|
647 | Index *indexBegin;
|
---|
648 | Index *indexEnd;
|
---|
649 | int color_index;
|
---|
650 |
|
---|
651 | color_index = 0;
|
---|
652 |
|
---|
653 | // Gets the actual submesh.
|
---|
654 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
655 |
|
---|
656 | // For each one of the strips.
|
---|
657 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
658 | {
|
---|
659 | // Paint the current strip.
|
---|
660 | glBegin(GL_TRIANGLE_STRIP);
|
---|
661 |
|
---|
662 | // First index of the strip.
|
---|
663 | indexBegin = geosubmesh->mStrip[strip];
|
---|
664 |
|
---|
665 | // If the strips is not the first.
|
---|
666 | //if (strip != 0)
|
---|
667 | //{
|
---|
668 | // indexBegin++;
|
---|
669 | //}
|
---|
670 |
|
---|
671 | // If is the final strip
|
---|
672 | if (strip == (geosubmesh->mStripCount - 1))
|
---|
673 | {
|
---|
674 | // The end of the index array.
|
---|
675 | indexEnd = &geosubmesh->mIndex[geosubmesh->mIndexCount];
|
---|
676 | }
|
---|
677 | else
|
---|
678 | {
|
---|
679 | // The beginning of the next strip.
|
---|
680 | indexEnd = geosubmesh->mStrip[strip + 1];
|
---|
681 |
|
---|
682 | // Remove degenerated
|
---|
683 | //indexEnd--;
|
---|
684 | }
|
---|
685 |
|
---|
686 | int i;
|
---|
687 | i = 0;
|
---|
688 |
|
---|
689 | if (getColorStrips())
|
---|
690 | {
|
---|
691 | // Gets the color of the strip.
|
---|
692 | r = mStripColors[submesh][color_index].r;
|
---|
693 | g = mStripColors[submesh][color_index].g;
|
---|
694 | b = mStripColors[submesh][color_index].b;
|
---|
695 |
|
---|
696 | // Change to the next color.
|
---|
697 | color_index++;
|
---|
698 |
|
---|
699 | // Paint a new color.
|
---|
700 | glColor3f(r,g,b);
|
---|
701 | }
|
---|
702 |
|
---|
703 | // For each index of the strip.
|
---|
704 | for (index = indexBegin; index < indexEnd; index++)
|
---|
705 | {
|
---|
706 | position = indexBegin[i];
|
---|
707 |
|
---|
708 | // Gets the vertex normal.
|
---|
709 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
710 |
|
---|
711 | x = vector3[0];
|
---|
712 | y = vector3[1];
|
---|
713 | z = vector3[2];
|
---|
714 |
|
---|
715 | // Sets the vertex normal.
|
---|
716 | glNormal3f(x,y,z);
|
---|
717 |
|
---|
718 | // Gets the vertex coordinates.
|
---|
719 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
720 |
|
---|
721 | x = vector3[0];
|
---|
722 | y = vector3[1];
|
---|
723 | z = vector3[2];
|
---|
724 |
|
---|
725 |
|
---|
726 | // Sets the vertex position.
|
---|
727 | glVertex3f(x,y,z);
|
---|
728 |
|
---|
729 | // Increments i.
|
---|
730 | i++;
|
---|
731 | }
|
---|
732 |
|
---|
733 | glEnd();
|
---|
734 |
|
---|
735 | }
|
---|
736 |
|
---|
737 | }//End drawTriangleStrip.
|
---|
738 |
|
---|
739 | //---------------------------------------------------------------------------
|
---|
740 | // Paint the array of idices.
|
---|
741 | //---------------------------------------------------------------------------
|
---|
742 | void GeoMeshView::drawTriangleList( int submesh,
|
---|
743 | bool selectedSubMesh)
|
---|
744 | {
|
---|
745 | SubMesh *geosubmesh;
|
---|
746 | Index position;
|
---|
747 | Vector3 vector3;
|
---|
748 | float x,y,z;
|
---|
749 | int idx_offset;
|
---|
750 | int vrt_offset;
|
---|
751 |
|
---|
752 | // Index offset.
|
---|
753 | idx_offset = 0;
|
---|
754 |
|
---|
755 | // Vertex offset.
|
---|
756 | vrt_offset = 0;
|
---|
757 |
|
---|
758 | // Gets the actual submesh.
|
---|
759 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
760 |
|
---|
761 | // If a mesh is selected.
|
---|
762 | if (selectedSubMesh)
|
---|
763 | {
|
---|
764 | glColor3d(1.0,0.0,0.0);
|
---|
765 | GLfloat red[] = {1.0f,0.0f,0.0f,1.0f};
|
---|
766 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red);
|
---|
767 | }
|
---|
768 | // If is a tree.
|
---|
769 | else if (leavesSubmesh >= 0)
|
---|
770 | {
|
---|
771 | glColor3d(1.0, 0.5, 0.5);
|
---|
772 | GLfloat brown[] = {1.0f,0.5f,0.5f,1.0f};
|
---|
773 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown);
|
---|
774 |
|
---|
775 | // For each index of the strip.
|
---|
776 | if (submesh == leavesSubmesh)
|
---|
777 | {
|
---|
778 | glColor3d(0.0,1.0,0.0);
|
---|
779 | GLfloat green[] = {0.0f,1.0f,0.0f,1.0f};
|
---|
780 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green);
|
---|
781 | }
|
---|
782 | }
|
---|
783 | // If is NOT a tree.
|
---|
784 | else
|
---|
785 | {
|
---|
786 | // Set white color to the object.
|
---|
787 | glColor3d(1.0, 1.0, 1.0);
|
---|
788 | GLfloat white[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
789 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
|
---|
790 | }
|
---|
791 |
|
---|
792 |
|
---|
793 | // Enable arrays.
|
---|
794 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
795 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
796 |
|
---|
797 | // If the submesh is shared vertex.
|
---|
798 | if (geosubmesh->mSharedVertexBuffer)
|
---|
799 | {
|
---|
800 | glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray);
|
---|
801 | glNormalPointer(GL_FLOAT, 0, mSharedNorArray);
|
---|
802 | }
|
---|
803 | else
|
---|
804 | {
|
---|
805 | glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]);
|
---|
806 | glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]);
|
---|
807 | }
|
---|
808 |
|
---|
809 | glDrawElements( GL_TRIANGLES,
|
---|
810 | geosubmesh->mIndexCount,
|
---|
811 | GL_UNSIGNED_INT,
|
---|
812 | mIndexArray[submesh]);
|
---|
813 |
|
---|
814 | }//End drawTriangleList
|
---|
815 |
|
---|
816 | //---------------------------------------------------------------------------
|
---|
817 | // Paint lodstrip object.
|
---|
818 | //---------------------------------------------------------------------------
|
---|
819 | void GeoMeshView::drawLodStrip()
|
---|
820 | {
|
---|
821 | SubMesh *geosubmesh;
|
---|
822 | Index position;
|
---|
823 | Vector3 vector3;
|
---|
824 | float x,y,z;
|
---|
825 | float r,g,b;
|
---|
826 | int color_index;
|
---|
827 | int current_strip;
|
---|
828 |
|
---|
829 | // Initialize current strip.
|
---|
830 | current_strip = 0;
|
---|
831 |
|
---|
832 | // For each submesh.
|
---|
833 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
834 | {
|
---|
835 |
|
---|
836 | color_index = 0;
|
---|
837 |
|
---|
838 | // Gets the actual submesh.
|
---|
839 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
840 |
|
---|
841 | // For each one of the strips.
|
---|
842 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
843 | {
|
---|
844 | // Paint the current strip.
|
---|
845 | glBegin(GL_TRIANGLE_STRIP);
|
---|
846 |
|
---|
847 | if (getColorStrips())
|
---|
848 | {
|
---|
849 | // Gets the color of the strip.
|
---|
850 | r = mStripColors[submesh][color_index].r;
|
---|
851 | g = mStripColors[submesh][color_index].g;
|
---|
852 | b = mStripColors[submesh][color_index].b;
|
---|
853 |
|
---|
854 | // Change to the next color.
|
---|
855 | color_index++;
|
---|
856 |
|
---|
857 | // Paint a new color.
|
---|
858 | glColor3f(r,g,b);
|
---|
859 | }
|
---|
860 |
|
---|
861 | // For each index of the strip.
|
---|
862 | for (int index = 0; index < lodStripsLib->mStrips[current_strip].size(); index++)
|
---|
863 | {
|
---|
864 | position = lodStripsLib->mStrips[current_strip][index];
|
---|
865 |
|
---|
866 | // Gets the vertex normal.
|
---|
867 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
868 |
|
---|
869 | x = vector3[0];
|
---|
870 | y = vector3[1];
|
---|
871 | z = vector3[2];
|
---|
872 |
|
---|
873 | // Sets the vertex normal.
|
---|
874 | glNormal3f(x,y,z);
|
---|
875 |
|
---|
876 | // Gets the vertex coordinates.
|
---|
877 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
878 |
|
---|
879 | x = vector3[0];
|
---|
880 | y = vector3[1];
|
---|
881 | z = vector3[2];
|
---|
882 |
|
---|
883 | // Sets the vertex position.
|
---|
884 | glVertex3f(x,y,z);
|
---|
885 | }
|
---|
886 |
|
---|
887 | // Increments current strip.
|
---|
888 | current_strip++;
|
---|
889 |
|
---|
890 | // A strip is generated.
|
---|
891 | glEnd();
|
---|
892 |
|
---|
893 | }
|
---|
894 | }
|
---|
895 |
|
---|
896 | }//End drawTriangleStrip.
|
---|
897 |
|
---|
898 |
|
---|
899 | //---------------------------------------------------------------------------
|
---|
900 | // Paint lodtree object.
|
---|
901 | //---------------------------------------------------------------------------
|
---|
902 | void GeoMeshView::drawLodTree()
|
---|
903 | {
|
---|
904 | SubMesh *geosubmesh;
|
---|
905 | Index position;
|
---|
906 | Vector3 vector3;
|
---|
907 | float x,y,z;
|
---|
908 | float r,g,b;
|
---|
909 | int color_index;
|
---|
910 | int current_strip;
|
---|
911 |
|
---|
912 | // Initialize current strip.
|
---|
913 | current_strip = 0;
|
---|
914 |
|
---|
915 | // For each submesh.
|
---|
916 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
917 | {
|
---|
918 |
|
---|
919 | color_index = 0;
|
---|
920 |
|
---|
921 | // Gets the actual submesh.
|
---|
922 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
923 |
|
---|
924 | // For each one of the strips.
|
---|
925 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
926 | {
|
---|
927 | // Paint the current strip.
|
---|
928 | glBegin(GL_TRIANGLE_STRIP);
|
---|
929 |
|
---|
930 | if (getColorStrips())
|
---|
931 | {
|
---|
932 | // Gets the color of the strip.
|
---|
933 | r = mStripColors[submesh][color_index].r;
|
---|
934 | g = mStripColors[submesh][color_index].g;
|
---|
935 | b = mStripColors[submesh][color_index].b;
|
---|
936 |
|
---|
937 | // Change to the next color.
|
---|
938 | color_index++;
|
---|
939 |
|
---|
940 | // Paint a new color.
|
---|
941 | glColor3f(r,g,b);
|
---|
942 | }
|
---|
943 |
|
---|
944 | // For each index of the strip.
|
---|
945 | for (int index = 0; index < lodTreeLib->GetStrip(current_strip).size(); index++)
|
---|
946 | {
|
---|
947 | position = lodTreeLib->GetStrip(current_strip)[index];
|
---|
948 |
|
---|
949 | // Gets the vertex normal.
|
---|
950 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
951 |
|
---|
952 | x = vector3[0];
|
---|
953 | y = vector3[1];
|
---|
954 | z = vector3[2];
|
---|
955 |
|
---|
956 | // Sets the vertex normal.
|
---|
957 | glNormal3f(x,y,z);
|
---|
958 |
|
---|
959 | // Gets the vertex coordinates.
|
---|
960 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
961 |
|
---|
962 | x = vector3[0];
|
---|
963 | y = vector3[1];
|
---|
964 | z = vector3[2];
|
---|
965 |
|
---|
966 | // Sets the vertex position.
|
---|
967 | glVertex3f(x,y,z);
|
---|
968 | }
|
---|
969 |
|
---|
970 | // Increments current strip.
|
---|
971 | current_strip++;
|
---|
972 |
|
---|
973 | // A strip is generated.
|
---|
974 | glEnd();
|
---|
975 |
|
---|
976 | }
|
---|
977 | }
|
---|
978 |
|
---|
979 | }//End drawTriangleStrip.
|
---|
980 |
|
---|
981 |
|
---|
982 |
|
---|
983 | //---------------------------------------------------------------------------
|
---|
984 | // Sets the lodstripslibrary object.
|
---|
985 | //---------------------------------------------------------------------------
|
---|
986 | void GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips)
|
---|
987 | {
|
---|
988 | lodStripsLib = lodstrips;
|
---|
989 | activeLodStrip();
|
---|
990 | }
|
---|
991 |
|
---|
992 | //---------------------------------------------------------------------------
|
---|
993 | // Sets the lodtreeslibrary object.
|
---|
994 | //---------------------------------------------------------------------------
|
---|
995 | void GeoMeshView::setLodTreesLibrary(LodTreeLibrary *lodtrees)
|
---|
996 | {
|
---|
997 | lodTreeLib = lodtrees;
|
---|
998 | activeLodTree();
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | //---------------------------------------------------------------------------
|
---|
1002 | // Change de Level of detail of the object.
|
---|
1003 | //---------------------------------------------------------------------------
|
---|
1004 | void GeoMeshView::GoToLod(unsigned int lod)
|
---|
1005 | {
|
---|
1006 | if (mLodStrip)
|
---|
1007 | lodStripsLib->GoToLod(lod);
|
---|
1008 | if (mLodTree)
|
---|
1009 | lodTreeLib->GoToTrunkLod(lod);
|
---|
1010 | draw();
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | //---------------------------------------------------------------------------
|
---|
1014 | // Draw the object in the window.
|
---|
1015 | //---------------------------------------------------------------------------
|
---|
1016 | void GeoMeshView::draw()
|
---|
1017 | {
|
---|
1018 | glMatrixMode(GL_PROJECTION);
|
---|
1019 | glLoadIdentity();
|
---|
1020 |
|
---|
1021 | glEnable(GL_LIGHTING);
|
---|
1022 |
|
---|
1023 | // Frustrum.
|
---|
1024 | glViewport(0,0,w(),h());
|
---|
1025 | gluPerspective(60,1,0.1,1000);
|
---|
1026 |
|
---|
1027 | glMatrixMode(GL_MODELVIEW);
|
---|
1028 | glLoadIdentity();
|
---|
1029 |
|
---|
1030 | glEnable(GL_BLEND);
|
---|
1031 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
1032 |
|
---|
1033 | GLfloat light_ambient[] = {0.3,0.3,0.3,1.0};
|
---|
1034 | GLfloat luzpos[] = {0.0,0.0,1.0,0.0};
|
---|
1035 | GLfloat luzcolor[] = {1.0,1.0,1.0,1.0};
|
---|
1036 |
|
---|
1037 | // glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
|
---|
1038 | glEnable(GL_LIGHT0);
|
---|
1039 | glLightfv(GL_LIGHT0, GL_POSITION, luzpos);
|
---|
1040 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
|
---|
1041 | glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor);
|
---|
1042 | glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1);
|
---|
1043 | glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0);
|
---|
1044 | glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
|
---|
1045 |
|
---|
1046 | glEnable (GL_POLYGON_OFFSET_LINE);
|
---|
1047 | glPolygonOffset(-1,-1);
|
---|
1048 | glEnable(GL_DEPTH_TEST);
|
---|
1049 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
1050 |
|
---|
1051 | gluLookAt(0,0,mScaleFactor*2+size, 0,0,0, 0,1,0);
|
---|
1052 |
|
---|
1053 | glTranslatef(xshift,yshift,0);
|
---|
1054 | glRotatef(hAng,0,1,0);
|
---|
1055 | glRotatef(vAng,1,0,0);
|
---|
1056 | glTranslatef(-xMed,-yMed,-zMed);
|
---|
1057 |
|
---|
1058 | // Set white color to the object.
|
---|
1059 | glColor3d(1.0, 1.0, 1.0);
|
---|
1060 | GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
1061 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
1062 |
|
---|
1063 | if (mCW)
|
---|
1064 | {
|
---|
1065 | glFrontFace(GL_CW);
|
---|
1066 | glEnable(GL_CULL_FACE);
|
---|
1067 | }
|
---|
1068 | else
|
---|
1069 | {
|
---|
1070 | if (mCCW)
|
---|
1071 | {
|
---|
1072 | glFrontFace(GL_CCW);
|
---|
1073 | glEnable(GL_CULL_FACE);
|
---|
1074 | }
|
---|
1075 | else
|
---|
1076 | {
|
---|
1077 | glDisable(GL_CULL_FACE);
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | if (mSolid)
|
---|
1082 | {
|
---|
1083 | enableColorStrips();
|
---|
1084 | glDisable(GL_LIGHTING);
|
---|
1085 | glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
|
---|
1086 | drawGeoMesh(colorsubmeshi);
|
---|
1087 | }
|
---|
1088 | else
|
---|
1089 | {
|
---|
1090 | glEnable(GL_LIGHTING);
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | if (mWire)
|
---|
1094 | {
|
---|
1095 | disableColorStrips();
|
---|
1096 | glDisable(GL_LIGHTING);
|
---|
1097 | glColor3d(0.0, 0.0, 1.0);
|
---|
1098 | glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
|
---|
1099 | drawGeoMesh(-1);
|
---|
1100 | }
|
---|
1101 | else
|
---|
1102 | {
|
---|
1103 | if (!mSolid)
|
---|
1104 | {
|
---|
1105 | enableColorStrips();
|
---|
1106 | glEnable(GL_LIGHTING);
|
---|
1107 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
---|
1108 | drawGeoMesh(colorsubmeshi);
|
---|
1109 | }
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | swap_buffers();
|
---|
1113 |
|
---|
1114 | // Calculate the FPS.
|
---|
1115 | calcFPS();
|
---|
1116 | }
|
---|
1117 |
|
---|