aboutsummaryrefslogtreecommitdiff
path: root/tool_src/QvLib/QvTraverse.cpp
blob: 07da40c8ddcde4c00b948dcf4558f54f881c1585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <QvElement.h>
#include <QvNodes.h>
#include <QvState.h>
//#ifdef WIN32
#include <QvUnknownNode.h>
//#endif


//////////////////////////////////////////////////////////////////////////////
//
// Traversal code for all nodes. The default method (in QvNode) does
// nothing. Because traverse() is defined in the header for ALL node
// classes, each one has an implementation here.
//
//////////////////////////////////////////////////////////////////////////////

// For debugging
static int indent = 0;
static void
announce(const char *className)
{
    for (int i = 0; i < indent; i++)
	printf("\t");
    printf("Traversing a %s\n", className);
}
#define ANNOUNCE(className) announce(QV__QUOTE(className))

#define DEFAULT_TRAVERSE(className)					      \
void									      \
className::traverse(QvState *)						      \
{									      \
    ANNOUNCE(className);						      \
}

//////////////////////////////////////////////////////////////////////////////
//
// Groups.
//
//////////////////////////////////////////////////////////////////////////////

void
QvGroup::traverse(QvState *state)
{
    ANNOUNCE(QvGroup);
    indent++;
    for (int i = 0; i < getNumChildren(); i++)
	getChild(i)->traverse(state);
    indent--;
}

void
QvLevelOfDetail::traverse(QvState *state)
{
    ANNOUNCE(QvLevelOfDetail);
    indent++;

    // ??? In a real implementation, this would choose a child based
    // ??? on the projected screen areas.
    if (getNumChildren() > 0)
	getChild(0)->traverse(state);

    indent--;
}

void
QvSeparator::traverse(QvState *state)
{
    ANNOUNCE(QvSeparator);
    state->push();
    indent++;
    for (int i = 0; i < getNumChildren(); i++)
	getChild(i)->traverse(state);
    indent--;
    state->pop();
}

void
QvSwitch::traverse(QvState *state)
{
    ANNOUNCE(QvSwitch);
    indent++;

    int which = whichChild.value;

    if (which == QV_SWITCH_NONE)
	;

    else if (which == QV_SWITCH_ALL)
	for (int i = 0; i < getNumChildren(); i++)
	    getChild(i)->traverse(state);

    else
	if (which < getNumChildren())
	    getChild(which)->traverse(state);

    indent--;
}

void
QvTransformSeparator::traverse(QvState *state)
{
    ANNOUNCE(QvTransformSeparator);

    // We need to "push" just the transformation stack. We'll
    // accomplish this by just pushing a no-op transformation onto
    // that stack. When we "pop", we'll restore that stack to its
    // previous state.

    QvElement *markerElt = new QvElement;
    markerElt->data = this;
    markerElt->type = QvElement::NoOpTransform;
    state->addElement(QvState::TransformationIndex, markerElt);

    indent++;
    for (int i = 0; i < getNumChildren(); i++)
	getChild(i)->traverse(state);
    indent--;

    // Now do the "pop"
    while (state->getTopElement(QvState::TransformationIndex) != markerElt)
	state->popElement(QvState::TransformationIndex);
}

//////////////////////////////////////////////////////////////////////////////
//
// Properties.
//
//////////////////////////////////////////////////////////////////////////////

#define DO_PROPERTY(className, stackIndex)				      \
void									      \
className::traverse(QvState *state)					      \
{									      \
    ANNOUNCE(className);						      \
    QvElement *elt = new QvElement;					      \
    elt->data = this;							      \
    state->addElement(QvState::stackIndex, elt);			      \
}

#define DO_TYPED_PROPERTY(className, stackIndex, eltType)		      \
void									      \
className::traverse(QvState *state)					      \
{									      \
    ANNOUNCE(className);						      \
    QvElement *elt = new QvElement;					      \
    elt->data = this;							      \
    elt->type = QvElement::eltType;					      \
    state->addElement(QvState::stackIndex, elt);			      \
}


void QvCoordinate3::traverse(QvState *state)
{
    ANNOUNCE(QvCoordinate3);
    QvElement *elt = new QvElement;
    elt->data = this;
    state->addElement(QvState::Coordinate3Index, elt);

//[MSH_START]
/*
	for ( int i = 0; i < point.num; i++ )
		printf( "point[%d]=%f %f %f\n", i, point.values[ i*3 ], point.values[ i*3 + 1 ], point.values[ i*3 + 2 ] );
*/
//[MSH_END]

	printf( "---------------------------------------\n" );
}
//DO_PROPERTY(QvCoordinate3,		Coordinate3Index)
//--------------------------------------------

DO_PROPERTY(QvMaterial,			MaterialIndex)
DO_PROPERTY(QvMaterialBinding,		MaterialBindingIndex)
DO_PROPERTY(QvNormal,			NormalIndex)
DO_PROPERTY(QvNormalBinding,		NormalBindingIndex)
DO_PROPERTY(QvShapeHints,		ShapeHintsIndex)
DO_PROPERTY(QvTextureCoordinate2,	TextureCoordinate2Index)
DO_PROPERTY(QvTexture2,			Texture2Index)
DO_PROPERTY(QvTexture2Transform,	Texture2TransformationIndex)

DO_TYPED_PROPERTY(QvDirectionalLight,	LightIndex, DirectionalLight)
DO_TYPED_PROPERTY(QvPointLight,		LightIndex, PointLight)
DO_TYPED_PROPERTY(QvSpotLight,		LightIndex, SpotLight)

DO_TYPED_PROPERTY(QvOrthographicCamera,	CameraIndex, OrthographicCamera)
DO_TYPED_PROPERTY(QvPerspectiveCamera,	CameraIndex, PerspectiveCamera)

DO_TYPED_PROPERTY(QvTransform,	     TransformationIndex, Transform)
DO_TYPED_PROPERTY(QvRotation,	     TransformationIndex, Rotation)
DO_TYPED_PROPERTY(QvMatrixTransform, TransformationIndex, MatrixTransform)
DO_TYPED_PROPERTY(QvTranslation,     TransformationIndex, Translation)
DO_TYPED_PROPERTY(QvScale,	     TransformationIndex, Scale)

//////////////////////////////////////////////////////////////////////////////
//
// Shapes.
//
//////////////////////////////////////////////////////////////////////////////

static void
printProperties(QvState *state)
{
    printf("--------------------------------------------------------------\n");
    state->print();
    printf("--------------------------------------------------------------\n");
}

#define DO_SHAPE(className)						      \
void									      \
className::traverse(QvState *state)					      \
{									      \
    ANNOUNCE(className);						      \
    printProperties(state);						      \
}

//[MSH_START]
/*
void QvCone::traverse(QvState *state)
{
	ANNOUNCE(QvCone);
	printf( "\tbottomRadius=%f\n", bottomRadius.value );
	printf( "\theight=%f\n", height.value );
	printf( "\tparts=%d\n", parts.value );
	printf( "---------------------------------------\n" );
	printProperties(state);
}
*/
//[MSH_END]



//[MSH_START]

//DO_SHAPE(QvSphere)
//DO_SHAPE(QvCone)
//DO_SHAPE(QvCube)
//DO_SHAPE(QvCylinder)

#include "BspObjectList.h"
#include "BRep.h"

void QvSphere::traverse(QvState *state)
{
	ANNOUNCE(QvSphere);
	BSPLIB_NAMESPACE::BRep brep( state );
	brep.BuildFromSpherePrimitive( *this );
}

void QvCone::traverse(QvState *state)
{
	ANNOUNCE(QvCone);
	BSPLIB_NAMESPACE::BRep brep( state );
	brep.BuildFromConePrimitive( *this );
}

void QvCube::traverse(QvState *state)
{
	ANNOUNCE(QvCube);
	BSPLIB_NAMESPACE::BRep brep( state );
	brep.BuildFromCubePrimitive( *this );
}


void QvCylinder::traverse(QvState *state)
{
	ANNOUNCE(QvCylinder);
	BSPLIB_NAMESPACE::BRep brep( state );
	brep.BuildFromCylinderPrimitive( *this );
}

void QvIndexedFaceSet::traverse(QvState *state)
{
	ANNOUNCE(QvIndexedFaceSet);
/*
	for ( int i = 0; i < coordIndex.num; i++ )
		printf( "coordIndex[%d]=%d\n", i, coordIndex.values[ i ] );
	printf( "---------------------------------------\n" );
*/
	// build b-rep out of this indexed face set and current state
	BSPLIB_NAMESPACE::BRep brep( state );
	brep.BuildFromIndexedFaceSet( *this );
}
//[MSH_END]

//DO_SHAPE(QvIndexedFaceSet)
//--------------------------------------------

DO_SHAPE(QvIndexedLineSet)
DO_SHAPE(QvPointSet)



//////////////////////////////////////////////////////////////////////////////
//
// WWW-specific nodes.
//
//////////////////////////////////////////////////////////////////////////////

// ???
DEFAULT_TRAVERSE(QvWWWAnchor)
DEFAULT_TRAVERSE(QvWWWInline)

//////////////////////////////////////////////////////////////////////////////
//
// Default traversal methods. These nodes have no effects during traversal.
//
//////////////////////////////////////////////////////////////////////////////

DEFAULT_TRAVERSE(QvInfo)
DEFAULT_TRAVERSE(QvUnknownNode)

//////////////////////////////////////////////////////////////////////////////

#undef ANNOUNCE
#undef DEFAULT_TRAVERSE
#undef DO_PROPERTY
#undef DO_SHAPE
#undef DO_TYPED_PROPERTY