comparison Renderer/Engine/SceneGraph.cc @ 507:735f76483bb2

Reorganization..
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 12 Oct 2009 09:39:35 +0900
parents
children da82a47ece92
comparison
equal deleted inserted replaced
506:1d4a8a86f26b 507:735f76483bb2
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
24 no_move(SceneGraphPtr self, int screen_w, int screen_h) {}
25
26 static void
27 no_collision(SceneGraphPtr self, int screen_w, int screen_h,
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 */
98 SceneGraph::SceneGraph(void)
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
153 SceneGraph::init(void)
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
189 SceneGraph::~SceneGraph(void)
190 {
191 (this->*finalize)();
192 }
193
194 /**
195 * xml ファイルから生成されたオリジナル SceneGraph なので
196 * polygon data を削除
197 */
198 void
199 SceneGraph::finalize_original(void)
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
212 SceneGraph::finalize_copy(void)
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
284 SceneGraph::tree_check(void)
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
326 SceneGraph::print_member(void)
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);
406 SDL_Surface *tmpImage
407 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w,
408 texture_image->h, 32, redMask,
409 greenMask, blueMask, alphaMask);
410 SDL_Surface *converted;
411 converted = SDL_ConvertSurface(texture_image, tmpImage->format,
412 SDL_HWSURFACE);
413 if (converted != NULL) {
414 SDL_FreeSurface(texture_image);
415 texture_image = converted;
416 }
417
418 return texture_image;
419 }
420
421 int
422 SceneGraph::makeTapestries(TaskManager *manager, SDL_Surface *texture_image, int id) {
423 uint32 *tapestry;
424 int scale = 1;
425 int tex_w = texture_image->w;
426 int tex_h = texture_image->h;
427 int all_pixel_num = 0;
428
429 /**
430 * テクスチャの w or h が 8 pixel で分割できる間、
431 * 1/2 の縮小画像を作る。
432 * ここでは、最大の scale (1/scale) を見つける
433 *
434 * (ex)
435 * (128,128) => 64,64 : 32,32: 16,16 : 8,8
436 * scale = 16
437 * (128, 64) => 64,32 : 32,16: 16,8
438 * scale = 8
439 */
440 while (tex_w % TEXTURE_SPLIT_PIXEL == 0 &&
441 tex_h % TEXTURE_SPLIT_PIXEL == 0) {
442 all_pixel_num += tex_w*tex_h;
443 tex_w >>= 1; /* tex_w /= 2 */
444 tex_h >>= 1;
445 scale <<= 1; /* scale *= 2 */
446 }
447
448 scale >>= 1;
449
450 tapestry = makeTapestry(manager, texture_image->w, texture_image->h,
451 (uint32*)texture_image->pixels,
452 all_pixel_num, scale);
453
454 list[id].t_w = texture_image->w;
455 list[id].t_h = texture_image->h;
456 list[id].pixels_orig = (Uint32*)texture_image->pixels;
457 list[id].pixels = tapestry;
458 list[id].scale_max = scale;
459
460 return id;
461 }
462
463 void
464 SceneGraph::get_image(TaskManager *manager, xmlNodePtr cur)
465 {
466 char image_name[20] = "/tmp/image_XXXXXX";
467 char *filename = (char *)xmlGetProp(cur, (xmlChar *)"name");
468
469 if (filename == NULL || filename[0] == 0) {
470 return;
471 }
472
473 /**
474 * image_name を既に Load していれば何もしない
475 */
476 int tex_id;
477 if (!texture_hash.hash_regist(filename, tex_id)) {
478
479 SDL_Surface *texture_image = load_decode_image(image_name, cur);
480
481 texture_id = makeTapestries(manager, texture_image, tex_id);
482
483 if (unlink(image_name)) {
484 cout << "unlink error\n";
485 }
486 } else {
487 /**
488 * 以前に Load されている Texture を共用
489 */
490 texture_id = tex_id;
491 }
492
493 // こんなことすると list[] のいみあるのかなーと
494 // 微妙に思う、自分で書き換えた感想 by gongo
495 texture_info.t_w = list[texture_id].t_w;
496 texture_info.t_h = list[texture_id].t_h;;
497 texture_info.pixels_orig = list[texture_id].pixels_orig;
498 texture_info.pixels = list[texture_id].pixels;
499 texture_info.scale_max = list[texture_id].scale_max;
500 }
501
502
503 void
504 SceneGraph::delete_data(void)
505 {
506 SceneGraphPtr n = this->next, m;
507
508 //n = this;
509 //delete [] n->data;
510
511 if (next) {
512 while (n) {
513 m = n->next;
514 delete n;
515 n = m;
516 }
517 }
518 }
519
520 void
521 SceneGraph::move_execute(int w, int h)
522 {
523 (*move)(this, w, h);
524 }
525
526 void
527 SceneGraph::collision_check(int w, int h, SceneGraphPtr tree)
528 {
529 (*collision)(this, w, h, tree);
530 }
531
532 void
533 SceneGraph::all_execute(int screen_w, int screen_h)
534 {
535 SceneGraphPtr top = this;
536 SceneGraphPtr t = top;
537
538 while (t) {
539 t->move_execute(screen_w, screen_h);
540 t->collision_check(screen_w, screen_h, top);
541
542 t->frame++;
543
544 if (t->parent != NULL) {
545 get_matrix(t->matrix, t->angle, t->xyz, t->parent->matrix);
546 } else {
547 get_matrix(t->matrix, t->angle, t->xyz, NULL);
548 }
549
550 if (t->children != NULL) {
551 t = t->children;
552 } else if (t->brother != NULL) {
553 t = t->brother;
554 } else {
555 while (t) {
556 if (t->brother != NULL) {
557 t = t->brother;
558 break;
559 } else {
560 if (t->parent == NULL) {
561 t = NULL;
562 break;
563 } else {
564 t = t->parent;
565 }
566 }
567 }
568 }
569 }
570 }
571
572 void
573 SceneGraph::set_move_collision(SceneGraphPtr node, move_func new_move,
574 collision_func new_collision)
575 {
576 node->move = new_move;
577 node->collision = new_collision;
578 }
579
580 void
581 SceneGraph::set_move_collision(move_func new_move,
582 collision_func new_collision)
583 {
584 this->move = new_move;
585 this->collision = new_collision;
586 }
587
588 void
589 SceneGraph::add_next(SceneGraphPtr next)
590 {
591 /* next のリストの最後に加える */
592 if (this->next != NULL) {
593 SceneGraphPtr tmp = this->last;
594 tmp->next = next;
595 } else {
596 this->next = next;
597 }
598
599 this->last = next;
600 }
601
602 /**
603 * SceneGraph の clone
604 * @return clone SceneGraph
605 */
606 SceneGraphPtr
607 SceneGraph::clone(void) {
608 SceneGraphPtr p = new SceneGraph(this);
609 return p;
610 }
611
612 /**
613 * SceneGraph の clone
614 * 予め allocate されてる領域への placement new を行う
615 *
616 * @param buf clone 領域
617 * @return clone SceneGraph
618 */
619 SceneGraphPtr
620 SceneGraph::clone(void *buf) {
621 SceneGraphPtr p = new(buf) SceneGraph(this);
622 return p;
623 }
624
625 void
626 SceneGraph::remove(void)
627 {
628 this->flag_remove = 1;
629 }
630
631 /**
632 * tree から node を削除する
633 *
634 * @param tree SceneGraphTree
635 * @return node削除後の SceneGraphTree
636 */
637 SceneGraphPtr
638 SceneGraph::realRemoveFromTree(SceneGraphPtr tree)
639 {
640 SceneGraphPtr node = this;
641 SceneGraphPtr parent = node->parent;
642 SceneGraphPtr ret = tree;
643
644 if (parent) {
645 SceneGraphPtr brother = parent->children;
646 SceneGraphPtr p, p1 = NULL;
647
648 p = brother;
649 if (p) {
650 if (p == node) {
651 parent->children = NULL;
652 parent->lastChild = NULL;
653 } else {
654 p1 = p->brother;
655
656 while (p1 && p1 != node) {
657 p1 = p1->brother;
658 p = p->brother;
659 }
660
661 if (p1) {
662 p->brother = p1->brother;
663
664 // node が最後尾なら、lastChild を変更
665 if (parent->lastChild == p1) {
666 parent->lastChild = p;
667 }
668 } else {
669 // Can't find remove node
670 }
671 }
672 }
673 } else {
674 // 親が居ない = tree root なので
675 // NULL を返す
676 ret = NULL;
677 }
678
679 return ret;
680 }
681
682 /**
683 * list から node を削除する
684 *
685 * @param list SceneGraphList
686 * @return node削除後の SceneGraphList
687 */
688 SceneGraphPtr
689 SceneGraph::realRemoveFromList(SceneGraphPtr list)
690 {
691 SceneGraphPtr node = this;
692 SceneGraphPtr prev = node->prev;
693 SceneGraphPtr next = node->next;
694 SceneGraphPtr ret = list;
695
696 if (prev) {
697 prev->next = next;
698 } else {
699 ret = next;
700 }
701
702 if (next) {
703 next->prev = prev;
704 }
705
706 return ret;
707 }
708
709 int
710 SceneGraph::isRemoved(void)
711 {
712 return flag_remove;
713 }
714
715 /**
716 * 平行移動
717 *
718 * @param x Ttranslate in the x direction
719 * @param y Ttranslate in the y direction
720 * @param z Ttranslate in the z direction
721 */
722 void
723 SceneGraph::translate(float x, float y, float z)
724 {
725 this->xyz[0] = x;
726 this->xyz[1] = y;
727 this->xyz[2] = z;
728 }
729
730 /**
731 * x 軸方向への平行移動
732 *
733 * @param x Ttranslate in the x direction
734 */
735 void
736 SceneGraph::translateX(float x)
737 {
738 this->xyz[0] = x;
739 }
740
741 /**
742 * y 軸方向への平行移動
743 *
744 * @param y Ttranslate in the y direction
745 */
746 void
747 SceneGraph::translateY(float y)
748 {
749 this->xyz[1] = y;
750 }
751
752 /**
753 * z 軸方向への平行移動
754 *
755 * @param z Ttranslate in the z direction
756 */
757 void
758 SceneGraph::translateZ(float z)
759 {
760 this->xyz[2] = z;
761 }