Changeset 994


Ignore:
Timestamp:
05/30/06 12:53:21 (18 years ago)
Author:
gumbau
Message:

Added support for texture mapping to the GeoTool?

Location:
GTP/trunk/Lib/Geom/shared/GeoTool
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Geom/shared/GeoTool/include/GeoMeshView.h

    r993 r994  
    195195        // load a image file as a texture 
    196196        void    LoadTexture(const char *); 
     197        void    LoadTextureSubMesh(int isubmesh, const char *); 
     198 
     199        int getSubMeshCount(void) const { return mSubMeshCount; } 
     200        void resetTextures(void); 
    197201 
    198202        private: 
     
    237241        // Current texture handle (OpenGL handle: 0=no texture) 
    238242        GLuint current_texture; 
     243        GLuint *current_texture_submesh; 
    239244        bool use_texture_mapping; // this flag enables texture mapping when a texture is loaded 
    240245 
  • GTP/trunk/Lib/Geom/shared/GeoTool/include/GeoMeshViewUI.h

    r993 r994  
    5252#include        "../resource.h" 
    5353 
    54 //#include <IL/IL.h> 
    55  
    5654//      State of the button process. 
    5755enum    ProcessState 
     
    117115 
    118116        MeshSimplificationSequence      *oMeshSimpSequence; 
    119         LodStripsConstructor                            *oLodStrip; 
    120         GeoMeshLoader                                                           *geoMeshLoader; 
     117        LodStripsConstructor            *oLodStrip; 
     118        GeoMeshLoader                           *geoMeshLoader; 
    121119 
    122120        inline void cb_menuFileOpen_i(fltk::Item*, void*); 
     
    128126        inline void cb_menuFileLoadTexture_i(fltk::Item*, void*); 
    129127        static void cb_menuFileLoadTexture(fltk::Item*, void*); 
     128        inline void cb_menuFileLoadTextureSubMesh_i(fltk::Item*, void*); 
     129        static void cb_menuFileLoadTextureSubMesh(fltk::Item*, void*); 
    130130 
    131131        inline void cb_menuMeshInfo_i(fltk::Item*, void*); 
     
    296296        fltk::Item                                      *menuMeshExportOBJ; 
    297297        fltk::Item                                      *menuFileTransformSharedVertex; 
    298         fltk::Item                                      *menuFileLoadTexture; 
     298        fltk::ItemGroup                         *menuLoadTextures; 
     299        fltk::Item                                              *menuFileLoadTexture; 
    299300 
    300301        fltk::Item                                      *menuFileQuit; 
     
    380381        float updateProgressBar(float); 
    381382 
     383        void BuildLoadTextureSubMeshMenu(void); 
     384        int numSubMeshNames; 
     385        char **SubMeshNames; 
     386 
    382387}; 
    383388 
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshView.cpp

    r993 r994  
    436436 
    437437                current_texture = 0; 
     438                current_texture_submesh=NULL; 
    438439                use_texture_mapping = true; 
    439440} 
     
    461462         
    462463        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; 
    463470} 
    464471 
     
    663670        geosubmesh      =       &geoMesh->mSubMesh[submesh]; 
    664671 
     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 
    665684        //      For each one of the strips. 
    666685        for (int strip = 0; strip < geosubmesh->mStripCount; strip++) 
     
    726745 
    727746                        // set the texture coordinates if needed 
    728                         if (current_texture && use_texture_mapping) 
     747                        if (usetex) 
    729748                        { 
    730749                                vector2 = geosubmesh->mVertexBuffer->mTexCoords[position]; 
    731                                 x                               =       vector3[0]; 
    732                                 y                               =       vector3[1]; 
     750                                x                               =       vector2[0]; 
     751                                y                               =       vector2[1]; 
    733752                                glTexCoord2f(x,y); 
    734753                        } 
     
    812831                } 
    813832        } 
     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); 
    814844         
    815845        //      Enable arrays. 
    816846        glEnableClientState(GL_VERTEX_ARRAY); 
    817847        glEnableClientState(GL_NORMAL_ARRAY); 
    818         if (current_texture && use_texture_mapping) 
     848        if (usetex) 
    819849                glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
    820850        else 
     
    826856                glVertexPointer(3, GL_FLOAT, 0, mSharedPosArray); 
    827857                glNormalPointer(GL_FLOAT, 0, mSharedNorArray); 
    828                 if (current_texture && use_texture_mapping) 
     858                if (usetex) 
    829859                        glTexCoordPointer(2, GL_FLOAT, 0, mSharedTexCoordArray); 
    830860        } 
     
    833863                glVertexPointer(3, GL_FLOAT, 0, mPosArray[submesh]); 
    834864                glNormalPointer(GL_FLOAT, 0, mNorArray[submesh]); 
    835                 if (current_texture && use_texture_mapping) 
     865                if (usetex) 
    836866                        glTexCoordPointer(2, GL_FLOAT, 0, mTexCoordArray[submesh]); 
    837867        } 
     
    848878//--------------------------------------------------------------------------- 
    849879void 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() 
    850983{ 
    851984        SubMesh                         *geosubmesh; 
     
    860993        current_strip   =       0; 
    861994 
    862         //      For each submesh. 
    863         for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) 
    864         { 
    865                  
    866                 color_index     =       0; 
    867  
    868                 //      Gets the actual submesh. 
    869                 geosubmesh      =       &geoMesh->mSubMesh[submesh]; 
    870  
    871                 //      For each one of the strips. 
    872                 for (int strip = 0; strip < geosubmesh->mStripCount; strip++) 
    873                 { 
    874                         //      Paint the current strip. 
    875                         glBegin(GL_TRIANGLE_STRIP); 
    876  
    877                         if (getColorStrips()) 
    878                         { 
    879                                 //      Gets the color of the strip. 
    880                                 r       =       mStripColors[submesh][color_index].r; 
    881                                 g       =       mStripColors[submesh][color_index].g; 
    882                                 b       =       mStripColors[submesh][color_index].b; 
    883  
    884                                 //      Change to the next color. 
    885                                 color_index++; 
    886  
    887                                 //      Paint a new color. 
    888                                 glColor3f(r,g,b); 
    889                         } 
    890  
    891                         //      For each index of the strip. 
    892                         for (int index = 0; index < lodStripsLib->mStrips[current_strip].size(); index++) 
    893                         { 
    894                                 position        =       lodStripsLib->mStrips[current_strip][index]; 
    895  
    896                                 //      Gets the vertex normal. 
    897                                 vector3 =       geosubmesh->mVertexBuffer->mNormal[position]; 
    898  
    899                                 x                               =       vector3[0]; 
    900                                 y                               =       vector3[1]; 
    901                                 z                               =       vector3[2]; 
    902  
    903                                 //      Sets the vertex normal. 
    904                                 glNormal3f(x,y,z); 
    905  
    906                                 //      Gets the vertex coordinates. 
    907                                 vector3 =       geosubmesh->mVertexBuffer->mPosition[position]; 
    908  
    909                                 x                               =       vector3[0]; 
    910                                 y                               =       vector3[1]; 
    911                                 z                               =       vector3[2]; 
    912  
    913                                 //      Sets the vertex position. 
    914                                 glVertex3f(x,y,z); 
    915                         } 
    916  
    917                         //      Increments current strip. 
    918                         current_strip++; 
    919  
    920                         //      A strip is generated. 
    921                         glEnd(); 
    922  
    923                 } 
    924         } 
    925          
    926 }//End drawTriangleStrip. 
    927  
    928  
    929 //--------------------------------------------------------------------------- 
    930 //      Paint lodtree object. 
    931 //--------------------------------------------------------------------------- 
    932 void GeoMeshView::drawLodTree() 
    933 { 
    934         SubMesh                         *geosubmesh; 
    935         Index                                   position; 
    936         Vector3                         vector3; 
    937         float                                   x,y,z; 
    938         float                                   r,g,b; 
    939         int                                             color_index; 
    940         int                                             current_strip; 
    941  
    942         //      Initialize current strip. 
    943         current_strip   =       0; 
    944  
    945995 
    946996        // DRAW THE TRUNK AS A LODSTRIP OBJECT 
     
    10981148 
    10991149        GLfloat light_ambient[] =       {0.3,0.3,0.3,1.0}; 
    1100         GLfloat luzpos[]                                =       {0.0,0.0,1.0,0.0}; 
    1101         GLfloat luzcolor[]                      =       {1.0,1.0,1.0,1.0}; 
     1150        GLfloat luzpos[]                =       {0.0,0.0,1.0,0.0}; 
     1151        GLfloat luzcolor[]              =       {1.0,1.0,1.0,1.0}; 
    11021152 
    11031153        //      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); 
     
    11991249        ilShutDown(); 
    12001250} 
     1251 
     1252 
     1253void    GeoMeshView::LoadTextureSubMesh(int isubmesh, const char *imgfile) 
     1254{ 
     1255        ilInit(); 
     1256        ilutEnable(ILUT_OPENGL_CONV); 
     1257        ilutRenderer(ILUT_OPENGL); 
     1258 
     1259        current_texture_submesh[isubmesh] = ilutGLLoadImage((const ILstring)imgfile); 
     1260        if (!current_texture_submesh[isubmesh]) 
     1261                fltk::alert("Error loading texture!"); 
     1262 
     1263        ilShutDown(); 
     1264} 
     1265 
     1266void    GeoMeshView::resetTextures(void) 
     1267{ 
     1268        if (current_texture) 
     1269        { 
     1270                glDeleteTextures(1,&current_texture); 
     1271                current_texture=0; 
     1272        } 
     1273        if (current_texture_submesh) 
     1274        { 
     1275                glDeleteTextures(getSubMeshCount(),current_texture_submesh); 
     1276                for (int i=0; i<getSubMeshCount(); i++) 
     1277                        current_texture_submesh[i]=0; 
     1278        } 
     1279} 
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshViewUI.cpp

    r993 r994  
    2525        geoMeshView->deactiveLodTree(); 
    2626 
     27        geoMeshView->resetTextures(); 
     28        BuildLoadTextureSubMeshMenu(); 
     29 
    2730        //      Repaint the window. 
    2831        mMainWindow->redraw(); 
     
    3336        fltk::FileChooser       *fcho = new fltk::FileChooser("","*.jpg",fltk::FileChooser::CREATE,"Open image file"); 
    3437        fcho->exec(); 
     38        char filename[256]="";   
    3539        if (fcho->value()) 
    36                 geoMeshView->LoadTexture(fcho->value()); 
     40        { 
     41                strcpy(filename,fcho->value()); 
     42                geoMeshView->LoadTexture(filename); 
     43        } 
    3744} 
    3845 
    3946void GeoMeshViewUI::cb_menuFileLoadTexture(fltk::Item *o, void *v) 
    4047{ 
    41         ((GeoMeshViewUI*)       (o->parent()->parent()->parent()->user_data())) 
     48        ((GeoMeshViewUI*)       (o->parent()->parent()->parent()->parent()->user_data())) 
    4249                                                                                -> 
    4350                                                                                cb_menuFileLoadTexture_i(o,v); 
    4451} 
     52 
     53inline void GeoMeshViewUI::cb_menuFileLoadTextureSubMesh_i(fltk::Item *item, void *) 
     54{ 
     55        const char * label = item->label(); 
     56        int isubmesh = label[strlen(label)-1]-48; 
     57 
     58        fltk::FileChooser       *fcho = new fltk::FileChooser("","*.jpg",fltk::FileChooser::CREATE,"Open image file"); 
     59        fcho->exec(); 
     60        char filename[256]=""; 
     61        if (fcho->value()) 
     62        { 
     63                strcpy(filename,fcho->value()); 
     64                geoMeshView->LoadTextureSubMesh(isubmesh,filename); 
     65        } 
     66} 
     67 
     68void GeoMeshViewUI::cb_menuFileLoadTextureSubMesh(fltk::Item *o, void *v) 
     69{ 
     70        ((GeoMeshViewUI*)       (o->parent()->parent()->parent()->parent()->user_data())) 
     71                                                                                -> 
     72                                                                                cb_menuFileLoadTextureSubMesh_i(o,v); 
     73} 
     74 
    4575 
    4676 
     
    25402570        //      Initialize the menus application state. 
    25412571        mApplicationState       =       NONE; 
     2572 
     2573        SubMeshNames=NULL; 
    25422574         
    25432575        { 
     
    27912823 
    27922824                                { 
    2793                                         fltk::Item* o = menuFileLoadTexture = new fltk::Item("Load texture"); 
    2794                                         o->callback((fltk::Callback*)cb_menuFileLoadTexture); 
     2825                                        menuLoadTextures = new fltk::ItemGroup("Load Textures"); 
     2826                                        o->add(menuLoadTextures); 
    27952827                                } 
    27962828 
     
    30973129} 
    30983130 
     3131 
     3132void GeoMeshViewUI::BuildLoadTextureSubMeshMenu(void) 
     3133{ 
     3134        if (SubMeshNames) 
     3135        { 
     3136                menuLoadTextures->clear(); 
     3137                for (int i=0; i<numSubMeshNames; i++) 
     3138                { 
     3139                        delete[] SubMeshNames[i]; 
     3140                } 
     3141                delete[] SubMeshNames; 
     3142        } 
     3143 
     3144        menuFileLoadTexture = new fltk::Item("Entire model"); 
     3145        menuFileLoadTexture->callback((fltk::Callback*)cb_menuFileLoadTexture); 
     3146        menuLoadTextures->add(menuFileLoadTexture); 
     3147 
     3148        numSubMeshNames=geoMeshView->getSubMeshCount(); 
     3149        SubMeshNames=new char*[numSubMeshNames]; 
     3150 
     3151        for (int i=0; i<numSubMeshNames; i++) 
     3152        { 
     3153                SubMeshNames[i]=new char[10]; 
     3154 
     3155                sprintf(SubMeshNames[i],"submesh %d",i); 
     3156                fltk::Item *item = new fltk::Item(SubMeshNames[i]); 
     3157                item->callback((fltk::Callback*)cb_menuFileLoadTextureSubMesh); 
     3158                menuLoadTextures->add(item); 
     3159        } 
     3160} 
Note: See TracChangeset for help on using the changeset viewer.