~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to render/attributes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
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.
 
10
//
 
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.
 
15
//
 
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
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Implements the CqAttributes class for handling RenderMan attributes.
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
*/
 
25
 
 
26
#include        "aqsis.h"
 
27
 
 
28
#include        "attributes.h"
 
29
#include        "renderer.h"
 
30
#include        "shaders.h"
 
31
#include        "trimcurve.h"
 
32
#include        "imagebuffer.h"
 
33
#include        "lights.h"
 
34
 
 
35
START_NAMESPACE( Aqsis )
 
36
 
 
37
 
 
38
std::list<CqAttributes*>        Attribute_stack;
 
39
 
 
40
 
 
41
const TqInt CqAttributes::CqHashTable::tableSize = 127;
 
42
 
 
43
 
 
44
/** A macro to take care of adding a system attribute given a name.
 
45
 *  Creates a new CqParameter derived class, initialises it to the given default value and 
 
46
 *  adds it to the default attributes member.
 
47
 */
 
48
#define ADD_SYSTEM_ATTR(name, type, sltype, id, def) \
 
49
        CqParameterTypedUniform<type,id, sltype>* p##name = new CqParameterTypedUniform<type,id, sltype>(#name); \
 
50
        p##name->pValue()[0] = ( def ); \
 
51
        pdefattrs->AddParameter(p##name);
 
52
 
 
53
/** A macro to take care of adding a system attribute given a name.
 
54
 *  Creates a new CqParameter derived class, initialises it to the given default value and 
 
55
 *  adds it to the default attributes member.
 
56
 */
 
57
#define ADD_SYSTEM_ATTR2(name, type, sltype, id, def0, def1) \
 
58
        CqParameterTypedUniformArray<type,id, sltype>* p##name = new CqParameterTypedUniformArray<type,id, sltype>(#name,2); \
 
59
        p##name->pValue()[0] = ( def0 ); \
 
60
        p##name->pValue()[1] = ( def1 ); \
 
61
        pdefattrs->AddParameter(p##name);
 
62
 
 
63
/** A macro to take care of adding a system attribute given a name.
 
64
 *  Creates a new CqParameter derived class, initialises it to the given default value and 
 
65
 *  adds it to the default attributes member.
 
66
 */
 
67
#define ADD_SYSTEM_ATTR4(name, type, sltype, id, def0, def1, def2, def3) \
 
68
        CqParameterTypedUniformArray<type,id, sltype>* p##name = new CqParameterTypedUniformArray<type,id, sltype>(#name,4); \
 
69
        p##name->pValue()[0] = ( def0 ); \
 
70
        p##name->pValue()[1] = ( def1 ); \
 
71
        p##name->pValue()[2] = ( def2 ); \
 
72
        p##name->pValue()[3] = ( def3 ); \
 
73
        pdefattrs->AddParameter(p##name);
 
74
 
 
75
/** A macro to take care of adding a system attribute given a name.
 
76
 *  Creates a new CqParameter derived class, initialises it to the given default value and 
 
77
 *  adds it to the default attributes member.
 
78
 */
 
79
#define ADD_SYSTEM_ATTR6(name, type, sltype, id, def0, def1, def2, def3, def4, def5) \
 
80
        CqParameterTypedUniformArray<type,id, sltype>* p##name = new CqParameterTypedUniformArray<type,id, sltype>(#name,8); \
 
81
        p##name->pValue()[0] = ( def0 ); \
 
82
        p##name->pValue()[1] = ( def1 ); \
 
83
        p##name->pValue()[2] = ( def2 ); \
 
84
        p##name->pValue()[3] = ( def3 ); \
 
85
        p##name->pValue()[4] = ( def4 ); \
 
86
        p##name->pValue()[5] = ( def5 ); \
 
87
        pdefattrs->AddParameter(p##name);
 
88
 
 
89
 
 
90
/** A macro to take care of adding a system attribute given a name.
 
91
 *  Creates a new CqParameter derived class, initialises it to the given default value and 
 
92
 *  adds it to the default attributes member.
 
93
 */
 
94
#define ADD_SYSTEM_ATTR8(name, type, sltype, id, def0, def1, def2, def3, def4, def5, def6, def7) \
 
95
        CqParameterTypedUniformArray<type,id, sltype>* p##name = new CqParameterTypedUniformArray<type,id, sltype>(#name,8); \
 
96
        p##name->pValue()[0] = ( def0 ); \
 
97
        p##name->pValue()[1] = ( def1 ); \
 
98
        p##name->pValue()[2] = ( def2 ); \
 
99
        p##name->pValue()[3] = ( def3 ); \
 
100
        p##name->pValue()[4] = ( def4 ); \
 
101
        p##name->pValue()[5] = ( def5 ); \
 
102
        p##name->pValue()[6] = ( def6 ); \
 
103
        p##name->pValue()[7] = ( def7 ); \
 
104
        pdefattrs->AddParameter(p##name);
 
105
 
 
106
//---------------------------------------------------------------------
 
107
/** Constructor.
 
108
 */
 
109
 
 
110
CqAttributes::CqAttributes()
 
111
{
 
112
    Attribute_stack.push_front( this );
 
113
    m_StackIterator = Attribute_stack.begin();
 
114
 
 
115
    boost::shared_ptr<CqNamedParameterList> pdefattrs( new CqNamedParameterList( "System" ) );
 
116
 
 
117
    ADD_SYSTEM_ATTR( Color, CqColor, CqColor, type_color, CqColor( 1.0f, 1.0f, 1.0f ) );                // the current color attribute.
 
118
    ADD_SYSTEM_ATTR( Opacity, CqColor, CqColor, type_color, CqColor( 1.0f, 1.0f, 1.0f ) );      // the current opacity attribute.
 
119
    ADD_SYSTEM_ATTR8( TextureCoordinates, TqFloat, TqFloat, type_float, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f );       // an array of 2D vectors representing the coordinate space.
 
120
    ADD_SYSTEM_ATTR( ShadingRate, TqFloat, TqFloat, type_float, 1.0f );                                 // the current effective shading rate.
 
121
    ADD_SYSTEM_ATTR( ShadingRateSqrt, TqFloat, TqFloat, type_float, 1.0f );                                     // the current effective sqrt(shading rate).
 
122
    ADD_SYSTEM_ATTR( ShadingInterpolation, TqFloat, TqInt, type_integer, ShadingConstant );     // the current shading interpolation mode.
 
123
    ADD_SYSTEM_ATTR( Matte, TqInt, TqFloat, type_integer, 0 );                          // the current state of the matte flag.
 
124
    ADD_SYSTEM_ATTR4( DetailRange, TqFloat, TqFloat, type_float, 0.0f, 0.0f, FLT_MAX, FLT_MAX );        // the detail range minimum visible distance.
 
125
    ADD_SYSTEM_ATTR2( Basis, CqMatrix, CqMatrix, type_matrix, RiBezierBasis, RiBezierBasis );   // the basis matrix for the u direction.
 
126
    ADD_SYSTEM_ATTR2( BasisStep, TqInt, TqFloat, type_integer, 3, 3 );  // the steps to advance the evaluation window in the u direction.
 
127
    ADD_SYSTEM_ATTR( Orientation, TqInt, TqFloat, type_integer, 0 );    // the orientation associated primitives are described in.
 
128
    ADD_SYSTEM_ATTR( Sides, TqInt, TqFloat, type_integer, 2 );          // the number of visible sides associated primitives have.
 
129
    ADD_SYSTEM_ATTR( LevelOfDetailRulerSize, TqFloat, TqFloat, type_float, FLT_MAX );           // current LOD ruler size
 
130
    ADD_SYSTEM_ATTR2( LevelOfDetailBounds, TqFloat, TqFloat, type_float, 0.0f, 1.0f );  // relative importance bounds for this LOD representation
 
131
 
 
132
    AddAttribute( pdefattrs );
 
133
}
 
134
 
 
135
 
 
136
//---------------------------------------------------------------------
 
137
/** Copy constructor.
 
138
 */
 
139
 
 
140
CqAttributes::CqAttributes( const CqAttributes& From )
 
141
{
 
142
    *this = From;
 
143
 
 
144
    // Register ourself with the global attribute stack.
 
145
    Attribute_stack.push_front( this );
 
146
    m_StackIterator = Attribute_stack.begin();
 
147
}
 
148
 
 
149
 
 
150
//---------------------------------------------------------------------
 
151
/** Destructor.
 
152
 */
 
153
 
 
154
CqAttributes::~CqAttributes()
 
155
{
 
156
    assert( RefCount() == 0 );
 
157
 
 
158
    // Unreference the system attributes.
 
159
    //  TqInt i = m_aAttributes.size();
 
160
    //  while ( i-- > 0 )
 
161
    //  {
 
162
    //          m_aAttributes[ i ] ->Release();
 
163
    //          m_aAttributes[ i ] = 0;
 
164
    //  }
 
165
 
 
166
    // Remove ourself from the stack
 
167
    Attribute_stack.erase( m_StackIterator );
 
168
}
 
169
 
 
170
//---------------------------------------------------------------------
 
171
/** Copy function.
 
172
 */
 
173
 
 
174
CqAttributes& CqAttributes::operator=( const CqAttributes& From )
 
175
{
 
176
    // Copy the system attributes.
 
177
    //  m_aAttributes.resize( From.m_aAttributes.size() );
 
178
    //  TqInt i = From.m_aAttributes.size();
 
179
    //  while ( i-- > 0 )
 
180
    //  {
 
181
    //          m_aAttributes[ i ] = From.m_aAttributes[ i ];
 
182
    //          m_aAttributes[ i ] ->AddRef();
 
183
    //  }
 
184
    m_aAttributes = From.m_aAttributes;
 
185
 
 
186
    m_apLightsources = From.m_apLightsources;
 
187
 
 
188
    m_pshadDisplacement = From.m_pshadDisplacement;
 
189
    m_pshadAreaLightSource = From.m_pshadAreaLightSource;
 
190
    m_pshadSurface = From.m_pshadSurface;
 
191
    m_pshadAtmosphere = From.m_pshadAtmosphere;
 
192
    m_pshadInteriorVolume = From.m_pshadInteriorVolume;
 
193
    m_pshadExteriorVolume = From.m_pshadExteriorVolume;
 
194
 
 
195
    return ( *this );
 
196
}
 
197
 
 
198
 
 
199
//---------------------------------------------------------------------
 
200
/** Get a system attribute parameter.
 
201
 * \param strName The name of the attribute.
 
202
 * \param strParam The name of the paramter on the attribute.
 
203
 * \return CqParameter pointer or 0 if not found.
 
204
 */
 
205
 
 
206
const CqParameter* CqAttributes::pParameter( const char* strName, const char* strParam ) const
 
207
{
 
208
    const CqNamedParameterList* pList = pAttribute( strName ).get();
 
209
    if ( pList )
 
210
    {
 
211
        return ( pList->pParameter( strParam ) );
 
212
    }
 
213
    return ( 0 );
 
214
}
 
215
 
 
216
 
 
217
//---------------------------------------------------------------------
 
218
/** Get a system attribute parameter.
 
219
 * \param strName The name of the attribute.
 
220
 * \param strParam The name of the paramter on the attribute.
 
221
 * \return CqParameter pointer or 0 if not found.
 
222
 */
 
223
 
 
224
CqParameter* CqAttributes::pParameterWrite( const char* strName, const char* strParam )
 
225
{
 
226
    CqNamedParameterList * pList = pAttributeWrite( strName ).get();
 
227
    if ( pList )
 
228
    {
 
229
        return (pList->pParameter( strParam ) );
 
230
    }
 
231
    return ( 0 );
 
232
}
 
233
 
 
234
 
 
235
//---------------------------------------------------------------------
 
236
/** Get a float system attribute parameter.
 
237
 * \param strName The name of the attribute.
 
238
 * \param strParam The name of the paramter on the attribute.
 
239
 * \return Float pointer 0 if not found.
 
240
 */
 
241
 
 
242
TqFloat* CqAttributes::GetFloatAttributeWrite( const char* strName, const char* strParam )
 
243
{
 
244
    CqParameter * pParam = pParameterWrite( strName, strParam );
 
245
    if ( pParam != 0 )
 
246
        return ( static_cast<CqParameterTyped<TqFloat, TqFloat>*>( pParam ) ->pValue() );
 
247
    else
 
248
        return ( 0 );
 
249
}
 
250
 
 
251
 
 
252
//---------------------------------------------------------------------
 
253
/** Get an integer system attribute parameter.
 
254
 * \param strName The name of the attribute.
 
255
 * \param strParam The name of the paramter on the attribute.
 
256
 * \return Integer pointer 0 if not found.
 
257
 */
 
258
 
 
259
TqInt* CqAttributes::GetIntegerAttributeWrite( const char* strName, const char* strParam )
 
260
{
 
261
    CqParameter * pParam = pParameterWrite( strName, strParam );
 
262
    if ( pParam != 0 )
 
263
        return ( static_cast<CqParameterTyped<TqInt, TqFloat>*>( pParam ) ->pValue() );
 
264
    else
 
265
        return ( 0 );
 
266
}
 
267
 
 
268
 
 
269
//---------------------------------------------------------------------
 
270
/** Get a string system attribute parameter.
 
271
 * \param strName The name of the attribute.
 
272
 * \param strParam The name of the paramter on the attribute.
 
273
 * \return CqString pointer 0 if not found.
 
274
 */
 
275
 
 
276
CqString* CqAttributes::GetStringAttributeWrite( const char* strName, const char* strParam )
 
277
{
 
278
    CqParameter * pParam = pParameterWrite( strName, strParam );
 
279
    if ( pParam != 0 )
 
280
        return ( static_cast<CqParameterTyped<CqString, CqString>*>( pParam ) ->pValue() );
 
281
    else
 
282
        return ( 0 );
 
283
}
 
284
 
 
285
 
 
286
//---------------------------------------------------------------------
 
287
/** Get a point system attribute parameter.
 
288
 * \param strName The name of the attribute.
 
289
 * \param strParam The name of the paramter on the attribute.
 
290
 * \return CqVetor3D pointer 0 if not found.
 
291
 */
 
292
 
 
293
CqVector3D* CqAttributes::GetPointAttributeWrite( const char* strName, const char* strParam )
 
294
{
 
295
    CqParameter * pParam = pParameterWrite( strName, strParam );
 
296
    if ( pParam != 0 )
 
297
        return ( static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( pParam ) ->pValue() );
 
298
    else
 
299
        return ( 0 );
 
300
}
 
301
 
 
302
 
 
303
//---------------------------------------------------------------------
 
304
/** Get a vector system attribute parameter.
 
305
 * \param strName The name of the attribute.
 
306
 * \param strParam The name of the paramter on the attribute.
 
307
 * \return CqVetor3D pointer 0 if not found.
 
308
 */
 
309
 
 
310
CqVector3D* CqAttributes::GetVectorAttributeWrite( const char* strName, const char* strParam )
 
311
{
 
312
    return ( GetPointAttributeWrite( strName, strParam ) );
 
313
}
 
314
 
 
315
 
 
316
//---------------------------------------------------------------------
 
317
/** Get a normal system attribute parameter.
 
318
 * \param strName The name of the attribute.
 
319
 * \param strParam The name of the paramter on the attribute.
 
320
 * \return CqVetor3D pointer 0 if not found.
 
321
 */
 
322
 
 
323
CqVector3D* CqAttributes::GetNormalAttributeWrite( const char* strName, const char* strParam )
 
324
{
 
325
    return ( GetPointAttributeWrite( strName, strParam ) );
 
326
}
 
327
 
 
328
 
 
329
//---------------------------------------------------------------------
 
330
/** Get a color system attribute parameter.
 
331
 * \param strName The name of the attribute.
 
332
 * \param strParam The name of the paramter on the attribute.
 
333
 * \return CqColor pointer 0 if not found.
 
334
 */
 
335
 
 
336
CqColor* CqAttributes::GetColorAttributeWrite( const char* strName, const char* strParam )
 
337
{
 
338
    CqParameter * pParam = pParameterWrite( strName, strParam );
 
339
    if ( pParam != 0 )
 
340
        return ( static_cast<CqParameterTyped<CqColor, CqColor>*>( pParam ) ->pValue() );
 
341
    else
 
342
        return ( 0 );
 
343
}
 
344
 
 
345
 
 
346
//---------------------------------------------------------------------
 
347
/** Get a matrix system attribute parameter.
 
348
 * \param strName The name of the attribute.
 
349
 * \param strParam The name of the paramter on the attribute.
 
350
 * \return CqMatrix pointer 0 if not found.
 
351
 */
 
352
 
 
353
CqMatrix* CqAttributes::GetMatrixAttributeWrite( const char* strName, const char* strParam )
 
354
{
 
355
    CqParameter * pParam = pParameterWrite( strName, strParam );
 
356
    if ( pParam != 0 )
 
357
        return ( static_cast<CqParameterTyped<CqMatrix, CqMatrix>*>( pParam ) ->pValue() );
 
358
    else
 
359
        return ( 0 );
 
360
}
 
361
 
 
362
 
 
363
//---------------------------------------------------------------------
 
364
/** Get a float system attribute parameter.
 
365
 * \param strName The name of the attribute.
 
366
 * \param strParam The name of the paramter on the attribute.
 
367
 * \return Float pointer 0 if not found.
 
368
 */
 
369
 
 
370
const TqFloat* CqAttributes::GetFloatAttribute( const char* strName, const char* strParam ) const
 
371
{
 
372
    const CqParameter * pParam = pParameter( strName, strParam );
 
373
    if ( pParam != 0 )
 
374
        return ( static_cast<const CqParameterTyped<TqFloat, TqFloat>*>( pParam ) ->pValue() );
 
375
    else
 
376
        return ( 0 );
 
377
}
 
378
 
 
379
 
 
380
//---------------------------------------------------------------------
 
381
/** Get an integer system attribute parameter.
 
382
 * \param strName The name of the attribute.
 
383
 * \param strParam The name of the paramter on the attribute.
 
384
 * \return Integer pointer 0 if not found.
 
385
 */
 
386
 
 
387
const TqInt* CqAttributes::GetIntegerAttribute( const char* strName, const char* strParam ) const
 
388
{
 
389
    const CqParameter * pParam = pParameter( strName, strParam );
 
390
    if ( pParam != 0 )
 
391
        return ( static_cast<const CqParameterTyped<TqInt, TqFloat>*>( pParam ) ->pValue() );
 
392
    else
 
393
        return ( 0 );
 
394
}
 
395
 
 
396
 
 
397
//---------------------------------------------------------------------
 
398
/** Get a string system attribute parameter.
 
399
 * \param strName The name of the attribute.
 
400
 * \param strParam The name of the paramter on the attribute.
 
401
 * \return CqString pointer 0 if not found.
 
402
 */
 
403
 
 
404
const CqString* CqAttributes::GetStringAttribute( const char* strName, const char* strParam ) const
 
405
{
 
406
    const CqParameter * pParam = pParameter( strName, strParam );
 
407
    if ( pParam != 0 )
 
408
        return ( static_cast<const CqParameterTyped<CqString, CqString>*>( pParam ) ->pValue() );
 
409
    else
 
410
        return ( 0 );
 
411
}
 
412
 
 
413
 
 
414
//---------------------------------------------------------------------
 
415
/** Get a point system attribute parameter.
 
416
 * \param strName The name of the attribute.
 
417
 * \param strParam The name of the paramter on the attribute.
 
418
 * \return CqVector3D pointer 0 if not found.
 
419
 */
 
420
 
 
421
const CqVector3D* CqAttributes::GetPointAttribute( const char* strName, const char* strParam ) const
 
422
{
 
423
    const CqParameter * pParam = pParameter( strName, strParam );
 
424
    if ( pParam != 0 )
 
425
        return ( static_cast<const CqParameterTyped<CqVector3D, CqVector3D>*>( pParam ) ->pValue() );
 
426
    else
 
427
        return ( 0 );
 
428
}
 
429
 
 
430
 
 
431
//---------------------------------------------------------------------
 
432
/** Get a vector system attribute parameter.
 
433
 * \param strName The name of the attribute.
 
434
 * \param strParam The name of the paramter on the attribute.
 
435
 * \return CqVector3D pointer 0 if not found.
 
436
 */
 
437
 
 
438
const CqVector3D* CqAttributes::GetVectorAttribute( const char* strName, const char* strParam ) const
 
439
{
 
440
    return ( GetPointAttribute( strName, strParam ) );
 
441
}
 
442
 
 
443
 
 
444
//---------------------------------------------------------------------
 
445
/** Get a normal system attribute parameter.
 
446
 * \param strName The name of the attribute.
 
447
 * \param strParam The name of the paramter on the attribute.
 
448
 * \return CqVector3D pointer 0 if not found.
 
449
 */
 
450
 
 
451
const CqVector3D* CqAttributes::GetNormalAttribute( const char* strName, const char* strParam ) const
 
452
{
 
453
    return ( GetPointAttribute( strName, strParam ) );
 
454
}
 
455
 
 
456
 
 
457
//---------------------------------------------------------------------
 
458
/** Get a color system attribute parameter.
 
459
 * \param strName The name of the attribute.
 
460
 * \param strParam The name of the paramter on the attribute.
 
461
 * \return CqColor pointer 0 if not found.
 
462
 */
 
463
 
 
464
const CqColor* CqAttributes::GetColorAttribute( const char* strName, const char* strParam ) const
 
465
{
 
466
    const CqParameter * pParam = pParameter( strName, strParam );
 
467
    if ( pParam != 0 )
 
468
        return ( static_cast<const CqParameterTyped<CqColor, CqColor>*>( pParam ) ->pValue() );
 
469
    else
 
470
        return ( 0 );
 
471
}
 
472
 
 
473
 
 
474
//---------------------------------------------------------------------
 
475
/** Get a matrix system attribute parameter.
 
476
 * \param strName The name of the attribute.
 
477
 * \param strParam The name of the paramter on the attribute.
 
478
 * \return CqMatrix pointer 0 if not found.
 
479
 */
 
480
 
 
481
const CqMatrix* CqAttributes::GetMatrixAttribute( const char* strName, const char* strParam ) const
 
482
{
 
483
    const CqParameter * pParam = pParameter( strName, strParam );
 
484
    if ( pParam != 0 )
 
485
        return ( static_cast<const CqParameterTyped<CqMatrix, CqMatrix>*>( pParam ) ->pValue() );
 
486
    else
 
487
        return ( 0 );
 
488
}
 
489
 
 
490
 
 
491
IqLightsource*  CqAttributes::pLight( TqInt index )
 
492
{
 
493
    return ( m_apLightsources[ index ].get() );
 
494
}
 
495
 
 
496
//---------------------------------------------------------------------
 
497
 
 
498
END_NAMESPACE( Aqsis )
 
499
 
 
500