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

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