~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to render/attributes.h

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

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 Declares the CqAttributes class for handling RenderMan attributes.
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
*/
 
25
 
 
26
//? Is attributes.h included already?
 
27
#ifndef ATTRIBUTES_H_INCLUDED 
 
28
//{
 
29
#define ATTRIBUTES_H_INCLUDED 1
 
30
 
 
31
#include        <vector>
 
32
#include        <list>
 
33
#include        <map>
 
34
 
 
35
#include        "aqsis.h"
 
36
 
 
37
#include        "color.h"
 
38
#include        "ri.h"
 
39
#include        "matrix.h"
 
40
#include        "options.h"
 
41
#include        "bound.h"
 
42
#include        "spline.h"
 
43
#include        "trimcurve.h"
 
44
#include        "iattributes.h"
 
45
 
 
46
START_NAMESPACE( Aqsis )
 
47
struct IqShader;
 
48
class   CqLightsource;
 
49
 
 
50
 
 
51
//----------------------------------------------------------------------
 
52
/**
 
53
        Container class for the attributes definitions of the graphics state.
 
54
*/
 
55
 
 
56
 
 
57
class CqAttributes : public CqRefCount, public IqAttributes
 
58
{
 
59
public:
 
60
    CqAttributes();
 
61
    CqAttributes( const CqAttributes& From );
 
62
    virtual     ~CqAttributes();
 
63
 
 
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.
 
67
     */
 
68
    CqAttributes* Write()
 
69
    {
 
70
        // We are about to write to this attribute,so clone if references exist.
 
71
        if ( RefCount() > 1 )
 
72
        {
 
73
            CqAttributes * pWrite = Clone();
 
74
            ADDREF( pWrite );
 
75
            RELEASEREF( this );
 
76
            return ( pWrite );
 
77
        }
 
78
        else
 
79
            return ( this );
 
80
    }
 
81
 
 
82
    CqAttributes& operator=( const CqAttributes& From );
 
83
 
 
84
    /** Add a new user defined attribute.
 
85
     * \param pAttribute a pointer to the new user defined attribute.
 
86
     */
 
87
    void        AddAttribute( const boost::shared_ptr<CqNamedParameterList>& pAttribute )
 
88
    {
 
89
        m_aAttributes.Add( pAttribute );
 
90
    }
 
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.
 
94
     */
 
95
    const       boost::shared_ptr<CqNamedParameterList> pAttribute( const char* strName ) const
 
96
    {
 
97
        return ( m_aAttributes.Find( strName ) );
 
98
    }
 
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.
 
104
     */
 
105
    boost::shared_ptr<CqNamedParameterList> pAttributeWrite( const char* strName )
 
106
    {
 
107
        boost::shared_ptr<CqNamedParameterList> pAttr = m_aAttributes.Find( strName );
 
108
        if ( pAttr )
 
109
        {
 
110
            if ( pAttr.unique() )
 
111
                return ( pAttr );
 
112
            else
 
113
            {
 
114
                boost::shared_ptr<CqNamedParameterList> pNew( new CqNamedParameterList( *pAttr ) );
 
115
                m_aAttributes.Remove( pAttr );
 
116
                m_aAttributes.Add( pNew );
 
117
                return ( pNew );
 
118
            }
 
119
        }
 
120
        boost::shared_ptr<CqNamedParameterList> pNew( new CqNamedParameterList( strName ) );
 
121
        m_aAttributes.Add( pNew );
 
122
        return ( pNew );
 
123
    }
 
124
 
 
125
    /** Add a lightsource to the current available list.
 
126
     * \param pL a pointer to the new lightsource.
 
127
     */
 
128
    void        AddLightsource( CqLightsource* pL )
 
129
    {
 
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++ )
 
133
        {
 
134
            if ( ( *i ) == pL )
 
135
                return ;
 
136
        }
 
137
        m_apLightsources.push_back( pL );
 
138
    }
 
139
    /** Remove a lightsource from the current available list.
 
140
     * \param pL a pointer to the lightsource to remove.
 
141
     */
 
142
    void        RemoveLightsource( CqLightsource* pL )
 
143
    {
 
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++ )
 
147
        {
 
148
            if ( *i == pL )
 
149
            {
 
150
                m_apLightsources.erase( i );
 
151
                return ;
 
152
            }
 
153
        }
 
154
    }
 
155
    /** Get a reference to the lightsource list.
 
156
     * \return a reference to the vector of lightsource pointers.
 
157
     */
 
158
    virtual const       std::vector<CqLightsource*>&    apLights() const
 
159
    {
 
160
        return ( m_apLightsources );
 
161
    }
 
162
 
 
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).
 
165
     */
 
166
    void        FlipeOrientation( TqFloat time )
 
167
    {
 
168
        TqBool co = GetIntegerAttribute( "System", "Orientation" ) [ 0 ] == 0;
 
169
        GetIntegerAttributeWrite( "System", "Orientation" ) [ 0 ] = ( co ) ? 1 : 0;
 
170
    }
 
171
 
 
172
    virtual IqShader*   pshadDisplacement( TqFloat time ) const
 
173
    {
 
174
        return ( m_pshadDisplacement );
 
175
    }
 
176
    virtual void        SetpshadDisplacement( IqShader* pshadDisplacement, TqFloat time )
 
177
    {
 
178
        m_pshadDisplacement = pshadDisplacement;
 
179
    }
 
180
    virtual IqShader*   pshadAreaLightSource( TqFloat time ) const
 
181
    {
 
182
        return ( m_pshadAreaLightSource );
 
183
    }
 
184
    virtual void        SetpshadAreaLightSource( IqShader* pshadAreaLightSource, TqFloat time )
 
185
    {
 
186
        m_pshadAreaLightSource = pshadAreaLightSource;
 
187
    }
 
188
    virtual IqShader*   pshadSurface( TqFloat time ) const
 
189
    {
 
190
        return ( m_pshadSurface );
 
191
    }
 
192
    virtual void        SetpshadSurface( IqShader* pshadSurface, TqFloat time )
 
193
    {
 
194
        m_pshadSurface = pshadSurface;
 
195
    }
 
196
    virtual IqShader*   pshadAtmosphere( TqFloat time ) const
 
197
    {
 
198
        return ( m_pshadAtmosphere );
 
199
    }
 
200
    virtual void        SetpshadAtmosphere( IqShader* pshadAtmosphere, TqFloat time )
 
201
    {
 
202
        m_pshadAtmosphere = pshadAtmosphere;
 
203
    }
 
204
    virtual IqShader*   pshadExteriorVolume( TqFloat time ) const
 
205
    {
 
206
        return ( m_pshadExteriorVolume );
 
207
    }
 
208
    virtual void        SetpshadExteriorVolume( IqShader* pshadExteriorVolume, TqFloat time )
 
209
    {
 
210
        m_pshadExteriorVolume = pshadExteriorVolume;
 
211
    }
 
212
    virtual IqShader*   pshadAreaInteriorVolume( TqFloat time ) const
 
213
    {
 
214
        return ( m_pshadInteriorVolume );
 
215
    }
 
216
    virtual void        SetpshadInteriorVolume( IqShader* pshadInteriorVolume, TqFloat time )
 
217
    {
 
218
        m_pshadInteriorVolume = pshadInteriorVolume;
 
219
    }
 
220
 
 
221
    /** Get the array of trim curve loops.
 
222
     *  \return A pointer to the trim loops array object.
 
223
     */
 
224
    const CqTrimLoopArray& TrimLoops() const
 
225
    {
 
226
        return ( m_TrimLoops );
 
227
    }
 
228
    /** Get the array of trim curve loops.
 
229
     *  \return A pointer to the trim loops array object.
 
230
     */
 
231
    CqTrimLoopArray& TrimLoops()
 
232
    {
 
233
        return ( m_TrimLoops );
 
234
    }
 
235
 
 
236
    /** Clone the entire attribute state.
 
237
     * \return a pointer to the new attribute state.
 
238
     */
 
239
    CqAttributes*       Clone() const
 
240
    {
 
241
        return ( new CqAttributes( *this ) );
 
242
    }
 
243
 
 
244
    const       CqParameter* pParameter( const char* strName, const char* strParam ) const;
 
245
    CqParameter* pParameterWrite( const char* strName, const char* strParam );
 
246
 
 
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;
 
255
 
 
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 );
 
264
 
 
265
    virtual     TqInt   cLights() const
 
266
    {
 
267
        return ( apLights().size() );
 
268
    }
 
269
    virtual     IqLightsource*  pLight( TqInt index );
 
270
 
 
271
#ifndef _DEBUG
 
272
    virtual     void    Release()
 
273
    {
 
274
        CqRefCount::Release();
 
275
    }
 
276
    virtual     void    AddRef()
 
277
    {
 
278
        CqRefCount::AddRef();
 
279
    }
 
280
#else
 
281
    virtual void AddRef(const TqChar* file, TqInt line)
 
282
    {
 
283
        CqRefCount::AddRef(file, line);
 
284
    }
 
285
    virtual void Release(const TqChar* file, TqInt line)
 
286
    {
 
287
        CqRefCount::Release(file, line);
 
288
    }
 
289
    CqString className() const { return CqString("CqAttributes"); }
 
290
#endif
 
291
 
 
292
private:
 
293
#ifndef REQUIRED
 
294
    class CqHashTable
 
295
    {
 
296
    private:
 
297
        static const TqInt tableSize;
 
298
 
 
299
    public:
 
300
        CqHashTable()
 
301
        {
 
302
            m_aLists.resize( tableSize );
 
303
        }
 
304
        virtual ~CqHashTable()
 
305
        {
 
306
        }
 
307
 
 
308
        const boost::shared_ptr<CqNamedParameterList>   Find( const TqChar* pname ) const
 
309
        {
 
310
            TqUlong hash = CqParameter::hash(pname);
 
311
            TqInt i = _hash( hash);
 
312
 
 
313
            if ( m_aLists[ i ].empty() )
 
314
            {
 
315
                boost::shared_ptr<CqNamedParameterList> retval;
 
316
                return ( retval );
 
317
                // return ( boost::shared_ptr<CqNamedParameterList>() );
 
318
            }
 
319
 
 
320
 
 
321
            std::list<boost::shared_ptr<CqNamedParameterList> >::const_iterator iEntry = m_aLists[ i ].begin();
 
322
            if ( iEntry == m_aLists[ i ].end() )
 
323
                return ( *iEntry );
 
324
            else
 
325
            {
 
326
                while ( iEntry != m_aLists[ i ].end() )
 
327
                {
 
328
                    if ( ( *iEntry ) ->hash() == hash )
 
329
                        return ( *iEntry );
 
330
                    ++iEntry;
 
331
                }
 
332
            }
 
333
 
 
334
            boost::shared_ptr<CqNamedParameterList> retval;
 
335
            return ( retval );
 
336
            // return ( boost::shared_ptr<CqNamedParameterList>() );
 
337
        }
 
338
 
 
339
        boost::shared_ptr<CqNamedParameterList> Find( const TqChar* pname )
 
340
        {
 
341
            TqUlong hash = CqParameter::hash(pname);
 
342
            TqInt i = _hash( hash);
 
343
 
 
344
            if ( m_aLists[ i ].empty() )
 
345
            {
 
346
                boost::shared_ptr<CqNamedParameterList> retval;
 
347
                return ( retval );
 
348
                // return ( boost::shared_ptr<CqNamedParameterList>() );
 
349
            }
 
350
 
 
351
 
 
352
            std::list<boost::shared_ptr<CqNamedParameterList> >::const_iterator iEntry = m_aLists[ i ].begin();
 
353
            if ( iEntry == m_aLists[ i ].end() )
 
354
                return ( *iEntry );
 
355
            else
 
356
            {
 
357
                while ( iEntry != m_aLists[ i ].end() )
 
358
                {
 
359
                    if ( ( *iEntry ) ->hash() == hash )
 
360
                        return ( *iEntry );
 
361
                    ++iEntry;
 
362
                }
 
363
            }
 
364
 
 
365
            boost::shared_ptr<CqNamedParameterList> retval;
 
366
            return ( retval );
 
367
            // return ( boost::shared_ptr<CqNamedParameterList>() );
 
368
        }
 
369
 
 
370
        void Add( const boost::shared_ptr<CqNamedParameterList>& pOption )
 
371
        {
 
372
            TqUlong hash = CqParameter::hash(pOption->strName().c_str());
 
373
            TqInt i = _hash( hash);
 
374
            m_aLists[ i ].push_back( pOption );
 
375
        }
 
376
 
 
377
        void Remove( const boost::shared_ptr<CqNamedParameterList>& pOption )
 
378
        {
 
379
            TqUlong hash = CqParameter::hash(pOption->strName().c_str());
 
380
            TqInt i = _hash( hash);
 
381
 
 
382
            std::list<boost::shared_ptr<CqNamedParameterList> >::iterator iEntry = m_aLists[ i ].begin();
 
383
            while ( iEntry != m_aLists[ i ].end() )
 
384
            {
 
385
                if ( ( *iEntry ) == pOption )
 
386
                {
 
387
                    m_aLists[ i ].remove( *iEntry );
 
388
                    return ;
 
389
                }
 
390
                iEntry++;
 
391
            }
 
392
        }
 
393
 
 
394
        CqHashTable& operator=( const CqHashTable& From )
 
395
        {
 
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++ )
 
398
            {
 
399
                std::list<boost::shared_ptr<CqNamedParameterList> >::const_iterator i2;
 
400
                for ( i2 = ( *i ).begin(); i2 != ( *i ).end(); i2++ )
 
401
                    Add( *i2 );
 
402
            }
 
403
            return ( *this );
 
404
        }
 
405
 
 
406
    private:
 
407
        TqInt _hash( TqUlong h ) const
 
408
        {
 
409
            return (h % tableSize);
 
410
        }
 
411
        TqInt _hash( const TqChar* string ) const
 
412
        {
 
413
            assert ( string != 0 && string[ 0 ] != 0 );
 
414
 
 
415
            TqUlong h = CqParameter::hash( string );
 
416
            return ( (TqUlong) h % tableSize ); // remainder
 
417
        }
 
418
 
 
419
        std::vector<std::list<boost::shared_ptr<CqNamedParameterList> > >       m_aLists;
 
420
    };
 
421
#else
 
422
    class CqHashTable
 
423
    {
 
424
    private:
 
425
        static const TqInt tableSize;
 
426
 
 
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;
 
431
 
 
432
    public:
 
433
        CqHashTable()
 
434
        {}
 
435
        virtual ~CqHashTable()
 
436
        {
 
437
        }
 
438
 
 
439
        const boost::shared_ptr<CqNamedParameterList>   Find( const TqChar* pname ) const
 
440
        {
 
441
            std::string strName( pname );
 
442
            plist_const_iterator it = m_ParameterLists.find( strName );
 
443
            if( it != m_ParameterLists.end() )
 
444
                return ( it->second );
 
445
            else
 
446
                return ( boost::shared_ptr<CqNamedParameterList>() );
 
447
        }
 
448
 
 
449
        boost::shared_ptr<CqNamedParameterList> Find( const TqChar* pname )
 
450
        {
 
451
            std::string strName( pname );
 
452
            plist_iterator it = m_ParameterLists.find( strName );
 
453
            if( it != m_ParameterLists.end() )
 
454
                return ( it->second );
 
455
            else
 
456
                return ( boost::shared_ptr<CqNamedParameterList>() );
 
457
        }
 
458
 
 
459
        void Add( const boost::shared_ptr<CqNamedParameterList>& pOption )
 
460
        {
 
461
            m_ParameterLists.insert(value_type(pOption->strName(), pOption) );
 
462
        }
 
463
 
 
464
        void Remove( const boost::shared_ptr<CqNamedParameterList>& pOption )
 
465
        {
 
466
            plist_iterator it = m_ParameterLists.find( pOption->strName() );
 
467
            if( it != m_ParameterLists.end() )
 
468
            {
 
469
                m_ParameterLists.erase(it);
 
470
            }
 
471
        }
 
472
 
 
473
        CqHashTable& operator=( const CqHashTable& From )
 
474
        {
 
475
            plist_const_iterator it = From.m_ParameterLists.begin();
 
476
            while( it != From.m_ParameterLists.end() )
 
477
            {
 
478
                Add( (*it).second );
 
479
                ++it;
 
480
            }
 
481
            return ( *this );
 
482
        }
 
483
 
 
484
    private:
 
485
        plist_type      m_ParameterLists;
 
486
    };
 
487
#endif
 
488
 
 
489
    CqHashTable m_aAttributes;                                          ///< a vector of user defined attribute pointers.
 
490
 
 
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.
 
497
 
 
498
    CqTrimLoopArray m_TrimLoops;                                        ///< the array of closed trimcurve loops.
 
499
    std::vector<CqLightsource*> m_apLightsources;       ///< a vector of currently available lightsources.
 
500
 
 
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.
 
502
}
 
503
;
 
504
 
 
505
/// Global attribute stack.
 
506
extern std::list<CqAttributes*> Attribute_stack;
 
507
 
 
508
 
 
509
//-----------------------------------------------------------------------
 
510
 
 
511
END_NAMESPACE( Aqsis )
 
512
 
 
513
//}  // End of #ifdef ATTRIBUTES_H_INCLUDED
 
514
#endif