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

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