2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
4
// Contact: pgregory@aqsis.com
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation; either
9
// version 2 of the License, or (at your option) any later version.
11
// This library is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
// General Public License for more details.
16
// You should have received a copy of the GNU General Public
17
// License along with this library; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
\brief Declares the CqAttributes class for handling RenderMan attributes.
23
\author Paul C. Gregory (pgregory@aqsis.com)
26
//? Is attributes.h included already?
27
#ifndef ATTRIBUTES_H_INCLUDED
29
#define ATTRIBUTES_H_INCLUDED 1
43
#include "trimcurve.h"
44
#include "iattributes.h"
46
START_NAMESPACE( Aqsis )
51
//----------------------------------------------------------------------
53
Container class for the attributes definitions of the graphics state.
57
class CqAttributes : public CqRefCount, public IqAttributes
61
CqAttributes( const CqAttributes& From );
62
virtual ~CqAttributes();
64
/** Get a pointer to this attribute state suitable for writing.
65
* I the external references count is greater than 1, then create a copy on the stack and return that.
66
* \return a pointer to these attribute safe to write into.
70
// We are about to write to this attribute,so clone if references exist.
73
CqAttributes * pWrite = Clone();
82
CqAttributes& operator=( const CqAttributes& From );
84
/** Add a new user defined attribute.
85
* \param pAttribute a pointer to the new user defined attribute.
87
void AddAttribute( const boost::shared_ptr<CqNamedParameterList>& pAttribute )
89
m_aAttributes.Add( pAttribute );
91
/** Get a pointer to a named user defined attribute.
92
* \param strName the name of the attribute to retrieve.
93
* \return a pointer to the attribute or 0 if not found.
95
const boost::shared_ptr<CqNamedParameterList> pAttribute( const char* strName ) const
97
return ( m_aAttributes.Find( strName ) );
99
/** Get a pointer to a named user defined attribute suitable for writing.
100
* If the attribute has more than 1 external reference, create a duplicate an return that.
101
* \attention If the attribute does not exist in the list, one will automatically be created and added.
102
* \param strName the name of the attribute to retrieve.
103
* \return a pointer to the attribute.
105
boost::shared_ptr<CqNamedParameterList> pAttributeWrite( const char* strName )
107
boost::shared_ptr<CqNamedParameterList> pAttr = m_aAttributes.Find( strName );
110
if ( pAttr.unique() )
114
boost::shared_ptr<CqNamedParameterList> pNew( new CqNamedParameterList( *pAttr ) );
115
m_aAttributes.Remove( pAttr );
116
m_aAttributes.Add( pNew );
120
boost::shared_ptr<CqNamedParameterList> pNew( new CqNamedParameterList( strName ) );
121
m_aAttributes.Add( pNew );
125
/** Add a lightsource to the current available list.
126
* \param pL a pointer to the new lightsource.
128
void AddLightsource( CqLightsource* pL )
130
// Check if the ligthsource is already active
131
std::vector<CqLightsource*>::iterator end = m_apLightsources.end();
132
for ( std::vector<CqLightsource*>::iterator i = m_apLightsources.begin(); i != end; i++ )
137
m_apLightsources.push_back( pL );
139
/** Remove a lightsource from the current available list.
140
* \param pL a pointer to the lightsource to remove.
142
void RemoveLightsource( CqLightsource* pL )
144
// Check if the ligthsource is in the active list.
145
std::vector<CqLightsource*>::iterator end = m_apLightsources.end();
146
for ( std::vector<CqLightsource*>::iterator i = m_apLightsources.begin(); i != end; i++ )
150
m_apLightsources.erase( i );
155
/** Get a reference to the lightsource list.
156
* \return a reference to the vector of lightsource pointers.
158
virtual const std::vector<CqLightsource*>& apLights() const
160
return ( m_apLightsources );
163
/** Flip the orientation in which primitives are described between left and right handed.
164
* \param time the frame time to get the values in the case of a motion blurred attribute. (not used).
166
void FlipeOrientation( TqFloat time )
168
TqBool co = GetIntegerAttribute( "System", "Orientation" ) [ 0 ] == 0;
169
GetIntegerAttributeWrite( "System", "Orientation" ) [ 0 ] = ( co ) ? 1 : 0;
172
virtual IqShader* pshadDisplacement( TqFloat time ) const
174
return ( m_pshadDisplacement );
176
virtual void SetpshadDisplacement( IqShader* pshadDisplacement, TqFloat time )
178
m_pshadDisplacement = pshadDisplacement;
180
virtual IqShader* pshadAreaLightSource( TqFloat time ) const
182
return ( m_pshadAreaLightSource );
184
virtual void SetpshadAreaLightSource( IqShader* pshadAreaLightSource, TqFloat time )
186
m_pshadAreaLightSource = pshadAreaLightSource;
188
virtual IqShader* pshadSurface( TqFloat time ) const
190
return ( m_pshadSurface );
192
virtual void SetpshadSurface( IqShader* pshadSurface, TqFloat time )
194
m_pshadSurface = pshadSurface;
196
virtual IqShader* pshadAtmosphere( TqFloat time ) const
198
return ( m_pshadAtmosphere );
200
virtual void SetpshadAtmosphere( IqShader* pshadAtmosphere, TqFloat time )
202
m_pshadAtmosphere = pshadAtmosphere;
204
virtual IqShader* pshadExteriorVolume( TqFloat time ) const
206
return ( m_pshadExteriorVolume );
208
virtual void SetpshadExteriorVolume( IqShader* pshadExteriorVolume, TqFloat time )
210
m_pshadExteriorVolume = pshadExteriorVolume;
212
virtual IqShader* pshadAreaInteriorVolume( TqFloat time ) const
214
return ( m_pshadInteriorVolume );
216
virtual void SetpshadInteriorVolume( IqShader* pshadInteriorVolume, TqFloat time )
218
m_pshadInteriorVolume = pshadInteriorVolume;
221
/** Get the array of trim curve loops.
222
* \return A pointer to the trim loops array object.
224
const CqTrimLoopArray& TrimLoops() const
226
return ( m_TrimLoops );
228
/** Get the array of trim curve loops.
229
* \return A pointer to the trim loops array object.
231
CqTrimLoopArray& TrimLoops()
233
return ( m_TrimLoops );
236
/** Clone the entire attribute state.
237
* \return a pointer to the new attribute state.
239
CqAttributes* Clone() const
241
return ( new CqAttributes( *this ) );
244
const CqParameter* pParameter( const char* strName, const char* strParam ) const;
245
CqParameter* pParameterWrite( const char* strName, const char* strParam );
247
virtual const TqFloat* GetFloatAttribute( const char* strName, const char* strParam ) const;
248
virtual const TqInt* GetIntegerAttribute( const char* strName, const char* strParam ) const;
249
virtual const CqString* GetStringAttribute( const char* strName, const char* strParam ) const;
250
virtual const CqVector3D* GetPointAttribute( const char* strName, const char* strParam ) const;
251
virtual const CqVector3D* GetVectorAttribute( const char* strName, const char* strParam ) const;
252
virtual const CqVector3D* GetNormalAttribute( const char* strName, const char* strParam ) const;
253
virtual const CqColor* GetColorAttribute( const char* strName, const char* strParam ) const;
254
virtual const CqMatrix* GetMatrixAttribute( const char* strName, const char* strParam ) const;
256
virtual TqFloat* GetFloatAttributeWrite( const char* strName, const char* strParam );
257
virtual TqInt* GetIntegerAttributeWrite( const char* strName, const char* strParam );
258
virtual CqString* GetStringAttributeWrite( const char* strName, const char* strParam );
259
virtual CqVector3D* GetPointAttributeWrite( const char* strName, const char* strParam );
260
virtual CqVector3D* GetVectorAttributeWrite( const char* strName, const char* strParam );
261
virtual CqVector3D* GetNormalAttributeWrite( const char* strName, const char* strParam );
262
virtual CqColor* GetColorAttributeWrite( const char* strName, const char* strParam );
263
virtual CqMatrix* GetMatrixAttributeWrite( const char* strName, const char* strParam );
265
virtual TqInt cLights() const
267
return ( apLights().size() );
269
virtual IqLightsource* pLight( TqInt index );
272
virtual void Release()
274
CqRefCount::Release();
276
virtual void AddRef()
278
CqRefCount::AddRef();
281
virtual void AddRef(const TqChar* file, TqInt line)
283
CqRefCount::AddRef(file, line);
285
virtual void Release(const TqChar* file, TqInt line)
287
CqRefCount::Release(file, line);
289
CqString className() const { return CqString("CqAttributes"); }
297
static const TqInt tableSize;
302
m_aLists.resize( tableSize );
304
virtual ~CqHashTable()
308
const boost::shared_ptr<CqNamedParameterList> Find( const TqChar* pname ) const
310
TqUlong hash = CqParameter::hash(pname);
311
TqInt i = _hash( hash);
313
if ( m_aLists[ i ].empty() )
315
boost::shared_ptr<CqNamedParameterList> retval;
317
// return ( boost::shared_ptr<CqNamedParameterList>() );
321
std::list<boost::shared_ptr<CqNamedParameterList> >::const_iterator iEntry = m_aLists[ i ].begin();
322
if ( iEntry == m_aLists[ i ].end() )
326
while ( iEntry != m_aLists[ i ].end() )
328
if ( ( *iEntry ) ->hash() == hash )
334
boost::shared_ptr<CqNamedParameterList> retval;
336
// return ( boost::shared_ptr<CqNamedParameterList>() );
339
boost::shared_ptr<CqNamedParameterList> Find( const TqChar* pname )
341
TqUlong hash = CqParameter::hash(pname);
342
TqInt i = _hash( hash);
344
if ( m_aLists[ i ].empty() )
346
boost::shared_ptr<CqNamedParameterList> retval;
348
// return ( boost::shared_ptr<CqNamedParameterList>() );
352
std::list<boost::shared_ptr<CqNamedParameterList> >::const_iterator iEntry = m_aLists[ i ].begin();
353
if ( iEntry == m_aLists[ i ].end() )
357
while ( iEntry != m_aLists[ i ].end() )
359
if ( ( *iEntry ) ->hash() == hash )
365
boost::shared_ptr<CqNamedParameterList> retval;
367
// return ( boost::shared_ptr<CqNamedParameterList>() );
370
void Add( const boost::shared_ptr<CqNamedParameterList>& pOption )
372
TqUlong hash = CqParameter::hash(pOption->strName().c_str());
373
TqInt i = _hash( hash);
374
m_aLists[ i ].push_back( pOption );
377
void Remove( const boost::shared_ptr<CqNamedParameterList>& pOption )
379
TqUlong hash = CqParameter::hash(pOption->strName().c_str());
380
TqInt i = _hash( hash);
382
std::list<boost::shared_ptr<CqNamedParameterList> >::iterator iEntry = m_aLists[ i ].begin();
383
while ( iEntry != m_aLists[ i ].end() )
385
if ( ( *iEntry ) == pOption )
387
m_aLists[ i ].remove( *iEntry );
394
CqHashTable& operator=( const CqHashTable& From )
396
std::vector<std::list<boost::shared_ptr<CqNamedParameterList> > >::const_iterator i;
397
for ( i = From.m_aLists.begin(); i != From.m_aLists.end(); i++ )
399
std::list<boost::shared_ptr<CqNamedParameterList> >::const_iterator i2;
400
for ( i2 = ( *i ).begin(); i2 != ( *i ).end(); i2++ )
407
TqInt _hash( TqUlong h ) const
409
return (h % tableSize);
411
TqInt _hash( const TqChar* string ) const
413
assert ( string != 0 && string[ 0 ] != 0 );
415
TqUlong h = CqParameter::hash( string );
416
return ( (TqUlong) h % tableSize ); // remainder
419
std::vector<std::list<boost::shared_ptr<CqNamedParameterList> > > m_aLists;
425
static const TqInt tableSize;
427
typedef std::map<std::string, boost::shared_ptr<CqNamedParameterList>, std::less<std::string> > plist_type;
428
typedef plist_type::value_type value_type;
429
typedef plist_type::iterator plist_iterator;
430
typedef plist_type::const_iterator plist_const_iterator;
435
virtual ~CqHashTable()
439
const boost::shared_ptr<CqNamedParameterList> Find( const TqChar* pname ) const
441
std::string strName( pname );
442
plist_const_iterator it = m_ParameterLists.find( strName );
443
if( it != m_ParameterLists.end() )
444
return ( it->second );
446
return ( boost::shared_ptr<CqNamedParameterList>() );
449
boost::shared_ptr<CqNamedParameterList> Find( const TqChar* pname )
451
std::string strName( pname );
452
plist_iterator it = m_ParameterLists.find( strName );
453
if( it != m_ParameterLists.end() )
454
return ( it->second );
456
return ( boost::shared_ptr<CqNamedParameterList>() );
459
void Add( const boost::shared_ptr<CqNamedParameterList>& pOption )
461
m_ParameterLists.insert(value_type(pOption->strName(), pOption) );
464
void Remove( const boost::shared_ptr<CqNamedParameterList>& pOption )
466
plist_iterator it = m_ParameterLists.find( pOption->strName() );
467
if( it != m_ParameterLists.end() )
469
m_ParameterLists.erase(it);
473
CqHashTable& operator=( const CqHashTable& From )
475
plist_const_iterator it = From.m_ParameterLists.begin();
476
while( it != From.m_ParameterLists.end() )
485
plist_type m_ParameterLists;
489
CqHashTable m_aAttributes; ///< a vector of user defined attribute pointers.
491
IqShader* m_pshadDisplacement; ///< a pointer to the current displacement shader.
492
IqShader* m_pshadAreaLightSource; ///< a pointer to the current area ligthsource shader.
493
IqShader* m_pshadSurface; ///< a pointer to the current surface shader.
494
IqShader* m_pshadAtmosphere; ///< a pointer to the current atmosphere shader.
495
IqShader* m_pshadInteriorVolume; ///< a pointer to the current interior shader.
496
IqShader* m_pshadExteriorVolume; ///< a pointer to the current exterior shader.
498
CqTrimLoopArray m_TrimLoops; ///< the array of closed trimcurve loops.
499
std::vector<CqLightsource*> m_apLightsources; ///< a vector of currently available lightsources.
501
std::list<CqAttributes*>::iterator m_StackIterator; ///< the index of this attribute state in the global stack, used for destroying when last reference is removed.
505
/// Global attribute stack.
506
extern std::list<CqAttributes*> Attribute_stack;
509
//-----------------------------------------------------------------------
511
END_NAMESPACE( Aqsis )
513
//} // End of #ifdef ATTRIBUTES_H_INCLUDED