comparison TaskManager/Test/test_render/polygon.cpp @ 137:91c74dbc32e4 draft

SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
author gongo@gendarme.cr.ie.u-ryukyu.ac.jp
date Fri, 28 Nov 2008 10:07:48 +0900
parents 40978d4b608a
children f35504025f73
comparison
equal deleted inserted replaced
136:ba896af69945 137:91c74dbc32e4
20 extern int decode(char *cont, FILE *outfile); 20 extern int decode(char *cont, FILE *outfile);
21 21
22 22
23 SDL_Surface* Polygon::texture_image; 23 SDL_Surface* Polygon::texture_image;
24 24
25 Polygon::Polygon() 25 Polygon::Polygon(void)
26 { 26 {
27 xyz[0] = 0; 27 xyz[0] = 0;
28 xyz[1] = 0; 28 xyz[1] = 0;
29 xyz[2] = 0; 29 xyz[2] = 0;
30 xyz[3] = 1; 30 xyz[3] = 1;
31 c_xyz[0] = 0; 31 c_xyz[0] = 0;
32 c_xyz[1] = 0; 32 c_xyz[1] = 0;
33 c_xyz[2] = 0; 33 c_xyz[2] = 0;
34 c_xyz[3] = 1; 34 c_xyz[3] = 1;
35 angle[0] = 0; 35 angle[0] = 0;
36 angle[1] = 0; 36 angle[1] = 0;
37 angle[2] = 0; 37 angle[2] = 0;
38 angle[3] = 1; 38 angle[3] = 1;
39 next = NULL; 39
40 parent = NULL; 40 for (int i = 0; i < 16; i++) {
41 brother = NULL; 41 matrix[i] = 0;
42 children = NULL; 42 }
43 lastChild = NULL; 43 }
44 } 44
45 45 #if 0
46
47
48 void Polygon::tree_check()
49 {
50 Polygon *t;
51 t = this;
52
53 while(t)
54 {
55 cout << "my_name : " << t->name << endl;
56 if(t->children != NULL)
57 {
58 cout << "--move children : " << t->children->name << endl;
59 t = t->children;
60 }
61 else if(t->brother != NULL)
62 {
63 cout << "--move brother : " << t->brother->name << endl;
64 t = t->brother;
65 }
66 else
67 {
68 while(t)
69 {
70 if(t->brother != NULL)
71 {
72 cout << "--move brother : " << t->brother->name << endl;
73 t = t->brother;
74 break;
75 }
76 else
77 {
78 if(t->parent)
79 {
80 cout << "--move parent : " << t->parent->name << endl;
81 }
82 t = t->parent;
83 }
84 }
85 }
86 }
87 }
88
89 void Polygon::print_member()
90 {
91 //int n;
92
93 cout << "size = " << size << endl;
94 cout << "name = " << name << endl;
95 cout << "parent_name = " << parent_name << endl;
96 /*
97 for(n=0; n<size*3; n+=3)
98 {
99 cout<<"coordinate = "<<data[n]<<" "<<data[n+1]<<" "<< data[n+2]<<endl;
100 }
101 */
102 /*
103 for(n=size*3; n<size*6; n+=3)
104 {
105 cout<<"narmal = "<<data[n]<<" "<<data[n+1]<<" "<<data[n+2]<<endl;
106 }
107 */
108 cout<<"c_xyz = "<<c_xyz[0]<<" "<<c_xyz[1]<<" "<<c_xyz[2]<<endl;
109 /*
110 for(n=size*6; n<size*9; n+=3)
111 {
112 cout<<"texture = "<<data[n]<<" "<<data[n+1]<<" "<<data[n+2]<<endl;
113 }
114 */
115 /*
116 for(n=0; n<size*3; n+=3)
117 {
118 cout<<"coordinate = "<<data[n]<<" "<<data[n+1]<<" "<<data[n+2]<<endl;
119 cout<<"normal = "<<data[n+size*3]<<" "<<data[n+size*3+1]<<" "<<data[n+size*3+2]<<endl;
120 cout<<"texture = "<<data[n+size*6]<<" "<<1-data[n+size*6+1]<<endl;
121 }
122 */
123 //cout << "image_name = " << image_name << endl;
124 if(parent != NULL)
125 {
126 cout << "parent->name = " << parent->name << endl;
127 }
128 if(children != NULL)
129 {
130 cout << "children->name = " << children->name << endl;
131 }
132 }
133
134 void Polygon::draw(SceneGraphPack *sgp) 46 void Polygon::draw(SceneGraphPack *sgp)
135 { 47 {
136 float xyz1[4],xyz2[4],xyz3[4]; 48 float xyz1[4],xyz2[4],xyz3[4];
137 49
138 /***SceneGraphUpdate***/ 50 /***SceneGraphUpdate***/
139 //sgp_update(); 51 //sgp_update();
140 for (int i = 0; i < sgp->info.size; i++) { 52 for (int i = 0; i < sgp->info.size; i++) {
141 SceneGraphNode node = sgp->node[i]; 53 SceneGraphNode node = sgp->node[i];
142 54
143 /***draw***/ 55 /***draw***/
144 int n,nt; 56 int n,nt;
145 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) { 57 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) {
146 xyz1[0] = node.vertex[n]; 58 xyz1[0] = node.vertex[n];
147 xyz1[1] = node.vertex[n+1]; 59 xyz1[1] = node.vertex[n+1];
148 xyz1[2] = node.vertex[n+2]*-1; 60 xyz1[2] = node.vertex[n+2]*-1;
149 xyz1[3] = 1; 61 xyz1[3] = 1;
150 xyz2[0] = node.vertex[n+3]; 62 xyz2[0] = node.vertex[n+3];
151 xyz2[1] = node.vertex[n+3+1]; 63 xyz2[1] = node.vertex[n+3+1];
152 xyz2[2] = node.vertex[n+3+2]*-1; 64 xyz2[2] = node.vertex[n+3+2]*-1;
153 xyz2[3] = 1; 65 xyz2[3] = 1;
154 xyz3[0] = node.vertex[n+6]; 66 xyz3[0] = node.vertex[n+6];
155 xyz3[1] = node.vertex[n+6+1]; 67 xyz3[1] = node.vertex[n+6+1];
156 xyz3[2] = node.vertex[n+6+2]*-1; 68 xyz3[2] = node.vertex[n+6+2]*-1;
157 xyz3[3] = 1; 69 xyz3[3] = 1;
158 70
159 rotate(xyz1, node.translation); 71 rotate(xyz1, node.translation);
160 rotate(xyz2, node.translation); 72 rotate(xyz2, node.translation);
161 rotate(xyz3, node.translation); 73 rotate(xyz3, node.translation);
162 74
163 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]); 75 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]);
164 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]); 76 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]);
165 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]); 77 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]);
166 78
167 Triangle *tri = new Triangle(ver1,ver2,ver3); 79 Triangle *tri = new Triangle(ver1,ver2,ver3);
168 Span_c *span = new Span_c(); 80 Span_c *span = new Span_c();
169 span->viewer = viewer; 81 span->viewer = viewer;
170 span->p = this; 82 span->p = this;
171 span->create_span(tri,texture_image); 83 span->create_span(tri,texture_image);
172 delete ver1; 84 delete ver1;
173 delete ver2; 85 delete ver2;
174 delete ver3; 86 delete ver3;
175 delete tri; 87 delete tri;
176 delete span; 88 delete span;
177 } 89 }
178 } 90 }
179 } 91 }
180 92
181 93
182 void Polygon::draw(PolygonPack *pp) 94 void Polygon::draw(PolygonPack *pp)
183 { 95 {
184 for(int n=0; n<pp->info.size; n++) 96 for(int n=0; n<pp->info.size; n++)
185 { 97 {
186 Vertex *ver1 = new Vertex(pp->tri[n].ver1.x,pp->tri[n].ver1.y,pp->tri[n].ver1.z,pp->tri[n].ver1.tex_x,pp->tri[n].ver1.tex_y); 98 Vertex *ver1 = new Vertex(pp->tri[n].ver1.x,pp->tri[n].ver1.y,pp->tri[n].ver1.z,pp->tri[n].ver1.tex_x,pp->tri[n].ver1.tex_y);
187 Vertex *ver2 = new Vertex(pp->tri[n].ver2.x,pp->tri[n].ver2.y,pp->tri[n].ver2.z,pp->tri[n].ver2.tex_x,pp->tri[n].ver2.tex_y); 99 Vertex *ver2 = new Vertex(pp->tri[n].ver2.x,pp->tri[n].ver2.y,pp->tri[n].ver2.z,pp->tri[n].ver2.tex_x,pp->tri[n].ver2.tex_y);
188 Vertex *ver3 = new Vertex(pp->tri[n].ver3.x,pp->tri[n].ver3.y,pp->tri[n].ver3.z,pp->tri[n].ver3.tex_x,pp->tri[n].ver3.tex_y); 100 Vertex *ver3 = new Vertex(pp->tri[n].ver3.x,pp->tri[n].ver3.y,pp->tri[n].ver3.z,pp->tri[n].ver3.tex_x,pp->tri[n].ver3.tex_y);
189 101
190 Triangle *tri = new Triangle(ver1,ver2,ver3); 102 Triangle *tri = new Triangle(ver1,ver2,ver3);
191 Span_c *span = new Span_c(); 103 Span_c *span = new Span_c();
192 span->viewer = viewer; 104 span->viewer = viewer;
193 span->p = this; 105 span->p = this;
194 span->create_span(tri,texture_image); 106 span->create_span(tri,texture_image);
195 delete ver1; 107 delete ver1;
196 delete ver2; 108 delete ver2;
197 delete ver3; 109 delete ver3;
198 delete tri; 110 delete tri;
199 delete span; 111 delete span;
200 } 112 }
201 } 113 }
202 114
203 void Polygon::draw(SpanPack *sp) 115 void Polygon::draw(SpanPack *sp)
204 { 116 {
242 } 154 }
243 } 155 }
244 } 156 }
245 } 157 }
246 158
247 void Polygon::add_next() 159 #endif
248 {
249 Polygon *p;
250 p = new Polygon;
251
252 next = p;
253 }
254
255
256 void Polygon::create_tree()
257 {
258 Polygon *list;
259 Polygon *check_list;
260 Polygon *p;
261 Polygon *bros;
262
263 check_list = this;
264
265 for (list=this; list; list=list->next) {
266 if (xmlStrcmp((xmlChar *)list->parent_name, (xmlChar *)"NULL")) {
267 p = this;
268
269 while (p) {
270 if (!xmlStrcmp((xmlChar *)p->name,(xmlChar *)list->parent_name))
271 {
272 list->parent = p;
273 if(p->children == NULL)
274 {
275 p->children = list;
276 }
277 else
278 {
279 bros = p->children;
280 while(bros->brother != NULL)
281 {
282 bros = bros->brother;
283 }
284 bros->brother = list;
285 }
286 break;
287 }
288 p = p->next;
289 }
290 }
291 }
292 }
293 160
294 void Polygon::pickup_coordinate(char *cont) 161 void Polygon::pickup_coordinate(char *cont)
295 { 162 {
296 for(int n=0; n<size*3; n+=3) 163 for(int n=0; n<size*3; n+=3)
297 { 164 {
298 cont = pickup_float(cont, data+n); 165 cont = pickup_float(cont, data+n);
299 cont = pickup_float(cont, data+n+1); 166 cont = pickup_float(cont, data+n+1);
300 cont = pickup_float(cont, data+n+2); 167 cont = pickup_float(cont, data+n+2);
301 168
302 if (cont == NULL) 169 if (cont == NULL)
303 { 170 {
304 cout << "Analyzing obj data failed coordinate\n"; 171 cout << "Analyzing obj data failed coordinate\n";
305 } 172 }
306 } 173 }
307 } 174 }
308 175
309 void Polygon::pickup_normal(char *cont) 176 void Polygon::pickup_normal(char *cont)
310 { 177 {
311 for(int n=size*3;n<size*6;n+=3) 178 for(int n=size*3;n<size*6;n+=3)
312 { 179 {
313 cont = pickup_float(cont,data+n); 180 cont = pickup_float(cont,data+n);
314 cont = pickup_float(cont,data+n+1); 181 cont = pickup_float(cont,data+n+1);
315 cont = pickup_float(cont,data+n+2); 182 cont = pickup_float(cont,data+n+2);
316 183
317 if (cont == NULL) 184 if (cont == NULL)
318 { 185 {
319 cout << "Analyzing obj data failed normal\n"; 186 cout << "Analyzing obj data failed normal\n";
320 } 187 }
321 } 188 }
322 } 189 }
323 190
324 void Polygon::pickup_model(char *cont) 191 void Polygon::pickup_model(char *cont)
325 { 192 {
326 cont = pickup_float(cont,c_xyz); 193 cont = pickup_float(cont,c_xyz);
327 cont = pickup_float(cont,c_xyz+1); 194 cont = pickup_float(cont,c_xyz+1);
328 cont = pickup_float(cont,c_xyz+2); 195 cont = pickup_float(cont,c_xyz+2);
329 196
330 if (cont == NULL) 197 if (cont == NULL)
331 { 198 {
332 cout << "Analyzing obj data failed model\n"; 199 cout << "Analyzing obj data failed model\n";
333 } 200 }
334 } 201 }
335 202
336 void Polygon::pickup_texture(char *cont) 203 void Polygon::pickup_texture(char *cont)
337 { 204 {
338 for(int n=size*6; n<size*9; n+=3) 205 for(int n=size*6; n<size*9; n+=3)
339 { 206 {
340 cont = pickup_float(cont,data+n); 207 cont = pickup_float(cont,data+n);
341 cont = pickup_float(cont,data+n+1); 208 cont = pickup_float(cont,data+n+1);
342 data[n+2] = 1.0; 209 data[n+2] = 1.0;
343 210
344 if (cont == NULL) 211 if (cont == NULL)
345 { 212 {
346 cout << "Analyzing obj data failed texture\n"; 213 cout << "Analyzing obj data failed texture\n";
347 } 214 }
348 } 215 }
349 } 216 }
350 217
351 char *get_pixel(int tx, int ty, SDL_Surface *texture_image) 218 char *get_pixel(int tx, int ty, SDL_Surface *texture_image)
352 { 219 {
353 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx)); 220 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx));
354 } 221 }
355 222
356 unsigned my_ntohl(unsigned u) { 223 unsigned my_ntohl(unsigned u) {
357 // rr gg bb 00 224 // rr gg bb 00
358 // rr 225 // rr
359 // bb gg rr 226 // bb gg rr
360 //unsigned u1 = ((u&0xff)<<24) + 227 //unsigned u1 = ((u&0xff)<<24) +
361 // ((u&0xff00)<<8) + 228 // ((u&0xff00)<<8) +
362 // ((u&0xff0000)>>8) + 229 // ((u&0xff0000)>>8) +
363 // ((u&0xff000000)>>24); 230 // ((u&0xff000000)>>24);
364 unsigned u1; 231 unsigned u1;
365 unsigned b = (u&0xff000000)>>24; 232 unsigned b = (u&0xff000000)>>24;
366 unsigned g = (u&0xff0000)>>16; 233 unsigned g = (u&0xff0000)>>16;
367 unsigned r = (u&0xff00)>>8; 234 unsigned r = (u&0xff00)>>8;
368 u1 = r + (g<<8) + (b<<16); 235 u1 = r + (g<<8) + (b<<16);
369 //printf("pixel %x->%x\n",u,u1); 236 //printf("pixel %x->%x\n",u,u1);
370 return u1; 237 return u1;
371 } 238 }
372 239
373 Uint32 Polygon::get_rgb(int tx, int ty) 240 Uint32 Polygon::get_rgb(int tx, int ty)
374 { 241 {
375 SDL_PixelFormat *fmt; 242 SDL_PixelFormat *fmt;
376 //Uint32 temp, pixel; 243 //Uint32 temp, pixel;
377 Uint8 red, green, blue; 244 Uint8 red, green, blue;
378 245
379 fmt = texture_image->format; 246 fmt = texture_image->format;
380 247
381 if (tx<0) tx = 0; 248 if (tx<0) tx = 0;
382 if (texture_image->w-1< tx) tx = texture_image->w-1 ; 249 if (texture_image->w-1< tx) tx = texture_image->w-1 ;
383 if (ty<0) ty = 0; 250 if (ty<0) ty = 0;
384 if (texture_image->h-1< ty) ty = texture_image->h-1 ; 251 if (texture_image->h-1< ty) ty = texture_image->h-1 ;
385 252
386 253
387 254
388 //SDL_LockSurface(texture_image); 255 //SDL_LockSurface(texture_image);
389 char *p = get_pixel(tx,ty,texture_image); 256 char *p = get_pixel(tx,ty,texture_image);
390 #if 0 257 #if 0
391 pixel = my_ntohl(*(Uint32*)p); 258 pixel = my_ntohl(*(Uint32*)p);
392 //printf("pixel = %d\n", pixel); 259 //printf("pixel = %d\n", pixel);
393 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); 260 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel);
394 //SDL_UnlockSurface(texture_image); 261 //SDL_UnlockSurface(texture_image);
395 262
396 temp = pixel&fmt->Rmask; 263 temp = pixel&fmt->Rmask;
397 temp = temp>>fmt->Rshift; 264 temp = temp>>fmt->Rshift;
398 temp = temp<<fmt->Rloss; 265 temp = temp<<fmt->Rloss;
399 red = (Uint8)temp; 266 red = (Uint8)temp;
400 267
401 temp = pixel&fmt->Gmask; 268 temp = pixel&fmt->Gmask;
402 temp = temp>>fmt->Gshift; 269 temp = temp>>fmt->Gshift;
403 temp = temp<<fmt->Gloss; 270 temp = temp<<fmt->Gloss;
404 green = (Uint8)temp; 271 green = (Uint8)temp;
405 272
406 temp = pixel&fmt->Bmask; 273 temp = pixel&fmt->Bmask;
407 temp = temp>>fmt->Bshift; 274 temp = temp>>fmt->Bshift;
408 temp = temp<<fmt->Bloss; 275 temp = temp<<fmt->Bloss;
409 blue = (Uint8)temp; 276 blue = (Uint8)temp;
410 #endif 277 #endif
411 blue = (Uint8) p[0]; 278 blue = (Uint8) p[0];
412 green = (Uint8) p[1]; 279 green = (Uint8) p[1];
413 red = (Uint8) p[2]; 280 red = (Uint8) p[2];
414 281
415 //printf("tx = %d ty = %d ", tx,ty); 282 //printf("tx = %d ty = %d ", tx,ty);
416 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue); 283 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue);
417 284
418 SDL_PixelFormat *pf; 285 SDL_PixelFormat *pf;
419 //pf = viewer->screen->format; 286 //pf = viewer->screen->format;
420 287
421 //cout << SDL_MapRGB(pf, red, green, blue) << endl; 288 //cout << SDL_MapRGB(pf, red, green, blue) << endl;
422 return SDL_MapRGB(pf, red, green, blue); 289 return SDL_MapRGB(pf, red, green, blue);
423 } 290 }
424
425
426 /*
427 * surface nodeからポリゴンの情報を読み出す 再帰しない
428 */
429 void Polygon::get_data(xmlNodePtr cur)
430 {
431 char *cont;
432 //char *image_name;
433
434 for(;cur;cur=cur->next)
435 {
436 if(!xmlStrcmp(cur->name,(xmlChar*)"coordinate"))
437 {
438 cont = (char *)xmlNodeGetContent(cur);
439 pickup_coordinate(cont);
440 }
441 else if(!xmlStrcmp(cur->name,(xmlChar*)"normal"))
442 {
443 cont = (char *)xmlNodeGetContent(cur);
444 pickup_normal(cont);
445 }
446 else if(!xmlStrcmp(cur->name,(xmlChar*)"model"))
447 {
448 cont = (char *)xmlNodeGetContent(cur);
449 pickup_model(cont);
450 }
451 else if(!xmlStrcmp(cur->name,(xmlChar*)"texture"))
452 {
453 cont = (char *)xmlNodeGetContent(cur);
454 pickup_texture(cont);
455 }
456 else if(!xmlStrcmp(cur->name,(xmlChar*)"image"))
457 {
458 char image_name[20] = "/tmp/image_XXXXXX";
459 int fd = mkstemp(image_name);
460 FILE *outfile = fdopen(fd, "wb");
461 if(NULL == outfile)
462 {
463 cout << "error open file\n";
464 }
465 cont = (char *)xmlNodeGetContent(cur);
466 //decode(cont, image_name);
467 decode(cont, outfile);
468 fclose(outfile);
469
470 texture_image = IMG_Load(image_name);
471
472 /**
473 * image を 32bit(RGBA) に変換する
474 */
475 SDL_Surface *tmpImage
476 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w,
477 texture_image->h, 32,
478 redMask, greenMask, blueMask, alphaMask);
479 SDL_Surface *converted;
480 converted = SDL_ConvertSurface(texture_image,
481 tmpImage->format, SDL_HWSURFACE);
482 if( converted != NULL){
483 SDL_FreeSurface(texture_image);
484 texture_image = converted;
485 }
486
487 //load_texture(image_name);
488 if(unlink(image_name))
489 {
490 cout << "unlink error\n";
491 }
492 }
493 }
494 }
495
496
497 /*
498 * ノードを受け取って、そこからポリゴンを生成
499 * リークしてない? data上書きしちゃだめ
500 */
501 void Polygon::create_data(xmlNodePtr cur)
502 {
503 char *p = (char *)xmlGetProp(cur,(xmlChar *)"size");
504 //error 出力?
505
506 #ifndef _DEBUG
507 if(strlen(p)>8){
508 __debug("Polygon size is too large.\n");
509 exit(1);
510 }
511 #endif
512
513 size = atoi((char *)xmlGetProp(cur,(xmlChar *)"size"));
514 name = (char *)xmlGetProp(cur,(xmlChar *)"name");
515 parent_name = (char *)xmlGetProp(cur,(xmlChar *)"parent");
516 next = NULL;
517
518 data = new float[size*3*3];
519
520 get_data(cur->children);
521 }
522
523 /*
524 * xmlをオープンして、Surfaceノードからポリゴンを作る
525 *
526 */
527 void Polygon::set_data(char *file_name)
528 {
529 xmlDocPtr doc;
530 xmlNodePtr cur;
531 Polygon *tmp;
532
533 doc = xmlParseFile(file_name);
534
535 cur = xmlDocGetRootElement(doc);
536
537 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
538
539 tmp = this;
540
541 for (cur=cur->children; cur; cur=cur->next)
542 {
543 if (!xmlStrcmp(cur->name,(xmlChar*)"surface"))
544 {
545 tmp->create_data(cur);
546 if(cur->next->next)
547 {
548 tmp->add_next();
549 tmp = tmp->next;
550 }
551 }
552 }
553
554 create_tree();
555 //tree_check();
556 //create_scene_graph_pack();
557
558 xmlFreeDoc(doc);
559
560 /*
561 for(int s=0; s<sgp->info.size; s++)
562 {
563 printf("sgp->node[%d].pn = %d\n",s,sgp->node[s].pn);
564 }
565 */
566 //delete sgp;
567 }
568
569 void Polygon::delete_data()
570 {
571 Polygon *n,*m;
572
573 n = this;
574 delete [] n->data;
575
576 if(next)
577 {
578 for(n = this->next; n; n=m)
579 {
580 m = n->next;
581 delete [] n->data;
582 delete n;
583 }
584 }
585 }
586
587
588
589 /* construct polygon from xml. */
590 /*
591 Polygon::Polygon(char *xmlfile)
592 {
593 Polygon();
594 xmlDocPtr doc;
595 xmlNodePtr cur;
596 Polygon *tmp;
597
598 doc = xmlParseFile(file_name);
599
600 cur = xmlDocGetRootElement(doc);
601
602 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
603
604 tmp = this;
605
606 for (cur=cur->children; cur; cur=cur->next)
607 {
608 if ( xmlStrcmp(cur->name,(xmlChar*)"surface")!=0 )
609 continue;
610
611 if ( xmlStrcmp(cur->parent ))
612 tmp->create_data(cur);
613 if(cur->next->next)
614 {
615 tmp->add_next();
616 tmp = tmp->next;
617 }
618 }
619
620 create_tree();
621 //tree_check();
622 //create_scene_graph_pack();
623
624 xmlFreeDoc(doc);
625 }
626 */
627
628 /* construct polygon from xmlNode. */
629 Polygon::Polygon(xmlNodePtr surface)
630 {
631 #if 1
632 xyz[0] = 0;
633 xyz[1] = 0;
634 xyz[2] = 0;
635 xyz[3] = 1;
636 c_xyz[0] = 0;
637 c_xyz[1] = 0;
638 c_xyz[2] = 0;
639 c_xyz[3] = 1;
640 angle[0] = 0;
641 angle[1] = 0;
642 angle[2] = 0;
643 angle[3] = 1;
644 next = NULL;
645 parent = NULL;
646 brother = NULL;
647 children = NULL;
648 lastChild = NULL;
649 #else
650 /* これ使うにはどうすればいいの? */
651 this->Polygon();
652 #endif
653 size = atoi((char *)xmlGetProp(surface,(xmlChar *)"size"));
654 name = (char *)xmlGetProp(surface,(xmlChar *)"name");
655 parent_name = (char *)xmlGetProp(surface,(xmlChar *)"parent");
656 next = NULL;
657
658 data = new float[size*3*3];
659
660 get_data(surface->children);
661 }
662
663 /* XMLファイルからポリゴンを作成 */
664 Polygon *Polygon::createFromXMLfile(char *xmlfile)
665 {
666 xmlDocPtr doc;
667 xmlNodePtr cur;
668 Polygon *root=NULL, *tmp, *parent;
669
670 /* パース DOM生成 */
671 doc = xmlParseFile(xmlfile);
672 cur = xmlDocGetRootElement(doc);
673
674 /* ?? */
675 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
676
677 /* XMLのノードを一つずつ解析 */
678 for (cur=cur->children; cur; cur=cur->next)
679 {
680 /* 扱うのはsurfaceオンリー */
681 if ( xmlStrcmp(cur->name,(xmlChar*)"surface")!=0 )
682 continue;
683
684 /* ポリゴン生成 */
685 tmp = new Polygon(cur);
686 if ( tmp->parent_name==NULL || 0==strcmp(tmp->parent_name, "NULL")) {
687 /* このsurfaceがroot */
688 root = tmp;
689 } else {
690 /* 親はこのsurfaceより前に定義されているものとする (していい?) */
691 // ここで parent_name を用いるのは間違っていて、
692 // *cur->properties->children から探すべきらしい kono
693 parent = root->searchPolygon(tmp->parent_name);
694 if (parent==NULL) {
695 fprintf(stderr, "[%s] No such parent %s\n",
696 tmp->name, tmp->parent_name);
697 root->addChild(tmp);
698 } else {
699 parent->addChild(tmp);
700 }
701 }
702 }
703
704 xmlFreeDoc(doc);
705 return root;
706 }
707
708 /* 子供を追加 */
709 Polygon *Polygon::addChild(Polygon *child)
710 {
711 Polygon *tmp;
712
713 /* childrenのリストの最後に加える */
714 if (this->lastChild != NULL) {
715 tmp = this->lastChild;
716 tmp->children = child;
717 }
718
719 this->lastChild = child;
720
721 if (this->children == NULL) {
722 this->children = child;
723 }
724
725 return child;
726 }
727
728 /* thisの子や子孫にnameのものが存在すればそいつを返す なければNULL. */
729 Polygon *Polygon::searchPolygon(char *name)
730 {
731 Polygon *tmp, *result;
732
733 /* 本人か */
734 if( 0==strcmp(this->name, name) ) return this;
735
736 /* 子供から再帰的に探す */
737 for( tmp=this->children; tmp; tmp=tmp->next )
738 {
739 if( (result=tmp->searchPolygon(name))!=NULL )
740 return result;
741 }
742
743 /* 無かったら NULL. */
744 return NULL;
745 }
746