1
2
3
4
5
6
7 #include "node.h"
8
9 #include <assert.h>
10 #include <iostream.h>
11 #include <GL/glut.h>
12
13
14
15
16
17 LeafNode::LeafNode(Shape* shape)
18 : shape_(shape) {
19 }
20
21 void LeafNode::traverse() {
22 if(shape_) { shape_->render(); }
23
24
25 }
26
27
28
29
30
31 GroupNode::GroupNode() {
32
33 for(unsigned int i = 0; i < MAX_ATTRIBUTES; ++i) {
34 attributes_[i] = 0;
35 }
36
37
38 for(unsigned int j = 0; j < MAX_CHILD_NODES; ++j) {
39 children_[j] = 0;
40 }
41 }
42
43 GroupNode* GroupNode::shareNode(Node* nodeObject, int nCount, Attribute* attrTransform1, Attribute* attrTransform2)
44 {
45 GroupNode* sharedNode = NULL;
46
47 for (int shape = 1; shape <= nCount; shape++)
48 {
49 static GroupNode* oldNode;
50 oldNode = sharedNode;
51 sharedNode = new GroupNode;
52
53 if (shape < nCount)
54 {
55 if (attrTransform1 != NULL)
56 sharedNode->setAttribute(0, attrTransform1);
57 if (attrTransform2 != NULL)
58 sharedNode->setAttribute(1, attrTransform2);
59 }
60
61 sharedNode->setChildNode(0, nodeObject);
62
63 if (oldNode != NULL)
64 sharedNode->setChildNode(1, oldNode);
65 }
66
67 return sharedNode;
68 }
69
70
71 Attribute* GroupNode::getAttribute(unsigned int index) const {
72 assert(index < MAX_ATTRIBUTES);
73 return attributes_[index];
74 }
75
76 void GroupNode::setAttribute(unsigned int index, Attribute* attribute) {
77 assert(index < MAX_ATTRIBUTES);
78 attributes_[index] = attribute;
79 }
80
81 Node* GroupNode::getChildNode(unsigned int index) const {
82 assert(index < MAX_CHILD_NODES);
83 return children_[index];
84 }
85
86 void GroupNode::setChildNode(unsigned int index, Node* child) {
87 assert(index < MAX_CHILD_NODES);
88 children_[index] = child;
89 }
90
91 void GroupNode::traverse() {
92
93 for(unsigned int i = 0; i < MAX_ATTRIBUTES; ++i) {
94 if(attributes_[i]) { attributes_[i]->set(); }
95 }
96
97
98 for(unsigned int j = 0; j < MAX_CHILD_NODES; ++j) {
99 if(children_[j]) { children_[j]->traverse(); }
100 }
101
102
103 for(unsigned int k = MAX_ATTRIBUTES; k > 0; --k) {
104 if(attributes_[k - 1]) { attributes_[k - 1]->unset(); }
105 }
106 }
107
108