source: GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshView.cpp @ 895

Revision 895, 26.8 KB checked in by gumbau, 18 years ago (diff)

Working LODTree constructor and visualizer

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