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

Revision 1058, 32.0 KB checked in by gumbau, 18 years ago (diff)

LODs parameters changed to the range 1-0 (max-min)

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                if (vertex_buffer->mTexCoords)
508                        mSharedTexCoordArray = new GLfloat[vertex_buffer->mVertexCount * 2];
509                else
510                        mSharedTexCoordArray = NULL;
511
512                for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
513                {
514                        mSharedPosArray[3 * vertex]                     =       vertex_buffer->mPosition[vertex].x;
515                        mSharedPosArray[(3 * vertex) + 1]       =       vertex_buffer->mPosition[vertex].y;
516                        mSharedPosArray[(3 * vertex) + 2]       =       vertex_buffer->mPosition[vertex].z;
517
518                        mSharedNorArray[3 * vertex]                     =       vertex_buffer->mNormal[vertex].x;
519                        mSharedNorArray[(3 * vertex) + 1]       =       vertex_buffer->mNormal[vertex].y;
520                        mSharedNorArray[(3 * vertex) + 2]       =       vertex_buffer->mNormal[vertex].z;
521
522                        if (vertex_buffer->mTexCoords)
523                        {
524                                mSharedTexCoordArray[2 * vertex]                =       vertex_buffer->mTexCoords[vertex].x;
525                                mSharedTexCoordArray[(2 * vertex) + 1]  =       vertex_buffer->mTexCoords[vertex].y;
526                        }
527                }
528        }
529       
530        //      Build the arrays of vertices and indices.
531        mPosArray               =       new GLfloat*[geomesh->mSubMeshCount];
532        mNorArray               =       new GLfloat*[geomesh->mSubMeshCount];
533        mTexCoordArray  =       new GLfloat*[geomesh->mSubMeshCount];
534        mIndexArray             =       new GLuint*[geomesh->mSubMeshCount];
535
536        //      For each submesh.
537        for (int submesh = 0; submesh < geomesh->mSubMeshCount; submesh++)
538        {
539                //      Gets the actual submesh.
540                geosubmesh      =       &geomesh->mSubMesh[submesh];
541               
542                //      Allocate memory for index array of the actual submesh.
543                mIndexArray[submesh]    =       new GLuint[geosubmesh->mIndexCount];
544
545                //      Gets the indices of the actual submesh.
546                for (int index = 0; index < geosubmesh->mIndexCount; index++)
547                {
548                        mIndexArray[submesh][index]     =       geosubmesh->mIndex[index];
549                }
550               
551                //      If the submesh is NOT shared vertex.
552                if (!geosubmesh->mSharedVertexBuffer)
553                {
554                        vertex_buffer   =       geosubmesh->mVertexBuffer;
555
556                        //      Allocate memory for vertices of the actual submesh.
557                        mPosArray[submesh]      =       new GLfloat[vertex_buffer->mVertexCount * 3];
558                        mNorArray[submesh]      =       new GLfloat[vertex_buffer->mVertexCount * 3];
559                        mTexCoordArray[submesh] =       new GLfloat[vertex_buffer->mVertexCount * 2];
560
561                        //      For each one of the vertex.
562                        for (int vertex = 0; vertex < vertex_buffer->mVertexCount; vertex++)
563                        {
564                                mPosArray[submesh][3 * vertex]                  =       vertex_buffer->mPosition[vertex].x;
565                                mPosArray[submesh][(3 * vertex) + 1]    =       vertex_buffer->mPosition[vertex].y;
566                                mPosArray[submesh][(3 * vertex) + 2]    =       vertex_buffer->mPosition[vertex].z;
567
568                                mNorArray[submesh][3 * vertex]                  =       vertex_buffer->mNormal[vertex].x;
569                                mNorArray[submesh][(3 * vertex) + 1]    =       vertex_buffer->mNormal[vertex].y;
570                                mNorArray[submesh][(3 * vertex) + 2]    =       vertex_buffer->mNormal[vertex].z;
571
572                                mTexCoordArray[submesh][2 * vertex]             =       vertex_buffer->mTexCoords[vertex].x;
573                                mTexCoordArray[submesh][(2 * vertex) + 1]       =       vertex_buffer->mTexCoords[vertex].y;
574                        }//     End for each vertex.
575                }
576                //      If the submesh is shared vertex.
577                else
578                {
579                        mPosArray[submesh]      =       NULL;
580                        mNorArray[submesh]      =       NULL;
581                        mTexCoordArray[submesh] =       NULL;
582                }
583
584        }//     End for each submesh.
585
586        mSubMeshCount   =       geoMesh->mSubMeshCount;
587}
588
589//---------------------------------------------------------------------------
590//      Gets the triangle count of the current lod.
591//---------------------------------------------------------------------------
592size_t  GeoMeshView::getLodStripsTriangleCount()
593{
594        size_t  triangle_count;
595
596        //      Initialize triangle count.
597        triangle_count  =       0;
598               
599        //      For each strip.
600//      for (int strip  =       0; strip < lodStripsLib->GetStripCount(); strip++)
601        MultiIndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
602        for (int strip  =       0; strip < dataInterface->GetNumPrims(); strip++)
603        {
604//              triangle_count  +=      lodStripsLib->mStrips[strip].size() - 2;
605                triangle_count  +=      dataInterface->GetNumValidIndices(strip) - 2;
606        }
607
608        return  triangle_count;
609}
610
611//---------------------------------------------------------------------------
612//      Repaint the Mesh.
613//---------------------------------------------------------------------------
614void GeoMeshView::drawGeoMesh(bool      paintWire)
615{
616        SubMesh *geosubmesh;
617        bool            submesh_selected;
618       
619        if (geoMesh)
620        {
621                if (mLodStrip)
622                {
623                        drawLodStrip();
624                }
625                else if (mLodTree)
626                {
627                        drawLodTree();
628                }
629                else
630                {
631                        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
632                        {
633                                //      Gets the actual submesh.
634                                geosubmesh      =       &geoMesh->mSubMesh[submesh];
635                               
636                                if (geosubmesh->mType   ==      GEO_TRIANGLE_LIST)
637                                {
638                                        if (mSubMeshSelected == submesh)
639                                        {
640                                                submesh_selected        = true;
641                                        }
642                                        else
643                                        {
644                                                submesh_selected        =       false;
645                                        }
646                                       
647                                        //      Draw the actual submesh in triangle list.
648                                        drawTriangleList(submesh,submesh_selected,paintWire);
649                                }
650                                else
651                                {
652                                        //      Draw the actual submesh in triangle strip.
653                                        drawTriangleStrip(submesh);
654                                }
655                        }
656                }
657        }
658}//     End drawGeoMesh.
659
660//---------------------------------------------------------------------------
661//      Paint eah strip separately.
662//---------------------------------------------------------------------------
663void GeoMeshView::drawTriangleStrip(int submesh)
664{
665        SubMesh                         *geosubmesh;
666        Index                                   position;
667        Vector3                         vector3;
668        Vector2                         vector2;
669        float                                   x,y,z;
670        float                                   r,g,b;
671        Index                                   *index;
672        Index                                   *indexBegin;
673        Index                                   *indexEnd;
674        int                                             color_index;
675
676        color_index     =       0;
677
678        //      Gets the actual submesh.
679        geosubmesh      =       &geoMesh->mSubMesh[submesh];
680
681        // bind current texture to use
682        bool usetex = SetTextureRenderingState(submesh);
683
684        //      For each one of the strips.
685        for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
686        {
687                //      Paint the current strip.
688
689                //      First index of the strip.
690                indexBegin      =       geosubmesh->mStrip[strip];
691
692                //      If the strips is not the first.
693                //if (strip != 0)
694                //{
695                //      indexBegin++;
696                //}
697
698                int size= 0;
699                //      If is the final strip
700                if (strip       == (geosubmesh->mStripCount - 1))
701                {
702                        //      The end of the index array.
703                        indexEnd        = &geosubmesh->mIndex[geosubmesh->mIndexCount];
704            size= indexEnd - indexBegin;
705                }
706                else
707                {
708                        //      The beginning of the next strip.
709                        indexEnd        = geosubmesh->mStrip[strip + 1];
710
711                for (index = indexBegin; index < indexEnd; index++)
712                {
713                        size++;
714                }
715                        //      Remove degenerated
716                        //indexEnd--;
717                }
718
719                int i;
720                i       = 0;
721
722                if (getColorStrips())
723                {
724                        //      Gets the color of the strip.
725                        r       =       mStripColors[submesh][color_index].r;
726                        g       =       mStripColors[submesh][color_index].g;
727                        b       =       mStripColors[submesh][color_index].b;
728
729                        //      Change to the next color.
730                        color_index++;
731
732                        //      Paint a new color.
733                        glColor3f(r,g,b);
734                }
735
736
737        //VERTEX ARRAYS
738        glEnableClientState(GL_VERTEX_ARRAY);
739        glEnableClientState(GL_NORMAL_ARRAY);
740        if (usetex)
741                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
742        else
743                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
744
745                glVertexPointer(3, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mPosition);
746                glNormalPointer(GL_FLOAT, 0, geosubmesh->mVertexBuffer->mNormal);
747                if (usetex)
748                        glTexCoordPointer(2, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mTexCoords);
749
750                glDrawElements( GL_TRIANGLE_STRIP,
751                        size
752                        ,
753                        GL_UNSIGNED_INT,
754                        indexBegin
755                        );
756
757        if (submesh==leavesSubMesh)
758        {
759                glDisable(GL_ALPHA_TEST);
760                glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
761        }
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                        for (int index = 0; index < lodStripsLib->dataRetrievalInterface->GetNumValidIndices(current_strip); index++)
932                        {
933//                              position        =       lodStripsLib->mStrips[current_strip][index];
934                                position        =       lodStripsLib->dataRetrievalInterface->GetIndex(current_strip,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                                        glTexCoord2f(x,y);
953                                }
954
955                                //      Gets the vertex coordinates.
956                                vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
957
958                                x                               =       vector3[0];
959                                y                               =       vector3[1];
960                                z                               =       vector3[2];
961
962                                //      Sets the vertex position.
963                                glVertex3f(x,y,z);
964                        }
965
966                        //      Increments current strip.
967                        current_strip++;
968
969                        //      A strip is generated.
970                        glEnd();
971
972                }
973        }
974       
975}//End drawTriangleStrip.
976
977
978//---------------------------------------------------------------------------
979//      Paint lodtree object.
980//---------------------------------------------------------------------------
981void GeoMeshView::drawLodTree()
982{
983        SubMesh                         *geosubmesh;
984        Index                                   position;
985        Vector3                         vector3;
986        Vector2                         vector2;
987        float                                   x,y,z;
988        float                                   r,g,b;
989        int                                             color_index;
990        int                                             current_strip;
991
992        //      Initialize current strip.
993        current_strip   =       0;
994
995        // DRAW THE TRUNK AS A LODSTRIP OBJECT
996
997        //      For each submesh.
998        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
999        {
1000                if (submesh==leavesSubMesh)
1001                        continue;
1002
1003                // bind current texture to use
1004                bool usetex = SetTextureRenderingState(submesh);
1005
1006                color_index     =       0;
1007
1008                //      Gets the actual submesh.
1009                geosubmesh      =       &geoMesh->mSubMesh[submesh];
1010
1011                //      For each one of the strips.
1012                for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
1013                {
1014                        //      Paint the current strip.
1015                        glBegin(GL_TRIANGLE_STRIP);
1016
1017                        if (getColorStrips())
1018                        {
1019                                //      Gets the color of the strip.
1020                                r       =       mStripColors[submesh][color_index].r;
1021                                g       =       mStripColors[submesh][color_index].g;
1022                                b       =       mStripColors[submesh][color_index].b;
1023
1024                                //      Change to the next color.
1025                                color_index++;
1026
1027                                //      Paint a new color.
1028                                glColor3f(r,g,b);
1029                        }
1030
1031                        //      For each index of the strip.
1032//                      for (int index = 0; index < lodTreeLib->GetStrip(current_strip).size(); index++)
1033                        for (int index = 0; index < lodTreeLib->CurrentLOD_Trunk_IndexCountByStrip(current_strip); index++)
1034                        {
1035//                              position        =       lodTreeLib->GetStrip(current_strip)[index];
1036                                position = lodTreeLib->CurrentLOD_Trunk_Indices()->GetIndex(current_strip,index);
1037
1038                                //      Gets the vertex normal.
1039                                vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
1040
1041                                x                               =       vector3[0];
1042                                y                               =       vector3[1];
1043                                z                               =       vector3[2];
1044
1045                                //      Sets the vertex normal.
1046                                glNormal3f(x,y,z);
1047
1048                                // set the texture coordinates if needed
1049                                if (usetex)
1050                                {
1051                                        vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
1052                                        x                               =       vector2[0];
1053                                        y                               =       vector2[1];
1054                                        glTexCoord2f(x,y);
1055                                }
1056
1057                                //      Gets the vertex coordinates.
1058                                vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
1059
1060                                x                               =       vector3[0];
1061                                y                               =       vector3[1];
1062                                z                               =       vector3[2];
1063
1064                                //      Sets the vertex position.
1065                                glVertex3f(x,y,z);
1066                        }
1067
1068                        //      Increments current strip.
1069                        current_strip++;
1070
1071                        //      A strip is generated.
1072                        glEnd();
1073
1074                }
1075        }
1076
1077        // DRAW THE LEAVES AS A TRIANGLE SOUP
1078        // bind current texture to use
1079        bool usetex = SetTextureRenderingState(leavesSubMesh);
1080
1081        glEnable(GL_ALPHA_TEST);
1082        glAlphaFunc(GL_GREATER,0.5f);
1083        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
1084
1085        glBegin(GL_TRIANGLES);
1086        const Geometry::VertexData *foliage_verts = lodTreeLib->Get_Foliage_VertexData();
1087        const Geometry::IndexData *foliage_inds = lodTreeLib->CurrentLOD_Foliage_Indices();
1088        for (int j=0; j<lodTreeLib->CurrentLOD_Foliage_IndexCount(); j++)
1089        {
1090                float vx,vy,vz;
1091                if (usetex)
1092                {
1093                        foliage_verts->GetVertexTexCoord(foliage_inds->GetIndex(j),vx,vy);
1094                        glTexCoord2f(vx,vy);
1095                }
1096                foliage_verts->GetVertexNormal(foliage_inds->GetIndex(j),vx,vy,vz);
1097                glNormal3f(vx,vy,vz);
1098                foliage_verts->GetVertexCoord(foliage_inds->GetIndex(j),vx,vy,vz);
1099                glVertex3f(vx,vy,vz);
1100        }
1101        glEnd();
1102
1103        glColor3f(1,1,1);
1104        glDisable(GL_ALPHA_TEST);
1105        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);       
1106}
1107
1108
1109
1110//---------------------------------------------------------------------------
1111//      Sets the lodstripslibrary object.
1112//---------------------------------------------------------------------------
1113void    GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips)
1114{
1115        lodStripsLib    =       lodstrips;
1116        activeLodStrip();
1117}
1118
1119//---------------------------------------------------------------------------
1120//      Sets the lodtreeslibrary object.
1121//---------------------------------------------------------------------------
1122void    GeoMeshView::setLodTreesLibrary(LodTreeLibrary *lodtrees)
1123{
1124        lodTreeLib      =       lodtrees;
1125        activeLodTree();
1126}
1127
1128//---------------------------------------------------------------------------
1129//      Change de Level of detail of the object.
1130//---------------------------------------------------------------------------
1131void    GeoMeshView::GoToLod_LodStrip(float     lod)
1132{
1133        if (mLodStrip)
1134                lodStripsLib->GoToLod(lod);
1135        if (mLodTree)
1136                lodTreeLib->GoToTrunkLod(lod);
1137        draw();
1138}
1139
1140//---------------------------------------------------------------------------
1141//      Change de Level of detail of the object.
1142//---------------------------------------------------------------------------
1143void    GeoMeshView::GoToLod_LodTree(float      lod)
1144{
1145        if (mLodTree)
1146                lodTreeLib->GoToFoliageLod(lod);
1147        draw();
1148}
1149
1150
1151//---------------------------------------------------------------------------
1152//      Draw the object in the window.
1153//---------------------------------------------------------------------------
1154void GeoMeshView::draw()
1155{
1156        glMatrixMode(GL_PROJECTION);
1157        glLoadIdentity();
1158
1159        glEnable(GL_LIGHTING);
1160
1161        //      Frustrum.
1162        glViewport(0,0,w(),h());
1163        gluPerspective(60,(float)w()/(float)h(),0.1,1000);
1164
1165        glMatrixMode(GL_MODELVIEW);
1166        glLoadIdentity();
1167
1168        glEnable(GL_BLEND);
1169        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1170
1171        GLfloat light_ambient[] =       {0.3,0.3,0.3,1.0};
1172        GLfloat luzpos[]                =       {0.0,0.0,1.0,0.0};
1173        GLfloat luzcolor[]              =       {1.0,1.0,1.0,1.0};
1174
1175        //      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
1176        glEnable(GL_LIGHT0);
1177        glLightfv(GL_LIGHT0, GL_POSITION, luzpos);
1178        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
1179        glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor);
1180        glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1);
1181        glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0);
1182        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
1183
1184        glEnable (GL_POLYGON_OFFSET_LINE);
1185        glPolygonOffset(-1,-1);
1186        glEnable(GL_DEPTH_TEST);
1187        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1188
1189        gluLookAt(0,0,2/*mScaleFactor*/ + size * 0.25, 0,0,0, 0,1,0);
1190
1191        glTranslatef(xshift,yshift,0);
1192        glRotatef(hAng,0,1,0);
1193        glRotatef(vAng,1,0,0);
1194        //glTranslatef(-xMed,-yMed,-zMed);
1195
1196        //      Set white color to the object.
1197        glColor3d(1.0, 1.0, 1.0);
1198        GLfloat color_blanco[]  =       {1.0f,1.0f,1.0f,1.0f};
1199        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
1200
1201        glClearColor(0,0,0,0);
1202       
1203        if (mCW)
1204        {
1205                glFrontFace(GL_CW);
1206                glEnable(GL_CULL_FACE);
1207        }
1208        else
1209        {
1210                if (mCCW)
1211                {
1212                        glFrontFace(GL_CCW);
1213                        glEnable(GL_CULL_FACE);
1214                }
1215                else
1216                {
1217                        glDisable(GL_CULL_FACE);
1218                }
1219        }
1220
1221        if (mSolid)
1222        {
1223                enableColorStrips();
1224                glDisable(GL_LIGHTING);
1225                glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1226                drawGeoMesh(false);
1227        }
1228        else
1229        {
1230                glEnable(GL_LIGHTING);
1231        }
1232
1233        if (mWire)
1234        {
1235                disableColorStrips();
1236                glDisable(GL_LIGHTING);
1237                glColor3d(0.0, 0.0, 1.0);
1238                glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1239                drawGeoMesh(true);
1240        }
1241        else
1242        {
1243                if (!mSolid)
1244                {
1245                        enableColorStrips();
1246                        glEnable(GL_LIGHTING);
1247                        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1248                        drawGeoMesh(false);
1249                }
1250        }
1251
1252        swap_buffers();
1253
1254        //      Calculate the FPS.
1255        calcFPS();
1256}
1257
1258
1259#include <IL/ILUT.h>
1260
1261void    GeoMeshView::LoadTexture(const char *imgfile)
1262{
1263        ilInit();
1264        ilutEnable(ILUT_OPENGL_CONV);
1265        ilutRenderer(ILUT_OPENGL);
1266
1267        current_texture = ilutGLLoadImage((const ILstring)imgfile);
1268        if (!current_texture)
1269                fltk::alert("Error loading texture!");
1270
1271        ilShutDown();
1272}
1273
1274
1275void    GeoMeshView::LoadTextureSubMesh(int isubmesh, const char *imgfile)
1276{
1277        ilInit();
1278        ilutEnable(ILUT_OPENGL_CONV);
1279        ilutRenderer(ILUT_OPENGL);
1280
1281        current_texture_submesh[isubmesh] = ilutGLLoadImage((const ILstring)imgfile);
1282        if (!current_texture_submesh[isubmesh])
1283                fltk::alert("Error loading texture!");
1284
1285        ilShutDown();
1286}
1287
1288void    GeoMeshView::resetTextures(void)
1289{
1290        if (current_texture)
1291        {
1292                glDeleteTextures(1,&current_texture);
1293                current_texture=0;
1294        }
1295        if (current_texture_submesh)
1296        {
1297                glDeleteTextures(getSubMeshCount(),current_texture_submesh);
1298                for (int i=0; i<getSubMeshCount(); i++)
1299                        current_texture_submesh[i]=0;
1300        }
1301}
1302
1303
1304bool    GeoMeshView::SetTextureRenderingState(int submesh)
1305{
1306        GLuint usetex = 0;
1307        if (use_texture_mapping)
1308        {
1309                if (current_texture_submesh[submesh])
1310                        usetex=current_texture_submesh[submesh];
1311                else if (current_texture)
1312                        usetex=current_texture;
1313        }
1314        if (usetex)
1315                glEnable(GL_TEXTURE_2D);
1316        else
1317                glDisable(GL_TEXTURE_2D);
1318
1319        glBindTexture(GL_TEXTURE_2D,usetex);
1320
1321        return (usetex!=0);
1322}
1323
1324
1325int             GeoMeshView::findLeavesSubMesh(void)
1326{
1327        for (int i=0; i<geoMesh->mSubMeshCount; i++)
1328                if (geoMesh->mSubMesh[i].mType==GEO_TRIANGLE_LIST)
1329                        return i;
1330        return -1;
1331}
Note: See TracBrowser for help on using the repository browser.