Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/SceneGraph.cc @ 751:8b45afad160f
add task/DataLoad task/DataUpdate
author | hiroki |
---|---|
date | Mon, 25 Jan 2010 19:25:40 +0900 |
parents | b7376415fa5f |
children | 24a37fe8419a |
rev | line source |
---|---|
507 | 1 #include <iostream> |
2 #include <SDL.h> | |
3 #include <SDL_opengl.h> | |
4 #include <SDL_image.h> | |
5 #include <libxml/parser.h> | |
6 #include "SceneGraph.h" | |
7 #include "xml.h" | |
8 #include "sys.h" | |
9 #include "TextureHash.h" | |
10 #include "texture.h" | |
11 #include "TaskManager.h" | |
12 | |
13 using namespace std; | |
14 | |
15 SceneGraphPtr scene_graph = NULL; | |
16 SceneGraphPtr scene_graph_viewer = NULL; | |
17 | |
18 static TextureHash texture_hash; | |
19 struct texture_list list[TABLE_SIZE]; | |
20 | |
21 extern int decode(char *cont, FILE *outfile); | |
22 | |
23 static void | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
24 no_move(SceneGraphPtr self, void *sgroot_, int screen_w, int screen_h) {} |
507 | 25 |
26 static void | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
27 no_collision(SceneGraphPtr self, void *sgroot_, int screen_w, int screen_h, |
507 | 28 SceneGraphPtr tree) {} |
29 | |
30 /** | |
31 * 事前に計算したテクスチャの最大縮小率 scale まで、 | |
32 * テクスチャを 1/2 縮小していく。 | |
33 * このとき、テクスチャは TEXTURE_SPLIT_PIXELx2 のブロック (Tile) で分割し、 | |
34 * これらを連続したメモリ領域に格納していく。 | |
35 * 以下の (1), (2), (3) を Tapestry と呼ぶ | |
36 * | |
37 * 例 scale = 4 の場合 | |
38 * | |
39 * Tapestry(1) 1/1 | |
40 * +---+---+---+---+ | |
41 * | 0 | 1 | 2 | 3 | | |
42 * +---+---+---+---+ | |
43 * | 4 | 5 | 6 | 7 | (2) 1/2 | |
44 * +---+---+---+---+ +---+---+ | |
45 * | 8 | 9 | 10| 11| | 16| 17| (3) 1/4 | |
46 * +---+---+---+---+ +---+---+ +---+ | |
47 * | 12| 13| 14| 15| | 18| 19| | 20| | |
48 * +---+---+---+---+ +---+---+ +---| | |
49 * | |
50 * (1) (2) (3) | |
51 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | |
52 * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | * | * | * | 14| 15| 16| 17| 18| 19| 20| | |
53 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | |
54 * | |
55 * @param[in] tex_w Width of orignal texture | |
56 * @param[in] tex_h Height of orignal texture | |
57 * @param[in] tex_src Original texture | |
58 * @param[in] all_pixel_num Tapestry の合計 pixel 数 | |
59 * @param[in] scale テクスチャの最大縮小率 (= 2^n) | |
60 * @return (1) のアドレス | |
61 */ | |
62 static uint32* | |
63 makeTapestry(TaskManager *manager, int tex_w, int tex_h, uint32 *tex_src, | |
64 int all_pixel_num, int scale_cnt) | |
65 { | |
66 | |
67 int t = 0; | |
68 int diff = TEXTURE_SPLIT_PIXEL; | |
69 int p_diff = 1; | |
70 | |
71 uint32 *tex_dest = (uint32*)manager->allocate(sizeof(int)*all_pixel_num); | |
72 | |
73 while (scale_cnt) { | |
74 for (int y = 0; y < tex_h; y += diff) { | |
75 for (int x = 0; x < tex_w; x += diff) { | |
76 for (int j = 0; j < diff; j += p_diff) { | |
77 for (int i = 0; i < diff; i += p_diff) { | |
78 tex_dest[t++] | |
79 = tex_src[(x+i) + tex_w*(y+j)]; | |
80 } | |
81 } | |
82 } | |
83 } | |
84 | |
85 diff <<= 1; | |
86 p_diff <<= 1; | |
87 scale_cnt >>= 1; | |
88 } | |
89 | |
90 return tex_dest; | |
91 } | |
92 | |
93 | |
94 /** | |
95 * 何の情報も持ってない SceneGraph の生成 | |
96 * 今のところ、とりあえず木構造の繋がりに使うぐらい | |
97 */ | |
748 | 98 SceneGraph::SceneGraph() |
507 | 99 { |
100 init(); | |
101 finalize = &SceneGraph::finalize_copy; | |
102 | |
103 this->name = "NULLPO"; | |
104 } | |
105 | |
106 /** | |
107 * orig のコピーとして SceneGraph を生成する | |
108 */ | |
109 SceneGraph::SceneGraph(SceneGraphPtr orig) | |
110 { | |
111 init(); | |
112 memcpy(this, orig, sizeof(SceneGraph)); | |
113 | |
114 // コピーしない | |
115 //flag_remove = 0; | |
116 //flag_drawable = 1; | |
117 next = NULL; | |
118 prev = NULL; | |
119 last = NULL; | |
120 | |
121 parent = NULL; | |
122 brother = NULL; | |
123 children = NULL; | |
124 lastChild = NULL; | |
125 | |
126 finalize = &SceneGraph::finalize_copy; | |
127 | |
128 | |
129 frame = 0; | |
130 } | |
131 | |
132 | |
133 /* construct polygon from xmlNode. */ | |
134 SceneGraph::SceneGraph(TaskManager *manager, xmlNodePtr surface) | |
135 { | |
136 init(); | |
137 | |
138 size = atoi((char *)xmlGetProp(surface,(xmlChar *)"size")); | |
139 name = (char *)xmlGetProp(surface,(xmlChar *)"name"); | |
140 parent_name = (char *)xmlGetProp(surface,(xmlChar *)"parent"); | |
141 | |
142 //data = new float[size*3*3]; | |
143 coord_xyz = (float*)manager->allocate(sizeof(float)*size*3); | |
144 coord_tex = (float*)manager->allocate(sizeof(float)*size*3); | |
145 normal = (float*)manager->allocate(sizeof(float)*size*3); | |
146 | |
147 get_data(manager, surface->children); | |
148 | |
149 finalize = &SceneGraph::finalize_original; | |
150 } | |
151 | |
152 void | |
748 | 153 SceneGraph::init() |
507 | 154 { |
155 next = NULL; | |
156 prev = NULL; | |
157 last = NULL; | |
158 | |
159 parent = NULL; | |
160 brother = NULL; | |
161 children = NULL; | |
162 lastChild = NULL; | |
163 | |
164 stack_xyz[0] = 0.0f; | |
165 stack_xyz[2] = 0.0f; | |
166 stack_xyz[1] = 0.0f; | |
167 stack_angle[0] = 0.0f; | |
168 stack_angle[1] = 0.0f; | |
169 stack_angle[2] = 0.0f; | |
170 | |
171 size = 0; | |
172 //data = NULL; | |
173 coord_xyz = NULL; | |
174 normal = NULL; | |
175 coord_tex = NULL; | |
176 | |
177 texture_id = -1; | |
178 move = no_move; | |
179 collision = no_collision; | |
180 | |
181 flag_remove = 0; | |
182 flag_drawable = 1; | |
183 sgid = -1; | |
184 gid = -1; | |
185 | |
186 frame = 0; | |
187 } | |
188 | |
748 | 189 SceneGraph::~SceneGraph() |
507 | 190 { |
191 (this->*finalize)(); | |
192 } | |
193 | |
194 /** | |
195 * xml ファイルから生成されたオリジナル SceneGraph なので | |
196 * polygon data を削除 | |
197 */ | |
198 void | |
748 | 199 SceneGraph::finalize_original() |
507 | 200 { |
201 //delete [] data; | |
202 free(coord_xyz); | |
203 free(coord_tex); | |
204 free(normal); | |
205 } | |
206 | |
207 /** | |
208 * SceneGraph ID から生成された、コピー SceneGraph なので | |
209 * polygon data は削除しない。オリジナルの方で削除する。 | |
210 */ | |
211 void | |
748 | 212 SceneGraph::finalize_copy() |
507 | 213 { |
214 } | |
215 | |
216 | |
217 /** | |
218 * add Children | |
219 * 親の登録と、brother のリストへ加える | |
220 * | |
221 * @param child new child | |
222 */ | |
223 SceneGraphPtr | |
224 SceneGraph::addChild(SceneGraphPtr child) | |
225 { | |
226 /* childrenのリストの最後に加える (brother として)*/ | |
227 if (this->lastChild != NULL) { | |
228 SceneGraphPtr last = this->lastChild; | |
229 last->brother = child; | |
230 } | |
231 | |
232 this->lastChild = child; | |
233 | |
234 if (this->children == NULL) { | |
235 this->children = child; | |
236 } | |
237 | |
238 child->parent = this; | |
239 | |
240 return child; | |
241 } | |
242 | |
243 | |
244 /** | |
245 * add Brother | |
246 * addChild() でも brother の操作をしないといけないので、そっちに回す | |
247 * | |
248 * @param bro new Brother | |
249 */ | |
250 SceneGraphPtr | |
251 SceneGraph::addBrother(SceneGraphPtr bro) | |
252 { | |
253 if (this->parent) { | |
254 parent->addChild(bro); | |
255 } else { | |
256 fprintf(stderr, "error : SceneGraph::%s : %s doesn't have parent\n", | |
257 __FUNCTION__, this->name); | |
258 } | |
259 | |
260 return bro; | |
261 } | |
262 | |
263 /* thisの子や子孫にnameのものが存在すればそいつを返す なければNULL. */ | |
264 SceneGraphPtr | |
265 SceneGraph::searchSceneGraph(const char *name) | |
266 { | |
267 SceneGraphPtr tmp; | |
268 SceneGraphPtr result; | |
269 | |
270 /* 本人か */ | |
271 if( 0==strcmp(this->name, name) ) return this; | |
272 | |
273 /* 子供から再帰的に探す */ | |
274 for(tmp = this->children; tmp; tmp = tmp->next) { | |
275 if ((result=tmp->searchSceneGraph(name)) != NULL) | |
276 return result; | |
277 } | |
278 | |
279 /* 無かったら NULL. */ | |
280 return NULL; | |
281 } | |
282 | |
283 void | |
748 | 284 SceneGraph::tree_check() |
507 | 285 { |
286 SceneGraphPtr t = this; | |
287 | |
288 while(t) | |
289 { | |
290 cout << "my_name : " << t->name << endl; | |
291 if(t->children != NULL) | |
292 { | |
293 cout << "--move children : " << t->children->name << endl; | |
294 t = t->children; | |
295 } | |
296 else if(t->brother != NULL) | |
297 { | |
298 cout << "--move brother : " << t->brother->name << endl; | |
299 t = t->brother; | |
300 } | |
301 else | |
302 { | |
303 while(t) | |
304 { | |
305 if(t->brother != NULL) | |
306 { | |
307 cout << "--move brother : " << t->brother->name << endl; | |
308 t = t->brother; | |
309 break; | |
310 } | |
311 else | |
312 { | |
313 if(t->parent) | |
314 { | |
315 cout << "--move parent : " << t->parent->name << endl; | |
316 } | |
317 t = t->parent; | |
318 } | |
319 } | |
320 } | |
321 } | |
322 } | |
323 | |
324 | |
325 void | |
748 | 326 SceneGraph::print_member() |
507 | 327 { |
328 cout << "size = " << size << endl; | |
329 cout << "name = " << name << endl; | |
330 cout << "parent_name = " << parent_name << endl; | |
331 | |
332 if (parent != NULL) { | |
333 cout << "parent->name = " << parent->name << endl; | |
334 } | |
335 | |
336 if (children != NULL) { | |
337 cout << "children->name = " << children->name << endl; | |
338 } | |
339 } | |
340 | |
341 | |
342 /* | |
343 * surface nodeからポリゴンの情報を読み出す 再帰しない | |
344 */ | |
345 void | |
346 SceneGraph::get_data(TaskManager *manager, xmlNodePtr cur) | |
347 { | |
348 //char *image_name; | |
349 | |
350 for(;cur;cur=cur->next) | |
351 { | |
352 if(!xmlStrcmp(cur->name,(xmlChar*)"coordinate")) | |
353 { | |
354 char *cont = (char *)xmlNodeGetContent(cur); | |
355 pickup_coordinate(cont); | |
356 } | |
357 else if(!xmlStrcmp(cur->name,(xmlChar*)"normal")) | |
358 { | |
359 char *cont = (char *)xmlNodeGetContent(cur); | |
360 pickup_normal(cont); | |
361 } | |
362 else if(!xmlStrcmp(cur->name,(xmlChar*)"model")) | |
363 { | |
364 char *cont = (char *)xmlNodeGetContent(cur); | |
365 pickup_model(cont); | |
366 } | |
367 else if(!xmlStrcmp(cur->name,(xmlChar*)"texture")) | |
368 { | |
369 char *cont = (char *)xmlNodeGetContent(cur); | |
370 pickup_texture(cont); | |
371 } | |
372 else if(!xmlStrcmp(cur->name,(xmlChar*)"imageflag")) | |
373 { | |
374 int id; | |
375 char *filename = (char *)xmlGetProp(cur, (xmlChar *)"name"); | |
376 texture_hash.hash_regist(filename, id); | |
377 } | |
378 else if(!xmlStrcmp(cur->name,(xmlChar*)"image")) | |
379 { | |
380 get_image(manager, cur); | |
381 } | |
382 } | |
383 } | |
384 | |
385 SDL_Surface* | |
386 SceneGraph::load_decode_image(char *image_name, xmlNodePtr cur) | |
387 { | |
388 int fd = mkstemp(image_name); | |
389 FILE *outfile = fdopen(fd, "wb"); | |
390 | |
391 if (NULL == outfile) { | |
392 cout << "error open file\n"; | |
393 return 0; | |
394 } | |
395 | |
396 char *cont = (char *)xmlNodeGetContent(cur); | |
397 //decode(cont, image_name); | |
398 decode(cont, outfile); | |
399 fclose(outfile); | |
400 | |
401 | |
402 /** | |
403 * image を 32bit(RGBA) に変換する | |
404 */ | |
405 SDL_Surface *texture_image = IMG_Load(image_name); | |
580
da82a47ece92
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
406 if (!texture_image) return 0; |
507 | 407 SDL_Surface *tmpImage |
408 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w, | |
409 texture_image->h, 32, redMask, | |
410 greenMask, blueMask, alphaMask); | |
411 SDL_Surface *converted; | |
412 converted = SDL_ConvertSurface(texture_image, tmpImage->format, | |
413 SDL_HWSURFACE); | |
414 if (converted != NULL) { | |
415 SDL_FreeSurface(texture_image); | |
416 texture_image = converted; | |
417 } | |
418 | |
419 return texture_image; | |
420 } | |
421 | |
422 int | |
423 SceneGraph::makeTapestries(TaskManager *manager, SDL_Surface *texture_image, int id) { | |
424 uint32 *tapestry; | |
425 int scale = 1; | |
426 int tex_w = texture_image->w; | |
427 int tex_h = texture_image->h; | |
428 int all_pixel_num = 0; | |
429 | |
430 /** | |
431 * テクスチャの w or h が 8 pixel で分割できる間、 | |
432 * 1/2 の縮小画像を作る。 | |
433 * ここでは、最大の scale (1/scale) を見つける | |
434 * | |
435 * (ex) | |
436 * (128,128) => 64,64 : 32,32: 16,16 : 8,8 | |
437 * scale = 16 | |
438 * (128, 64) => 64,32 : 32,16: 16,8 | |
439 * scale = 8 | |
440 */ | |
441 while (tex_w % TEXTURE_SPLIT_PIXEL == 0 && | |
442 tex_h % TEXTURE_SPLIT_PIXEL == 0) { | |
443 all_pixel_num += tex_w*tex_h; | |
444 tex_w >>= 1; /* tex_w /= 2 */ | |
445 tex_h >>= 1; | |
446 scale <<= 1; /* scale *= 2 */ | |
447 } | |
448 | |
449 scale >>= 1; | |
450 | |
451 tapestry = makeTapestry(manager, texture_image->w, texture_image->h, | |
452 (uint32*)texture_image->pixels, | |
453 all_pixel_num, scale); | |
454 | |
455 list[id].t_w = texture_image->w; | |
456 list[id].t_h = texture_image->h; | |
457 list[id].pixels_orig = (Uint32*)texture_image->pixels; | |
458 list[id].pixels = tapestry; | |
459 list[id].scale_max = scale; | |
460 | |
461 return id; | |
462 } | |
463 | |
464 void | |
465 SceneGraph::get_image(TaskManager *manager, xmlNodePtr cur) | |
466 { | |
467 char image_name[20] = "/tmp/image_XXXXXX"; | |
468 char *filename = (char *)xmlGetProp(cur, (xmlChar *)"name"); | |
469 | |
470 if (filename == NULL || filename[0] == 0) { | |
471 return; | |
472 } | |
473 | |
474 /** | |
475 * image_name を既に Load していれば何もしない | |
476 */ | |
477 int tex_id; | |
478 if (!texture_hash.hash_regist(filename, tex_id)) { | |
479 | |
480 SDL_Surface *texture_image = load_decode_image(image_name, cur); | |
580
da82a47ece92
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
481 if (texture_image==0) { |
da82a47ece92
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
482 printf("Can't load image %s\n",filename); |
da82a47ece92
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
483 exit(0); |
da82a47ece92
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
484 } |
507 | 485 |
486 texture_id = makeTapestries(manager, texture_image, tex_id); | |
487 | |
488 if (unlink(image_name)) { | |
489 cout << "unlink error\n"; | |
490 } | |
491 } else { | |
492 /** | |
493 * 以前に Load されている Texture を共用 | |
494 */ | |
495 texture_id = tex_id; | |
496 } | |
497 | |
498 // こんなことすると list[] のいみあるのかなーと | |
499 // 微妙に思う、自分で書き換えた感想 by gongo | |
500 texture_info.t_w = list[texture_id].t_w; | |
501 texture_info.t_h = list[texture_id].t_h;; | |
502 texture_info.pixels_orig = list[texture_id].pixels_orig; | |
503 texture_info.pixels = list[texture_id].pixels; | |
504 texture_info.scale_max = list[texture_id].scale_max; | |
505 } | |
506 | |
507 | |
508 void | |
748 | 509 SceneGraph::delete_data() |
507 | 510 { |
511 SceneGraphPtr n = this->next, m; | |
512 | |
513 //n = this; | |
514 //delete [] n->data; | |
515 | |
516 if (next) { | |
517 while (n) { | |
518 m = n->next; | |
519 delete n; | |
520 n = m; | |
521 } | |
522 } | |
523 } | |
524 | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
525 /* move_func 実行 sgroot 渡す */ |
507 | 526 void |
527 SceneGraph::move_execute(int w, int h) | |
528 { | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
529 (*move)(this, this->sgroot, w, h); |
507 | 530 } |
531 | |
532 void | |
533 SceneGraph::collision_check(int w, int h, SceneGraphPtr tree) | |
534 { | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
535 (*collision)(this, this->sgroot, w, h, tree); |
507 | 536 } |
537 | |
538 | |
539 void | |
540 SceneGraph::set_move_collision(move_func new_move, | |
541 collision_func new_collision) | |
542 { | |
543 this->move = new_move; | |
544 this->collision = new_collision; | |
545 } | |
546 | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
547 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
548 void |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
549 SceneGraph::set_move_collision(move_func new_move, |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
550 collision_func new_collision, void *sgroot_) |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
551 { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
552 this->move = new_move; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
553 this->collision = new_collision; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
554 // add |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
555 this->sgroot = sgroot_; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
556 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
580
diff
changeset
|
557 |
507 | 558 void |
559 SceneGraph::add_next(SceneGraphPtr next) | |
560 { | |
561 /* next のリストの最後に加える */ | |
562 if (this->next != NULL) { | |
563 SceneGraphPtr tmp = this->last; | |
564 tmp->next = next; | |
565 } else { | |
566 this->next = next; | |
567 } | |
568 | |
569 this->last = next; | |
570 } | |
571 | |
572 /** | |
573 * SceneGraph の clone | |
574 * @return clone SceneGraph | |
575 */ | |
576 SceneGraphPtr | |
748 | 577 SceneGraph::clone() { |
507 | 578 SceneGraphPtr p = new SceneGraph(this); |
579 return p; | |
580 } | |
581 | |
582 /** | |
583 * SceneGraph の clone | |
584 * 予め allocate されてる領域への placement new を行う | |
585 * | |
586 * @param buf clone 領域 | |
587 * @return clone SceneGraph | |
588 */ | |
589 SceneGraphPtr | |
590 SceneGraph::clone(void *buf) { | |
591 SceneGraphPtr p = new(buf) SceneGraph(this); | |
592 return p; | |
593 } | |
594 | |
595 void | |
748 | 596 SceneGraph::remove() |
507 | 597 { |
598 this->flag_remove = 1; | |
599 } | |
600 | |
601 /** | |
602 * tree から node を削除する | |
603 * | |
604 * @param tree SceneGraphTree | |
605 * @return node削除後の SceneGraphTree | |
606 */ | |
607 SceneGraphPtr | |
608 SceneGraph::realRemoveFromTree(SceneGraphPtr tree) | |
609 { | |
610 SceneGraphPtr node = this; | |
611 SceneGraphPtr parent = node->parent; | |
612 SceneGraphPtr ret = tree; | |
613 | |
614 if (parent) { | |
615 SceneGraphPtr brother = parent->children; | |
616 SceneGraphPtr p, p1 = NULL; | |
617 | |
618 p = brother; | |
619 if (p) { | |
620 if (p == node) { | |
621 parent->children = NULL; | |
622 parent->lastChild = NULL; | |
623 } else { | |
624 p1 = p->brother; | |
625 | |
626 while (p1 && p1 != node) { | |
627 p1 = p1->brother; | |
628 p = p->brother; | |
629 } | |
630 | |
631 if (p1) { | |
632 p->brother = p1->brother; | |
633 | |
634 // node が最後尾なら、lastChild を変更 | |
635 if (parent->lastChild == p1) { | |
636 parent->lastChild = p; | |
637 } | |
638 } else { | |
639 // Can't find remove node | |
640 } | |
641 } | |
642 } | |
643 } else { | |
644 // 親が居ない = tree root なので | |
645 // NULL を返す | |
646 ret = NULL; | |
647 } | |
648 | |
649 return ret; | |
650 } | |
651 | |
652 /** | |
653 * list から node を削除する | |
654 * | |
655 * @param list SceneGraphList | |
656 * @return node削除後の SceneGraphList | |
657 */ | |
658 SceneGraphPtr | |
659 SceneGraph::realRemoveFromList(SceneGraphPtr list) | |
660 { | |
661 SceneGraphPtr node = this; | |
662 SceneGraphPtr prev = node->prev; | |
663 SceneGraphPtr next = node->next; | |
664 SceneGraphPtr ret = list; | |
665 | |
666 if (prev) { | |
667 prev->next = next; | |
668 } else { | |
669 ret = next; | |
670 } | |
671 | |
672 if (next) { | |
673 next->prev = prev; | |
674 } | |
675 | |
676 return ret; | |
677 } | |
678 | |
679 int | |
748 | 680 SceneGraph::isRemoved() |
507 | 681 { |
682 return flag_remove; | |
683 } | |
684 | |
685 /** | |
686 * 平行移動 | |
687 * | |
688 * @param x Ttranslate in the x direction | |
689 * @param y Ttranslate in the y direction | |
690 * @param z Ttranslate in the z direction | |
691 */ | |
692 void | |
693 SceneGraph::translate(float x, float y, float z) | |
694 { | |
695 this->xyz[0] = x; | |
696 this->xyz[1] = y; | |
697 this->xyz[2] = z; | |
698 } | |
699 | |
700 /** | |
701 * x 軸方向への平行移動 | |
702 * | |
703 * @param x Ttranslate in the x direction | |
704 */ | |
705 void | |
706 SceneGraph::translateX(float x) | |
707 { | |
708 this->xyz[0] = x; | |
709 } | |
710 | |
711 /** | |
712 * y 軸方向への平行移動 | |
713 * | |
714 * @param y Ttranslate in the y direction | |
715 */ | |
716 void | |
717 SceneGraph::translateY(float y) | |
718 { | |
719 this->xyz[1] = y; | |
720 } | |
721 | |
722 /** | |
723 * z 軸方向への平行移動 | |
724 * | |
725 * @param z Ttranslate in the z direction | |
726 */ | |
727 void | |
728 SceneGraph::translateZ(float z) | |
729 { | |
730 this->xyz[2] = z; | |
731 } | |
748 | 732 |
733 /* end */ |