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

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