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

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