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

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