00001 #include "Types.h"
00002 #include "Rendernode.h"
00003 #include "Vertexbuffer.h"
00004 #include "Program.h"
00005 #include "ShadowProxy.h"
00006 #include "Stateset.h"
00007 #include "Sampler.h"
00008
00009 #include <float.h>
00010
00011 namespace RenderTools {
00012
00013 using namespace Matrix;
00014
00015 Rendernode::Rendernode( void ):
00016 RelationalNode(),
00017 m_componentMask( NONE ),
00018 m_index( -1 ),
00019 m_color( -1.0f ){
00020
00021 }
00022
00023 PropertyPtr Rendernode::create( const XMLNodePtr & xml ){
00024 RendernodePtr p( new Rendernode() );
00025 p->setName( xml );
00026 p->createProperties();
00027 p->setProperties( xml, false );
00028 return( dynamic_pointer_cast< AbstractProperty, Rendernode >( p ) );
00029 }
00030
00031 void Rendernode::createProperties( void ){
00032 RelationalNode::createProperties();
00033 createProperty( this, "color", & m_color, AbstractProperty::COLOR );
00034 createProperty( this, "componentMask", RenderComponentMaskStruct( & m_componentMask ), getRenderComponentsEnums(), AbstractProperty::MASK );
00035 createProperty( this, "sampler", & m_sampler );
00036 createProperty( this, "stateset", & m_stateset );
00037 createProperty( this, "vertexbuffers", & m_buffers );
00038 createProperty( this, "visible", BooleanStruct( & m_visible ) );
00039 #ifndef RT_GLES1
00040 createProperty( this, "program", & m_program );
00041 #endif
00042 }
00043
00044 const string Rendernode::getTypeName( bool ofComponent ) const {
00045 return( "Rendernode" );
00046 }
00047
00048 Rendernode::~Rendernode( void ){
00049 }
00050
00051 void Rendernode::onInitialize( void ){
00052 for( unsigned int i = 0; i < m_buffers->size(); i++ ){
00053 m_buffers->getItem( i )->initialize();
00054 }
00055 for( unsigned int i = 0; i < m_children->size(); i++ ){
00056 m_children->getItem( i )->initialize();
00057 }
00058 #ifndef RT_GLES1
00059 m_program->initialize();
00060 #endif
00061 m_stateset->initialize();
00062 m_sampler->initialize();
00063 }
00064
00065 void Rendernode::onBind( int unit ){
00066 #ifndef RT_GLES1
00067 m_program->bind();
00068 #endif
00069 m_sampler->bind();
00070 m_stateset->bind();
00071 }
00072
00073 void Rendernode::onUnbind( int unit ){
00074 }
00075
00076 void Rendernode::onRender( void ){
00077 #ifdef RT_PROFILER
00078 beginProfiler( "rendernode::render", __FILE__, __LINE__ );
00079 #endif
00080 Error::assertNoErrors( __FILE__, __LINE__ );
00081 GLuint i;
00083 m_program->bind();
00084 m_sampler->bind();
00085 m_stateset->bind();
00086 Error::assertNoErrors( __FILE__, __LINE__ );
00087
00088 push( MODELVIEW, get( MODELVIEW ) * m_local );
00089 #ifndef RT_AS3
00090
00091 if( m_componentMask & AXES ){
00092 # ifndef RT_GLES1
00093 ProgramPtr current = Program::getCurrent();
00094 if( current ){
00095 Program::unbind();
00096 }
00097 # endif
00098 drawAxes();
00099 # ifndef RT_GLES1
00100 if( current ){
00101 current->bind();
00102 }
00103 # endif
00104 }
00105 if( m_componentMask & NORMALS ){
00106 # ifndef RT_GLES1
00107 ProgramPtr current = Program::getCurrent();
00108 if( current ){
00109 Program::unbind();
00110 }
00111 # endif
00112 # ifdef RT_GLEW
00113 glDisable( GL_TEXTURE_2D );
00114 glBegin( GL_LINES );
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 glEnd();
00126 # endif
00127 # ifndef RT_GLES1
00128 if( current ){
00129 current->bind();
00130 }
00131 # endif
00132 }
00133 if( m_componentMask & BOUNDS ){
00134 # ifndef RT_GLES1
00135 ProgramPtr current = Program::getCurrent();
00136 if( current ){
00137 Program::unbind();
00138 }
00139 # endif
00140 push( MODELVIEW, Mat4::fromIdentity() );
00141
00142 m_naabb.render();
00143 m_aabb.render( m_local );
00144
00145 pop( MODELVIEW );
00146
00147 # ifndef RT_GLES1
00148 if( current ){
00149 current->bind();
00150 }
00151 # endif
00152 }
00153
00154 # ifndef RT_GLES1
00156 #if glColor4f
00157 if( ! Program::getCurrent() && m_color[0] >= 0.0f && m_color[1] >= 0.0f && m_color[2] >= 0.0f ){
00158 glColor4f( (GLfloat)m_color[0], (GLfloat)m_color[1], (GLfloat)m_color[2], (GLfloat)1.0f );
00159 }
00160 #endif
00161 Error::assertNoErrors( __FILE__, __LINE__ );
00162 ProgramPtr current = Program::getCurrent();
00163 if( current ){
00164 current->bind( "proxyIndex", toString( ShadowProxy::retrieveIndex( dynamic_pointer_cast< Rendernode, AbstractProperty >( getSharedPtr< AbstractProperty >() ) ) ) );
00165 current->bind( "nodeIndex", toString( m_index ) );
00166 current->bind( "currentColor", toString( m_color ) );
00167 }
00168 # endif
00169
00170 if( m_visible ){
00171 for( i = 0; i < m_buffers->size(); i++ ){
00172 m_buffers->getItem( i )->getPointer()->render();
00173 }
00174 }
00175 Error::assertNoErrors( __FILE__, __LINE__ );
00176 for( i = 0; i < m_children->size(); i++ ){
00177 push( MODELVIEW );
00178 dynamic_pointer_cast< Rendernode >( m_children->getItem( i ) )->render();
00179 pop( MODELVIEW );
00180 }
00181 #endif // AS3
00182 pop( MODELVIEW );
00183 #ifdef RT_PROFILER
00184 endProfiler( "rendernode::render" );
00185 #endif
00186 }
00187
00189 void Rendernode::removeVertexBuffers( bool send ){
00190 m_buffers->removeProperties();
00191 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_buffers ) ) );
00192 }
00193
00195 void Rendernode::addVertexBuffer( const VertexbufferPtr p, bool send ){
00196 if( Rendergroup::getActiveRendergroup() && ! Rendergroup::getActiveRendergroup()->isPointer( p ) ){
00197 Rendergroup::getActiveRendergroup()->addVertexbuffer( p );
00198 }
00199 addVertexBuffer( p->getName(), send );
00200 }
00201
00203 void Rendernode::addVertexBuffer( const string buffer, bool send ){
00204
00205
00206 createProperty( m_buffers, buffer );
00207
00208
00209 m_naabb.reset();
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 }
00225
00226 const VertexbufferProxyContainerPtr Rendernode::getVertexbufferProxies( void ) const {
00227 return( m_buffers );
00228 }
00229
00230 const Vec3 & Rendernode::getColor( void ) const {
00231 return( m_color );
00232 }
00233
00234 void Rendernode::setColor( const Vec3 & color, bool send ){
00235 m_color = color;
00236 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_color ) ) );
00237 }
00238
00239 #ifndef RT_GLES1
00240
00241 void Rendernode::setProgram( string name, bool send ){
00242 m_program->setName( name );
00243 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_program ) ) );
00244 }
00245
00246 const ProgramProxyPtr Rendernode::getProgramProxy( void ) const {
00247 return( m_program );
00248 }
00249
00250 #endif
00251
00252 void Rendernode::setSampler( string name, bool send ){
00253 m_sampler->setName( name );
00254 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_sampler ) ) );
00255 }
00256
00257 const SamplerProxyPtr Rendernode::getSamplerProxy( void ) const {
00258 return( m_sampler );
00259 }
00260
00261 void Rendernode::setStateset( string name, bool send ){
00262 m_stateset->setName( name );
00263 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_stateset ) ) );
00264 }
00265
00266 const StatesetProxyPtr Rendernode::getStatesetProxy( void ) const {
00267 return( m_stateset );
00268 }
00269
00270 void Rendernode::setComponentMask( ComponentMask mask, bool send ){
00271 m_componentMask = mask;
00272 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_componentMask ) ) );
00273 }
00274
00275 void Rendernode::setComponentMask( int mask, bool send ){
00276 m_componentMask = ( ComponentMask )mask;
00277 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_componentMask ) ) );
00278 }
00279
00280 Rendernode::ComponentMask Rendernode::getComponentMask( void ) const {
00281 return( m_componentMask );
00282 }
00283
00284 void Rendernode::setIndex( int index, bool send ){
00285 m_index = index;
00286 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_index ) ) );
00287 }
00288
00289 bool Rendernode::getVisible( void ) const {
00290 return( m_visible );
00291 }
00292
00293 void Rendernode::setVisible( bool state, bool send ){
00294 m_visible = state;
00295 sendPropertyEvent( PropertyEvent( PropertyEvent::CHANGED, findProperty( & m_visible ) ) );
00296 }
00297
00298 };
00299