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

Revision 1083, 33.9 KB checked in by gumbau, 19 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                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        //      For each strip.
595//      MultiIndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
596        IndexData *dataInterface = lodStripsLib->dataRetrievalInterface;
597        size_t triangle_count = dataInterface->GetNumValidIndices() - 2;
598/*      for (int strip  =       0; strip < dataInterface->GetNumPrims(); strip++)
599        {
600//              triangle_count  +=      lodStripsLib->mStrips[strip].size() - 2;
601                triangle_count  +=      dataInterface->GetNumValidIndices(strip) - 2;
602        }*/
603
604        return  triangle_count;
605}
606
607//---------------------------------------------------------------------------
608//      Repaint the Mesh.
609//---------------------------------------------------------------------------
610void GeoMeshView::drawGeoMesh(bool      paintWire)
611{
612        SubMesh *geosubmesh;
613        bool            submesh_selected;
614       
615        if (geoMesh)
616        {
617                if (mLodStrip)
618                {
619                        drawLodStrip();
620                }
621                else if (mLodTree)
622                {
623                        drawLodTree();
624                }
625                else
626                {
627                        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
628                        {
629                                //      Gets the actual submesh.
630                                geosubmesh      =       &geoMesh->mSubMesh[submesh];
631                               
632                                if (geosubmesh->mType   ==      GEO_TRIANGLE_LIST)
633                                {
634                                        if (mSubMeshSelected == submesh)
635                                        {
636                                                submesh_selected        = true;
637                                        }
638                                        else
639                                        {
640                                                submesh_selected        =       false;
641                                        }
642                                       
643                                        //      Draw the actual submesh in triangle list.
644                                        drawTriangleList(submesh,submesh_selected,paintWire);
645                                }
646                                else
647                                {
648                                        //      Draw the actual submesh in triangle strip.
649                                        drawTriangleStrip(submesh);
650                                }
651                        }
652                }
653        }
654}//     End drawGeoMesh.
655
656//---------------------------------------------------------------------------
657//      Paint eah strip separately.
658//---------------------------------------------------------------------------
659void GeoMeshView::drawTriangleStrip(int submesh)
660{
661        SubMesh                         *geosubmesh;
662        Index                                   position;
663        Vector3                         vector3;
664        Vector2                         vector2;
665        float                                   x,y,z;
666        float                                   r,g,b;
667        Index                                   *index;
668        Index                                   *indexBegin;
669        Index                                   *indexEnd;
670        int                                             color_index;
671
672        color_index     =       0;
673
674        //      Gets the actual submesh.
675        geosubmesh      =       &geoMesh->mSubMesh[submesh];
676
677        // bind current texture to use
678        bool usetex = SetTextureRenderingState(submesh);
679
680        //      For each one of the strips.
681        for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
682        {
683                //      Paint the current strip.
684
685                //      First index of the strip.
686                indexBegin      =       geosubmesh->mStrip[strip];
687
688                //      If the strips is not the first.
689                //if (strip != 0)
690                //{
691                //      indexBegin++;
692                //}
693
694                int size= 0;
695                //      If is the final strip
696                if (strip       == (geosubmesh->mStripCount - 1))
697                {
698                        //      The end of the index array.
699                        indexEnd        = &geosubmesh->mIndex[geosubmesh->mIndexCount];
700            size= indexEnd - indexBegin;
701                }
702                else
703                {
704                        //      The beginning of the next strip.
705                        indexEnd        = geosubmesh->mStrip[strip + 1];
706
707                for (index = indexBegin; index < indexEnd; index++)
708                {
709                        size++;
710                }
711                        //      Remove degenerated
712                        //indexEnd--;
713                }
714
715                int i;
716                i       = 0;
717
718/*              if (getColorStrips())
719                {
720                        //      Gets the color of the strip.
721                        r       =       mStripColors[submesh][color_index].r;
722                        g       =       mStripColors[submesh][color_index].g;
723                        b       =       mStripColors[submesh][color_index].b;
724
725                        //      Change to the next color.
726                        color_index++;
727
728                        //      Paint a new color.
729                        glColor3f(r,g,b);
730                }*/
731
732
733        //VERTEX ARRAYS
734        glEnableClientState(GL_VERTEX_ARRAY);
735        glEnableClientState(GL_NORMAL_ARRAY);
736        if (usetex)
737                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
738        else
739                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
740
741                glVertexPointer(3, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mPosition);
742                glNormalPointer(GL_FLOAT, 0, geosubmesh->mVertexBuffer->mNormal);
743                if (usetex)
744                        glTexCoordPointer(2, GL_FLOAT, 0, geosubmesh->mVertexBuffer->mTexCoords);
745
746                glDrawElements( GL_TRIANGLE_STRIP,
747                        size
748                        ,
749                        GL_UNSIGNED_INT,
750                        indexBegin
751                        );
752
753        if (submesh==leavesSubMesh)
754        {
755                glDisable(GL_ALPHA_TEST);
756                glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
757        }
758
759        }
760
761}//End drawTriangleStrip.
762
763//---------------------------------------------------------------------------
764//      Paint the array of idices.
765//---------------------------------------------------------------------------
766void GeoMeshView::drawTriangleList(     int                     submesh,
767                                                                                                                                                bool            selectedSubMesh,
768                                                                                                                                                bool            paintWire)
769{
770        SubMesh *geosubmesh;
771        Index           position;
772        Vector3 vector3;
773        float           x,y,z;
774        int                     idx_offset;
775        int                     vrt_offset;
776
777        //      Index offset.
778        idx_offset      =       0;
779
780        //      Vertex offset.
781        vrt_offset      =       0;
782
783        //      Gets the actual submesh.
784        geosubmesh      =       &geoMesh->mSubMesh[submesh];
785       
786        // bind current texture to use
787        bool usetex = SetTextureRenderingState(submesh);
788
789        //      If wire is not selected.
790        if (!paintWire)
791        {
792                //      If a mesh is selected.
793                if (selectedSubMesh)
794                {
795                        glColor3d(1.0,0.0,0.0);
796                        GLfloat red[]   =       {1.0f,0.0f,0.0f,1.0f};
797                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red);
798                }
799                //      If is a tree.
800                else if (leavesSubMesh >= 0)
801                {
802                        glColor3d(1.0, 0.5, 0.5);
803                        GLfloat brown[] =       {1.0f,0.5f,0.5f,1.0f};
804                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown);
805
806                        //      For each index of the strip.
807                        if (submesh == leavesSubMesh)
808                        {
809                                if (!usetex)
810                                {
811                                        glColor3d(0.0,1.0,0.0);
812                                        GLfloat green[] =       {0.0f,1.0f,0.0f,1.0f};
813                                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green);
814                                }
815                                else
816                                {
817                                        glEnable(GL_ALPHA_TEST);
818                                        glAlphaFunc(GL_GREATER,0.5f);
819                                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
820                                }
821                        }
822                }
823                //      If is NOT a tree.
824                else
825                {
826                        //      Set white color to the object.
827                        glColor3d(1.0, 1.0, 1.0);
828                        GLfloat white[] =       {1.0f,1.0f,1.0f,1.0f};
829                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
830                }
831        }
832
833        if (usetex)
834        {
835                glColor4f(1.0f,1.0f,1.0f,1.0f);
836                GLfloat white[] =       {1.0f,1.0f,1.0f,1.0f};
837                glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
838        }
839
840        //      Enable arrays.
841        glEnableClientState(GL_VERTEX_ARRAY);
842        glEnableClientState(GL_NORMAL_ARRAY);
843        if (usetex)
844                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
845        else
846                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
847
848        //      If the submesh is shared vertex.
849        if (geosubmesh->mSharedVertexBuffer)
850        {
851                glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray);
852                glNormalPointer(GL_FLOAT, 0, mSharedNorArray);
853                if (usetex)
854                        glTexCoordPointer(2, GL_FLOAT, 0, mSharedTexCoordArray);
855        }
856        else
857        {
858                glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]);
859                glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]);
860                if (usetex)
861                        glTexCoordPointer(2, GL_FLOAT, 0, mTexCoordArray[submesh]);
862        }
863
864        glDrawElements( GL_TRIANGLES,
865                        geosubmesh->mIndexCount,
866                        GL_UNSIGNED_INT,
867                        mIndexArray[submesh]);
868
869        if (submesh==leavesSubMesh)
870        {
871                glDisable(GL_ALPHA_TEST);
872                glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
873        }
874
875}//End drawTriangleList
876
877//---------------------------------------------------------------------------
878//      Paint lodstrip object.
879//---------------------------------------------------------------------------
880void GeoMeshView::drawLodStrip()
881{
882        SubMesh         *geosubmesh;
883        Index           position;
884        Vector3         vector3;
885        Vector2         vector2;
886        float           x,y,z;
887        float           r,g,b;
888        int                     color_index;
889        int                     current_strip;
890
891        //      Initialize current strip.
892        current_strip = 0;
893
894        //      For each submesh.
895        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
896        {
897                // bind current texture to use
898                bool usetex = SetTextureRenderingState(submesh);
899
900                color_index     =       0;
901
902                //      Gets the actual submesh.
903                geosubmesh      =       &geoMesh->mSubMesh[submesh];
904
905                //      For each one of the strips.
906/*              for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
907                {
908                        //      Paint the current strip.
909                        glBegin(GL_TRIANGLE_STRIP);
910
911                        if (getColorStrips())
912                        {
913                                //      Gets the color of the strip.
914                                r       =       mStripColors[submesh][color_index].r;
915                                g       =       mStripColors[submesh][color_index].g;
916                                b       =       mStripColors[submesh][color_index].b;
917
918                                //      Change to the next color.
919                                color_index++;
920
921                                //      Paint a new color.
922                                glColor3f(r,g,b);
923                        }
924
925                        //      For each index of the strip.
926                        for (int index = 0; index < lodStripsLib->dataRetrievalInterface->GetNumValidIndices(current_strip); index++)
927                        {
928                                position        =       lodStripsLib->dataRetrievalInterface->GetIndex(current_strip,index);
929
930                                //      Gets the vertex normal.
931                                vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
932
933                                x                               =       vector3[0];
934                                y                               =       vector3[1];
935                                z                               =       vector3[2];
936
937                                //      Sets the vertex normal.
938                                glNormal3f(x,y,z);
939
940                                // set the texture coordinates if needed
941                                if (usetex)
942                                {
943                                        vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
944                                        x                               =       vector2[0];
945                                        y                               =       vector2[1];
946                                        glTexCoord2f(x,y);
947                                }
948
949                                //      Gets the vertex coordinates.
950                                vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
951
952                                x                               =       vector3[0];
953                                y                               =       vector3[1];
954                                z                               =       vector3[2];
955
956                                //      Sets the vertex position.
957                                glVertex3f(x,y,z);
958                        }
959
960                        //      Increments current strip.
961                        current_strip++;
962
963                        //      A strip is generated.
964                        glEnd();
965
966                }*/
967
968                int indices_to_render = lodStripsLib->GetValidIndexCount(submesh);
969                int offset = lodStripsLib->GetValidOffset(submesh);
970
971                glBegin(GL_TRIANGLE_STRIP);
972                //for (int index = 0; index < lodStripsLib->dataRetrievalInterface->GetNumValidIndices(); index ++)
973                for (int index = offset; index < indices_to_render+offset; index ++)
974                {
975                        position = lodStripsLib->dataRetrievalInterface->GetIndex(index);
976
977                        //      Gets the vertex normal.
978                        vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
979
980                        x                               =       vector3[0];
981                        y                               =       vector3[1];
982                        z                               =       vector3[2];
983
984                        //      Sets the vertex normal.
985                        glNormal3f(x,y,z);
986
987                        // set the texture coordinates if needed
988                        if (usetex)
989                        {
990                                vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
991                                x                               =       vector2[0];
992                                y                               =       vector2[1];
993                                glTexCoord2f(x,y);
994                        }
995
996                        //      Gets the vertex coordinates.
997                        vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
998
999                        x                               =       vector3[0];
1000                        y                               =       vector3[1];
1001                        z                               =       vector3[2];
1002
1003                        //      Sets the vertex position.
1004                        glVertex3f(x,y,z);
1005                }
1006                glEnd();
1007
1008        }
1009
1010       
1011}//End drawTriangleStrip.
1012
1013//---------------------------------------------------------------------------
1014//      Paint lodtree object.
1015//---------------------------------------------------------------------------
1016void GeoMeshView::drawLodTree()
1017{
1018        SubMesh                         *geosubmesh;
1019        Index                                   position;
1020        Vector3                         vector3;
1021        Vector2                         vector2;
1022        float                                   x,y,z;
1023        float                                   r,g,b;
1024        int                                             color_index;
1025        int                                             current_strip;
1026
1027        //      Initialize current strip.
1028        current_strip   =       0;
1029
1030        // DRAW THE TRUNK AS A LODSTRIP OBJECT
1031        //      For each submesh.
1032        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++)
1033        {
1034                if (submesh==leavesSubMesh)
1035                        continue;
1036
1037                // bind current texture to use
1038                bool usetex = SetTextureRenderingState(submesh);
1039
1040                color_index     =       0;
1041
1042                //      Gets the actual submesh.
1043                geosubmesh      =       &geoMesh->mSubMesh[submesh];
1044
1045                //      For each one of the strips.
1046/*              for (int strip = 0; strip < geosubmesh->mStripCount; strip++)
1047                {
1048                        //      Paint the current strip.
1049                        glBegin(GL_TRIANGLE_STRIP);
1050
1051                        if (getColorStrips())
1052                        {
1053                                //      Gets the color of the strip.
1054                                r       =       mStripColors[submesh][color_index].r;
1055                                g       =       mStripColors[submesh][color_index].g;
1056                                b       =       mStripColors[submesh][color_index].b;
1057
1058                                //      Change to the next color.
1059                                color_index++;
1060
1061                                //      Paint a new color.
1062                                glColor3f(r,g,b);
1063                        }
1064
1065                        //      For each index of the strip.
1066                        for (int index = 0; index < lodTreeLib->CurrentLOD_Trunk_IndexCountByStrip(current_strip); index++)
1067                        {
1068                                position = lodTreeLib->CurrentLOD_Trunk_Indices()->GetIndex(current_strip,index);
1069
1070                                //      Gets the vertex normal.
1071                                vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
1072
1073                                x                               =       vector3[0];
1074                                y                               =       vector3[1];
1075                                z                               =       vector3[2];
1076
1077                                //      Sets the vertex normal.
1078                                glNormal3f(x,y,z);
1079
1080                                // set the texture coordinates if needed
1081                                if (usetex)
1082                                {
1083                                        vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
1084                                        x                               =       vector2[0];
1085                                        y                               =       vector2[1];
1086                                        glTexCoord2f(x,y);
1087                                }
1088
1089                                //      Gets the vertex coordinates.
1090                                vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
1091
1092                                x                               =       vector3[0];
1093                                y                               =       vector3[1];
1094                                z                               =       vector3[2];
1095
1096                                //      Sets the vertex position.
1097                                glVertex3f(x,y,z);
1098                        }
1099
1100                        //      Increments current strip.
1101                        current_strip++;
1102
1103                        //      A strip is generated.
1104                        glEnd();
1105
1106                }*/
1107       
1108//              int indices_to_render = lodTreeLib->trunk->GetValidIndexCount(submesh);
1109                int indices_to_render = lodTreeLib->GetValidTrunkIndexCount(submesh);
1110                int begin_index = lodTreeLib->GetValidTrunkOffset(submesh);
1111                       
1112                glBegin(GL_TRIANGLE_STRIP);
1113                for (int index = begin_index; index < indices_to_render+begin_index; index ++)
1114                {
1115                        position = lodTreeLib->CurrentLOD_Trunk_Indices()->GetIndex(index);
1116
1117                        //      Gets the vertex normal.
1118                        vector3 =       geosubmesh->mVertexBuffer->mNormal[position];
1119
1120                        x                               =       vector3[0];
1121                        y                               =       vector3[1];
1122                        z                               =       vector3[2];
1123
1124                        //      Sets the vertex normal.
1125                        glNormal3f(x,y,z);
1126
1127                        // set the texture coordinates if needed
1128                        if (usetex)
1129                        {
1130                                vector2 = geosubmesh->mVertexBuffer->mTexCoords[position];
1131                                x                               =       vector2[0];
1132                                y                               =       vector2[1];
1133                                glTexCoord2f(x,y);
1134                        }
1135
1136                        //      Gets the vertex coordinates.
1137                        vector3 =       geosubmesh->mVertexBuffer->mPosition[position];
1138
1139                        x                               =       vector3[0];
1140                        y                               =       vector3[1];
1141                        z                               =       vector3[2];
1142
1143                        //      Sets the vertex position.
1144                        glVertex3f(x,y,z);
1145                }
1146                glEnd();
1147        }
1148
1149        // DRAW THE LEAVES AS A TRIANGLE SOUP
1150        // bind current texture to use
1151        bool usetex = SetTextureRenderingState(leavesSubMesh);
1152
1153        glEnable(GL_ALPHA_TEST);
1154        glAlphaFunc(GL_GREATER,0.5f);
1155        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
1156
1157        glBegin(GL_TRIANGLES);
1158        const Geometry::VertexData *foliage_verts = lodTreeLib->Get_Foliage_VertexData();
1159        const Geometry::IndexData *foliage_inds = lodTreeLib->CurrentLOD_Foliage_Indices();
1160        for (int j=0; j<lodTreeLib->CurrentLOD_Foliage_IndexCount(); j++)
1161        {
1162                float vx,vy,vz;
1163                if (usetex)
1164                {
1165                        foliage_verts->GetVertexTexCoord(foliage_inds->GetIndex(j),vx,vy);
1166                        glTexCoord2f(vx,vy);
1167                }
1168                foliage_verts->GetVertexNormal(foliage_inds->GetIndex(j),vx,vy,vz);
1169                glNormal3f(vx,vy,vz);
1170                foliage_verts->GetVertexCoord(foliage_inds->GetIndex(j),vx,vy,vz);
1171                glVertex3f(vx,vy,vz);
1172        }
1173        glEnd();
1174
1175        glColor3f(1,1,1);
1176        glDisable(GL_ALPHA_TEST);
1177        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);       
1178}
1179
1180//---------------------------------------------------------------------------
1181//      Sets the lodstripslibrary object.
1182//---------------------------------------------------------------------------
1183void    GeoMeshView::setLodStripsLibrary(LodStripsLibrary *lodstrips)
1184{
1185        lodStripsLib    =       lodstrips;
1186        activeLodStrip();
1187}
1188
1189//---------------------------------------------------------------------------
1190//      Sets the lodtreeslibrary object.
1191//---------------------------------------------------------------------------
1192void    GeoMeshView::setLodTreesLibrary(LodTreeLibrary *lodtrees)
1193{
1194        lodTreeLib      =       lodtrees;
1195        activeLodTree();
1196}
1197
1198//---------------------------------------------------------------------------
1199//      Change de Level of detail of the object.
1200//---------------------------------------------------------------------------
1201void    GeoMeshView::GoToLod_LodStrip(float     lod)
1202{
1203        if (mLodStrip)
1204                lodStripsLib->GoToLod(lod);
1205        if (mLodTree)
1206                lodTreeLib->GoToTrunkLod(lod);
1207        draw();
1208}
1209
1210//---------------------------------------------------------------------------
1211//      Change de Level of detail of the object.
1212//---------------------------------------------------------------------------
1213void    GeoMeshView::GoToLod_LodTree(float      lod)
1214{
1215        if (mLodTree)
1216                lodTreeLib->GoToFoliageLod(lod);
1217        draw();
1218}
1219
1220//---------------------------------------------------------------------------
1221//      Draw the object in the window.
1222//---------------------------------------------------------------------------
1223void GeoMeshView::draw()
1224{
1225        glMatrixMode(GL_PROJECTION);
1226        glLoadIdentity();
1227
1228        glEnable(GL_LIGHTING);
1229
1230        //      Frustrum.
1231        glViewport(0,0,w(),h());
1232        gluPerspective(60,(float)w()/(float)h(),0.1,1000);
1233
1234        glMatrixMode(GL_MODELVIEW);
1235        glLoadIdentity();
1236
1237        glEnable(GL_BLEND);
1238        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1239
1240        GLfloat light_ambient[] =       {0.3,0.3,0.3,1.0};
1241        GLfloat luzpos[]                =       {0.0,0.0,1.0,0.0};
1242        GLfloat luzcolor[]              =       {1.0,1.0,1.0,1.0};
1243
1244        //      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
1245        glEnable(GL_LIGHT0);
1246        glLightfv(GL_LIGHT0, GL_POSITION, luzpos);
1247        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
1248        glLightfv(GL_LIGHT0, GL_DIFFUSE, luzcolor);
1249        glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1);
1250        glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0);
1251        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
1252
1253        glEnable (GL_POLYGON_OFFSET_LINE);
1254        glPolygonOffset(-1,-1);
1255        glEnable(GL_DEPTH_TEST);
1256        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1257
1258        gluLookAt(0,0,2/*mScaleFactor*/ + size * 0.25, 0,0,0, 0,1,0);
1259
1260        glTranslatef(xshift,yshift,0);
1261        glRotatef(hAng,0,1,0);
1262        glRotatef(vAng,1,0,0);
1263        //glTranslatef(-xMed,-yMed,-zMed);
1264
1265        //      Set white color to the object.
1266        glColor3d(1.0, 1.0, 1.0);
1267        GLfloat color_blanco[]  =       {1.0f,1.0f,1.0f,1.0f};
1268        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco);
1269
1270        glClearColor(0,0,0,0);
1271       
1272        if (mCW)
1273        {
1274                glFrontFace(GL_CW);
1275                glEnable(GL_CULL_FACE);
1276        }
1277        else
1278        {
1279                if (mCCW)
1280                {
1281                        glFrontFace(GL_CCW);
1282                        glEnable(GL_CULL_FACE);
1283                }
1284                else
1285                {
1286                        glDisable(GL_CULL_FACE);
1287                }
1288        }
1289
1290        if (mSolid)
1291        {
1292                enableColorStrips();
1293                glDisable(GL_LIGHTING);
1294                glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
1295                drawGeoMesh(false);
1296        }
1297        else
1298        {
1299                glEnable(GL_LIGHTING);
1300        }
1301
1302        if (mWire)
1303        {
1304                disableColorStrips();
1305                glDisable(GL_LIGHTING);
1306                glColor3d(0.0, 0.0, 1.0);
1307                glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
1308                drawGeoMesh(true);
1309        }
1310        else
1311        {
1312                if (!mSolid)
1313                {
1314                        enableColorStrips();
1315                        glEnable(GL_LIGHTING);
1316                        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1317                        drawGeoMesh(false);
1318                }
1319        }
1320
1321        swap_buffers();
1322
1323        //      Calculate the FPS.
1324        //calcFPS();
1325}
1326
1327#include <IL/ILUT.h>
1328
1329void    GeoMeshView::LoadTexture(const char *imgfile)
1330{
1331        ilInit();
1332        ilutEnable(ILUT_OPENGL_CONV);
1333        ilutRenderer(ILUT_OPENGL);
1334
1335        current_texture = ilutGLLoadImage((const ILstring)imgfile);
1336        if (!current_texture)
1337                fltk::alert("Error loading texture!");
1338
1339        ilShutDown();
1340}
1341
1342void    GeoMeshView::LoadTextureSubMesh(int isubmesh, const char *imgfile)
1343{
1344        ilInit();
1345        ilutEnable(ILUT_OPENGL_CONV);
1346        ilutRenderer(ILUT_OPENGL);
1347
1348        current_texture_submesh[isubmesh] = ilutGLLoadImage((const ILstring)imgfile);
1349        if (!current_texture_submesh[isubmesh])
1350                fltk::alert("Error loading texture!");
1351
1352        ilShutDown();
1353}
1354
1355void    GeoMeshView::resetTextures(void)
1356{
1357        if (current_texture)
1358        {
1359                glDeleteTextures(1,&current_texture);
1360                current_texture=0;
1361        }
1362        if (current_texture_submesh)
1363        {
1364                glDeleteTextures(getSubMeshCount(),current_texture_submesh);
1365                for (int i=0; i<getSubMeshCount(); i++)
1366                        current_texture_submesh[i]=0;
1367        }
1368}
1369
1370bool    GeoMeshView::SetTextureRenderingState(int submesh)
1371{
1372        GLuint usetex = 0;
1373        if (use_texture_mapping)
1374        {
1375                if (current_texture_submesh[submesh])
1376                        usetex=current_texture_submesh[submesh];
1377                else if (current_texture)
1378                        usetex=current_texture;
1379        }
1380        if (usetex)
1381                glEnable(GL_TEXTURE_2D);
1382        else
1383                glDisable(GL_TEXTURE_2D);
1384
1385        glBindTexture(GL_TEXTURE_2D,usetex);
1386
1387        return (usetex!=0);
1388}
1389
1390int             GeoMeshView::findLeavesSubMesh(void)
1391{
1392        for (int i=0; i<geoMesh->mSubMeshCount; i++)
1393                if (geoMesh->mSubMesh[i].mType==GEO_TRIANGLE_LIST)
1394                        return i;
1395        return -1;
1396}
1397
Note: See TracBrowser for help on using the repository browser.