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

« back to all changes in this revision

Viewing changes to render/surface.cpp

  • 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 Implements the base GPrim handling classes.
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
*/
 
25
 
 
26
#include        "aqsis.h"
 
27
#include        "renderer.h"
 
28
#include        "micropolygon.h"
 
29
#include        "surface.h"
 
30
#include        "vector2d.h"
 
31
#include        "imagebuffer.h"
 
32
 
 
33
START_NAMESPACE( Aqsis )
 
34
 
 
35
TqFloat CqBasicSurface::m_fGridSize = sqrt(256.0);
 
36
 
 
37
//---------------------------------------------------------------------
 
38
/** Default constructor
 
39
 */
 
40
 
 
41
CqBasicSurface::CqBasicSurface() : m_fDiceable( TqTrue ), m_fDiscard( TqFalse ), m_EyeSplitCount( 0 ),
 
42
        m_pAttributes( 0 ), m_pTransform( 0 ), m_SplitDir( SplitDir_U )
 
43
{
 
44
    // Set a refernce with the current attributes.
 
45
    m_pAttributes = const_cast<CqAttributes*>( QGetRenderContext() ->pattrCurrent() );
 
46
    ADDREF( m_pAttributes );
 
47
 
 
48
    m_pTransform = const_cast<CqTransform*>( QGetRenderContext() ->ptransCurrent() );
 
49
    ADDREF( m_pTransform );
 
50
 
 
51
    m_CachedBound = TqFalse;
 
52
 
 
53
    // If the current context is a solid node, and is a 'primitive', attatch this surface to the node.
 
54
    if ( QGetRenderContext() ->pconCurrent() ->isSolid() )
 
55
    {
 
56
        CqModeBlock * pSolid = QGetRenderContext() ->pconCurrent().get();
 
57
        if ( pSolid->pCSGNode() ->NodeType() == CqCSGTreeNode::CSGNodeType_Primitive )
 
58
        {
 
59
            m_pCSGNode = pSolid->pCSGNode();
 
60
        }
 
61
        else
 
62
        {
 
63
            CqString objname( "unnamed" );
 
64
            const CqString* pattrName = m_pAttributes->GetStringAttribute( "identifier", "name" );
 
65
            if ( pattrName != 0 ) objname = pattrName[ 0 ];
 
66
            std::cerr << warning << "Primitive \"" << objname.c_str() << "\" defined when not in 'Primitive' solid block" << std::endl;
 
67
        }
 
68
    }
 
69
 
 
70
    STATS_INC( GPR_allocated );
 
71
    STATS_INC( GPR_current );
 
72
    TqInt cGprim = STATS_GETI( GPR_current );
 
73
    TqInt cPeak = STATS_GETI( GPR_peak );
 
74
    STATS_SETI( GPR_peak, cGprim > cPeak ? cGprim : cPeak );
 
75
}
 
76
 
 
77
 
 
78
//---------------------------------------------------------------------
 
79
/** Copy constructor
 
80
 */
 
81
 
 
82
CqBasicSurface::CqBasicSurface( const CqBasicSurface& From ) : m_fDiceable( TqTrue ), m_SplitDir( SplitDir_U )
 
83
{
 
84
    *this = From;
 
85
 
 
86
    // Set a reference with the donors attributes.
 
87
    m_pAttributes = From.m_pAttributes;
 
88
    ADDREF( m_pAttributes );
 
89
 
 
90
    m_pTransform = From.m_pTransform;
 
91
    ADDREF( m_pTransform );
 
92
 
 
93
    m_CachedBound = From.m_CachedBound;
 
94
    m_Bound = From.m_Bound;
 
95
 
 
96
    STATS_INC( GPR_allocated );
 
97
    STATS_INC( GPR_current );
 
98
    TqInt cGprim = STATS_GETI( GPR_current );
 
99
    TqInt cPeak = STATS_GETI( GPR_peak );
 
100
    STATS_SETI( GPR_peak, cGprim > cPeak ? cGprim : cPeak );
 
101
}
 
102
 
 
103
 
 
104
//---------------------------------------------------------------------
 
105
/** Assignement operator
 
106
 */
 
107
 
 
108
CqBasicSurface& CqBasicSurface::operator=( const CqBasicSurface& From )
 
109
{
 
110
    m_fDiceable = From.m_fDiceable;
 
111
    m_EyeSplitCount = From.m_EyeSplitCount;
 
112
    m_fDiscard = From.m_fDiscard;
 
113
 
 
114
    SetSurfaceParameters( From );
 
115
 
 
116
    return ( *this );
 
117
}
 
118
 
 
119
 
 
120
//---------------------------------------------------------------------
 
121
/** Copy the local surface parameters from the donor surface.
 
122
 */
 
123
 
 
124
void CqBasicSurface::SetSurfaceParameters( const CqBasicSurface& From )
 
125
{
 
126
    // If we already have attributes, unreference them now as we don't need them anymore.
 
127
    if ( m_pAttributes ) RELEASEREF( m_pAttributes );
 
128
    if ( m_pTransform ) RELEASEREF( m_pTransform );
 
129
 
 
130
    // Now store and reference our new attributes.
 
131
    m_pAttributes = From.m_pAttributes;
 
132
    ADDREF( m_pAttributes );
 
133
 
 
134
    m_pTransform = From.m_pTransform;
 
135
    ADDREF( m_pTransform );
 
136
 
 
137
    m_pCSGNode = From.m_pCSGNode;
 
138
}
 
139
 
 
140
 
 
141
//---------------------------------------------------------------------
 
142
/** Return the name of this primitive surface if specified as a "identifier" "name" attribute,
 
143
 * otherwise return "not named"
 
144
 */
 
145
 
 
146
CqString CqBasicSurface::strName() const
 
147
{
 
148
    const CqString * pattrLightName = pAttributes() ->GetStringAttribute( "identifier", "name" );
 
149
    CqString strName( "not named" );
 
150
    if ( pattrLightName != 0 ) strName = pattrLightName[ 0 ];
 
151
 
 
152
    return ( strName );
 
153
}
 
154
 
 
155
 
 
156
//---------------------------------------------------------------------
 
157
/** Work out which standard shader variables this surface requires by looking at the shaders.
 
158
 */
 
159
 
 
160
TqInt CqBasicSurface::Uses() const
 
161
{
 
162
    TqInt Uses = gDefUses | QGetRenderContext()->pDDmanager()->Uses();
 
163
    IqShader* pshadSurface = pAttributes() ->pshadSurface(QGetRenderContextI()->Time());
 
164
    IqShader* pshadDisplacement = pAttributes() ->pshadDisplacement(QGetRenderContextI()->Time());
 
165
    IqShader* pshadAtmosphere = pAttributes() ->pshadAtmosphere(QGetRenderContextI()->Time());
 
166
 
 
167
    if ( NULL == pshadSurface && NULL == pshadDisplacement && NULL == pshadAtmosphere )
 
168
        return ( 0 );
 
169
 
 
170
    if ( pshadSurface ) Uses |= pshadSurface->Uses();
 
171
    if ( pshadDisplacement ) Uses |= pshadDisplacement->Uses();
 
172
    if ( pshadAtmosphere ) Uses |= pshadAtmosphere->Uses();
 
173
 
 
174
    // Just a quick check, if it uses dPdu/dPdv must also use du/dv
 
175
    if ( USES( Uses, EnvVars_dPdu ) ) Uses |= ( 1 << EnvVars_du );
 
176
    if ( USES( Uses, EnvVars_dPdv ) ) Uses |= ( 1 << EnvVars_dv );
 
177
    // Just a quick check, if it uses du/dv must also use u/v
 
178
    if ( USES( Uses, EnvVars_du ) ) Uses |= ( 1 << EnvVars_u );
 
179
    if ( USES( Uses, EnvVars_dv ) ) Uses |= ( 1 << EnvVars_v );
 
180
 
 
181
    return ( Uses );
 
182
}
 
183
 
 
184
 
 
185
//---------------------------------------------------------------------
 
186
/** Adjust the bound of the quadric taking into account transformation motion blur.
 
187
 */
 
188
 
 
189
CqBound CqBasicSurface::AdjustBoundForTransformationMotion( const CqBound& B ) const
 
190
{
 
191
    CqBound Bm( B );
 
192
 
 
193
    if( pTransform()->cTimes() > 1 )
 
194
    {
 
195
        CqMatrix matCameraToObject0 = QGetRenderContext() ->matSpaceToSpace( "camera", "object", CqMatrix(), pTransform()->matObjectToWorld( pTransform()->Time( 0 ) ), pTransform()->Time( 0 ) );
 
196
        CqBound B0( B );
 
197
        B0.Transform( matCameraToObject0 );
 
198
 
 
199
        TqInt i;
 
200
        for( i = 1; i < pTransform()->cTimes(); i++ )
 
201
        {
 
202
            CqBound Btx( B0 );
 
203
            CqMatrix matObjectToCameraT = QGetRenderContext() ->matSpaceToSpace( "object", "camera", CqMatrix(), pTransform()->matObjectToWorld( pTransform()->Time( i ) ), pTransform()->Time( i ) );
 
204
            Btx.Transform( matObjectToCameraT );
 
205
            Bm.Encapsulate( Btx );
 
206
        }
 
207
    }
 
208
 
 
209
    return ( Bm );
 
210
}
 
211
 
 
212
 
 
213
//---------------------------------------------------------------------
 
214
/** Default constructor
 
215
 */
 
216
 
 
217
CqSurface::CqSurface() : CqBasicSurface()
 
218
{
 
219
    // Nullify the standard primitive variables index table.
 
220
    TqInt i;
 
221
    for ( i = 0; i < EnvVars_Last; i++ )
 
222
        m_aiStdPrimitiveVars[ i ] = -1;
 
223
}
 
224
 
 
225
//---------------------------------------------------------------------
 
226
/** Copy constructor
 
227
 */
 
228
 
 
229
CqSurface::CqSurface( const CqSurface& From )
 
230
{
 
231
    *this = From;
 
232
}
 
233
 
 
234
 
 
235
//---------------------------------------------------------------------
 
236
/** Assignement operator
 
237
 */
 
238
 
 
239
CqSurface& CqSurface::operator=( const CqSurface& From )
 
240
{
 
241
    CqBasicSurface::operator=( From );
 
242
    // Nullify the standard primitive variables index table.
 
243
    TqInt i;
 
244
    for ( i = 0; i < EnvVars_Last; i++ )
 
245
        m_aiStdPrimitiveVars[ i ] = -1;
 
246
    return ( *this );
 
247
}
 
248
 
 
249
//---------------------------------------------------------------------
 
250
/** Copy all the primitive variables from the donor to this.
 
251
 */
 
252
 
 
253
void CqSurface::ClonePrimitiveVariables( const CqSurface& From )
 
254
{
 
255
    // Clone any primitive variables.
 
256
    m_aUserParams.clear();
 
257
    std::vector<CqParameter*>::const_iterator iUP;
 
258
    std::vector<CqParameter*>::const_iterator end = From.m_aUserParams.end() ;
 
259
    for ( iUP = From.m_aUserParams.begin(); iUP != end; iUP++ )
 
260
        AddPrimitiveVariable( ( *iUP ) ->Clone() );
 
261
 
 
262
    // Copy the standard primitive variables index table.
 
263
    TqInt i;
 
264
    for ( i = 0; i < EnvVars_Last; i++ )
 
265
        m_aiStdPrimitiveVars[ i ] = From.m_aiStdPrimitiveVars[ i ];
 
266
}
 
267
 
 
268
//---------------------------------------------------------------------
 
269
/** Set the default values (where available) from the attribute state for all standard
 
270
 * primitive variables.
 
271
 */
 
272
 
 
273
void CqSurface::SetDefaultPrimitiveVariables( TqBool bUseDef_st )
 
274
{
 
275
    TqInt bUses = Uses();
 
276
 
 
277
    // Set default values for all of our parameters
 
278
 
 
279
    // s and t default to four values, if the particular surface type requires different it is up
 
280
    // to the surface to override or change this after the fact.
 
281
    if ( USES( bUses, EnvVars_s ) && bUseDef_st && !bHasVar(EnvVars_s) )
 
282
    {
 
283
        AddPrimitiveVariable( new CqParameterTypedVarying<TqFloat, type_float, TqFloat>( "s" ) );
 
284
        s() ->SetSize( 4 );
 
285
        TqInt i;
 
286
        for ( i = 0; i < 4; i++ )
 
287
            s() ->pValue() [ i ] = m_pAttributes->GetFloatAttribute( "System", "TextureCoordinates" ) [ i * 2 ];
 
288
    }
 
289
 
 
290
    if ( USES( bUses, EnvVars_t ) && bUseDef_st && !bHasVar(EnvVars_t))
 
291
    {
 
292
        AddPrimitiveVariable( new CqParameterTypedVarying<TqFloat, type_float, TqFloat>( "t" ) );
 
293
        t() ->SetSize( 4 );
 
294
        TqInt i;
 
295
        for ( i = 0; i < 4; i++ )
 
296
            t() ->pValue() [ i ] = m_pAttributes->GetFloatAttribute( "System", "TextureCoordinates" ) [ ( i * 2 ) + 1 ];
 
297
    }
 
298
 
 
299
    if ( USES( bUses, EnvVars_u ) )
 
300
    {
 
301
        AddPrimitiveVariable( new CqParameterTypedVarying<TqFloat, type_float, TqFloat>( "u" ) );
 
302
        u() ->SetSize( 4 );
 
303
        u() ->pValue() [ 0 ] = u() ->pValue() [ 2 ] = 0.0;
 
304
        u() ->pValue() [ 1 ] = u() ->pValue() [ 3 ] = 1.0;
 
305
    }
 
306
 
 
307
    if ( USES( bUses, EnvVars_v ) )
 
308
    {
 
309
        AddPrimitiveVariable( new CqParameterTypedVarying<TqFloat, type_float, TqFloat>( "v" ) );
 
310
        v() ->SetSize( 4 );
 
311
        v() ->pValue() [ 0 ] = v() ->pValue() [ 1 ] = 0.0;
 
312
        v() ->pValue() [ 2 ] = v() ->pValue() [ 3 ] = 1.0;
 
313
    }
 
314
}
 
315
 
 
316
 
 
317
void CqSurface::NaturalSubdivide( CqParameter* pParam, CqParameter* pParam1, CqParameter* pParam2, TqBool u )
 
318
{
 
319
    switch ( pParam->Type() )
 
320
    {
 
321
    case type_float:
 
322
        {
 
323
            CqParameterTyped<TqFloat, TqFloat>* pTParam = static_cast<CqParameterTyped<TqFloat, TqFloat>*>( pParam );
 
324
            CqParameterTyped<TqFloat, TqFloat>* pTResult1 = static_cast<CqParameterTyped<TqFloat, TqFloat>*>( pParam1 );
 
325
            CqParameterTyped<TqFloat, TqFloat>* pTResult2 = static_cast<CqParameterTyped<TqFloat, TqFloat>*>( pParam2 );
 
326
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
327
            break;
 
328
        }
 
329
 
 
330
    case type_integer:
 
331
        {
 
332
            CqParameterTyped<TqInt, TqFloat>* pTParam = static_cast<CqParameterTyped<TqInt, TqFloat>*>( pParam );
 
333
            CqParameterTyped<TqInt, TqFloat>* pTResult1 = static_cast<CqParameterTyped<TqInt, TqFloat>*>( pParam1 );
 
334
            CqParameterTyped<TqInt, TqFloat>* pTResult2 = static_cast<CqParameterTyped<TqInt, TqFloat>*>( pParam2 );
 
335
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
336
            break;
 
337
        }
 
338
 
 
339
    case type_point:
 
340
    case type_vector:
 
341
    case type_normal:
 
342
        {
 
343
            CqParameterTyped<CqVector3D, CqVector3D>* pTParam = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( pParam );
 
344
            CqParameterTyped<CqVector3D, CqVector3D>* pTResult1 = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( pParam1 );
 
345
            CqParameterTyped<CqVector3D, CqVector3D>* pTResult2 = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( pParam2 );
 
346
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
347
            break;
 
348
        }
 
349
 
 
350
    case type_hpoint:
 
351
        {
 
352
            CqParameterTyped<CqVector4D, CqVector3D>* pTParam = static_cast<CqParameterTyped<CqVector4D, CqVector3D>*>( pParam );
 
353
            CqParameterTyped<CqVector4D, CqVector3D>* pTResult1 = static_cast<CqParameterTyped<CqVector4D, CqVector3D>*>( pParam1 );
 
354
            CqParameterTyped<CqVector4D, CqVector3D>* pTResult2 = static_cast<CqParameterTyped<CqVector4D, CqVector3D>*>( pParam2 );
 
355
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
356
            break;
 
357
        }
 
358
 
 
359
 
 
360
    case type_color:
 
361
        {
 
362
            CqParameterTyped<CqColor, CqColor>* pTParam = static_cast<CqParameterTyped<CqColor, CqColor>*>( pParam );
 
363
            CqParameterTyped<CqColor, CqColor>* pTResult1 = static_cast<CqParameterTyped<CqColor, CqColor>*>( pParam1 );
 
364
            CqParameterTyped<CqColor, CqColor>* pTResult2 = static_cast<CqParameterTyped<CqColor, CqColor>*>( pParam2 );
 
365
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
366
            break;
 
367
        }
 
368
 
 
369
    case type_string:
 
370
        {
 
371
            CqParameterTyped<CqString, CqString>* pTParam = static_cast<CqParameterTyped<CqString, CqString>*>( pParam );
 
372
            CqParameterTyped<CqString, CqString>* pTResult1 = static_cast<CqParameterTyped<CqString, CqString>*>( pParam1 );
 
373
            CqParameterTyped<CqString, CqString>* pTResult2 = static_cast<CqParameterTyped<CqString, CqString>*>( pParam2 );
 
374
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
375
            break;
 
376
        }
 
377
 
 
378
    case type_matrix:
 
379
        {
 
380
            CqParameterTyped<CqMatrix, CqMatrix>* pTParam = static_cast<CqParameterTyped<CqMatrix, CqMatrix>*>( pParam );
 
381
            CqParameterTyped<CqMatrix, CqMatrix>* pTResult1 = static_cast<CqParameterTyped<CqMatrix, CqMatrix>*>( pParam1 );
 
382
            CqParameterTyped<CqMatrix, CqMatrix>* pTResult2 = static_cast<CqParameterTyped<CqMatrix, CqMatrix>*>( pParam2 );
 
383
            TypedNaturalSubdivide( pTParam, pTResult1, pTResult2, u );
 
384
            break;
 
385
        }
 
386
 
 
387
    default:
 
388
        {
 
389
            // blank to avoid compiler warnings about unhandled cases
 
390
            break;
 
391
        }
 
392
    }
 
393
}
 
394
 
 
395
 
 
396
void CqSurface::NaturalDice( CqParameter* pParameter, TqInt uDiceSize, TqInt vDiceSize, IqShaderData* pData )
 
397
{
 
398
    switch ( pParameter->Type() )
 
399
    {
 
400
    case type_float:
 
401
        {
 
402
            CqParameterTyped<TqFloat, TqFloat>* pTParam = static_cast<CqParameterTyped<TqFloat, TqFloat>*>( pParameter );
 
403
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
404
            break;
 
405
        }
 
406
 
 
407
    case type_integer:
 
408
        {
 
409
            CqParameterTyped<TqInt, TqFloat>* pTParam = static_cast<CqParameterTyped<TqInt, TqFloat>*>( pParameter );
 
410
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
411
            break;
 
412
        }
 
413
 
 
414
    case type_point:
 
415
    case type_vector:
 
416
    case type_normal:
 
417
        {
 
418
            CqParameterTyped<CqVector3D, CqVector3D>* pTParam = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( pParameter );
 
419
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
420
            break;
 
421
        }
 
422
 
 
423
    case type_hpoint:
 
424
        {
 
425
            CqParameterTyped<CqVector4D, CqVector3D>* pTParam = static_cast<CqParameterTyped<CqVector4D, CqVector3D>*>( pParameter );
 
426
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
427
            break;
 
428
        }
 
429
 
 
430
    case type_color:
 
431
        {
 
432
            CqParameterTyped<CqColor, CqColor>* pTParam = static_cast<CqParameterTyped<CqColor, CqColor>*>( pParameter );
 
433
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
434
            break;
 
435
        }
 
436
 
 
437
    case type_string:
 
438
        {
 
439
            CqParameterTyped<CqString, CqString>* pTParam = static_cast<CqParameterTyped<CqString, CqString>*>( pParameter );
 
440
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
441
            break;
 
442
        }
 
443
 
 
444
    case type_matrix:
 
445
        {
 
446
            CqParameterTyped<CqMatrix, CqMatrix>* pTParam = static_cast<CqParameterTyped<CqMatrix, CqMatrix>*>( pParameter );
 
447
            TypedNaturalDice( uDiceSize, vDiceSize, pTParam, pData );
 
448
            break;
 
449
        }
 
450
 
 
451
    default:
 
452
        {
 
453
            // left blank to avoid compiler warnings about unhandled types
 
454
            break;
 
455
        }
 
456
    }
 
457
}
 
458
 
 
459
 
 
460
 
 
461
//---------------------------------------------------------------------
 
462
/** Dice the patch into a mesh of micropolygons.
 
463
 */
 
464
 
 
465
CqMicroPolyGridBase* CqSurface::Dice()
 
466
{
 
467
    PreDice( m_uDiceSize, m_vDiceSize );
 
468
 
 
469
    // Create a new CqMicorPolyGrid for this patch
 
470
    CqMicroPolyGrid* pGrid = new CqMicroPolyGrid( m_uDiceSize, m_vDiceSize, shared_from_this() );
 
471
 
 
472
    TqInt lUses = Uses();
 
473
 
 
474
    // Allow the surface to fill in as much as possible on the grid in one go for speed.
 
475
    TqInt lDone = DiceAll( pGrid );
 
476
 
 
477
    // Dice the primitive variables.
 
478
 
 
479
    // Special cases for s and t if "st" exists, it should override s and t.
 
480
    CqParameter* pParam;
 
481
    if( ( pParam = FindUserParam("st") ) != NULL )
 
482
    {
 
483
        if ( !isDONE( lDone, EnvVars_s ) && USES( lUses, EnvVars_s ) && ( NULL != pGrid->pVar(EnvVars_s) ) )
 
484
            pParam ->DiceOne( m_uDiceSize, m_vDiceSize, pGrid->pVar(EnvVars_s), this, 0 );
 
485
        if ( !isDONE( lDone, EnvVars_t ) && USES( lUses, EnvVars_t ) && ( NULL != pGrid->pVar(EnvVars_t) ) )
 
486
            pParam ->DiceOne( m_uDiceSize, m_vDiceSize, pGrid->pVar(EnvVars_t), this, 1 );
 
487
        DONE( lDone, EnvVars_s);
 
488
        DONE( lDone, EnvVars_t);
 
489
    }
 
490
 
 
491
    // Loop over all the variables checking if they have been specified in the scene, and
 
492
    // if they are needed by the shaders, and id the grid can accept them.
 
493
    // If all the tests pass, dice them into the grid based on their type.
 
494
    TqInt varID;
 
495
    for( varID = EnvVars_Cs; varID != EnvVars_Last; varID++ )
 
496
    {
 
497
        if ( !isDONE( lDone, varID ) && USES( lUses, varID ) && ( NULL != pGrid->pVar(varID) ) )
 
498
        {
 
499
            // Check if Cs has been specified by the user.
 
500
            if ( bHasVar(varID) )
 
501
            {
 
502
                if( pVar(varID)->Class() == class_vertex || pVar(varID)->Class() == class_facevarying )
 
503
                    // "vertex" and "facevarying" need to be dealt with by the surface as they are diced using the
 
504
                    // natural subdivision algorithms for that particular surface.
 
505
                    NaturalDice( pVar(varID), m_uDiceSize, m_vDiceSize, pGrid->pVar(varID) );
 
506
                else
 
507
                    // "varying" are just bilinearly interpolated, so can be handled by the primitive variable.
 
508
                    pVar(varID) ->Dice( m_uDiceSize, m_vDiceSize, pGrid->pVar(varID), this );
 
509
 
 
510
                // Mark this as done, so that the special case default handlers later don't need to worry about it.
 
511
                DONE(lDone, varID);
 
512
            }
 
513
        }
 
514
    }
 
515
 
 
516
    // Special case handlers for primitive variables that have defaults.
 
517
    if ( !isDONE( lDone, EnvVars_Cs ) && USES( lUses, EnvVars_Cs ) && ( NULL != pGrid->pVar(EnvVars_Cs) ) )
 
518
    {
 
519
        if ( NULL != pAttributes() ->GetColorAttribute( "System", "Color" ) )
 
520
            pGrid->pVar(EnvVars_Cs) ->SetColor( pAttributes() ->GetColorAttribute( "System", "Color" ) [ 0 ] );
 
521
        else
 
522
            pGrid->pVar(EnvVars_Cs) ->SetColor( CqColor( 1, 1, 1 ) );
 
523
    }
 
524
 
 
525
    if ( !isDONE( lDone, EnvVars_Os ) && USES( lUses, EnvVars_Os ) && ( NULL != pGrid->pVar(EnvVars_Os) ) )
 
526
    {
 
527
        if ( NULL != pAttributes() ->GetColorAttribute( "System", "Opacity" ) )
 
528
            pGrid->pVar(EnvVars_Os) ->SetColor( pAttributes() ->GetColorAttribute( "System", "Opacity" ) [ 0 ] );
 
529
        else
 
530
            pGrid->pVar(EnvVars_Os) ->SetColor( CqColor( 1, 1, 1 ) );
 
531
    }
 
532
 
 
533
    // If the shaders need N and they have been explicitly specified, then bilinearly interpolate them.
 
534
    if ( isDONE( lDone, EnvVars_N ) )
 
535
        pGrid->SetbShadingNormals( TqTrue );
 
536
 
 
537
    if ( !isDONE( lDone, EnvVars_Ng ) && CanGenerateNormals() && USES( lUses, EnvVars_Ng ) )
 
538
    {
 
539
        GenerateGeometricNormals( m_uDiceSize, m_vDiceSize, pGrid->pVar(EnvVars_Ng) );
 
540
        pGrid->SetbGeometricNormals( TqTrue );
 
541
    }
 
542
 
 
543
    // Now we need to dice the user specified parameters as appropriate.
 
544
    std::vector<CqParameter*>::iterator iUP;
 
545
    std::vector<CqParameter*>::iterator end = m_aUserParams.end();
 
546
    for ( iUP = m_aUserParams.begin(); iUP != end ; iUP++ )
 
547
    {
 
548
        IqShader* pShader;
 
549
                if ( NULL != (pShader=pGrid->pAttributes() ->pshadSurface(QGetRenderContext()->Time())) )
 
550
            pShader->SetArgument( ( *iUP ), this );
 
551
 
 
552
        if ( NULL != (pShader=pGrid->pAttributes() ->pshadDisplacement(QGetRenderContext()->Time())) )
 
553
            pShader->SetArgument( ( *iUP ), this );
 
554
 
 
555
        if ( NULL != (pShader=pGrid->pAttributes() ->pshadAtmosphere(QGetRenderContext()->Time())) )
 
556
            pShader->SetArgument( ( *iUP ), this );
 
557
    }
 
558
 
 
559
    PostDice( pGrid );
 
560
 
 
561
    return ( pGrid );
 
562
}
 
563
 
 
564
 
 
565
TqInt CqSurface::Split( std::vector<boost::shared_ptr<CqBasicSurface> >& aSplits )
 
566
{
 
567
    TqInt cSplits = PreSubdivide( aSplits, m_SplitDir == SplitDir_U );
 
568
 
 
569
    assert( aSplits.size() == 2 );
 
570
 
 
571
    aSplits[ 0 ] ->SetSurfaceParameters( *this );
 
572
    aSplits[ 0 ] ->SetSplitDir( ( SplitDir() == SplitDir_U ) ? SplitDir_V : SplitDir_U );
 
573
    aSplits[ 0 ] ->SetEyeSplitCount( EyeSplitCount() );
 
574
    aSplits[ 0 ] ->m_fDiceable = TqTrue;
 
575
    //ADDREF( aSplits[ 0 ] );
 
576
 
 
577
    aSplits[ 1 ] ->SetSurfaceParameters( *this );
 
578
    aSplits[ 1 ] ->SetSplitDir( ( SplitDir() == SplitDir_U ) ? SplitDir_V : SplitDir_U );
 
579
    aSplits[ 1 ] ->SetEyeSplitCount( EyeSplitCount() );
 
580
    aSplits[ 1 ] ->m_fDiceable = TqTrue;
 
581
    //ADDREF( aSplits[ 1 ] );
 
582
 
 
583
    // Iterate through any use parameters subdividing and storing the second value in the target surface.
 
584
    std::vector<CqParameter*>::iterator iUP;
 
585
    std::vector<CqParameter*>::iterator end = m_aUserParams.end();
 
586
    TqBool direction = SplitDir() == SplitDir_U;
 
587
 
 
588
    for ( iUP = m_aUserParams.begin(); iUP != end; iUP++ )
 
589
    {
 
590
        CqParameter* pNewA = ( *iUP ) ->Clone();
 
591
        CqParameter* pNewB = ( *iUP ) ->Clone();
 
592
        ( *iUP ) ->Subdivide( pNewA, pNewB, direction , this );
 
593
        static_cast<CqSurface*>( aSplits[ 0 ].get() ) ->AddPrimitiveVariable( pNewA );
 
594
        static_cast<CqSurface*>( aSplits[ 1 ].get() ) ->AddPrimitiveVariable( pNewB );
 
595
    }
 
596
 
 
597
    if ( !m_fDiceable )
 
598
    {
 
599
        std::vector<boost::shared_ptr<CqBasicSurface> > aSplits0;
 
600
        std::vector<boost::shared_ptr<CqBasicSurface> > aSplits1;
 
601
 
 
602
        cSplits = aSplits[ 0 ] ->Split( aSplits0 );
 
603
        cSplits += aSplits[ 1 ] ->Split( aSplits1 );
 
604
 
 
605
        aSplits.clear();
 
606
        aSplits.swap( aSplits0 );
 
607
        aSplits.insert( aSplits.end(), aSplits1.begin(), aSplits1.end() );
 
608
    }
 
609
 
 
610
    PostSubdivide( aSplits );
 
611
 
 
612
    return ( aSplits.size() );
 
613
}
 
614
 
 
615
 
 
616
//---------------------------------------------------------------------
 
617
/** uSubdivide any user defined parameter variables.
 
618
 */
 
619
 
 
620
void CqSurface::uSubdivideUserParameters( CqSurface* pA, CqSurface* pB )
 
621
{
 
622
    // Iterate through any use parameters subdividing and storing the second value in the target surface.
 
623
    std::vector<CqParameter*>::iterator iUP;
 
624
    std::vector<CqParameter*>::iterator end = m_aUserParams.end();
 
625
    for ( iUP = m_aUserParams.begin(); iUP != end; iUP++ )
 
626
    {
 
627
        CqParameter* pNewA = ( *iUP ) ->Clone();
 
628
        CqParameter* pNewB = ( *iUP ) ->Clone();
 
629
        ( *iUP ) ->Subdivide( pNewA, pNewB, TqTrue, this );
 
630
        pA->AddPrimitiveVariable( pNewA );
 
631
        pB->AddPrimitiveVariable( pNewB );
 
632
    }
 
633
}
 
634
 
 
635
 
 
636
//---------------------------------------------------------------------
 
637
/** vSubdivide any user defined parameter variables.
 
638
 */
 
639
 
 
640
void CqSurface::vSubdivideUserParameters( CqSurface* pA, CqSurface* pB )
 
641
{
 
642
    // Iterate through any use parameters subdividing and storing the second value in the target surface.
 
643
    std::vector<CqParameter*>::iterator iUP;
 
644
    std::vector<CqParameter*>::iterator end = m_aUserParams.end();
 
645
    for ( iUP = m_aUserParams.begin(); iUP != end; iUP++ )
 
646
    {
 
647
        CqParameter* pNewA = ( *iUP ) ->Clone();
 
648
        CqParameter* pNewB = ( *iUP ) ->Clone();
 
649
        ( *iUP ) ->Subdivide( pNewA, pNewB, TqFalse, this );
 
650
        pA->AddPrimitiveVariable( pNewA );
 
651
        pB->AddPrimitiveVariable( pNewB );
 
652
    }
 
653
}
 
654
 
 
655
 
 
656
//---------------------------------------------------------------------
 
657
/** Transform the surface by the specified matrix.
 
658
 */
 
659
 
 
660
void    CqSurface::Transform( const CqMatrix& matTx, const CqMatrix& matITTx, const CqMatrix& matRTx, TqInt iTime )
 
661
{
 
662
    // Tansform the control hull by the specified matrix.
 
663
    std::vector<CqParameter*>::iterator iUP;
 
664
    std::vector<CqParameter*>::iterator end = m_aUserParams.end();
 
665
    for ( iUP = m_aUserParams.begin(); iUP != end; iUP++ )
 
666
    {
 
667
        TqInt i;
 
668
 
 
669
        if ( ( *iUP ) ->Type() == type_point )
 
670
        {
 
671
            CqParameterTyped<CqVector3D, CqVector3D>* pTPV = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( ( *iUP ) );
 
672
            TqInt size = ( *iUP ) ->Size();
 
673
            for ( i = 0; i < size; i++ )
 
674
                pTPV->pValue() [ i ] = matTx * pTPV->pValue() [ i ];
 
675
        }
 
676
        else if ( ( *iUP ) ->Type() == type_normal )
 
677
        {
 
678
            CqParameterTyped<CqVector3D, CqVector3D>* pTPV = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( ( *iUP ) );
 
679
            TqInt size = ( *iUP ) ->Size();
 
680
            for ( i = 0; i < size; i++ )
 
681
                pTPV->pValue() [ i ] = matITTx * pTPV->pValue() [ i ];
 
682
        }
 
683
        if ( ( *iUP ) ->Type() == type_vector )
 
684
        {
 
685
            CqParameterTyped<CqVector3D, CqVector3D>* pTPV = static_cast<CqParameterTyped<CqVector3D, CqVector3D>*>( ( *iUP ) );
 
686
            TqInt size = ( *iUP ) ->Size();
 
687
            for ( i = 0; i < size; i++ )
 
688
                pTPV->pValue() [ i ] = matRTx * pTPV->pValue() [ i ];
 
689
        }
 
690
        if ( ( *iUP ) ->Type() == type_hpoint )
 
691
        {
 
692
            CqParameterTyped<CqVector4D, CqVector3D>* pTPV = static_cast<CqParameterTyped<CqVector4D, CqVector3D>*>( ( *iUP ) );
 
693
            TqInt size = ( *iUP ) ->Size();
 
694
            for ( i = 0; i < size; i++ )
 
695
                pTPV->pValue() [ i ] = matTx * pTPV->pValue() [ i ];
 
696
        }
 
697
    }
 
698
}
 
699
 
 
700
 
 
701
 
 
702
/** Find out if a named user parameter exists on this surface.
 
703
 */
 
704
CqParameter* CqSurface::FindUserParam( const char* name ) const
 
705
{
 
706
    CqString strName( name );
 
707
    std::vector<CqParameter*>::const_iterator iUP;
 
708
    std::vector<CqParameter*>::const_iterator end = m_aUserParams.end();
 
709
    for ( iUP = m_aUserParams.begin(); iUP != end ; iUP++ )
 
710
    {
 
711
        if( ( *iUP )->strName() == strName )
 
712
            return( *iUP );
 
713
    }
 
714
    return( NULL );
 
715
}
 
716
 
 
717
 
 
718
//---------------------------------------------------------------------
 
719
 
 
720
END_NAMESPACE( Aqsis )
 
721
 
 
722