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

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