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

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