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

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