Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

model.cpp

00001 /***************************************************************************
00002                           model.cpp  -  description
00003           $Id: model.cpp 11 2006-06-25 11:47:22Z neoneurone $
00004                              -------------------
00005     begin                : sam mai 22 2004
00006     copyright            : (C) by Duong-Khang NGUYEN
00007     email                : neoneurone @ users sourceforge net
00008  ***************************************************************************/
00009 
00010 /***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   any later version.                                                    *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "model.h"
00020 
00021 #include "ocm.h"
00022 
00023 static GLuint uiTabIndex;
00024 static GLuint uiYIndex;
00025 static const GLfloat* ftabTemp;
00026 
00027 
00028    /*=====================================================================*/
00029 Model::Model(
00030     const GLfloat data [],
00031     const GLuint & size ):
00032 ftabData( data ),
00033 ftabRGB( NULL ),
00034 ftabTexCoord( NULL ),
00035 uitabTexName( NULL ),
00036 uiOpaqueList( 0 ),
00037 uiAlphaList( 0 )
00038 {
00039     OPENCITY_DEBUG( "ctor1" );
00040 
00041     uiTabSize = size;
00042 }
00043 
00044 
00045    /*=====================================================================*/
00046 Model::Model(
00047     const GLfloat data [],
00048     const GLuint & size,
00049     const GLfloat rgb [],
00050     const GLfloat tcoord [],
00051     const GLuint  tname [] ):
00052 ftabData( data ),
00053 ftabRGB( rgb ),
00054 ftabTexCoord( tcoord ),
00055 uitabTexName( tname ),
00056 uiOpaqueList( 0 ),
00057 uiAlphaList( 0 )
00058 {
00059     OPENCITY_DEBUG( "ctor2" );
00060 
00061     uiTabSize = size;
00062 }
00063 
00064 
00065    /*=====================================================================*/
00066 Model::Model
00067 (
00068     GLuint dlOpaque,
00069     GLuint dlAlpha,
00070     std::map<string, GLuint> mapTex
00071 ):
00072 ftabData( NULL ),
00073 ftabRGB( NULL ),
00074 ftabTexCoord( NULL ),
00075 uitabTexName( NULL ),
00076 uiTabSize( 0 ),
00077 uiOpaqueList( dlOpaque ),
00078 uiAlphaList( dlAlpha ),
00079 mapTexture( mapTex )
00080 {
00081     OPENCITY_DEBUG( "ctor3" );
00082 }
00083 
00084 
00085    /*=====================================================================*/
00086 Model::~Model()
00087 {
00088     OPENCITY_DEBUG( "dtor" );
00089 
00090     std::map<string, GLuint>::iterator iter;
00091     std::map<string, GLuint>::const_iterator end;
00092     
00093     delete [] ftabData;
00094     delete [] ftabRGB;
00095     delete [] ftabTexCoord;
00096     delete [] uitabTexName;
00097 
00098 // Delete display lists
00099     if (glIsList( this->uiOpaqueList ))
00100         glDeleteLists( this->uiOpaqueList, 1 );
00101     if (glIsList( this->uiAlphaList ))
00102         glDeleteLists( this->uiAlphaList, 1 );
00103 
00104 // Delete texture
00105     end = this->mapTexture.end();
00106     for (iter = this->mapTexture.begin(); iter != end; iter++)
00107         glDeleteTextures( 1, &(iter->second) );
00108 }
00109 
00110 
00111    /*=====================================================================*/
00112 void
00113 Model::Display(
00114     const OC_FLOAT & rcfW,
00115     const OC_FLOAT & rcfH,
00116     const OC_FLOAT & rcfY ) const
00117 {
00118     uiTabIndex = 0;
00119     ftabTemp = ftabData;
00120 
00121    // FIXME: this is ugly
00122     glBegin( GL_QUADS );
00123 
00124    // go through the data table
00125    // and display the correct primitive
00126     while (uiTabIndex < uiTabSize) {
00127         if (ftabTemp[uiTabIndex]
00128         ==  OC_OCM_VERTEX) {
00129             glVertex3f(
00130                 ftabTemp[uiTabIndex+1] + rcfW,
00131                 ftabTemp[uiTabIndex+2] + rcfY,
00132                 ftabTemp[uiTabIndex+3] + rcfH );
00133             uiTabIndex += 4;
00134 //debug cout << "vertex" << endl;
00135         } else
00136         if (ftabTemp[uiTabIndex]
00137         == OC_OCM_COLOR) {
00138             glColor4fv( ftabTemp + uiTabIndex + 1 );
00139             uiTabIndex += 5;
00140 //debug cout << "color" << endl;
00141         } else
00142         if (ftabTemp[uiTabIndex]
00143         ==  OC_OCM_TEXCOOR) {
00144             glTexCoord3fv( ftabTemp + uiTabIndex + 1 );
00145             uiTabIndex += 4;
00146         } else
00147         if (ftabTemp[uiTabIndex]
00148         == OC_OCM_TEXBIND) {
00149            //TODO: this is REALLY dirty
00150             glEnd();
00151             if ( (GLuint)ftabTemp[uiTabIndex+1] == 0 ) {
00152                // restore the current rendering context
00153                 glTexCoord4i( 0, 0, 0, 1 );
00154                 glDisable( GL_TEXTURE_2D );
00155                // NOTE: works around the ATI 9M bug
00156                 glColor4ub( 255, 255, 255, 255 );
00157                 glEnable( GL_COLOR_MATERIAL );
00158                 glEnable( GL_LIGHTING );
00159                 glDisable( GL_BLEND );
00160             }
00161             else {
00162                // NOTE: works around the ATI 9M bug
00163                 glDisable( GL_LIGHTING );
00164                 glDisable( GL_COLOR_MATERIAL );
00165 
00166                // enable alpha blending
00167                 glEnable( GL_BLEND );
00168                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00169 
00170                // enable texturing
00171                 glEnable( GL_TEXTURE_2D );
00172                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
00173                 glBindTexture( GL_TEXTURE_2D, (GLuint)ftabTemp[uiTabIndex+1] );
00174 //debug cout << "Binding texture id: " << (GLuint)ftabTemp[uiTabIndex+1] << endl;
00175             }
00176             glBegin( GL_QUADS );
00177             uiTabIndex += 2;
00178         }
00179         else {
00180             OPENCITY_DEBUG("WARNING: unknown OCM code");
00181             assert(0);          // Abort in under debuging mode
00182             uiTabIndex++;       // Avoid infinite loop in released code
00183         }
00184     }
00185 
00186     glEnd();
00187 }
00188 
00189 
00190    /*=====================================================================*/
00191 void
00192 Model::Display2(
00193     const OC_FLOAT & rcfW,
00194     const OC_FLOAT & rcfH,
00195     const OC_BYTE tabY [] ) const
00196 {
00197     uiTabIndex = 0;
00198     uiYIndex = 0;
00199     ftabTemp = ftabData;
00200 
00201    // FIXME: this is ugly
00202     glBegin( GL_QUADS );
00203 
00204    // go through the data table
00205    // and display the correct primitive
00206     while (uiTabIndex < uiTabSize) {
00207         if (ftabTemp[uiTabIndex]
00208         ==  OC_OCM_VERTEX) {
00209             glVertex3f(
00210                 ftabTemp[uiTabIndex+1] + rcfW,
00211                 ftabTemp[uiTabIndex+2] + tabY[uiYIndex],
00212                 ftabTemp[uiTabIndex+3] + rcfH );
00213             uiTabIndex += 4;
00214             uiYIndex++;
00215 //debug cout << "vertex" << endl;
00216 //debug cout << "tabYH : " << (int)tabY[uiYIndex] << endl;
00217         } else
00218         if (ftabTemp[uiTabIndex]
00219         == OC_OCM_COLOR) {
00220             glColor4fv( ftabTemp + uiTabIndex + 1 );
00221             uiTabIndex += 5;
00222 //debug cout << "color" << endl;
00223         } else
00224         if (ftabTemp[uiTabIndex]
00225         ==  OC_OCM_TEXCOOR) {
00226             glTexCoord3fv( ftabTemp + uiTabIndex + 1 );
00227             uiTabIndex += 4;
00228         } else
00229         if (ftabTemp[uiTabIndex]
00230         == OC_OCM_TEXBIND) {
00231            //TODO: this is REALLY dirty
00232             glEnd();
00233             if ( (GLuint)ftabTemp[uiTabIndex+1] == 0 ) {
00234                // restore the current rendering context
00235                 glTexCoord4i( 0, 0, 0, 1 );
00236                 glDisable( GL_TEXTURE_2D );
00237                // NOTE: works around the ATI 9M bug
00238                 glColor4ub( 255, 255, 255, 255 );
00239                 glEnable( GL_COLOR_MATERIAL );
00240                 glEnable( GL_LIGHTING );
00241                 glDisable( GL_BLEND );
00242             }
00243             else {
00244                // NOTE: works around the ATI 9M bug
00245                 glDisable( GL_LIGHTING );
00246                 glDisable( GL_COLOR_MATERIAL );
00247 
00248                // enable alpha blending
00249                 glEnable( GL_BLEND );
00250                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00251 
00252                // enable texturing
00253                 glEnable( GL_TEXTURE_2D );
00254                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
00255                 glBindTexture( GL_TEXTURE_2D, (GLuint)ftabTemp[uiTabIndex+1] );
00256 //debug cout << "Binding texture id: " << (GLuint)ftabTemp[uiTabIndex+1] << endl;
00257             }
00258             glBegin( GL_QUADS );
00259             uiTabIndex += 2;
00260         }
00261         else {
00262             OPENCITY_DEBUG("WARNING: unknown OCM code");
00263         }
00264     }
00265 
00266     glEnd();
00267 }
00268 
00269 
00270    /*=====================================================================*/
00271 void
00272 Model::DisplayPoly(
00273     const OC_FLOAT & rcfW,
00274     const OC_FLOAT & rcfH,
00275     const OC_BYTE tabY []
00276 ) const
00277 {
00278     uint    uiVertI = 0;
00279     uint    uiTCoordI = 0;
00280     int iMatI = -1;
00281     uint    uiMatINew = 0;
00282     int iOldTex = -1;
00283 
00284 // WARNING: GL_COLOR_MATERIAL is enabled with 
00285     glEnable( GL_TEXTURE_2D );
00286     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
00287     glEnable( GL_BLEND );
00288     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00289     glBegin(GL_QUADS);
00290 
00291     while (uiVertI < uiTabSize) {
00292         uiMatINew = uiVertI / 12;       // _Quad_
00293 
00294        // IF it's a new material THEN
00295         if (uiMatINew != (uint)iMatI) {
00296             iMatI = uiMatINew;
00297 
00298         // IF this quad needs a new tex THEN
00299             if (iOldTex != (GLint)uitabTexName[iMatI]) {
00300                 glEnd();
00301                 iOldTex = uitabTexName[iMatI];
00302                 glBindTexture(GL_TEXTURE_2D, uitabTexName[iMatI]);
00303                 glBegin( GL_QUADS );
00304             }
00305 
00306             glColor4f(
00307                 ftabRGB[iMatI*3],
00308                 ftabRGB[iMatI*3+1],
00309                 ftabRGB[iMatI*3+2], 1. );
00310         }
00311 
00312         glTexCoord2f(
00313             ftabTexCoord[uiTCoordI],
00314             ftabTexCoord[uiTCoordI+1]);
00315         glVertex3f(
00316             ftabData[uiVertI] + rcfW,
00317             ftabData[uiVertI+1],
00318             ftabData[uiVertI+2] + rcfH );
00319 
00320         uiTCoordI += 2;
00321         uiVertI += 3;
00322     }
00323 
00324     glEnd();
00325 
00326     glDisable( GL_BLEND );
00327     glDisable( GL_TEXTURE_2D );
00328 }
00329 
00330 
00331    /*=====================================================================*/
00332 void
00333 Model::DisplayList() const
00334 {
00335     assert( this->uiOpaqueList != 0 );
00336     glCallList( this->uiOpaqueList );
00337     if (glIsList( this->uiAlphaList ))
00338         glCallList( this->uiAlphaList );
00339 }
00340 
00341 
00342    /*=====================================================================*/
00343 void
00344 Model::DisplayList(
00345     const OC_FLOAT & rcfW,
00346     const OC_FLOAT & rcfL,
00347     const OC_BYTE tabY []
00348     ) const
00349 {
00350     assert( tabY != NULL );
00351     assert( this->uiOpaqueList != 0 );
00352 
00353 // Call the opaque list first, then the alpha list
00354 //  glMatrixMode( GL_MODELVIEW );
00355     glPushMatrix();
00356     glTranslatef( rcfW, tabY[0], rcfL );
00357     glCallList( this->uiOpaqueList );
00358     if (glIsList( this->uiAlphaList ))
00359         glCallList( this->uiAlphaList );
00360     glPopMatrix();
00361 }
00362 
00363 
00364 
00365 
00366 
00367 
00368 
00369 
00370 
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 

Generated on Sat Nov 11 10:21:10 2006 for OpenCity by  doxygen 1.4.2