Documentation


CGRenderingMachine.cpp

Go to the documentation of this file.
00001 #include "assertions.h"
00002 
00003 #include "CGRenderingMachine.h"
00004 
00005 
00006 #include <Cg/cg.h>
00007 #include <Cg/cgGL.h>
00008 
00009 #include <X3DTK/private/X3DBaseTypes.h>
00010 
00011 
00012 
00013 
00014 namespace animal
00015 {
00016 namespace octree
00017 {
00018  
00022 CGcontext vContext;
00023 CGprogram vProgram;
00024 CGprofile vProfile;
00025 
00026 CGparameter ModelViewProj;
00027 CGparameter ModelView;
00028 CGparameter ModelViewIT;
00029 
00030 CGparameter CellVerticesID;
00031 CGparameter CellID;
00032 
00033 void cgErrorCallback(void);
00034 void cleanExit(int exitval);
00035 void drawTest( void );
00036 static void InitializeCgGL( void );
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 CGRenderingMachine::CGRenderingMachine( Octree *octree, X3DTK::X3D::IndexedFaceSet *ifs ) :
00046     _octree(octree), _ifs(ifs), _cgInitiliazed(false)
00047 {
00048     _cgData.cellsVertexIDsArray = NULL;
00049 }
00050 
00051 
00052 
00053 void CGRenderingMachine::initializeCg( int method )
00054 {
00055     InitializeCgGL();
00056         
00057     structureUpdated( method );
00058     
00059     _cgInitiliazed = true;
00060 }
00061 bool CGRenderingMachine::cgIsInitialized() const
00062 {
00063     return _cgInitiliazed;
00064 }
00065 
00066               
00067 void CGRenderingMachine::render( int method )
00068 {
00069     Require( _octree != NULL );
00070     
00071     std::cerr << "\nCGRenderingMachine::render( "<< method << " )\n";
00072     
00073     switch( method )
00074     {
00075     
00076         case LINEAR:
00077             drawMethodLinear();
00078             break;
00079             
00080         default:
00081             std::cerr << "Unkown method !\n";
00082             break;
00083     }
00084     
00085     std::cerr << "CGRenderingMachine::render( "<< method << " ) end\n";
00086 }
00087 
00088 
00089 
00090 
00091 
00092 void CGRenderingMachine::structureUpdated( int method )
00093 {
00094     computeCGData( method );
00095     loadCGParameters( method );
00096 }
00097 
00098 
00099 
00100 CGData& CGRenderingMachine::computeCGData( int method )
00101 {   
00102     if( _cgData.cellsVertexIDsArray )
00103     {
00104         delete []_cgData.cellsVertexIDsArray;
00105     }
00106     
00107     
00108     Cell *cell = _octree->root();
00109     unsigned int nMeshPoints = cell->getData()._points.size();
00110         
00111     //
00112     // First get a unique list of octree's vertex with associated IDs
00113     //
00114     unsigned int nVertices = 0;
00115     for( Cell::vertex_width_iterator it( cell ) ; !it.empty() ; )
00116     {
00117         ConstrainedVertex* v = ++it;
00118         
00119         _cgData.verticesIDs[v] = nVertices;
00120         
00121         nVertices++;
00122     }
00123 
00124     
00125     // Now get the list of unique cell associated to each vertex of the mesh
00126     _cgData.meshPointsCell.resize( nMeshPoints );
00127     _cgData.meshPointsSFVCC.resize( nMeshPoints );
00128     
00129     unsigned int nCells = 0;
00130     std::map<Cell*,unsigned int> cellIDs;
00131     
00132     for( Octree::dfs_leaf_iterator itCell( cell ) ; !itCell.empty() ; )
00133     {
00134         Cell *tmpCell = ++itCell;
00135 
00136         if( tmpCell->isLeaf() )
00137         {
00138             cellIDs[ tmpCell ] = nCells;
00139             
00140             // Draw the vertices using the CG shader
00141             OctreeDataPoints *points = &(cell->getData()._points);
00142             for( OctreeDataPoints::iterator point = points->begin() ; point != points->end() ; ++point )
00143             {
00144                 _cgData.meshPointsCell[(*point)->getVertexID()] = nCells;
00145                 _cgData.meshPointsSFVCC[(*point)->getVertexID()] = *point;
00146             }
00147             
00148             nCells++;
00149             // Advance to the next cell
00150             // Becaus the iterator is not so good...
00151             ++itCell;           
00152         }
00153     }
00154     
00155     //std::cerr << "Getting the cellVertexIDs\n";
00156     // Now, for each cell, get it's vertex ids
00157     // For each cell of ID "i" store a vector containing the unique IDs of cell vertices
00158     //
00159     _cgData.cellsVertexIDs.resize(nCells);
00160     unsigned int i=0;
00161     
00162     //
00163     // Here we put the parameters for the cells IDs
00164     // for each cell, give the IDs of the 8 vertices
00165     //
00166     _cgData.cellsVertexIDsArray = new float[8*_cgData.cellsVertexIDs.size()];
00167     
00168     for( Octree::dfs_leaf_iterator itCell2( cell ) ; !itCell2.empty() ; )
00169     {
00170         Cell *tmpCell = ++itCell2;
00171 
00172         if( tmpCell->isLeaf() )
00173         {
00174             //std::cerr << "\tCell is" << *tmpCell << "\n";
00175             _cgData.cellsVertexIDs[ i ] = std::vector<unsigned int>(8);
00176             
00177             for( unsigned int j=0 ; j<8 ; ++j )
00178             {
00179                 _cgData.cellsVertexIDs[i][j] = _cgData.verticesIDs[ tmpCell->vertex(j) ];
00180                 _cgData.cellsVertexIDsArray[i*_cgData.cellsVertexIDs.size()+j] = _cgData.cellsVertexIDs[i][j];  
00181             }
00182             ++i;
00183             // Advance to the next cell
00184             // Becaus the iterator is not so good...
00185             ++itCell2;
00186         }
00187     }   
00188 
00189     return _cgData;
00190 }
00191 
00192 
00193 void CGRenderingMachine::loadCGParameters( int method ) const
00194 {
00195     cgGLSetStateMatrixParameter( ModelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
00196     
00197     switch( method )
00198     {
00199         case LINEAR:
00200             break;
00201             
00202         default:
00203             std::cerr << "loadCGParameters() : unknown method\n";
00204     
00205     }
00206 }
00207 
00208 
00209 void CGRenderingMachine::drawMethodLinear()
00210 {
00211     Require( cgIsInitialized() );
00212     
00213     cgGLBindProgram(vProgram);
00214     cgGLEnableProfile(vProfile);
00215 
00216     cgGLSetStateMatrixParameter( ModelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
00217     
00218     
00219     //
00220     // Now parse the ifs coordindex
00221     //
00222     const X3DTK::MFInt32 coordIndex = _ifs->getCoordIndex();
00223     
00224     glBegin( GL_POLYGON );
00225     for( X3DTK::MFInt32::const_iterator it = coordIndex.begin() ; it!=coordIndex.end() ; ++it )
00226     {           
00227         if( *it == -1 )
00228         {
00229             glEnd();
00230             glBegin( GL_POLYGON );
00231         }
00232         else
00233         {
00234             // Send the cell ID
00235             cgGLSetParameter1f( CellID, _cgData.meshPointsCell[*it] );
00236             // Send the parameters via the glVertex3f
00237             glVertex3fv( _cgData.meshPointsSFVCC[*it]->getLocalParams() );
00238         }       
00239     }
00240     glEnd();    
00241 
00242         /*        
00243     cgGLSetParameter4f( MyPosition, (float)(time(NULL) % 10) / 10.0, 0.0, 0.0, 0.0);
00244     cgGLSetStateMatrixParameter( ModelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
00245     */
00246 
00247     cgGLDisableProfile(vProfile);
00248 }
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 /*******************************************************************************
00257  * NOW CG STUFF
00258  */
00259 
00260 
00261 
00266 void cleanExit(int exitval)
00267 {
00268     if (vProgram) cgDestroyProgram(vProgram);
00269     if (vContext) cgDestroyContext(vContext);
00270 
00271     exit(exitval);
00272 }
00273 void cgErrorCallback(void)
00274 {
00275     CGerror LastError = cgGetError();
00276 
00277     if(LastError)
00278     {
00279         const char *Listing = cgGetLastListing(vContext);
00280         printf("\n---------------------------------------------------\n");
00281         printf("%s\n\n", cgGetErrorString(LastError));
00282         printf("%s\n", Listing);
00283         printf("---------------------------------------------------\n");
00284         printf("Cg error, exiting...\n");
00285         cleanExit(0);
00286     }
00287 }
00288 
00289 
00290 
00291 
00292 
00293 
00294 void drawTest( void )
00295 {    
00296    cgGLBindProgram(vProgram);
00297    cgGLEnableProfile(vProfile);
00298         
00299    //cgGLSetParameter4f( MyPosition, (float)(time(NULL) % 10) / 10.0, 0.0, 0.0, 0.0);
00300    cgGLSetStateMatrixParameter( ModelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
00301    
00302    /*
00303    cgGLSetStateMatrixParameter( ModelView, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);
00304    cgGLSetStateMatrixParameter( ModelViewIT, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);
00305    */
00306 
00307    glPointSize( 20 );
00308    
00309     glBegin( GL_TRIANGLES );
00310     glVertex3f( 0.0, 0.0, 0.1 );
00311     glVertex3f( 0.1, 0.0, 0.1 );
00312     glVertex3f( 0.1, 0.1, 0.1 );
00313     glEnd();
00314 
00315    cgGLDisableProfile(vProfile);
00316 }
00317 
00318 
00319 
00320 
00321 static void InitializeCgGL ( void)
00322 {
00323    cgSetErrorCallback( cgErrorCallback );
00324    
00325    // cgContext creation
00326    vContext = cgCreateContext();
00327    
00328    vProfile = CG_PROFILE_ARBVP1;
00329    /*
00330     if (cgGLIsProfileSupported(CG_PROFILE_ARBVP1))
00331    {
00332         vProfile = CG_PROFILE_ARBVP1;
00333     }
00334    else if (cgGLIsProfileSupported(CG_PROFILE_VP20))
00335    {
00336         vProfile = CG_PROFILE_VP20;
00337     }
00338     else
00339    {
00340         std::cout <<"Vertex programming extensions (GL_ARB_vertex_program or GL_NV_vertex_program) not supported"<<std::endl;
00341         exit(0);
00342    }
00343    */
00344 
00345    
00346 
00347    // create the vertex program
00348    char filename[] = "test.cg";
00349    
00350    vProgram = cgCreateProgramFromFile( vContext, 
00351                               CG_SOURCE, filename,
00352                               vProfile, NULL, NULL);
00353 
00354    cgGLLoadProgram(vProgram);
00355    
00356    ModelViewProj = cgGetNamedParameter( vProgram, "ModelViewProj" );
00357    /*
00358    ModelView = cgGetNamedParameter( vProgram, "ModelView" );
00359    ModelViewIT = cgGetNamedParameter( vProgram, "ModelViewIT");
00360    */
00361    
00362    CellVerticesID = cgGetNamedParameter( vProgram, "CellVerticesID" );
00363    CellID = cgGetNamedParameter( vProgram, "CellID" );
00364    
00365    // verify that parameters were retrieved successfully
00366    if(  !CellID || !ModelViewProj || !CellVerticesID )
00367     {
00368         std::cout << "Unable to retrieve program parameters, exiting.."<< std::endl;
00369         cleanExit(0);
00370     }
00371     
00372 }
00373 
00374 
00375 /*
00376 int main(int argc, char **argv)
00377 {
00378    keys();
00379 
00380    // Initialize the data for the rectangular grid of vertices.
00381    DataInitialize();
00382 
00383    InitializeGlut( argc, argv);
00384    
00385    // Create Cg Context
00386    InitializeCgGL ();
00387    
00388    glutMainLoop();
00389 
00390    glDeleteLists(vertexListSine,1);
00391 
00392    // free memory of vertices
00393    if (vertices)
00394      delete vertices;
00395      
00396    return (1);
00397 }     
00398 */        
00399         
00400 }
00401 }
00402 
00403 

Generated on Thu Dec 23 13:52:23 2004 by doxygen 1.3.6