Mercurial > hg > Members > e085722 > Cerium
comparison Renderer/Test_/property_chain.cc @ 4:b5b462ac9b3b
Cerium Blender ball_bound
author | Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 29 Nov 2010 16:42:42 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:3f6fe22ac669 | 4:b5b462ac9b3b |
---|---|
1 #include <math.h> | |
2 #include <stdlib.h> | |
3 #include "SceneGraphRoot.h" | |
4 #include "MainLoop.h" | |
5 #include "property_chain.h" | |
6 #include "types.h" | |
7 #include "Func.h" | |
8 #include "sys.h" | |
9 #include "SgChange.h" | |
10 | |
11 #define FALSE 0 | |
12 #define TRUE !FALSE | |
13 static const int PROPERTY_LENGTH = 50; | |
14 static double chain_width = 10; | |
15 | |
16 typedef struct { | |
17 float xyz[3]; | |
18 float angle[3]; | |
19 float stack_xyz[3]; | |
20 float next_xyz[3]; | |
21 float v_xyz[3]; | |
22 float next_v_xyz[3]; | |
23 int property_index; | |
24 int parent_index; | |
25 int have_parent; | |
26 int can_move; | |
27 memaddr parent; | |
28 memaddr children; | |
29 memaddr node; | |
30 int sgid; | |
31 } *PropertyPtr, Property; | |
32 | |
33 | |
34 Property *property, *update_property; | |
35 | |
36 // prototype | |
37 static void collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree); | |
38 static void move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h); | |
39 static void createSceneGraphFromProperty(SchedTask *s, void *sgroot, void *arg1); | |
40 static void set_property(Property *p, SceneGraphPtr sg, int index); | |
41 static void apply_property(SceneGraphPtr sg, Property *p); | |
42 static void regist_task(SceneGraphRoot *sgroot); | |
43 static void set_relation(SceneGraphPtr parent, SceneGraphPtr child); | |
44 | |
45 static void | |
46 init_chain(Property *p) { | |
47 for (int i = 0; i < 3; i++) { | |
48 p->xyz[i] = 0; | |
49 p->angle[i] = 0; | |
50 p->stack_xyz[i] = 0; | |
51 p->next_xyz[i] = 0; | |
52 p->v_xyz[i] = 0; | |
53 p->next_v_xyz[i] = 0; | |
54 } | |
55 | |
56 p->property_index = 0; | |
57 p->parent_index = 0; | |
58 p->have_parent = 0; | |
59 p->can_move = TRUE; | |
60 p->parent = NULL; | |
61 p->children = NULL; | |
62 p->node = NULL; | |
63 } | |
64 | |
65 static void | |
66 move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
67 { | |
68 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; | |
69 HTaskPtr property_task = sgroot->move_exec_task; | |
70 | |
71 property_task->add_inData(property, sizeof(Property)*PROPERTY_LENGTH); | |
72 property_task->add_outData(update_property, sizeof(Property)*PROPERTY_LENGTH); | |
73 property_task->set_cpu(SPE_ANY); | |
74 property_task->set_post(createSceneGraphFromProperty, (void *)sgroot, 0); | |
75 property_task->spawn(); | |
76 } | |
77 | |
78 static void | |
79 collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, | |
80 SceneGraphPtr tree) | |
81 { | |
82 } | |
83 | |
84 static void | |
85 createSceneGraphFromProperty(SchedTask *s, void *sgroot_, void *arg1) | |
86 { | |
87 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; | |
88 SceneGraphPtr camera = sgroot->camera; | |
89 SceneGraphPtr p_node; | |
90 SceneGraphPtr root; | |
91 | |
92 // ここが allExecute の tree をたどって clone して行くところに相当する | |
93 Property *p; | |
94 | |
95 for (int i = 0; i < PROPERTY_LENGTH; i++) { | |
96 p = &update_property[i]; | |
97 SceneGraphPtr node = sgroot->createSceneGraph(p->sgid); | |
98 apply_property(node, p); | |
99 } | |
100 | |
101 for (int j = 0; j < PROPERTY_LENGTH; j++) { | |
102 p = &update_property[j]; | |
103 p_node = (SceneGraphPtr)p->node; | |
104 if (p->have_parent) { | |
105 SceneGraphPtr parent = (SceneGraphPtr)update_property[p->parent_index].node; | |
106 parent->addChild(p_node); | |
107 get_matrix(p_node->matrix, p_node->angle, p_node->xyz, parent->matrix); | |
108 get_matrix(p_node->real_matrix, p_node->angle, p_node->xyz, parent->real_matrix); | |
109 } else { | |
110 get_matrix(p_node->matrix, p_node->angle, p_node->xyz, camera->matrix); | |
111 get_matrix(p_node->real_matrix, p_node->angle, p_node->xyz, camera->real_matrix); | |
112 } | |
113 } | |
114 | |
115 root = (SceneGraphPtr)update_property[0].node; | |
116 root->set_move_collision(move, collision); | |
117 sgroot->setSceneData(root); | |
118 | |
119 Property *tmp = property; | |
120 property = update_property; | |
121 update_property = tmp; | |
122 | |
123 sgroot->move_finish(); | |
124 } | |
125 | |
126 static void | |
127 apply_property(SceneGraphPtr node, Property *p) | |
128 { | |
129 for (int i = 0; i < 3; i++) { | |
130 node->xyz[i] = p->xyz[i]; | |
131 node->angle[i] = p->angle[i]; | |
132 node->stack_xyz[i] = p->stack_xyz[i]; | |
133 } | |
134 p->node = (memaddr)node; | |
135 node->property = (memaddr)p; | |
136 } | |
137 | |
138 /* | |
139 ここで必要な値をプロパティに格納 | |
140 */ | |
141 static void | |
142 set_property(Property *p, SceneGraphPtr node, int index) | |
143 { | |
144 for (int i = 0; i < 3; i++) { | |
145 p->xyz[i] = node->xyz[i]; | |
146 p->angle[i] = node->angle[i]; | |
147 p->stack_xyz[i] = node->stack_xyz[i]; | |
148 } | |
149 p->parent = (memaddr)node->parent; | |
150 p->children = (memaddr)node->children; | |
151 p->property_index = index; | |
152 p->sgid = node->sgid; | |
153 p->node = (memaddr)node; | |
154 node->property = (memaddr)p; | |
155 } | |
156 | |
157 static void | |
158 regist_task(SceneGraphRoot *sgroot) | |
159 { | |
160 TaskManager *manager = sgroot->tmanager; | |
161 HTaskPtr task = manager->create_task(ChainTask); | |
162 // sgroot->setExecTask(task); とやるべき? | |
163 sgroot->move_exec_task = task; | |
164 } | |
165 | |
166 /* | |
167 Property に親子関係を書き込む | |
168 これも API に入れちゃっていいかな | |
169 */ | |
170 static void | |
171 set_relation(SceneGraphPtr parent, SceneGraphPtr child) | |
172 { | |
173 Property *p = (Property *)parent->property; | |
174 Property *c = (Property *)child->property; | |
175 p->children = (memaddr)child; | |
176 c->parent = (memaddr)parent; | |
177 c->parent_index = p->property_index; | |
178 c->have_parent = 1; | |
179 } | |
180 | |
181 MainLoopPtr | |
182 property_chain::init(Viewer *viewer, int screen_w, int screen_h) | |
183 { | |
184 // SgChange を使うための2行 | |
185 SgChange *sgroot = new SgChange(viewer); | |
186 sgroot->run_init(); | |
187 // 上で書いた regist_task() を登録 | |
188 // sgroot->appTaskRegist(regist_task); がいいかな | |
189 sgroot->sgroot_A->appTaskRegist(regist_task); | |
190 | |
191 property = (Property *)sgroot->manager->allocate(sizeof(Property)*PROPERTY_LENGTH); | |
192 update_property = (Property *)sgroot->manager->allocate(sizeof(Property)*PROPERTY_LENGTH); | |
193 | |
194 // property の初期化 application ごとに固有 | |
195 for(int i = 0; i < PROPERTY_LENGTH; i++) { | |
196 init_chain(&property[i]); | |
197 } | |
198 | |
199 SceneGraphPtr root_chain, chain; | |
200 sgroot->createFromXMLfile("xml_file/chain.xml"); | |
201 root_chain = sgroot->createSceneGraph("CHAIN"); | |
202 //root_chain->xyz[0] = screen_w / 2; | |
203 //root_chain->xyz[0] = screen_w / 4; | |
204 root_chain->xyz[0] = 300.0f; | |
205 root_chain->xyz[1] = 0.0f; | |
206 root_chain->set_move_collision(move, collision); | |
207 set_property(&property[0], root_chain, 0); | |
208 | |
209 for (int i = 1; i < PROPERTY_LENGTH; i++) { | |
210 chain = sgroot->createSceneGraph("CHAIN"); | |
211 chain->xyz[0] = 0; | |
212 chain->xyz[1] = chain_width * i; | |
213 chain->angle[1] = -90 * (i % 2); | |
214 root_chain->addChild(chain); | |
215 set_property(&property[i], chain, i); | |
216 set_relation(root_chain, chain); | |
217 } | |
218 | |
219 // root の set_property は SceneGraph を作成してから実行 | |
220 // addChild したら set_relation しないとだめ、あんまりにもあんまり | |
221 | |
222 /* | |
223 apply_property は post_func の createSceneGraphFromProperty 中で使う | |
224 apply_property は sgroot に持たせてもいいと思います○ | |
225 */ | |
226 | |
227 sgroot->setSceneData(root_chain); | |
228 | |
229 return sgroot; | |
230 } | |
231 | |
232 extern Application * | |
233 application() { | |
234 return new property_chain(); | |
235 } | |
236 | |
237 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; | |
238 | |
239 extern int init(TaskManager *manager, int argc, char *argv[]); | |
240 extern void task_initialize(); | |
241 static void TMend(TaskManager *manager); | |
242 | |
243 int | |
244 TMmain(TaskManager *manager, int argc, char *argv[]) | |
245 { | |
246 task_initialize(); | |
247 manager->set_TMend(TMend); | |
248 return init(manager, argc, argv); | |
249 | |
250 } | |
251 | |
252 void | |
253 TMend(TaskManager *manager) | |
254 { | |
255 printf("test_nogl end\n"); | |
256 } | |
257 | |
258 /* end */ |