1 | #include "GeoMeshView.h"
|
---|
2 | #include <VertexData.h>
|
---|
3 |
|
---|
4 | using namespace Geometry;
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | //---------------------------------------------------------------------------
|
---|
8 | // Hanle events.
|
---|
9 | //---------------------------------------------------------------------------
|
---|
10 | int GeoMeshView::handle(int event)
|
---|
11 | {
|
---|
12 | int key;
|
---|
13 | int button;
|
---|
14 | int x;
|
---|
15 | int y;
|
---|
16 |
|
---|
17 | if (geoMesh != NULL)
|
---|
18 | {
|
---|
19 | draw();
|
---|
20 | }
|
---|
21 |
|
---|
22 | switch(event)
|
---|
23 | {
|
---|
24 | case fltk::KEY:
|
---|
25 | case fltk::PUSH:
|
---|
26 |
|
---|
27 | button = fltk::event_key();
|
---|
28 | mMouseX = fltk::event_x();
|
---|
29 | mMouseY = fltk::event_y();
|
---|
30 |
|
---|
31 | return 1;
|
---|
32 |
|
---|
33 | case fltk::DRAG:
|
---|
34 |
|
---|
35 | button = fltk::event_state();
|
---|
36 | x = fltk::event_x();
|
---|
37 | y = fltk::event_y();
|
---|
38 |
|
---|
39 | switch (button)
|
---|
40 | {
|
---|
41 | // Button 1 Pushed.
|
---|
42 | case 0x01100000:
|
---|
43 |
|
---|
44 | if (x > mMouseX)
|
---|
45 | {
|
---|
46 | if (mPan)
|
---|
47 | {
|
---|
48 | xshift = xshift + (x - mMouseX);
|
---|
49 | }
|
---|
50 | else
|
---|
51 | {
|
---|
52 | hAng = hAng + (x - mMouseX);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | else
|
---|
56 | {
|
---|
57 | if (x < mMouseX)
|
---|
58 | {
|
---|
59 | if (mPan)
|
---|
60 | {
|
---|
61 | xshift = xshift - (mMouseX - x);
|
---|
62 | }
|
---|
63 | else
|
---|
64 | {
|
---|
65 | hAng = hAng - (mMouseX - x);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (y > mMouseY)
|
---|
71 | {
|
---|
72 | if (mPan)
|
---|
73 | {
|
---|
74 | yshift = yshift - (y - mMouseY);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | vAng = vAng + (y - mMouseY);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | if (y < mMouseY)
|
---|
84 | {
|
---|
85 | if (mPan)
|
---|
86 | {
|
---|
87 | yshift = yshift + (mMouseY - y);
|
---|
88 | }
|
---|
89 | else
|
---|
90 | {
|
---|
91 | vAng = vAng - (mMouseY - y);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | hAng = float(int(hAng) % 360);
|
---|
97 | vAng = float(int(vAng) % 360);
|
---|
98 |
|
---|
99 | mMouseX = x;
|
---|
100 | mMouseY = y;
|
---|
101 |
|
---|
102 | draw();
|
---|
103 |
|
---|
104 | return 1;
|
---|
105 |
|
---|
106 | case 0x04100000:
|
---|
107 |
|
---|
108 | if (y > mSizeY)
|
---|
109 | {
|
---|
110 | size++;
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | if (y < mSizeY)
|
---|
115 | {
|
---|
116 | size--;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | mSizeY = y;
|
---|
121 |
|
---|
122 | draw();
|
---|
123 |
|
---|
124 | return 1;
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | case fltk::MOUSEWHEEL:
|
---|
129 |
|
---|
130 | size = size + fltk::event_dy();
|
---|
131 | draw();
|
---|
132 | return 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return fltk::GlWindow::handle(event);
|
---|
136 | }
|
---|
137 |
|
---|
138 | //---------------------------------------------------------------------------
|
---|
139 | // Get the number of frames per second.
|
---|
140 | //---------------------------------------------------------------------------
|
---|
141 | size_t GeoMeshView::getFPS()
|
---|
142 | {
|
---|
143 | return mFPS;
|
---|
144 | }
|
---|
145 |
|
---|
146 | //---------------------------------------------------------------------------
|
---|
147 | // Calculate the number of frames per second.
|
---|
148 | //---------------------------------------------------------------------------
|
---|
149 | void GeoMeshView::calcFPS()
|
---|
150 | {
|
---|
151 | double duration;
|
---|
152 | clock_t finish_time;
|
---|
153 |
|
---|
154 | // Gets the time.
|
---|
155 | finish_time = clock();
|
---|
156 |
|
---|
157 | // Calc the duration respect the start time.
|
---|
158 | duration = (double)((finish_time - mStartTime) / CLOCKS_PER_SEC);
|
---|
159 |
|
---|
160 | // Increments the number of frames per second.
|
---|
161 | mFPS++;
|
---|
162 |
|
---|
163 | // If a second have passed.
|
---|
164 | if (duration >= 1.0)
|
---|
165 | {
|
---|
166 | // Repaint the FPS.
|
---|
167 | mGeoMeshViewUI->refreshFPS(mFPS);
|
---|
168 |
|
---|
169 | mFPS = 0;
|
---|
170 | mStartTime = clock();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | //---------------------------------------------------------------------------
|
---|
175 | // Enable color strips.
|
---|
176 | //---------------------------------------------------------------------------
|
---|
177 | void GeoMeshView::enableColorStrips()
|
---|
178 | {
|
---|
179 | colorStripsFlag = true;
|
---|
180 | }
|
---|
181 |
|
---|
182 | //---------------------------------------------------------------------------
|
---|
183 | // Disable color strips.
|
---|
184 | //---------------------------------------------------------------------------
|
---|
185 | void GeoMeshView::disableColorStrips()
|
---|
186 | {
|
---|
187 | colorStripsFlag = false;
|
---|
188 | }
|
---|
189 |
|
---|
190 | //---------------------------------------------------------------------------
|
---|
191 | // Gets the color strips state.
|
---|
192 | //---------------------------------------------------------------------------
|
---|
193 | bool GeoMeshView::getColorStrips()
|
---|
194 | {
|
---|
195 | return colorStripsFlag;
|
---|
196 | }
|
---|
197 |
|
---|
198 | //---------------------------------------------------------------------------
|
---|
199 | // Set the color to the submesh sumbmeshIndex
|
---|
200 | //---------------------------------------------------------------------------
|
---|
201 | void GeoMeshView::setSubMeshSelected(int submeshIndex)
|
---|
202 | {
|
---|
203 | mSubMeshSelected = 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 | //---------------------------------------------------------------------------
|
---|
280 | // Get the bounding box of the object.
|
---|
281 | //---------------------------------------------------------------------------
|
---|
282 | GeometryBounds GeoMeshView::getBoundingBox()
|
---|
283 | {
|
---|
284 | SubMesh *geosubmesh;
|
---|
285 | Index position;
|
---|
286 | Vector3 vector3;
|
---|
287 | GeometryBounds bounds;
|
---|
288 | float x,y,z;
|
---|
289 | float xMin,yMin,zMin;
|
---|
290 | float xMax,yMax,zMax;
|
---|
291 |
|
---|
292 | // For each submesh.
|
---|
293 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
294 | {
|
---|
295 | // Gets the actual submesh.
|
---|
296 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
297 |
|
---|
298 | // For each index of the strip.
|
---|
299 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
300 | {
|
---|
301 | position = geosubmesh->mIndex[index];
|
---|
302 |
|
---|
303 | // Gets the vertex coordinates.
|
---|
304 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
305 |
|
---|
306 | x = vector3[0];
|
---|
307 | y = vector3[1];
|
---|
308 | z = vector3[2];
|
---|
309 |
|
---|
310 | // If is the first iteation.
|
---|
311 | if ((submesh == 0) && (index == 0))
|
---|
312 | {
|
---|
313 | xMin = xMax = x;
|
---|
314 | yMin = yMax = y;
|
---|
315 | zMin = zMax = z;
|
---|
316 | }
|
---|
317 | else
|
---|
318 | {
|
---|
319 | if (x < xMin)
|
---|
320 | {
|
---|
321 | xMin = x;
|
---|
322 | }
|
---|
323 | else if (x > xMax)
|
---|
324 | {
|
---|
325 | xMax = x;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (y < yMin)
|
---|
329 | {
|
---|
330 | yMin = y;
|
---|
331 | }
|
---|
332 | else if (y > yMax)
|
---|
333 | {
|
---|
334 | yMax = y;
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (z < zMin)
|
---|
338 | {
|
---|
339 | zMin = z;
|
---|
340 | }
|
---|
341 | else if (z > zMax)
|
---|
342 | {
|
---|
343 | zMax = z;
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | xMed = (xMax + xMin) / 2;
|
---|
350 | yMed = (yMax + yMin) / 2;
|
---|
351 | zMed = (zMax + zMin) / 2;
|
---|
352 |
|
---|
353 |
|
---|
354 | if ((xMax - xMin) > (yMax - yMin))
|
---|
355 | {
|
---|
356 | if ((xMax - xMin) > (zMax - zMin))
|
---|
357 | {
|
---|
358 | mScaleFactor = xMax - xMin;
|
---|
359 | }
|
---|
360 | else
|
---|
361 | {
|
---|
362 | mScaleFactor = zMax - zMin;
|
---|
363 | }
|
---|
364 | }
|
---|
365 | else
|
---|
366 | {
|
---|
367 | if ((yMax - yMin) > (zMax - zMin))
|
---|
368 | {
|
---|
369 | mScaleFactor = yMax - yMin;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | mScaleFactor = zMax - zMin;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | // Set the bounds.
|
---|
378 | bounds.minx = xMin;
|
---|
379 | bounds.miny = yMin;
|
---|
380 | bounds.minz = zMin;
|
---|
381 | bounds.maxx = xMax;
|
---|
382 | bounds.maxy = yMax;
|
---|
383 | bounds.maxz = zMax;
|
---|
384 | bounds.radius = mScaleFactor;
|
---|
385 |
|
---|
386 | return bounds;
|
---|
387 | }
|
---|
388 | */
|
---|
389 |
|
---|
390 | //---------------------------------------------------------------------------
|
---|
391 | // Constructor.
|
---|
392 | //---------------------------------------------------------------------------
|
---|
393 | GeoMeshView::GeoMeshView( int x,
|
---|
394 | int y,
|
---|
395 | int w,
|
---|
396 | int h,
|
---|
397 | const char *l,
|
---|
398 | GeoMeshViewUI *geoMeshViewUI)
|
---|
399 | : fltk::GlWindow(x,y,w,h,l)
|
---|
400 | {
|
---|
401 | mStripColors = NULL;
|
---|
402 | mSharedPosArray = NULL;
|
---|
403 | mSharedNorArray = NULL;
|
---|
404 | mSharedTexCoordArray = NULL;
|
---|
405 | mPosArray = NULL;
|
---|
406 | mNorArray = NULL;
|
---|
407 | mTexCoordArray = NULL;
|
---|
408 | mIndexArray = NULL;
|
---|
409 | vAng = 0.0;
|
---|
410 | hAng = 0.0;
|
---|
411 | size = 0.0;
|
---|
412 | mSizeY = 0;
|
---|
413 | xshift = 0.0;
|
---|
414 | yshift = 0.0;
|
---|
415 | geoMesh = 0;
|
---|
416 | mRotate = true;
|
---|
417 | mPan = false;
|
---|
418 | mLodStrip = false;
|
---|
419 | mLodTree = false;
|
---|
420 | mIdVisualList = 0;
|
---|
421 | mSubMeshCount = 0;
|
---|
422 | mStripColorsCount = 0;
|
---|
423 |
|
---|
424 | // Initialize the model view.
|
---|
425 | deactiveWire();
|
---|
426 | deactiveSolid();
|
---|
427 | activeRotate();
|
---|
428 | deactiveCW();
|
---|
429 | activeCCW();
|
---|
430 |
|
---|
431 | // Initialize start time for FPS.
|
---|
432 | mStartTime = clock();
|
---|
433 |
|
---|
434 | // Cross Reference.
|
---|
435 | mGeoMeshViewUI = geoMeshViewUI;
|
---|
436 | current_texture = 0;
|
---|
437 |
|
---|
438 | current_texture_submesh = NULL;
|
---|
439 | use_texture_mapping = true;
|
---|
440 | }
|
---|
441 |
|
---|
442 | // The color used for the edges of the bounding cube.
|
---|
443 | #define CUBECOLOR 255,255,255,255
|
---|
444 |
|
---|
445 | /* Draw a colored cube */
|
---|
446 | #define ALPHA 0.5
|
---|
447 |
|
---|
448 | //---------------------------------------------------------------------------
|
---|
449 | // Sets the Mesh to display.
|
---|
450 | //---------------------------------------------------------------------------
|
---|
451 | void GeoMeshView::setMesh(Mesh *geomesh)
|
---|
452 | {
|
---|
453 | GLfloat white_color[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
454 |
|
---|
455 | this->geoMesh = geomesh;
|
---|
456 | mSubMeshSelected = -1;
|
---|
457 | leavesSubMesh = -1;
|
---|
458 | mScaleFactor = geomesh->mMeshBounds.scaleFactor;
|
---|
459 |
|
---|
460 | // Refresh vertices to the vertex array.
|
---|
461 | refreshVertices(geomesh);
|
---|
462 |
|
---|
463 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white_color);
|
---|
464 |
|
---|
465 | if (current_texture_submesh)
|
---|
466 | {
|
---|
467 | delete [] current_texture_submesh;
|
---|
468 | }
|
---|
469 |
|
---|
470 | current_texture_submesh = new GLuint[getSubMeshCount()];
|
---|
471 |
|
---|
472 | for (int i = 0; i < getSubMeshCount(); i++)
|
---|
473 | {
|
---|
474 | current_texture_submesh[i] = 0;
|
---|
475 |
|
---|
476 | LoadTextureSubMesh(i, mMeshTexFiles[i].c_str());
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | //---------------------------------------------------------------------------
|
---|
481 | // Refresh vertices to the vertex array.
|
---|
482 | //---------------------------------------------------------------------------
|
---|
483 | void GeoMeshView::refreshVertices(Mesh *geomesh)
|
---|
484 | {
|
---|
485 | SubMesh *geosubmesh;
|
---|
486 | VertexBuffer *vertex_buffer;
|
---|
487 |
|
---|
488 | // If the vertex array exists.
|
---|
489 | if (mSubMeshCount > 0)
|
---|
490 | {
|
---|
491 | for (int submesh = 0; submesh < mSubMeshCount; submesh++)
|
---|
492 | {
|
---|
493 | delete [] mPosArray[submesh];
|
---|
494 | delete [] mNorArray[submesh];
|
---|
495 | delete [] mIndexArray[submesh];
|
---|
496 | delete [] mTexCoordArray[submesh];
|
---|
497 | }
|
---|
498 |
|
---|
499 | delete [] mSharedPosArray;
|
---|
500 | delete [] mSharedNorArray;
|
---|
501 | delete [] mSharedTexCoordArray;
|
---|
502 | delete [] mPosArray;
|
---|
503 | delete [] mNorArray;
|
---|
504 | delete [] mIndexArray;
|
---|
505 | }
|
---|
506 |
|
---|
507 | // If there is shared vertex.
|
---|
508 | if (geomesh->mVertexBuffer != NULL)
|
---|
509 | {
|
---|
510 | vertex_buffer = geomesh->mVertexBuffer;
|
---|
511 |
|
---|
512 | // Allocate memory.
|
---|
513 | mSharedPosArray = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
514 | mSharedNorArray = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
515 |
|
---|
516 | if (vertex_buffer->mTexCoords)
|
---|
517 | {
|
---|
518 | mSharedTexCoordArray = new GLfloat[vertex_buffer->mVertexCount * 2];
|
---|
519 | }
|
---|
520 | else
|
---|
521 | {
|
---|
522 | mSharedTexCoordArray = NULL;
|
---|
523 | }
|
---|
524 |
|
---|
525 | for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
|
---|
526 | {
|
---|
527 | mSharedPosArray[3 * vertex] = vertex_buffer->mPosition[vertex].x;
|
---|
528 | mSharedPosArray[(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y;
|
---|
529 | mSharedPosArray[(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z;
|
---|
530 |
|
---|
531 | mSharedNorArray[3 * vertex] = vertex_buffer->mNormal[vertex].x;
|
---|
532 | mSharedNorArray[(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y;
|
---|
533 | mSharedNorArray[(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z;
|
---|
534 |
|
---|
535 | if (vertex_buffer->mTexCoords)
|
---|
536 | {
|
---|
537 | mSharedTexCoordArray[2 * vertex] = vertex_buffer->
|
---|
538 | mTexCoords[vertex].x;
|
---|
539 |
|
---|
540 | mSharedTexCoordArray[(2 * vertex) + 1] = vertex_buffer->
|
---|
541 | mTexCoords[vertex].y;
|
---|
542 | }
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | // Build the arrays of vertices and indices.
|
---|
547 | mPosArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
548 | mNorArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
549 | mTexCoordArray = new GLfloat*[geomesh->mSubMeshCount];
|
---|
550 | mIndexArray = new GLuint*[geomesh->mSubMeshCount];
|
---|
551 |
|
---|
552 | // For each submesh.
|
---|
553 | for (int submesh = 0; submesh < geomesh->mSubMeshCount; submesh++)
|
---|
554 | {
|
---|
555 | // Gets the actual submesh.
|
---|
556 | geosubmesh = &geomesh->mSubMesh[submesh];
|
---|
557 |
|
---|
558 | // Allocate memory for index array of the actual submesh.
|
---|
559 | mIndexArray[submesh] = new GLuint[geosubmesh->mIndexCount];
|
---|
560 |
|
---|
561 | // Gets the indices of the actual submesh.
|
---|
562 | for (int index = 0; index < geosubmesh->mIndexCount; index++)
|
---|
563 | {
|
---|
564 | mIndexArray[submesh][index] = geosubmesh->mIndex[index];
|
---|
565 | }
|
---|
566 |
|
---|
567 | // If the submesh is NOT shared vertex.
|
---|
568 | if (!geosubmesh->mSharedVertexBuffer)
|
---|
569 | {
|
---|
570 | vertex_buffer = geosubmesh->mVertexBuffer;
|
---|
571 |
|
---|
572 | // Allocate memory for vertices of the actual submesh.
|
---|
573 | mPosArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
574 | mNorArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 3];
|
---|
575 | mTexCoordArray[submesh] = new GLfloat[vertex_buffer->mVertexCount * 2];
|
---|
576 |
|
---|
577 | // For each one of the vertex.
|
---|
578 | for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
|
---|
579 | {
|
---|
580 | mPosArray[submesh][3 * vertex] = vertex_buffer->mPosition[vertex].x;
|
---|
581 | mPosArray[submesh][(3 * vertex) + 1] = vertex_buffer->mPosition[vertex].y;
|
---|
582 | mPosArray[submesh][(3 * vertex) + 2] = vertex_buffer->mPosition[vertex].z;
|
---|
583 |
|
---|
584 | mNorArray[submesh][3 * vertex] = vertex_buffer->mNormal[vertex].x;
|
---|
585 | mNorArray[submesh][(3 * vertex) + 1] = vertex_buffer->mNormal[vertex].y;
|
---|
586 | mNorArray[submesh][(3 * vertex) + 2] = vertex_buffer->mNormal[vertex].z;
|
---|
587 |
|
---|
588 | mTexCoordArray[submesh][2 * vertex] = vertex_buffer->mTexCoords[vertex].x;
|
---|
589 | mTexCoordArray[submesh][(2 * vertex) + 1] = vertex_buffer->mTexCoords[vertex].y;
|
---|
590 | }// End for each vertex.
|
---|
591 | }
|
---|
592 | // If the submesh is shared vertex.
|
---|
593 | else
|
---|
594 | {
|
---|
595 | mPosArray[submesh] = NULL;
|
---|
596 | mNorArray[submesh] = NULL;
|
---|
597 | mTexCoordArray[submesh] = NULL;
|
---|
598 | }
|
---|
599 |
|
---|
600 | }// End for each submesh.
|
---|
601 |
|
---|
602 | mSubMeshCount = geoMesh->mSubMeshCount;
|
---|
603 | }
|
---|
604 |
|
---|
605 | //---------------------------------------------------------------------------
|
---|
606 | // Gets the triangle count of the current lod.
|
---|
607 | //---------------------------------------------------------------------------
|
---|
608 | size_t GeoMeshView::getLodStripsTriangleCount()
|
---|
609 | {
|
---|
610 | // For each strip.
|
---|
611 | // MultiIndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
|
---|
612 | /* IndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
|
---|
613 | size_t triangle_count = dataInterface->GetNumValidIndices() - 2;
|
---|
614 |
|
---|
615 | return triangle_count;*/
|
---|
616 |
|
---|
617 | return lodStripsLib->GetCurrentTriangleCount();
|
---|
618 | }
|
---|
619 |
|
---|
620 | //---------------------------------------------------------------------------
|
---|
621 | // Repaint the Mesh.
|
---|
622 | //---------------------------------------------------------------------------
|
---|
623 | void GeoMeshView::drawGeoMesh(bool paintWire)
|
---|
624 | {
|
---|
625 | SubMesh *geosubmesh;
|
---|
626 | bool submesh_selected;
|
---|
627 |
|
---|
628 | if (geoMesh)
|
---|
629 | {
|
---|
630 | if (mLodStrip)
|
---|
631 | {
|
---|
632 | drawLodStrip();
|
---|
633 | }
|
---|
634 | else if (mLodTree)
|
---|
635 | {
|
---|
636 | drawLodTree();
|
---|
637 | }
|
---|
638 | else
|
---|
639 | {
|
---|
640 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
641 | {
|
---|
642 | // Gets the actual submesh.
|
---|
643 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
644 |
|
---|
645 | if (geosubmesh->mType == GEO_TRIANGLE_LIST)
|
---|
646 | {
|
---|
647 | if (mSubMeshSelected == submesh)
|
---|
648 | {
|
---|
649 | submesh_selected = true;
|
---|
650 | }
|
---|
651 | else
|
---|
652 | {
|
---|
653 | submesh_selected = false;
|
---|
654 | }
|
---|
655 |
|
---|
656 | // Draw the actual submesh in triangle list.
|
---|
657 | drawTriangleList(submesh,submesh_selected,paintWire);
|
---|
658 | }
|
---|
659 | else
|
---|
660 | {
|
---|
661 | // Draw the actual submesh in triangle strip.
|
---|
662 | drawTriangleStrip(submesh);
|
---|
663 | }
|
---|
664 | }
|
---|
665 | }
|
---|
666 | }
|
---|
667 | }// End drawGeoMesh.
|
---|
668 |
|
---|
669 | //---------------------------------------------------------------------------
|
---|
670 | // Paint eah strip separately.
|
---|
671 | //---------------------------------------------------------------------------
|
---|
672 | void GeoMeshView::drawTriangleStrip(int submesh)
|
---|
673 | {
|
---|
674 | SubMesh *geosubmesh;
|
---|
675 | Index position;
|
---|
676 | Vector3 vector3;
|
---|
677 | Vector2 vector2;
|
---|
678 | float x,y,z;
|
---|
679 | float r,g,b;
|
---|
680 | Index *index;
|
---|
681 | Index *indexBegin;
|
---|
682 | Index *indexEnd;
|
---|
683 | int color_index;
|
---|
684 |
|
---|
685 | color_index = 0;
|
---|
686 |
|
---|
687 | // Gets the actual submesh.
|
---|
688 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
689 |
|
---|
690 | // bind current texture to use
|
---|
691 | bool usetex = SetTextureRenderingState(submesh);
|
---|
692 |
|
---|
693 | // For each one of the strips.
|
---|
694 | for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
|
---|
695 | {
|
---|
696 | // Paint the current strip.
|
---|
697 |
|
---|
698 | // First index of the strip.
|
---|
699 | indexBegin = geosubmesh->mStrip[strip];
|
---|
700 |
|
---|
701 | // If the strips is not the first.
|
---|
702 | //if (strip != 0)
|
---|
703 | //{
|
---|
704 | // indexBegin++;
|
---|
705 | //}
|
---|
706 |
|
---|
707 | int size= 0;
|
---|
708 | // If is the final strip
|
---|
709 | if (strip == (geosubmesh->mStripCount - 1))
|
---|
710 | {
|
---|
711 | // The end of the index array.
|
---|
712 | indexEnd = &geosubmesh->mIndex[geosubmesh->mIndexCount];
|
---|
713 | size= indexEnd - indexBegin;
|
---|
714 | }
|
---|
715 | else
|
---|
716 | {
|
---|
717 | // The beginning of the next strip.
|
---|
718 | indexEnd = geosubmesh->mStrip[strip + 1];
|
---|
719 |
|
---|
720 | for (index = indexBegin; index < indexEnd; index++)
|
---|
721 | {
|
---|
722 | size++;
|
---|
723 | }
|
---|
724 | // Remove degenerated
|
---|
725 | //indexEnd--;
|
---|
726 | }
|
---|
727 |
|
---|
728 | int i;
|
---|
729 | i = 0;
|
---|
730 |
|
---|
731 | if (getColorStrips())
|
---|
732 | {
|
---|
733 | // Gets the color of the strip.
|
---|
734 | r = mStripColors[submesh][color_index].r;
|
---|
735 | g = mStripColors[submesh][color_index].g;
|
---|
736 | b = mStripColors[submesh][color_index].b;
|
---|
737 |
|
---|
738 | // Change to the next color.
|
---|
739 | color_index++;
|
---|
740 |
|
---|
741 | // Paint a new color.
|
---|
742 | glColor3f(r,g,b);
|
---|
743 | GLfloat color[] = {r,g,b,1.0f};
|
---|
744 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color);
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | //VERTEX ARRAYS
|
---|
749 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
750 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
751 |
|
---|
752 | if (usetex)
|
---|
753 | {
|
---|
754 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
755 | }
|
---|
756 | else
|
---|
757 | {
|
---|
758 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
759 | }
|
---|
760 |
|
---|
761 | glVertexPointer(3, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mPosition);
|
---|
762 | glNormalPointer(GL_FLOAT, 0, geosubmesh->mVertexBuffer->mNormal);
|
---|
763 |
|
---|
764 | if (usetex)
|
---|
765 | {
|
---|
766 | glTexCoordPointer(2, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mTexCoords);
|
---|
767 | }
|
---|
768 |
|
---|
769 | glDrawElements( GL_TRIANGLE_STRIP,
|
---|
770 | size,
|
---|
771 | GL_UNSIGNED_INT,
|
---|
772 | indexBegin);
|
---|
773 |
|
---|
774 | if (submesh == leavesSubMesh)
|
---|
775 | {
|
---|
776 | glDisable(GL_ALPHA_TEST);
|
---|
777 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
|
---|
778 | }
|
---|
779 | }
|
---|
780 | }//End drawTriangleStrip.
|
---|
781 |
|
---|
782 | //---------------------------------------------------------------------------
|
---|
783 | // Paint the array of idices.
|
---|
784 | //---------------------------------------------------------------------------
|
---|
785 | void GeoMeshView::drawTriangleList( int submesh,
|
---|
786 | bool selectedSubMesh,
|
---|
787 | bool paintWire)
|
---|
788 | {
|
---|
789 | SubMesh *geosubmesh;
|
---|
790 | Index position;
|
---|
791 | Vector3 vector3;
|
---|
792 | float x,y,z;
|
---|
793 | int idx_offset;
|
---|
794 | int vrt_offset;
|
---|
795 |
|
---|
796 | // Index offset.
|
---|
797 | idx_offset = 0;
|
---|
798 |
|
---|
799 | // Vertex offset.
|
---|
800 | vrt_offset = 0;
|
---|
801 |
|
---|
802 | // Gets the actual submesh.
|
---|
803 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
804 |
|
---|
805 | // bind current texture to use
|
---|
806 | bool usetex = SetTextureRenderingState(submesh);
|
---|
807 |
|
---|
808 | // If wire is not selected.
|
---|
809 | if (!paintWire)
|
---|
810 | {
|
---|
811 | // If a mesh is selected.
|
---|
812 | if (selectedSubMesh)
|
---|
813 | {
|
---|
814 | glColor3d(1.0,0.0,0.0);
|
---|
815 | GLfloat red[] = {1.0f,0.0f,0.0f,1.0f};
|
---|
816 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red);
|
---|
817 | }
|
---|
818 | // If is a tree.
|
---|
819 | else if (leavesSubMesh >= 0)
|
---|
820 | {
|
---|
821 | glColor3d(1.0, 0.5, 0.5);
|
---|
822 | GLfloat brown[] = {1.0f,0.5f,0.5f,1.0f};
|
---|
823 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown);
|
---|
824 |
|
---|
825 | // For each index of the strip.
|
---|
826 | if (submesh == leavesSubMesh)
|
---|
827 | {
|
---|
828 | if (!usetex)
|
---|
829 | {
|
---|
830 | glColor3d(0.0,1.0,0.0);
|
---|
831 | GLfloat green[] = {0.0f,1.0f,0.0f,1.0f};
|
---|
832 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green);
|
---|
833 | }
|
---|
834 | else
|
---|
835 | {
|
---|
836 | glEnable(GL_ALPHA_TEST);
|
---|
837 | glAlphaFunc(GL_GREATER,0.5f);
|
---|
838 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
|
---|
839 | }
|
---|
840 | }
|
---|
841 | }
|
---|
842 | // If is NOT a tree.
|
---|
843 | else
|
---|
844 | {
|
---|
845 | // Set white color to the object.
|
---|
846 | glColor3d(1.0, 1.0, 1.0);
|
---|
847 | GLfloat white[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
848 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
|
---|
849 | }
|
---|
850 | }
|
---|
851 |
|
---|
852 | if (usetex)
|
---|
853 | {
|
---|
854 | glColor4f(1.0f,1.0f,1.0f,1.0f);
|
---|
855 | GLfloat white[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
856 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
|
---|
857 | }
|
---|
858 |
|
---|
859 | // Enable arrays.
|
---|
860 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
861 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
862 | if (usetex)
|
---|
863 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
864 | else
|
---|
865 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
866 |
|
---|
867 | // If the submesh is shared vertex.
|
---|
868 | if (geosubmesh->mSharedVertexBuffer)
|
---|
869 | {
|
---|
870 | glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray);
|
---|
871 | glNormalPointer(GL_FLOAT, 0, mSharedNorArray);
|
---|
872 | if (usetex)
|
---|
873 | glTexCoordPointer(2, GL_FLOAT, 0, mSharedTexCoordArray);
|
---|
874 | }
|
---|
875 | else
|
---|
876 | {
|
---|
877 | glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]);
|
---|
878 | glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]);
|
---|
879 | if (usetex)
|
---|
880 | glTexCoordPointer(2, GL_FLOAT, 0, mTexCoordArray[submesh]);
|
---|
881 | }
|
---|
882 |
|
---|
883 | glDrawElements( GL_TRIANGLES,
|
---|
884 | geosubmesh->mIndexCount,
|
---|
885 | GL_UNSIGNED_INT,
|
---|
886 | mIndexArray[submesh]);
|
---|
887 |
|
---|
888 | if (submesh==leavesSubMesh)
|
---|
889 | {
|
---|
890 | glDisable(GL_ALPHA_TEST);
|
---|
891 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
|
---|
892 | }
|
---|
893 |
|
---|
894 | }//End drawTriangleList
|
---|
895 |
|
---|
896 | //---------------------------------------------------------------------------
|
---|
897 | // Paint lodstrip object.
|
---|
898 | //---------------------------------------------------------------------------
|
---|
899 | void GeoMeshView::drawLodStrip()
|
---|
900 | {
|
---|
901 | SubMesh *geosubmesh;
|
---|
902 | Index position;
|
---|
903 | Vector3 vector3;
|
---|
904 | Vector2 vector2;
|
---|
905 | float x,y,z;
|
---|
906 | float r,g,b;
|
---|
907 | int color_index;
|
---|
908 | int current_strip;
|
---|
909 |
|
---|
910 | // Initialize current strip.
|
---|
911 | current_strip = 0;
|
---|
912 |
|
---|
913 | // For each submesh.
|
---|
914 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
915 | {
|
---|
916 | // bind current texture to use
|
---|
917 | bool usetex = SetTextureRenderingState(submesh);
|
---|
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 | int indices_to_render = lodStripsLib->GetValidIndexCount(submesh);
|
---|
926 |
|
---|
927 | glBegin(GL_TRIANGLE_STRIP);
|
---|
928 |
|
---|
929 | for ( int index = 0;
|
---|
930 | index < mGeoMeshViewUI->lod_index_data->indexCount[submesh];
|
---|
931 | index ++)
|
---|
932 | {
|
---|
933 | position = mGeoMeshViewUI->lod_index_data->indices[submesh][index];
|
---|
934 |
|
---|
935 | // Gets the vertex normal.
|
---|
936 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
937 |
|
---|
938 | x = vector3[0];
|
---|
939 | y = vector3[1];
|
---|
940 | z = vector3[2];
|
---|
941 |
|
---|
942 | // Sets the vertex normal.
|
---|
943 | glNormal3f(x,y,z);
|
---|
944 |
|
---|
945 | // set the texture coordinates if needed
|
---|
946 | if (usetex)
|
---|
947 | {
|
---|
948 | vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
|
---|
949 | x = vector2[0];
|
---|
950 | y = vector2[1];
|
---|
951 |
|
---|
952 | glTexCoord2f(x,y);
|
---|
953 | }
|
---|
954 |
|
---|
955 | // Gets the vertex coordinates.
|
---|
956 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
957 |
|
---|
958 | x = vector3[0];
|
---|
959 | y = vector3[1];
|
---|
960 | z = vector3[2];
|
---|
961 |
|
---|
962 | // Sets the vertex position.
|
---|
963 | glVertex3f(x,y,z);
|
---|
964 | }
|
---|
965 | glEnd();
|
---|
966 |
|
---|
967 | }
|
---|
968 |
|
---|
969 | }//End drawTriangleStrip.
|
---|
970 |
|
---|
971 | //---------------------------------------------------------------------------
|
---|
972 | // Paint lodtree object.
|
---|
973 | //---------------------------------------------------------------------------
|
---|
974 | void GeoMeshView::drawLodTree()
|
---|
975 | {
|
---|
976 | SubMesh *geosubmesh;
|
---|
977 | Index position;
|
---|
978 | Vector3 vector3;
|
---|
979 | Vector2 vector2;
|
---|
980 | float x,y,z;
|
---|
981 | float r,g,b;
|
---|
982 | int color_index;
|
---|
983 | int current_strip;
|
---|
984 |
|
---|
985 | // Initialize current strip.
|
---|
986 | current_strip = 0;
|
---|
987 |
|
---|
988 | // DRAW THE TRUNK AS A LODSTRIP OBJECT
|
---|
989 | // For each submesh.
|
---|
990 | for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
|
---|
991 | {
|
---|
992 | if (submesh == lodTreeLib->GetLeavesSubMesh())
|
---|
993 | {
|
---|
994 | continue;
|
---|
995 | }
|
---|
996 |
|
---|
997 | // bind current texture to use
|
---|
998 | bool usetex = SetTextureRenderingState(submesh);
|
---|
999 |
|
---|
1000 | color_index = 0;
|
---|
1001 |
|
---|
1002 | // Gets the actual submesh.
|
---|
1003 | geosubmesh = &geoMesh->mSubMesh[submesh];
|
---|
1004 |
|
---|
1005 | // For each one of the strips.
|
---|
1006 | int indices_to_render = lodTreeLib->GetValidTrunkIndexCount(submesh);
|
---|
1007 |
|
---|
1008 | glBegin(GL_TRIANGLE_STRIP);
|
---|
1009 |
|
---|
1010 | for ( int index = 0;
|
---|
1011 | index < mGeoMeshViewUI->lod_index_data->indexCount[submesh];
|
---|
1012 | index ++)
|
---|
1013 | {
|
---|
1014 | position = mGeoMeshViewUI->lod_index_data->indices[submesh][index];
|
---|
1015 |
|
---|
1016 | // Gets the vertex normal.
|
---|
1017 | vector3 = geosubmesh->mVertexBuffer->mNormal[position];
|
---|
1018 |
|
---|
1019 | x = vector3[0];
|
---|
1020 | y = vector3[1];
|
---|
1021 | z = vector3[2];
|
---|
1022 |
|
---|
1023 | // Sets the vertex normal.
|
---|
1024 | glNormal3f(x,y,z);
|
---|
1025 |
|
---|
1026 | // set the texture coordinates if needed
|
---|
1027 | if (usetex)
|
---|
1028 | {
|
---|
1029 | vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
|
---|
1030 | x = vector2[0];
|
---|
1031 | y = vector2[1];
|
---|
1032 | glTexCoord2f(x,y);
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | // Gets the vertex coordinates.
|
---|
1036 | vector3 = geosubmesh->mVertexBuffer->mPosition[position];
|
---|
1037 |
|
---|
1038 | x = vector3[0];
|
---|
1039 | y = vector3[1];
|
---|
1040 | z = vector3[2];
|
---|
1041 |
|
---|
1042 | // Sets the vertex position.
|
---|
1043 | glVertex3f(x,y,z);
|
---|
1044 | }
|
---|
1045 | glEnd();
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | // DRAW THE LEAVES AS A TRIANGLE SOUP
|
---|
1049 | // bind current texture to use
|
---|
1050 | bool usetex = SetTextureRenderingState(leavesSubMesh);
|
---|
1051 |
|
---|
1052 | glEnable(GL_ALPHA_TEST);
|
---|
1053 | glAlphaFunc(GL_GREATER,0.5f);
|
---|
1054 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
|
---|
1055 |
|
---|
1056 | glBegin(GL_TRIANGLES);
|
---|
1057 |
|
---|
1058 | SubMesh & foliageSubMesh = geoMesh->mSubMesh[lodTreeLib->GetLeavesSubMesh()];
|
---|
1059 | Vector3 auxv;
|
---|
1060 |
|
---|
1061 | for ( int j = 0;
|
---|
1062 | j < mGeoMeshViewUI->lod_index_data->
|
---|
1063 | indexCount[lodTreeLib->GetLeavesSubMesh()];
|
---|
1064 | j++)
|
---|
1065 | {
|
---|
1066 | int k = mGeoMeshViewUI->lod_index_data->
|
---|
1067 | indices[lodTreeLib->GetLeavesSubMesh()][j];
|
---|
1068 |
|
---|
1069 | if (usetex)
|
---|
1070 | {
|
---|
1071 | Vector2 tc = foliageSubMesh.mVertexBuffer->mTexCoords[k];
|
---|
1072 | glTexCoord2f(tc.x,tc.y);
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | auxv = foliageSubMesh.mVertexBuffer->mNormal[k];
|
---|
1076 |
|
---|
1077 | glNormal3f(auxv.x,auxv.y,auxv.z);
|
---|
1078 |
|
---|
1079 | auxv = foliageSubMesh.mVertexBuffer->mPosition[k];
|
---|
1080 |
|
---|
1081 | glVertex3f(auxv.x,auxv.y,auxv.z);
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | glEnd();
|
---|
1085 |
|
---|
1086 | glColor3f(1,1,1);
|
---|
1087 | glDisable(GL_ALPHA_TEST);
|
---|
1088 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | //---------------------------------------------------------------------------
|
---|
1092 | // Sets the lodstripslibrary object.
|
---|
1093 | //---------------------------------------------------------------------------
|
---|
1094 | void GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips)
|
---|
1095 | {
|
---|
1096 | lodStripsLib = lodstrips;
|
---|
1097 | activeLodStrip();
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | //---------------------------------------------------------------------------
|
---|
1101 | // Sets the lodtreeslibrary object.
|
---|
1102 | //---------------------------------------------------------------------------
|
---|
1103 | void GeoMeshView::setLodTreesLibrary(LodTreeLibrary *lodtrees)
|
---|
1104 | {
|
---|
1105 | lodTreeLib = lodtrees;
|
---|
1106 | activeLodTree();
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | //---------------------------------------------------------------------------
|
---|
1110 | // Change de Level of detail of the object.
|
---|
1111 | //---------------------------------------------------------------------------
|
---|
1112 | void GeoMeshView::GoToLod_LodStrip(float lod)
|
---|
1113 | {
|
---|
1114 | if (mLodStrip)
|
---|
1115 | {
|
---|
1116 | lodStripsLib->GoToLod(lod);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | if (mLodTree)
|
---|
1120 | {
|
---|
1121 | lodTreeLib->GoToTrunkLod(lod);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | draw();
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | //---------------------------------------------------------------------------
|
---|
1128 | // Change de Level of detail of the object.
|
---|
1129 | //---------------------------------------------------------------------------
|
---|
1130 | void GeoMeshView::GoToLod_LodTree(float lod)
|
---|
1131 | {
|
---|
1132 | if (mLodTree)
|
---|
1133 | {
|
---|
1134 | lodTreeLib->GoToFoliageLod(lod);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | draw();
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | //---------------------------------------------------------------------------
|
---|
1141 | // Draw the object in the window.
|
---|
1142 | //---------------------------------------------------------------------------
|
---|
1143 | void GeoMeshView::draw()
|
---|
1144 | {
|
---|
1145 | glMatrixMode(GL_PROJECTION);
|
---|
1146 | glLoadIdentity();
|
---|
1147 |
|
---|
1148 | glEnable(GL_LIGHTING);
|
---|
1149 |
|
---|
1150 | // Frustrum.
|
---|
1151 | glViewport(0,0,w(),h());
|
---|
1152 | gluPerspective(60,(float)w()/(float)h(),0.1,1000);
|
---|
1153 |
|
---|
1154 | glMatrixMode(GL_MODELVIEW);
|
---|
1155 | glLoadIdentity();
|
---|
1156 |
|
---|
1157 | glEnable(GL_BLEND);
|
---|
1158 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
1159 |
|
---|
1160 | GLfloat light_ambient[] = {0.3,0.3,0.3,1.0};
|
---|
1161 | GLfloat luzpos[] = {0.0,0.0,1.0,0.0};
|
---|
1162 | GLfloat luzcolor[] = {1.0,1.0,1.0,1.0};
|
---|
1163 |
|
---|
1164 | // glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
|
---|
1165 | glEnable(GL_LIGHT0);
|
---|
1166 | glLightfv(GL_LIGHT0, GL_POSITION, luzpos);
|
---|
1167 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
|
---|
1168 | glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor);
|
---|
1169 | glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1);
|
---|
1170 | glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0);
|
---|
1171 | glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
|
---|
1172 |
|
---|
1173 | glEnable (GL_POLYGON_OFFSET_LINE);
|
---|
1174 | glPolygonOffset(-0.5,1);
|
---|
1175 | glEnable(GL_DEPTH_TEST);
|
---|
1176 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
1177 |
|
---|
1178 | gluLookAt(0,0,2/*mScaleFactor*/ + size * 0.25, 0,0,0, 0,1,0);
|
---|
1179 |
|
---|
1180 | glTranslatef(xshift,yshift,0);
|
---|
1181 | glRotatef(hAng,0,1,0);
|
---|
1182 | glRotatef(vAng,1,0,0);
|
---|
1183 | //glTranslatef(-xMed,-yMed,-zMed);
|
---|
1184 |
|
---|
1185 | // Set white color to the object.
|
---|
1186 | glColor3d(1.0, 1.0, 1.0);
|
---|
1187 |
|
---|
1188 | GLfloat color_blanco[] = {1.0f,1.0f,1.0f,1.0f};
|
---|
1189 |
|
---|
1190 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
|
---|
1191 |
|
---|
1192 | glClearColor(0.3,0.3,0.3,0);
|
---|
1193 |
|
---|
1194 | if (mCW)
|
---|
1195 | {
|
---|
1196 | glFrontFace(GL_CW);
|
---|
1197 | glEnable(GL_CULL_FACE);
|
---|
1198 | }
|
---|
1199 | else
|
---|
1200 | {
|
---|
1201 | if (mCCW)
|
---|
1202 | {
|
---|
1203 | glFrontFace(GL_CCW);
|
---|
1204 | glEnable(GL_CULL_FACE);
|
---|
1205 | }
|
---|
1206 | else
|
---|
1207 | {
|
---|
1208 | glDisable(GL_CULL_FACE);
|
---|
1209 | }
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | if (mSolid)
|
---|
1213 | {
|
---|
1214 | enableColorStrips();
|
---|
1215 | //glDisable(GL_LIGHTING);
|
---|
1216 | glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
|
---|
1217 | drawGeoMesh(false);
|
---|
1218 | }
|
---|
1219 | else
|
---|
1220 | {
|
---|
1221 | glEnable(GL_LIGHTING);
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | if (mWire)
|
---|
1225 | {
|
---|
1226 | disableColorStrips();
|
---|
1227 |
|
---|
1228 | //glDisable(GL_LIGHTING);
|
---|
1229 |
|
---|
1230 | GLfloat color[4];
|
---|
1231 |
|
---|
1232 | if (mSolid)
|
---|
1233 | {
|
---|
1234 | glColor3d(0.0, 0.0, 0.0);
|
---|
1235 | color[0] = 0.0f;
|
---|
1236 | color[1] = 0.0f;
|
---|
1237 | color[2] = 0.0f;
|
---|
1238 | color[3] = 1.0f;
|
---|
1239 | }
|
---|
1240 | else
|
---|
1241 | {
|
---|
1242 | glColor3d(1.0, 1.0, 1.0);
|
---|
1243 | color[0] = 1.0f;
|
---|
1244 | color[1] = 1.0f;
|
---|
1245 | color[2] = 1.0f;
|
---|
1246 | color[3] = 1.0f;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color);
|
---|
1250 |
|
---|
1251 | glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
|
---|
1252 | drawGeoMesh(true);
|
---|
1253 | }
|
---|
1254 | else
|
---|
1255 | {
|
---|
1256 | if (!mSolid)
|
---|
1257 | {
|
---|
1258 | disableColorStrips();
|
---|
1259 | //glEnable(GL_LIGHTING);
|
---|
1260 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
---|
1261 | drawGeoMesh(false);
|
---|
1262 | }
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | swap_buffers();
|
---|
1266 |
|
---|
1267 | // Calculate the FPS.
|
---|
1268 | //calcFPS();
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | #include <IL/ILUT.h>
|
---|
1272 |
|
---|
1273 | void GeoMeshView::LoadTexture(const char *imgfile)
|
---|
1274 | {
|
---|
1275 | ilInit();
|
---|
1276 | ilutEnable(ILUT_OPENGL_CONV);
|
---|
1277 | ilutRenderer(ILUT_OPENGL);
|
---|
1278 |
|
---|
1279 | current_texture = ilutGLLoadImage((const ILstring)imgfile);
|
---|
1280 |
|
---|
1281 | if (!current_texture)
|
---|
1282 | {
|
---|
1283 | fltk::alert("Error loading texture!");
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 | ilShutDown();
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | void GeoMeshView::LoadTextureSubMesh(int isubmesh, const char *imgfile)
|
---|
1290 | {
|
---|
1291 | ilInit();
|
---|
1292 | ilutEnable(ILUT_OPENGL_CONV);
|
---|
1293 | ilutRenderer(ILUT_OPENGL);
|
---|
1294 |
|
---|
1295 | current_texture_submesh[isubmesh] = ilutGLLoadImage((const ILstring)imgfile);
|
---|
1296 |
|
---|
1297 | if (!current_texture_submesh[isubmesh])
|
---|
1298 | {
|
---|
1299 | fltk::alert("Error loading texture!");
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | ilShutDown();
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | // Add texture file name to the list of texture files of mesh.
|
---|
1306 | void GeoMeshView::addTexFile(const char *imgfile)
|
---|
1307 | {
|
---|
1308 | mMeshTexFiles.push_back(string(imgfile));
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | // Change texture file of a given submesh.
|
---|
1312 | void GeoMeshView::changeTexFile(int isubmesh, const char *imgfile)
|
---|
1313 | {
|
---|
1314 | mMeshTexFiles[isubmesh] = string(imgfile);
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | void GeoMeshView::resetTextures(void)
|
---|
1318 | {
|
---|
1319 | // Clears texture files list.
|
---|
1320 | mMeshTexFiles.clear();
|
---|
1321 |
|
---|
1322 | if (current_texture)
|
---|
1323 | {
|
---|
1324 | glDeleteTextures(1,¤t_texture);
|
---|
1325 |
|
---|
1326 | current_texture = 0;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | if (current_texture_submesh)
|
---|
1330 | {
|
---|
1331 | glDeleteTextures(getSubMeshCount(),current_texture_submesh);
|
---|
1332 |
|
---|
1333 | for (int i = 0; i < getSubMeshCount(); i++)
|
---|
1334 | {
|
---|
1335 | current_texture_submesh[i] = 0;
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | bool GeoMeshView::SetTextureRenderingState(int submesh)
|
---|
1341 | {
|
---|
1342 | GLuint usetex = 0;
|
---|
1343 |
|
---|
1344 | if (use_texture_mapping)
|
---|
1345 | {
|
---|
1346 | if (current_texture_submesh[submesh])
|
---|
1347 | {
|
---|
1348 | usetex = current_texture_submesh[submesh];
|
---|
1349 | }
|
---|
1350 | else if (current_texture)
|
---|
1351 | {
|
---|
1352 | usetex = current_texture;
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | if (usetex)
|
---|
1357 | {
|
---|
1358 | glEnable(GL_TEXTURE_2D);
|
---|
1359 | }
|
---|
1360 | else
|
---|
1361 | {
|
---|
1362 | glDisable(GL_TEXTURE_2D);
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | glBindTexture(GL_TEXTURE_2D,usetex);
|
---|
1366 |
|
---|
1367 | return (usetex != 0);
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | int GeoMeshView::findLeavesSubMesh(void)
|
---|
1371 | {
|
---|
1372 | for (int i = 0; i < geoMesh->mSubMeshCount; i++)
|
---|
1373 | {
|
---|
1374 | if (geoMesh->mSubMesh[i].mType == GEO_TRIANGLE_LIST)
|
---|
1375 | {
|
---|
1376 | return i;
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | return -1;
|
---|
1381 | }
|
---|
1382 |
|
---|