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

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