CPPGPGPU Library - Reference (Doxygen)

Main Page | General Use | Reference | Examples Info | Get/Download CPPGPGPU | SF.net Page

BasicNodes.cpp

Go to the documentation of this file.
00001 #include "BasicNodes.h"
00002 #include "GLUTCore.h"
00003 #include <stdio.h>
00004 #define STRUBUFFERSIZE 512
00005 
00006 float ShaderNode::m_fTimePerCheck = 1.0f;
00007 
00008 void ShaderNode::Render()
00009 {
00010     if( m_sShaderName.length() )
00011     {
00012         S.ActivateShader( vsnIs, vsnFs, vfnFs );
00013         NodeObject::Render();
00014         S.DeactivateShader();
00015     } else
00016         NodeObject::Render();
00017 }
00018 
00019 void ShaderNode::Update( const float fDtime )
00020 {
00021     m_fTimeSinceLastCheck += fDtime;
00022     if( m_fTimeSinceLastCheck > m_fTimePerCheck && m_sShaderName.length() )
00023     {
00024         S.CheckForNewer( m_sShaderName.c_str() );
00025         m_fTimeSinceLastCheck = 0.0f;
00026     }
00027     NodeObject::Update( fDtime );
00028 }
00029 
00030 REGISTER_CLASS( ShaderNode );
00031 
00032 TextureNode::TextureNode( const string & sName ) : NodeObject( sName )
00033 {
00034     if( sName.length() > 8 )
00035         if( sName.substr( 0, 7 ).compare( "DYNTEX:" ) == 0 )
00036         {
00037             string sP = sName.substr( 7 );
00038             int iWidth, iHeight;
00039             TextureType tex;
00040             char tt[32];
00041             if( sscanf( sP.c_str(), "%d,%d,%30s", &iWidth, &iHeight, tt ) == 3 )
00042             {
00043                 string stype = tt;
00044                 if( stype.substr(0,10).compare( "GRAYSCALE," ) == 0 )
00045                     tex = TTGRAYSCALE;
00046                 else if( stype.substr(0,4).compare( "RGB," ) == 0 )
00047                     tex = TTRGB;
00048                 else if( stype.substr(0,5).compare( "RGBA," ) == 0 )
00049                     tex = TTRGBA;
00050                 else if( stype.substr(0,7).compare( "RGBA16," ) == 0 )
00051                     tex = TTRGBA16;
00052                 else if( stype.substr(0,7).compare( "RGBA32," ) == 0 )
00053                     tex = TTRGBA32;
00054                 else
00055                     return;
00056                 T.MakeDynamicTexture( iWidth, iHeight, tex );
00057             }
00058             return;
00059         }
00060 
00061     T.LoadTextureFile( sName.c_str() );
00062 }
00063 
00064 void TextureNode::Render()
00065 {
00066     T.ActivateTexture();
00067     NodeObject::Render();
00068     T.DeactivateTexture();
00069 }
00070 
00071 REGISTER_CLASS( TextureNode );
00072 
00073 TextureSetNode::TextureSetNode( const string & sName )
00074  : NodeObject( sName )
00075 {
00076     //No code
00077 }
00078 
00079 void TextureSetNode::Render()
00080 {
00081     unsigned i;
00082     for( i = 0; i < m_vAllTextures.size(); ++i )
00083         m_vAllTextures[i].T.ActivateTexture( i );
00084     NodeObject::Render();
00085     for( i = 0; i < m_vAllTextures.size(); ++i )
00086         m_vAllTextures[i].T.DeactivateTexture( i );
00087 }
00088 
00089 REGISTER_CLASS( TextureSetNode );
00090 
00091 void RFBufferNode::ConfigureForRun( vector< TextureNode *> & vTextures, bool bNeedDepth )
00092 {
00093     m_vAllCoreTextures.clear();
00094     m_vAllTextures = vTextures;
00095     B.Setup( bNeedDepth );
00096     for( unsigned i = 0; i < m_vAllTextures.size(); ++i )
00097     {
00098         m_vAllCoreTextures.push_back( m_vAllTextures[i]->T );
00099     }
00100 }
00101 
00102 void RFBufferNode::Render()
00103 {
00104     if( m_vAllTextures.size() > 0 )
00105     {
00106         Texture & T = m_vAllTextures[0]->T;
00107         B.ConfigureAndStart( T.GetWidth(), T.GetHeight(), m_vAllTextures.size(), &m_vAllCoreTextures[0], true );
00108         NodeObject::Render();
00109         B.End( GLUT.miWidth, GLUT.miHeight );
00110     } else
00111         NodeObject::Render();
00112 }
00113 
00114 REGISTER_CLASS( RFBufferNode );
00115 
00116 void SquareNode::Render()
00117 {
00118     DrawSquare();
00119     NodeObject::Render();
00120 }
00121 
00122 REGISTER_CLASS( SquareNode );
00123 
00124 void TransformNode::Render()
00125 {
00126     glMatrixMode(GL_MODELVIEW);
00127     glPushMatrix();
00128     glTranslatef( m_fX, m_fY, m_fZ );
00129     glRotatef( m_rX, 1.0, 0.0, 0.0 );
00130     glRotatef( m_rY, 0.0, 1.0, 0.0 );
00131     glRotatef( m_rZ, 0.0, 0.0, 1.0 );
00132     glScalef( m_sX, m_sY, m_sZ );
00133     NodeObject::Render();
00134     glPopMatrix();
00135 }
00136 
00137 REGISTER_CLASS( TransformNode );
00138 
00139 bool ModelNode::LoadOBJ( const char * sFileToLoad )
00140 {
00141     if( !sFileToLoad )
00142         return false;
00143     char lbuffer[STRUBUFFERSIZE];
00144     std::vector< std::vector<float> > V;
00145     std::vector< std::vector<float> > VT;
00146     std::vector< std::vector<float> > VN;
00147     std::vector< float > fLine;
00148     fLine.resize( 3 );
00149 
00150     int iFaces[9];
00151 
00152     FILE * f = fopen( sFileToLoad, "r" );
00153     if( !f )
00154     {
00155         printf( "Could not open file \"%s\".", sFileToLoad );
00156         return false;
00157     }
00158 
00159     iList = glGenLists( 1 );
00160     glNewList( iList, GL_COMPILE );
00161     glBegin( GL_TRIANGLES );
00162 
00163     while( fgets( lbuffer, STRUBUFFERSIZE-1, f ) )
00164     {
00165         switch( lbuffer[0] )
00166         {
00167         case 'v':
00168             switch( lbuffer[1] )
00169             {
00170             case 't':
00171                 if( sscanf( &lbuffer[3], "%f %f %f", &fLine[0], &fLine[1], &fLine[2] ) )
00172                     VT.push_back( fLine );
00173                 break;
00174             case 'n':
00175                 if( sscanf( &lbuffer[3], "%f %f %f", &fLine[0], &fLine[1], &fLine[2] ) )
00176                     VN.push_back( fLine );
00177                 break;
00178             case ' ':
00179                 if( sscanf( &lbuffer[2], "%f %f %f", &fLine[0], &fLine[1], &fLine[2] ) )
00180                     V.push_back( fLine );
00181                 break;
00182             }
00183             break;
00184         case 'f':
00185             if( sscanf( &lbuffer[2], "%d/%d/%d %d/%d/%d %d/%d/%d",
00186                 &iFaces[0], &iFaces[1], &iFaces[2],
00187                 &iFaces[3], &iFaces[4], &iFaces[5],
00188                 &iFaces[6], &iFaces[7], &iFaces[8] ) )
00189             {
00190                 for( int i = 0; i < 9; ++i )
00191                     iFaces[i]--;
00192                 glNormal3f( VN[iFaces[8]][0], VN[iFaces[8]][1], VN[iFaces[8]][2] );
00193                 glTexCoord3f( VT[iFaces[7]][0], VT[iFaces[7]][1], VT[iFaces[7]][2] );
00194                 glVertex3f( V[iFaces[6]][0], V[iFaces[6]][1], V[iFaces[6]][2] );
00195                 glNormal3f( VN[iFaces[5]][0], VN[iFaces[5]][1], VN[iFaces[5]][2] );
00196                 glTexCoord3f( VT[iFaces[4]][0], VT[iFaces[4]][1], VT[iFaces[4]][2] );
00197                 glVertex3f( V[iFaces[3]][0], V[iFaces[3]][1], V[iFaces[3]][2] );
00198                 glNormal3f( VN[iFaces[2]][0], VN[iFaces[2]][1], VN[iFaces[2]][2] );
00199                 glTexCoord3f( VT[iFaces[1]][0], VT[iFaces[1]][1], VT[iFaces[1]][2] );
00200                 glVertex3f( V[iFaces[0]][0], V[iFaces[0]][1], V[iFaces[0]][2] );
00201             }
00202         default:
00203             break;
00204         }
00205     }
00206     glEnd();
00207     glEndList();
00208     fclose( f );
00209     return true;
00210 }
00211 
00212 ModelNode::~ModelNode()
00213 {
00214     if( iList )
00215         glDeleteLists( iList, 1 );
00216 }
00217 
00218 void ModelNode::Render()
00219 {
00220     glCallList( iList );
00221     NodeObject::Render();
00222 }
00223 
00224 REGISTER_CLASS( ModelNode );
00225 REGISTER_CLASS( TeapotNode );
00226 REGISTER_CLASS( DataRunNode );
00227 
00228 /*
00229 Copyright (c) 2007 Charles Lohr
00230 
00231 Permission is hereby granted, free of charge, to any person obtaining a copy
00232 of this software and associated documentation files (the "Software"), to deal
00233 in the Software without restriction, including without limitation the rights
00234 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00235 copies of the Software, and to permit persons to whom the Software is
00236 furnished to do so, subject to the following conditions:
00237 
00238 The above copyright notice and this permission notice shall be included in
00239 all copies or substantial portions of the Software.
00240 
00241 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00242 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00243 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00244 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00245 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00246 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00247 THE SOFTWARE.
00248 */

© 2005-2007 Charles Lohr, Joshua Allen, David Chapman, Andrew Lohr. All material including documentation under the MIT/X11 license.