-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgob_mthd.c
More file actions
executable file
·525 lines (437 loc) · 14 KB
/
gob_mthd.c
File metadata and controls
executable file
·525 lines (437 loc) · 14 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/****************************************************************************
* gob_mthd.c
* Author Joel Welling
* Copyright 1990, Pittsburgh Supercomputing Center, Carnegie Mellon University
*
* Permission use, copy, and modify this software and its documentation
* without fee for personal use or use within your organization is hereby
* granted, provided that the above copyright notice is preserved in all
* copies and that that copyright and this permission notice appear in
* supporting documentation. Permission to redistribute this software to
* other organizations or individuals is not granted; that must be
* negotiated with the PSC. Neither the PSC nor Carnegie Mellon
* University make any representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*****************************************************************************/
/*
This module provides methods for the root class gob.
*/
#include <stdio.h>
#include "p3dgen.h"
#include "pgen_objects.h"
#include "indent.h"
#include "ge_error.h"
/* Struct to hold data about renderer rep of gob */
typedef struct ren_data_struct {
P_Renderer *renderer;
P_Void_ptr data;
struct ren_data_struct *next;
} P_Ren_Data;
#define RENDERER(block) (block->renderer)
#define RENDATA(block) (block->data)
#define RENLIST(gob) ((P_Ren_Data *)(self->object_data))
#define WALK_RENLIST(gob, operation) { \
P_Ren_Data *thisrendata= RENLIST(gob); \
while (thisrendata) { \
METHOD_RDY(RENDERER(thisrendata)); \
(*(RENDERER(thisrendata)->operation))(RENDATA(thisrendata)); \
thisrendata= thisrendata->next; \
}}
static P_Gob_List *add_gob_cell( P_Gob_List *oldfirst )
/* This routine adds a cell to a gob list */
{
P_Gob_List *thiscell;
ger_debug("gob_mthd: add_gob_cell");
if ( !(thiscell= (P_Gob_List *)malloc(sizeof(P_Gob_List))) )
ger_fatal("gob_mthd: add_gob_cell: unable to allocate %d bytes!\n",
sizeof(P_Gob_List));
thiscell->next= oldfirst;
thiscell->prev= (P_Gob_List *)0;
if (oldfirst) oldfirst->prev= thiscell;
return(thiscell);
}
static void add_transform( P_Transform *thistrans )
/* This method premultiplies a tranform onto the gob's transform. */
{
P_Gob *self= (P_Gob *)po_this;
P_Transform_type *tmp;
METHOD_IN
ger_debug("gob_mthd: add_transform");
if (self->has_transform) {
premult_trans( thistrans, &(self->trans) );
tmp = duplicate_trans_type(thistrans->type_front);
self->trans.type_front = add_type_to_list(tmp,self->trans.type_front);
} else { /* This is the first transform. */
copy_trans( &(self->trans), thistrans );
self->trans.type_front = duplicate_trans_type(thistrans->type_front);
self->has_transform= 1;
}
METHOD_OUT
}
static void refuse_transform( P_Transform *thistrans )
/* This this is the transform-adding method for primitive gobs, which cannot
* have transforms associated with them.
*/
{
P_Gob *self= (P_Gob *)po_this;
METHOD_IN
ger_debug("gob_mthd: refuse_transform");
ger_error("gob_mthd: refuse_transform: \n\
Attempted to apply a transform to a primitive gob, which can't have any.");
METHOD_OUT
}
static void add_child( P_Gob *thischild )
/* This method adds a gob to the gob's child list. Notice that it does
* not make a new copy of the added gob.
*/
{
P_Gob *self= (P_Gob *)po_this;
METHOD_IN
ger_debug("gob_mthd: add_child");
self->children= add_gob_cell( self->children );
self->children->gob= thischild;
METHOD_RDY(thischild);
thischild->parents += 1;
METHOD_OUT
}
static void refuse_child( P_Gob *thischild )
/* This is the child-adding method for primitive gobs, which cannot
* have children.
*/
{
P_Gob *self= (P_Gob *)po_this;
METHOD_IN
ger_debug("gob_mthd: refuse_child");
ger_error("gob_mthd: refuse_child: \n\
Attempted to add a child gob to a primitive gob, which can't have any.");
METHOD_OUT
}
static void add_attribute( char *attribute, int type, P_Void_ptr value )
/* This method adds an attribute-value pair to the gob's attribute list.
* Note that it does not make a new copy of the attribute or value info.
*/
{
P_Gob *self= (P_Gob *)po_this;
METHOD_IN
ger_debug("gob_mthd: add_attribute");
self->attr= add_attr( self->attr, attribute, type, value );
METHOD_OUT
}
static void refuse_attribute( char *attribute, int type, P_Void_ptr value )
/* This is the attribute-adding method for primitive gobs, which
* cannot have attributes.
*/
{
P_Gob *self= (P_Gob *)po_this;
METHOD_IN
ger_debug("gob_mthd: refuse_attribute");
ger_error(
"gob_mthd: refuse_attr: \n\
Attempted to add an attribute to a primitive gob, which can't have any.");
METHOD_OUT
}
static void traverselights( P_Transform *thistrans, P_Attrib_List *thisattr )
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: traverselights");
thisrendata= RENLIST(self);
while (thisrendata) {
METHOD_RDY(RENDERER(thisrendata));
(*(RENDERER(thisrendata)->light_traverse_gob))
(RENDATA(thisrendata), thistrans, thisattr);
thisrendata= thisrendata->next;
}
METHOD_OUT
}
static void traverselights_to_ren( P_Renderer *ren, P_Transform *thistrans,
P_Attrib_List *thisattr)
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: traverselights_to_ren");
/* I hate to do a walk of the renderer list, but I don't
* see an alternative.
*/
thisrendata= RENLIST(self);
while (thisrendata) {
if ( RENDERER(thisrendata) == ren ) {
METHOD_RDY(RENDERER(thisrendata));
(*(RENDERER(thisrendata)->light_traverse_gob))
(RENDATA(thisrendata), thistrans, thisattr);
METHOD_OUT
return;
}
else thisrendata= thisrendata->next;
}
METHOD_OUT
}
static void render( P_Transform *thistrans, P_Attrib_List *thisattr )
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: render");
thisrendata= RENLIST(self);
while (thisrendata) {
METHOD_RDY(RENDERER(thisrendata));
(*(RENDERER(thisrendata)->ren_gob))
(RENDATA(thisrendata), thistrans, thisattr);
thisrendata= thisrendata->next;
}
METHOD_OUT
}
static void render_to_ren(P_Renderer *thisrenderer, P_Transform *thistrans,
P_Attrib_List *thisattr)
/* Render to the named renderer only */
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: render_to_ren");
/* I hate to do a walk of the renderer list, but I don't
* see an alternative.
*/
thisrendata= RENLIST(self);
while (thisrendata) {
if ( RENDERER(thisrendata) == thisrenderer ) {
METHOD_RDY(RENDERER(thisrendata));
(*(RENDERER(thisrendata)->ren_gob))
(RENDATA(thisrendata), thistrans, thisattr);
METHOD_OUT
return;
}
else thisrendata= thisrendata->next;
}
METHOD_OUT
}
static void define_gob(P_Renderer *thisrenderer)
/* This routine defines the gob within the context of the given renderer */
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: define_gob");
/* If already defined, return */
thisrendata= RENLIST(self);
while (thisrendata) {
if ( RENDERER(thisrendata) == thisrenderer ) {
METHOD_OUT
return;
}
thisrendata= thisrendata->next;
}
/* Create memory for the renderer data */
if ( !(thisrendata= (P_Ren_Data *)malloc(sizeof(P_Ren_Data))) )
ger_fatal("gob_mthd: define_gob: unable to allocate %d bytes!",
sizeof(P_Ren_Data) );
thisrendata->next= RENLIST(self);
self->object_data= (P_Void_ptr)thisrendata;
METHOD_RDY(thisrenderer)
RENDERER(thisrendata)= thisrenderer;
RENDATA(thisrendata)= (*(thisrenderer->def_gob))(self->name,self);
/* If the gob is held, tell the renderer to hold it as well */
if (self->held) (*(thisrenderer->hold_gob))(RENDATA(thisrendata));
METHOD_OUT
}
static P_Void_ptr get_ren_data(P_Renderer *thisrenderer)
/* This method returns the data given by the renderer at definition time */
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: get_ren_data");
thisrendata= RENLIST(self);
while (thisrendata) {
if ( RENDERER(thisrendata) == thisrenderer ) {
METHOD_OUT
return( RENDATA(thisrendata) );
}
thisrendata= thisrendata->next;
}
/* If not found, not defined for this renderer */
METHOD_OUT
return( (P_Void_ptr)0 );
}
static void print_gob( VOIDLIST )
/* This is the print method for the gob. */
{
P_Gob *self= (P_Gob *)po_this;
P_Gob_List *child_list;
METHOD_IN
ger_debug("gob_mthd: print_gob");
ind_write("Gob <%s>:",self->name);
if (self->held) ind_write(" %d parents", self->parents);
ind_eol();
ind_push();
if (self->has_transform) {
ind_write("Transformation:"); ind_eol();
dump_trans(&(self->trans));
}
if (self->attr) {
ind_write("Attributes:"); ind_eol();
print_attr(self->attr);
}
if (self->children) {
ind_write("Children:"); ind_eol();
ind_push();
child_list= self->children;
while (child_list) {
METHOD_RDY(child_list->gob)
(*(child_list->gob->print))();
child_list= child_list->next;
}
ind_pop();
}
ind_pop();
METHOD_OUT
}
static void hold( VOIDLIST )
/* This method marks a gob as being held, so that it cannot be destroyed */
{
P_Gob *self= (P_Gob *)po_this;
METHOD_IN
ger_debug("gob_mthd: hold");
self->held= 1;
WALK_RENLIST(self,hold_gob);
METHOD_OUT
}
static void unhold( VOIDLIST )
/* This method marks a gob as not being held, so that it can be destroyed */
{
P_Gob *self= (P_Gob *)po_this;
P_Ren_Data *thisrendata;
METHOD_IN
ger_debug("gob_mthd: unhold");
self->held= 0;
thisrendata= RENLIST(self);
while (thisrendata) {
METHOD_RDY(RENDERER(thisrendata));
(*(RENDERER(thisrendata)->unhold_gob))(RENDATA(thisrendata));
thisrendata= thisrendata->next;
}
METHOD_OUT
}
static void destroy( int destroy_ren_rep )
/* This is the destroy method for the gob. If the gob is not held and
* has no parents, it will be destroyed. Likewise, any of its children
* which are not held and have no parents are also destroyed, and so
* on recursively.
*/
{
P_Gob *self= (P_Gob *)po_this;
P_Gob *kidgob;
P_Gob_List *kids, *nextkids;
P_Ren_Data *thisrendata, *lastrendata;
METHOD_IN
if ( (self->held) || (self->parents) ) {
ger_debug("gob_mthd: destroy: held or with parents; not destroyed");
METHOD_OUT
return;
}
ger_debug("gob_mthd: destroy");
/* Clean up attribute list */
if (self->attr) destroy_attr( self->attr );
/* Clean up child list, destoying child gobs if appropriate */
kids= self->children;
while (kids) {
nextkids= kids->next;
kidgob= kids->gob;
METHOD_RDY(kidgob);
kidgob->parents -= 1;
(*(kidgob->destroy_self))(destroy_ren_rep);
/* does nothing if held or has parents */
free( (P_Void_ptr)kids );
kids= nextkids;
}
/* destroy renderer rep of this gob */
if (destroy_ren_rep) WALK_RENLIST(self,destroy_gob);
/* Clean up local memory */
thisrendata= RENLIST(self);
while (thisrendata) {
lastrendata= thisrendata;
thisrendata= thisrendata->next;
free( (P_Void_ptr)lastrendata );
}
free( (P_Void_ptr)self );
METHOD_DESTROYED
}
P_Gob *po_create_gob( char *name )
/* This function returns a gob of the sort which forms a non-terminal
* DAG node. It is created without children, however.
*/
{
P_Gob *thisgob;
ger_debug("po_create_gob: name= <%s>", name);
/* Create memory for the gob */
if ( !(thisgob= (P_Gob *)malloc(sizeof(P_Gob))) )
ger_fatal("po_create_gob: unable to allocate %d bytes!",
sizeof(P_Gob) );
/* Fill out object data */
strncpy( thisgob->name, name, P3D_NAMELENGTH-1 );
thisgob->name[P3D_NAMELENGTH-1]= '\0';
thisgob->held= 0;
thisgob->parents= 0;
thisgob->children= (P_Gob_List *)0;
thisgob->attr= (P_Attrib_List *)0;
thisgob->has_transform= 0;
copy_trans( &(thisgob->trans), Identity_trans );
thisgob->object_data= (P_Void_ptr)0;
thisgob->hold= hold;
thisgob->unhold= unhold;
thisgob->destroy_self= destroy;
thisgob->print= print_gob;
thisgob->define= define_gob;
thisgob->render= render;
thisgob->render_to_ren= render_to_ren;
thisgob->traverselights= traverselights;
thisgob->traverselights_to_ren= traverselights_to_ren;
thisgob->add_attribute= add_attribute;
thisgob->add_child= add_child;
thisgob->add_transform= add_transform;
thisgob->get_ren_data= get_ren_data;
return(thisgob);
}
P_Gob *po_create_primitive( char *name )
/* This function returns a gob of the sort which forms a terminal (leaf)
* DAG node. It is created without children, attributes, or transformation,
* and has no method to add them.
*/
{
P_Gob *thisgob;
ger_debug("po_create_primitive: name= <%s>", name);
/* Create memory for the gob */
if ( !(thisgob= (P_Gob *)malloc(sizeof(P_Gob))) )
ger_fatal("po_create_gob: unable to allocate %d bytes!",
sizeof(P_Gob) );
/* Fill out object data */
strncpy( thisgob->name, name, P3D_NAMELENGTH-1 );
thisgob->name[P3D_NAMELENGTH-1]= '\0';
thisgob->held= 0;
thisgob->parents= 0;
thisgob->children= (P_Gob_List *)0;
thisgob->attr= (P_Attrib_List *)0;
thisgob->has_transform= 0;
copy_trans( &(thisgob->trans), Identity_trans );
thisgob->object_data= (P_Void_ptr)0;
thisgob->add_attribute= refuse_attribute;
thisgob->add_child= refuse_child;
thisgob->add_transform= refuse_transform;
thisgob->hold= hold;
thisgob->unhold= unhold;
thisgob->destroy_self= (void (*)(int))null_method;
thisgob->print= null_method;
thisgob->define= (void (*)(P_Renderer *))null_method;
thisgob->render= (void (*)(P_Transform *, P_Attrib_List *))null_method;
thisgob->render_to_ren=
(void (*)(P_Renderer *,P_Transform *, P_Attrib_List *))null_method;
thisgob->traverselights=
(void (*)(P_Transform *, P_Attrib_List *))null_method;
thisgob->traverselights_to_ren=
(void (*)(P_Renderer *,P_Transform *, P_Attrib_List *))null_method;
thisgob->get_ren_data= (P_Void_ptr (*)(P_Renderer *))null_method;
return(thisgob);
}