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

Revision 1078, 33.9 KB checked in by gumbau, 18 years ago (diff)

Extended Mesh implementation

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//      Set the color to the submesh sumbmeshIndex
200//---------------------------------------------------------------------------
201void    GeoMeshView::setSubMeshSelected(int submeshIndex)
202{
203        mSubMeshSelected        =       submeshIndex;
204}
205
206//---------------------------------------------------------------------------
207//      Sets the leaves submesh index.
208//---------------------------------------------------------------------------
209void    GeoMeshView::setLeavesSubMesh(int submeshIndex)
210{
211        leavesSubMesh   =       submeshIndex;
212}
213
214//---------------------------------------------------------------------------
215//      Gets the leaves submesh index.
216//---------------------------------------------------------------------------
217int     GeoMeshView::getLeavesSubMesh()
218{
219        return  leavesSubMesh;
220}
221
222//---------------------------------------------------------------------------
223//      Set the list of strip colors.
224//---------------------------------------------------------------------------
225void    GeoMeshView::setStripColors()
226{
227        SubMesh *geosubmesh;
228       
229        //      Free memory of the strip colors array.
230        for (int submesh = 0; submesh < mStripColorsCount; submesh++)
231        {
232                delete  []mStripColors[submesh];
233        }
234       
235        delete  []mStripColors;
236       
237        //      Initialize the Colors array.
238        mStripColors    =       new     ColorElement*[geoMesh->mSubMeshCount];
239
240        //      Count the total number of strips y the submeshes.
241        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
242        {
243                //      Gets the actual submesh.
244                geosubmesh      =       &geoMesh->mSubMesh[submesh];
245               
246                if (geosubmesh->mType   == GEO_TRIANGLE_STRIPS)
247                {
248                        mStripColors[submesh]   =       new ColorElement[geosubmesh->mStripCount];
249                }
250                else
251                {
252                        mStripColors[submesh]   =       NULL;
253                }
254        }
255
256       
257        //      Sets random colors to each strip.
258        for (int submesh        = 0; submesh < geoMesh->mSubMeshCount; submesh++)
259        {
260                //      Gets the actual submesh.
261                geosubmesh      =       &geoMesh->mSubMesh[submesh];
262
263                if (geosubmesh->mType   == GEO_TRIANGLE_STRIPS)
264                {
265                        for     (int strip = 0; strip   <       geosubmesh->mStripCount; strip++)
266                        {
267                                mStripColors[submesh][strip].r  =       rand()/((float)RAND_MAX);
268                                mStripColors[submesh][strip].g  =       rand()/((float)RAND_MAX);
269                                mStripColors[submesh][strip].b  =       rand()/((float)RAND_MAX);
270                        }
271                }
272        }
273
274        //      Sets the submesh count.
275        mStripColorsCount       =       geoMesh->mSubMeshCount;
276}
277
278/*
279//---------------------------------------------------------------------------
280//      Get the bounding box of the object.
281//---------------------------------------------------------------------------
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//---------------------------------------------------------------------------
391//      Constructor.
392//---------------------------------------------------------------------------
393GeoMeshView::GeoMeshView(       int                                             x,
394                                                                                                        int                                             y,
395                                                                                                        int                                             w,
396                                                                                                        int                                             h,
397                                                                                                        const char              *l,
398                                                                                                        GeoMeshViewUI   *geoMeshViewUI)
399            : fltk::GlWindow(x,y,w,h,l)
400{
401                mStripColors            =       NULL;
402                mSharedPosArray         =       NULL;
403                mSharedNorArray         =       NULL;
404                mSharedTexCoordArray=   NULL;
405                mPosArray                       =       NULL;
406                mNorArray                       =       NULL;
407                mTexCoordArray          =       NULL;
408                mIndexArray                     =       NULL;
409                vAng                            = 0.0;
410                hAng                                                    =       0.0;
411                size                                                    =       0.0;
412                mSizeY                                          =       0;
413                xshift                                          =       0.0;
414                yshift                                          =       0.0;
415                geoMesh                                         =       0;
416                mRotate                                         =       true;
417                mPan                                                    =       false;
418                mLodStrip                                       =       false;
419                mLodTree                                        =       false;
420                mIdVisualList                   =       0;
421                mSubMeshCount                   =       0;
422                mStripColorsCount       =       0;
423               
424                //      Initialize the model view.
425                deactiveWire();
426                deactiveSolid();
427                activeRotate();
428                deactiveCW();
429                activeCCW();
430               
431                //      Initialize start time for FPS.
432                mStartTime      =       clock();
433
434                //      Cross Reference.
435                mGeoMeshViewUI  =       geoMeshViewUI;
436
437                current_texture = 0;
438                current_texture_submesh=NULL;
439                use_texture_mapping = true;
440}
441
442// The color used for the edges of the bounding cube.
443#define CUBECOLOR 255,255,255,255
444
445/* Draw a colored cube */
446#define ALPHA 0.5
447
448//---------------------------------------------------------------------------
449//      Sets the Mesh to display.
450//---------------------------------------------------------------------------
451void GeoMeshView::setMesh(Mesh  *geomesh)
452{
453        GLfloat color_blanco[]  =       {1.0f,1.0f,1.0f,1.0f};
454
455        this->geoMesh                   =       geomesh;
456        mSubMeshSelected        =       -1;
457        leavesSubMesh                   =       -1;
458        mScaleFactor                    =       geomesh->mMeshBounds.scaleFactor;
459       
460        //      Refresh vertices to the vertex array.
461        refreshVertices(geomesh);
462       
463        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
464
465        if (current_texture_submesh)
466                delete[] current_texture_submesh;
467        current_texture_submesh=new GLuint[getSubMeshCount()];
468        for (int i=0; i<getSubMeshCount(); i++)
469                current_texture_submesh[i]=0;
470}
471
472//---------------------------------------------------------------------------
473//       Refresh vertices to the vertex array.
474//---------------------------------------------------------------------------
475void    GeoMeshView::refreshVertices(Mesh       *geomesh)
476{
477        SubMesh                         *geosubmesh;
478        VertexBuffer    *vertex_buffer;
479
480        //      If the vertex array exists.
481        if (mSubMeshCount > 0)
482        {
483                for (int submesh = 0; submesh < mSubMeshCount; submesh++)
484                {
485                        delete  []      mPosArray[submesh];
486                        delete  []      mNorArray[submesh];
487                        delete  []      mIndexArray[submesh];
488                        delete  []      mTexCoordArray[submesh];
489                }
490               
491                delete  []      mSharedPosArray;
492                delete  []      mSharedNorArray;
493                delete  []      mSharedTexCoordArray;
494                delete  []      mPosArray;
495                delete  []      mNorArray;
496                delete  []      mIndexArray;
497        }
498
499        //      If there is shared vertex.
500        if (geomesh->mVertexBuffer != NULL)
501        {
502                vertex_buffer   =       geomesh->mVertexBuffer;
503
504                //      Allocate memory.
505                mSharedPosArray = new GLfloat[vertex_buffer->mVertexCount * 3];
506                mSharedNorArray = new GLfloat[vertex_buffer->mVertexCount * 3];
507                if (vertex_buffer->mTexCoords)
508                        mSharedTexCoordArray = new GLfloat[vertex_buffer->mVertexCount * 2];
509                else
510                        mSharedTexCoordArray = NULL;
511
512                for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
513                {
514                        mSharedPosArray[3 * vertex]                     =       vertex_buffer->mPosition[vertex].x;
515                        mSharedPosArray[(3 * vertex) + 1]       =       vertex_buffer->mPosition[vertex].y;
516                        mSharedPosArray[(3 * vertex) + 2]       =       vertex_buffer->mPosition[vertex].z;
517
518                        mSharedNorArray[3 * vertex]                     =       vertex_buffer->mNormal[vertex].x;
519                        mSharedNorArray[(3 * vertex) + 1]       =       vertex_buffer->mNormal[vertex].y;
520                        mSharedNorArray[(3 * vertex) + 2]       =       vertex_buffer->mNormal[vertex].z;
521
522                        if (vertex_buffer->mTexCoords)
523                        {
524                                mSharedTexCoordArray[2 * vertex]                =       vertex_buffer->mTexCoords[vertex].x;
525                                mSharedTexCoordArray[(2 * vertex) + 1]  =       vertex_buffer->mTexCoords[vertex].y;
526                        }
527                }
528        }
529       
530        //      Build the arrays of vertices and indices.
531        mPosArray               =       new GLfloat*[geomesh->mSubMeshCount];
532        mNorArray               =       new GLfloat*[geomesh->mSubMeshCount];
533        mTexCoordArray  =       new GLfloat*[geomesh->mSubMeshCount];
534        mIndexArray             =       new GLuint*[geomesh->mSubMeshCount];
535
536        //      For each submesh.
537        for (int submesh = 0; submesh < geomesh->mSubMeshCount; submesh++)
538        {
539                //      Gets the actual submesh.
540                geosubmesh      =       &geomesh->mSubMesh[submesh];
541               
542                //      Allocate memory for index array of the actual submesh.
543                mIndexArray[submesh]    =       new GLuint[geosubmesh->mIndexCount];
544
545                //      Gets the indices of the actual submesh.
546                for (int index = 0; index < geosubmesh->mIndexCount; index++)
547                {
548                        mIndexArray[submesh][index]     =       geosubmesh->mIndex[index];
549                }
550               
551                //      If the submesh is NOT shared vertex.
552                if (!geosubmesh->mSharedVertexBuffer)
553                {
554                        vertex_buffer   =       geosubmesh->mVertexBuffer;
555
556                        //      Allocate memory for vertices of the actual submesh.
557                        mPosArray[submesh]      =       new GLfloat[vertex_buffer->mVertexCount * 3];
558                        mNorArray[submesh]      =       new GLfloat[vertex_buffer->mVertexCount * 3];
559                        mTexCoordArray[submesh] =       new GLfloat[vertex_buffer->mVertexCount * 2];
560
561                        //      For each one of the vertex.
562                        for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
563                        {
564                                mPosArray[submesh][3 * vertex]                  =       vertex_buffer->mPosition[vertex].x;
565                                mPosArray[submesh][(3 * vertex) + 1]    =       vertex_buffer->mPosition[vertex].y;
566                                mPosArray[submesh][(3 * vertex) + 2]    =       vertex_buffer->mPosition[vertex].z;
567
568                                mNorArray[submesh][3 * vertex]                  =       vertex_buffer->mNormal[vertex].x;
569                                mNorArray[submesh][(3 * vertex) + 1]    =       vertex_buffer->mNormal[vertex].y;
570                                mNorArray[submesh][(3 * vertex) + 2]    =       vertex_buffer->mNormal[vertex].z;
571
572                                mTexCoordArray[submesh][2 * vertex]             =       vertex_buffer->mTexCoords[vertex].x;
573                                mTexCoordArray[submesh][(2 * vertex) + 1]       =       vertex_buffer->mTexCoords[vertex].y;
574                        }//     End for each vertex.
575                }
576                //      If the submesh is shared vertex.
577                else
578                {
579                        mPosArray[submesh]      =       NULL;
580                        mNorArray[submesh]      =       NULL;
581                        mTexCoordArray[submesh] =       NULL;
582                }
583
584        }//     End for each submesh.
585
586        mSubMeshCount   =       geoMesh->mSubMeshCount;
587}
588
589//---------------------------------------------------------------------------
590//      Gets the triangle count of the current lod.
591//---------------------------------------------------------------------------
592size_t  GeoMeshView::getLodStripsTriangleCount()
593{
594        //      For each strip.
595//      MultiIndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
596        IndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
597        size_t triangle_count = dataInterface->GetNumValidIndices() - 2;
598/*      for (int strip  =       0; strip < dataInterface->GetNumPrims(); strip++)
599        {
600//              triangle_count  +=      lodStripsLib->mStrips[strip].size() - 2;
601                triangle_count  +=      dataInterface->GetNumValidIndices(strip) - 2;
602        }*/
603
604        return  triangle_count;
605}
606
607//---------------------------------------------------------------------------
608//      Repaint the Mesh.
609//---------------------------------------------------------------------------
610void GeoMeshView::drawGeoMesh(bool      paintWire)
611{
612        SubMesh *geosubmesh;
613        bool            submesh_selected;
614       
615        if (geoMesh)
616        {
617                if (mLodStrip)
618                {
619                        drawLodStrip();
620                }
621                else if (mLodTree)
622                {
623                        drawLodTree();
624                }
625                else
626                {
627                        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
628                        {
629                                //      Gets the actual submesh.
630                                geosubmesh      =       &geoMesh->mSubMesh[submesh];
631                               
632                                if (geosubmesh->mType   ==      GEO_TRIANGLE_LIST)
633                                {
634                                        if (mSubMeshSelected == submesh)
635                                        {
636                                                submesh_selected        = true;
637                                        }
638                                        else
639                                        {
640                                                submesh_selected        =       false;
641                                        }
642                                       
643                                        //      Draw the actual submesh in triangle list.
644                                        drawTriangleList(submesh,submesh_selected,paintWire);
645                                }
646                                else
647                                {
648                                        //      Draw the actual submesh in triangle strip.
649                                        drawTriangleStrip(submesh);
650                                }
651                        }
652                }
653        }
654}//     End drawGeoMesh.
655
656//---------------------------------------------------------------------------
657//      Paint eah strip separately.
658//---------------------------------------------------------------------------
659void GeoMeshView::drawTriangleStrip(int submesh)
660{
661        SubMesh                         *geosubmesh;
662        Index                                   position;
663        Vector3                         vector3;
664        Vector2                         vector2;
665        float                                   x,y,z;
666        float                                   r,g,b;
667        Index                                   *index;
668        Index                                   *indexBegin;
669        Index                                   *indexEnd;
670        int                                             color_index;
671
672        color_index     =       0;
673
674        //      Gets the actual submesh.
675        geosubmesh      =       &geoMesh->mSubMesh[submesh];
676
677        // bind current texture to use
678        bool usetex = SetTextureRenderingState(submesh);
679
680        //      For each one of the strips.
681        for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
682        {
683                //      Paint the current strip.
684
685                //      First index of the strip.
686                indexBegin      =       geosubmesh->mStrip[strip];
687
688                //      If the strips is not the first.
689                //if (strip != 0)
690                //{
691                //      indexBegin++;
692                //}
693
694                int size= 0;
695                //      If is the final strip
696                if (strip       == (geosubmesh->mStripCount - 1))
697                {
698                        //      The end of the index array.
699                        indexEnd        = &geosubmesh->mIndex[geosubmesh->mIndexCount];
700            size= indexEnd - indexBegin;
701                }
702                else
703                {
704                        //      The beginning of the next strip.
705                        indexEnd        = geosubmesh->mStrip[strip + 1];
706
707                for (index = indexBegin; index < indexEnd; index++)
708                {
709                        size++;
710                }
711                        //      Remove degenerated
712                        //indexEnd--;
713                }
714
715                int i;
716                i       = 0;
717
718/*              if (getColorStrips())
719                {
720                        //      Gets the color of the strip.
721                        r       =       mStripColors[submesh][color_index].r;
722                        g       =       mStripColors[submesh][color_index].g;
723                        b       =       mStripColors[submesh][color_index].b;
724
725                        //      Change to the next color.
726                        color_index++;
727
728                        //      Paint a new color.
729                        glColor3f(r,g,b);
730                }*/
731
732
733        //VERTEX ARRAYS
734        glEnableClientState(GL_VERTEX_ARRAY);
735        glEnableClientState(GL_NORMAL_ARRAY);
736        if (usetex)
737                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
738        else
739                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
740
741                glVertexPointer(3, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mPosition);
742                glNormalPointer(GL_FLOAT, 0, geosubmesh->mVertexBuffer->mNormal);
743                if (usetex)
744                        glTexCoordPointer(2, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mTexCoords);
745
746                glDrawElements( GL_TRIANGLE_STRIP,
747                        size
748                        ,
749                        GL_UNSIGNED_INT,
750                        indexBegin
751                        );
752
753        if (submesh==leavesSubMesh)
754        {
755                glDisable(GL_ALPHA_TEST);
756                glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
757        }
758
759        }
760
761}//End drawTriangleStrip.
762
763//---------------------------------------------------------------------------
764//      Paint the array of idices.
765//---------------------------------------------------------------------------
766void GeoMeshView::drawTriangleList(     int                     submesh,
767                                                                                                                                                bool            selectedSubMesh,
768                                                                                                                                                bool            paintWire)
769{
770        SubMesh *geosubmesh;
771        Index           position;
772        Vector3 vector3;
773        float           x,y,z;
774        int                     idx_offset;
775        int                     vrt_offset;
776
777        //      Index offset.
778        idx_offset      =       0;
779
780        //      Vertex offset.
781        vrt_offset      =       0;
782
783        //      Gets the actual submesh.
784        geosubmesh      =       &geoMesh->mSubMesh[submesh];
785       
786        // bind current texture to use
787        bool usetex = SetTextureRenderingState(submesh);
788
789        //      If wire is not selected.
790        if (!paintWire)
791        {
792                //      If a mesh is selected.
793                if (selectedSubMesh)
794                {
795                        glColor3d(1.0,0.0,0.0);
796                        GLfloat red[]   =       {1.0f,0.0f,0.0f,1.0f};
797                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red);
798                }
799                //      If is a tree.
800                else if (leavesSubMesh >= 0)
801                {
802                        glColor3d(1.0, 0.5, 0.5);
803                        GLfloat brown[] =       {1.0f,0.5f,0.5f,1.0f};
804                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown);
805
806                        //      For each index of the strip.
807                        if (submesh == leavesSubMesh)
808                        {
809                                if (!usetex)
810                                {
811                                        glColor3d(0.0,1.0,0.0);
812                                        GLfloat green[] =       {0.0f,1.0f,0.0f,1.0f};
813                                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green);
814                                }
815                                else
816                                {
817                                        glEnable(GL_ALPHA_TEST);
818                                        glAlphaFunc(GL_GREATER,0.5f);
819                                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
820                                }
821                        }
822                }
823                //      If is NOT a tree.
824                else
825                {
826                        //      Set white color to the object.
827                        glColor3d(1.0, 1.0, 1.0);
828                        GLfloat white[] =       {1.0f,1.0f,1.0f,1.0f};
829                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
830                }
831        }
832
833        if (usetex)
834        {
835                glColor4f(1.0f,1.0f,1.0f,1.0f);
836                GLfloat white[] =       {1.0f,1.0f,1.0f,1.0f};
837                glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
838        }
839
840        //      Enable arrays.
841        glEnableClientState(GL_VERTEX_ARRAY);
842        glEnableClientState(GL_NORMAL_ARRAY);
843        if (usetex)
844                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
845        else
846                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
847
848        //      If the submesh is shared vertex.
849        if (geosubmesh->mSharedVertexBuffer)
850        {
851                glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray);
852                glNormalPointer(GL_FLOAT, 0, mSharedNorArray);
853                if (usetex)
854                        glTexCoordPointer(2, GL_FLOAT, 0, mSharedTexCoordArray);
855        }
856        else
857        {
858                glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]);
859                glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]);
860                if (usetex)
861                        glTexCoordPointer(2, GL_FLOAT, 0, mTexCoordArray[submesh]);
862        }
863
864        glDrawElements( GL_TRIANGLES,
865                        geosubmesh->mIndexCount,
866                        GL_UNSIGNED_INT,
867                        mIndexArray[submesh]);
868
869        if (submesh==leavesSubMesh)
870        {
871                glDisable(GL_ALPHA_TEST);
872                glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
873        }
874
875}//End drawTriangleList
876
877//---------------------------------------------------------------------------
878//      Paint lodstrip object.
879//---------------------------------------------------------------------------
880void GeoMeshView::drawLodStrip()
881{
882        SubMesh         *geosubmesh;
883        Index           position;
884        Vector3         vector3;
885        Vector2         vector2;
886        float           x,y,z;
887        float           r,g,b;
888        int                     color_index;
889        int                     current_strip;
890
891        //      Initialize current strip.
892        current_strip = 0;
893
894        int begin_index = 0;
895
896        //      For each submesh.
897        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
898        {
899                // bind current texture to use
900                bool usetex = SetTextureRenderingState(submesh);
901
902                color_index     =       0;
903
904                //      Gets the actual submesh.
905                geosubmesh      =       &geoMesh->mSubMesh[submesh];
906
907                //      For each one of the strips.
908/*              for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
909                {
910                        //      Paint the current strip.
911                        glBegin(GL_TRIANGLE_STRIP);
912
913                        if (getColorStrips())
914                        {
915                                //      Gets the color of the strip.
916                                r       =       mStripColors[submesh][color_index].r;
917                                g       =       mStripColors[submesh][color_index].g;
918                                b       =       mStripColors[submesh][color_index].b;
919
920                                //      Change to the next color.
921                                color_index++;
922
923                                //      Paint a new color.
924                                glColor3f(r,g,b);
925                        }
926
927                        //      For each index of the strip.
928                        for (int index = 0; index < lodStripsLib->dataRetrievalInterface->GetNumValidIndices(current_strip); index++)
929                        {
930                                position        =       lodStripsLib->dataRetrievalInterface->GetIndex(current_strip,index);
931
932                                //      Gets the vertex normal.
933                                vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
934
935                                x                               =       vector3[0];
936                                y                               =       vector3[1];
937                                z                               =       vector3[2];
938
939                                //      Sets the vertex normal.
940                                glNormal3f(x,y,z);
941
942                                // set the texture coordinates if needed
943                                if (usetex)
944                                {
945                                        vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
946                                        x                               =       vector2[0];
947                                        y                               =       vector2[1];
948                                        glTexCoord2f(x,y);
949                                }
950
951                                //      Gets the vertex coordinates.
952                                vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
953
954                                x                               =       vector3[0];
955                                y                               =       vector3[1];
956                                z                               =       vector3[2];
957
958                                //      Sets the vertex position.
959                                glVertex3f(x,y,z);
960                        }
961
962                        //      Increments current strip.
963                        current_strip++;
964
965                        //      A strip is generated.
966                        glEnd();
967
968                }*/
969
970                int indices_to_render = lodStripsLib->indices_x_submesh[submesh];
971
972
973                glBegin(GL_TRIANGLE_STRIP);
974                //for (int index = 0; index < lodStripsLib->dataRetrievalInterface->GetNumValidIndices(); index ++)
975                for (int index = begin_index; index < indices_to_render+begin_index; index ++)
976                {
977                        position = lodStripsLib->dataRetrievalInterface->GetIndex(index);
978
979                        //      Gets the vertex normal.
980                        vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
981
982                        x                               =       vector3[0];
983                        y                               =       vector3[1];
984                        z                               =       vector3[2];
985
986                        //      Sets the vertex normal.
987                        glNormal3f(x,y,z);
988
989                        // set the texture coordinates if needed
990                        if (usetex)
991                        {
992                                vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
993                                x                               =       vector2[0];
994                                y                               =       vector2[1];
995                                glTexCoord2f(x,y);
996                        }
997
998                        //      Gets the vertex coordinates.
999                        vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
1000
1001                        x                               =       vector3[0];
1002                        y                               =       vector3[1];
1003                        z                               =       vector3[2];
1004
1005                        //      Sets the vertex position.
1006                        glVertex3f(x,y,z);
1007                }
1008                glEnd();
1009                begin_index += indices_to_render;
1010
1011        }
1012
1013       
1014}//End drawTriangleStrip.
1015
1016//---------------------------------------------------------------------------
1017//      Paint lodtree object.
1018//---------------------------------------------------------------------------
1019void GeoMeshView::drawLodTree()
1020{
1021        SubMesh                         *geosubmesh;
1022        Index                                   position;
1023        Vector3                         vector3;
1024        Vector2                         vector2;
1025        float                                   x,y,z;
1026        float                                   r,g,b;
1027        int                                             color_index;
1028        int                                             current_strip;
1029
1030        //      Initialize current strip.
1031        current_strip   =       0;
1032
1033        // DRAW THE TRUNK AS A LODSTRIP OBJECT
1034
1035        int begin_index = 0;
1036
1037        //      For each submesh.
1038        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
1039        {
1040                if (submesh==leavesSubMesh)
1041                        continue;
1042
1043                // bind current texture to use
1044                bool usetex = SetTextureRenderingState(submesh);
1045
1046                color_index     =       0;
1047
1048                //      Gets the actual submesh.
1049                geosubmesh      =       &geoMesh->mSubMesh[submesh];
1050
1051                //      For each one of the strips.
1052/*              for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
1053                {
1054                        //      Paint the current strip.
1055                        glBegin(GL_TRIANGLE_STRIP);
1056
1057                        if (getColorStrips())
1058                        {
1059                                //      Gets the color of the strip.
1060                                r       =       mStripColors[submesh][color_index].r;
1061                                g       =       mStripColors[submesh][color_index].g;
1062                                b       =       mStripColors[submesh][color_index].b;
1063
1064                                //      Change to the next color.
1065                                color_index++;
1066
1067                                //      Paint a new color.
1068                                glColor3f(r,g,b);
1069                        }
1070
1071                        //      For each index of the strip.
1072                        for (int index = 0; index < lodTreeLib->CurrentLOD_Trunk_IndexCountByStrip(current_strip); index++)
1073                        {
1074                                position = lodTreeLib->CurrentLOD_Trunk_Indices()->GetIndex(current_strip,index);
1075
1076                                //      Gets the vertex normal.
1077                                vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
1078
1079                                x                               =       vector3[0];
1080                                y                               =       vector3[1];
1081                                z                               =       vector3[2];
1082
1083                                //      Sets the vertex normal.
1084                                glNormal3f(x,y,z);
1085
1086                                // set the texture coordinates if needed
1087                                if (usetex)
1088                                {
1089                                        vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
1090                                        x                               =       vector2[0];
1091                                        y                               =       vector2[1];
1092                                        glTexCoord2f(x,y);
1093                                }
1094
1095                                //      Gets the vertex coordinates.
1096                                vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
1097
1098                                x                               =       vector3[0];
1099                                y                               =       vector3[1];
1100                                z                               =       vector3[2];
1101
1102                                //      Sets the vertex position.
1103                                glVertex3f(x,y,z);
1104                        }
1105
1106                        //      Increments current strip.
1107                        current_strip++;
1108
1109                        //      A strip is generated.
1110                        glEnd();
1111
1112                }*/
1113       
1114                int indices_to_render = lodTreeLib->trunk->indices_x_submesh[submesh];
1115                       
1116                glBegin(GL_TRIANGLE_STRIP);
1117                for (int index = begin_index; index < lodTreeLib->trunk->indices_x_submesh[submesh]; index ++)
1118                {
1119                        position = lodTreeLib->CurrentLOD_Trunk_Indices()->GetIndex(index);
1120
1121                        //      Gets the vertex normal.
1122                        vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
1123
1124                        x                               =       vector3[0];
1125                        y                               =       vector3[1];
1126                        z                               =       vector3[2];
1127
1128                        //      Sets the vertex normal.
1129                        glNormal3f(x,y,z);
1130
1131                        // set the texture coordinates if needed
1132                        if (usetex)
1133                        {
1134                                vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
1135                                x                               =       vector2[0];
1136                                y                               =       vector2[1];
1137                                glTexCoord2f(x,y);
1138                        }
1139
1140                        //      Gets the vertex coordinates.
1141                        vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
1142
1143                        x                               =       vector3[0];
1144                        y                               =       vector3[1];
1145                        z                               =       vector3[2];
1146
1147                        //      Sets the vertex position.
1148                        glVertex3f(x,y,z);
1149                }
1150                glEnd();
1151                begin_index += indices_to_render;
1152
1153        }
1154
1155        // DRAW THE LEAVES AS A TRIANGLE SOUP
1156        // bind current texture to use
1157        bool usetex = SetTextureRenderingState(leavesSubMesh);
1158
1159        glEnable(GL_ALPHA_TEST);
1160        glAlphaFunc(GL_GREATER,0.5f);
1161        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
1162
1163        glBegin(GL_TRIANGLES);
1164        const Geometry::VertexData *foliage_verts = lodTreeLib->Get_Foliage_VertexData();
1165        const Geometry::IndexData *foliage_inds = lodTreeLib->CurrentLOD_Foliage_Indices();
1166        for (int j=0; j<lodTreeLib->CurrentLOD_Foliage_IndexCount(); j++)
1167        {
1168                float vx,vy,vz;
1169                if (usetex)
1170                {
1171                        foliage_verts->GetVertexTexCoord(foliage_inds->GetIndex(j),vx,vy);
1172                        glTexCoord2f(vx,vy);
1173                }
1174                foliage_verts->GetVertexNormal(foliage_inds->GetIndex(j),vx,vy,vz);
1175                glNormal3f(vx,vy,vz);
1176                foliage_verts->GetVertexCoord(foliage_inds->GetIndex(j),vx,vy,vz);
1177                glVertex3f(vx,vy,vz);
1178        }
1179        glEnd();
1180
1181        glColor3f(1,1,1);
1182        glDisable(GL_ALPHA_TEST);
1183        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);       
1184}
1185
1186//---------------------------------------------------------------------------
1187//      Sets the lodstripslibrary object.
1188//---------------------------------------------------------------------------
1189void    GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips)
1190{
1191        lodStripsLib    =       lodstrips;
1192        activeLodStrip();
1193}
1194
1195//---------------------------------------------------------------------------
1196//      Sets the lodtreeslibrary object.
1197//---------------------------------------------------------------------------
1198void    GeoMeshView::setLodTreesLibrary(LodTreeLibrary *lodtrees)
1199{
1200        lodTreeLib      =       lodtrees;
1201        activeLodTree();
1202}
1203
1204//---------------------------------------------------------------------------
1205//      Change de Level of detail of the object.
1206//---------------------------------------------------------------------------
1207void    GeoMeshView::GoToLod_LodStrip(float     lod)
1208{
1209        if (mLodStrip)
1210                lodStripsLib->GoToLod(lod);
1211        if (mLodTree)
1212                lodTreeLib->GoToTrunkLod(lod);
1213        draw();
1214}
1215
1216//---------------------------------------------------------------------------
1217//      Change de Level of detail of the object.
1218//---------------------------------------------------------------------------
1219void    GeoMeshView::GoToLod_LodTree(float      lod)
1220{
1221        if (mLodTree)
1222                lodTreeLib->GoToFoliageLod(lod);
1223        draw();
1224}
1225
1226//---------------------------------------------------------------------------
1227//      Draw the object in the window.
1228//---------------------------------------------------------------------------
1229void GeoMeshView::draw()
1230{
1231        glMatrixMode(GL_PROJECTION);
1232        glLoadIdentity();
1233
1234        glEnable(GL_LIGHTING);
1235
1236        //      Frustrum.
1237        glViewport(0,0,w(),h());
1238        gluPerspective(60,(float)w()/(float)h(),0.1,1000);
1239
1240        glMatrixMode(GL_MODELVIEW);
1241        glLoadIdentity();
1242
1243        glEnable(GL_BLEND);
1244        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1245
1246        GLfloat light_ambient[] =       {0.3,0.3,0.3,1.0};
1247        GLfloat luzpos[]                =       {0.0,0.0,1.0,0.0};
1248        GLfloat luzcolor[]              =       {1.0,1.0,1.0,1.0};
1249
1250        //      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
1251        glEnable(GL_LIGHT0);
1252        glLightfv(GL_LIGHT0, GL_POSITION, luzpos);
1253        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
1254        glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor);
1255        glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1);
1256        glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0);
1257        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
1258
1259        glEnable (GL_POLYGON_OFFSET_LINE);
1260        glPolygonOffset(-1,-1);
1261        glEnable(GL_DEPTH_TEST);
1262        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1263
1264        gluLookAt(0,0,2/*mScaleFactor*/ + size * 0.25, 0,0,0, 0,1,0);
1265
1266        glTranslatef(xshift,yshift,0);
1267        glRotatef(hAng,0,1,0);
1268        glRotatef(vAng,1,0,0);
1269        //glTranslatef(-xMed,-yMed,-zMed);
1270
1271        //      Set white color to the object.
1272        glColor3d(1.0, 1.0, 1.0);
1273        GLfloat color_blanco[]  =       {1.0f,1.0f,1.0f,1.0f};
1274        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
1275
1276        glClearColor(0,0,0,0);
1277       
1278        if (mCW)
1279        {
1280                glFrontFace(GL_CW);
1281                glEnable(GL_CULL_FACE);
1282        }
1283        else
1284        {
1285                if (mCCW)
1286                {
1287                        glFrontFace(GL_CCW);
1288                        glEnable(GL_CULL_FACE);
1289                }
1290                else
1291                {
1292                        glDisable(GL_CULL_FACE);
1293                }
1294        }
1295
1296        if (mSolid)
1297        {
1298                enableColorStrips();
1299                glDisable(GL_LIGHTING);
1300                glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1301                drawGeoMesh(false);
1302        }
1303        else
1304        {
1305                glEnable(GL_LIGHTING);
1306        }
1307
1308        if (mWire)
1309        {
1310                disableColorStrips();
1311                glDisable(GL_LIGHTING);
1312                glColor3d(0.0, 0.0, 1.0);
1313                glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1314                drawGeoMesh(true);
1315        }
1316        else
1317        {
1318                if (!mSolid)
1319                {
1320                        enableColorStrips();
1321                        glEnable(GL_LIGHTING);
1322                        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1323                        drawGeoMesh(false);
1324                }
1325        }
1326
1327        swap_buffers();
1328
1329        //      Calculate the FPS.
1330        //calcFPS();
1331}
1332
1333#include <IL/ILUT.h>
1334
1335void    GeoMeshView::LoadTexture(const char *imgfile)
1336{
1337        ilInit();
1338        ilutEnable(ILUT_OPENGL_CONV);
1339        ilutRenderer(ILUT_OPENGL);
1340
1341        current_texture = ilutGLLoadImage((const ILstring)imgfile);
1342        if (!current_texture)
1343                fltk::alert("Error loading texture!");
1344
1345        ilShutDown();
1346}
1347
1348void    GeoMeshView::LoadTextureSubMesh(int isubmesh, const char *imgfile)
1349{
1350        ilInit();
1351        ilutEnable(ILUT_OPENGL_CONV);
1352        ilutRenderer(ILUT_OPENGL);
1353
1354        current_texture_submesh[isubmesh] = ilutGLLoadImage((const ILstring)imgfile);
1355        if (!current_texture_submesh[isubmesh])
1356                fltk::alert("Error loading texture!");
1357
1358        ilShutDown();
1359}
1360
1361void    GeoMeshView::resetTextures(void)
1362{
1363        if (current_texture)
1364        {
1365                glDeleteTextures(1,&current_texture);
1366                current_texture=0;
1367        }
1368        if (current_texture_submesh)
1369        {
1370                glDeleteTextures(getSubMeshCount(),current_texture_submesh);
1371                for (int i=0; i<getSubMeshCount(); i++)
1372                        current_texture_submesh[i]=0;
1373        }
1374}
1375
1376bool    GeoMeshView::SetTextureRenderingState(int submesh)
1377{
1378        GLuint usetex = 0;
1379        if (use_texture_mapping)
1380        {
1381                if (current_texture_submesh[submesh])
1382                        usetex=current_texture_submesh[submesh];
1383                else if (current_texture)
1384                        usetex=current_texture;
1385        }
1386        if (usetex)
1387                glEnable(GL_TEXTURE_2D);
1388        else
1389                glDisable(GL_TEXTURE_2D);
1390
1391        glBindTexture(GL_TEXTURE_2D,usetex);
1392
1393        return (usetex!=0);
1394}
1395
1396int             GeoMeshView::findLeavesSubMesh(void)
1397{
1398        for (int i=0; i<geoMesh->mSubMeshCount; i++)
1399                if (geoMesh->mSubMesh[i].mType==GEO_TRIANGLE_LIST)
1400                        return i;
1401        return -1;
1402}
1403
Note: See TracBrowser for help on using the repository browser.