Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Test/test_render/polygon.cpp @ 132:e7c80537b6aa
add XML
author | gongo@charles.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Tue, 25 Nov 2008 18:19:53 +0900 |
parents | 8a33f4859ed8 |
children | 6cf991f28c6c |
rev | line source |
---|---|
109 | 1 #include <iostream> |
2 #include <SDL.h> | |
3 #include <SDL_opengl.h> | |
4 #include <SDL_image.h> | |
5 #include <libxml/parser.h> | |
6 #include "polygon.h" | |
7 #include "xml.h" | |
8 #include "texture.h" | |
9 #include "sys.h" | |
10 #include "triangle.h" | |
11 #include "vertex.h" | |
12 #include "Span.h" | |
13 #include "SpanC.h" | |
14 #include "scene_graph_pack.h" | |
15 #include "error.h" | |
120 | 16 #include "viewer_types.h" |
109 | 17 using namespace std; |
18 | |
19 //extern int decode(char *cont, char *file_name); | |
20 extern int decode(char *cont, FILE *outfile); | |
21 | |
22 | |
23 SDL_Surface* Polygon::texture_image; | |
24 | |
25 Polygon::Polygon() | |
26 { | |
27 xyz[0] = 0; | |
28 xyz[1] = 0; | |
29 xyz[2] = 0; | |
30 xyz[3] = 1; | |
31 c_xyz[0] = 0; | |
32 c_xyz[1] = 0; | |
33 c_xyz[2] = 0; | |
34 c_xyz[3] = 1; | |
35 angle[0] = 0; | |
36 angle[1] = 0; | |
37 angle[2] = 0; | |
38 angle[3] = 1; | |
39 next = NULL; | |
40 parent = NULL; | |
41 brother = NULL; | |
42 children = NULL; | |
43 lastChild = NULL; | |
44 } | |
45 | |
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; | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
56 if(t->children != NULL) |
109 | 57 { |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
58 cout << "--move children : " << t->children->name << endl; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
59 t = t->children; |
109 | 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 } | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
128 if(children != NULL) |
109 | 129 { |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
130 cout << "children->name = " << children->name << endl; |
109 | 131 } |
132 } | |
133 | |
134 void Polygon::draw(SceneGraphPack *sgp) | |
135 { | |
136 float xyz1[4],xyz2[4],xyz3[4]; | |
137 | |
138 /***SceneGraphUpdate***/ | |
139 //sgp_update(); | |
140 for (int i = 0; i < sgp->info.size; i++) { | |
141 SceneGraphNode node = sgp->node[i]; | |
142 | |
143 /***draw***/ | |
144 int n,nt; | |
145 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) { | |
146 xyz1[0] = node.vertex[n]; | |
147 xyz1[1] = node.vertex[n+1]; | |
148 xyz1[2] = node.vertex[n+2]*-1; | |
149 xyz1[3] = 1; | |
150 xyz2[0] = node.vertex[n+3]; | |
151 xyz2[1] = node.vertex[n+3+1]; | |
152 xyz2[2] = node.vertex[n+3+2]*-1; | |
153 xyz2[3] = 1; | |
154 xyz3[0] = node.vertex[n+6]; | |
155 xyz3[1] = node.vertex[n+6+1]; | |
156 xyz3[2] = node.vertex[n+6+2]*-1; | |
157 xyz3[3] = 1; | |
158 | |
159 rotate(xyz1, node.translation); | |
160 rotate(xyz2, node.translation); | |
161 rotate(xyz3, node.translation); | |
162 | |
163 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]); | |
165 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]); | |
166 | |
167 Triangle *tri = new Triangle(ver1,ver2,ver3); | |
168 Span_c *span = new Span_c(); | |
169 span->viewer = viewer; | |
170 span->p = this; | |
171 span->create_span(tri,texture_image); | |
172 delete ver1; | |
173 delete ver2; | |
174 delete ver3; | |
175 delete tri; | |
176 delete span; | |
177 } | |
178 } | |
179 } | |
180 | |
181 | |
182 void Polygon::draw(PolygonPack *pp) | |
183 { | |
184 for(int n=0; n<pp->info.size; n++) | |
185 { | |
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); | |
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); | |
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); | |
189 | |
190 Triangle *tri = new Triangle(ver1,ver2,ver3); | |
191 Span_c *span = new Span_c(); | |
192 span->viewer = viewer; | |
193 span->p = this; | |
194 span->create_span(tri,texture_image); | |
195 delete ver1; | |
196 delete ver2; | |
197 delete ver3; | |
198 delete tri; | |
199 delete span; | |
200 } | |
201 } | |
202 | |
203 void Polygon::draw(SpanPack *sp) | |
204 { | |
205 Span *span; | |
206 | |
207 for (int n = 0; n < sp->info.size; n++) { | |
208 span = &sp->span[n]; | |
209 | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
210 //int x = span->x; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
211 //int y = span->y; |
109 | 212 float z = span->start_z; |
213 int end = span->length_x; | |
214 float zpos = span->end_z; | |
215 float tex1 = span->tex_x1; | |
216 float tex2 = span->tex_x2; | |
217 float tey1 = span->tex_y1; | |
218 float tey2 = span->tex_y2; | |
219 Uint32 rgb; | |
220 int tex_xpos; | |
221 int tex_ypos; | |
222 int tex_zpos; | |
223 float tex_x, tex_y, tex_z; | |
224 | |
225 if (end == 1) { | |
226 tex_xpos = (int)((span->tex_height-1) * tex1); | |
227 tex_ypos = (int)((span->tex_width-1) * tey1); | |
228 tex_zpos = (int)z; | |
229 rgb = get_rgb(tex_xpos, tex_ypos); | |
230 //viewer->write_pixel(x, y, zpos, rgb); | |
231 } else { | |
232 for (int j = 0; j < end; j++) { | |
233 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); | |
234 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); | |
235 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); | |
236 if (tex_x > 1) tex_x = 1; | |
237 if (tex_y > 1) tex_y = 1; | |
238 tex_xpos = (int)((span->tex_height-1) * tex_x); | |
239 tex_ypos = (int)((span->tex_width-1) * tex_y); | |
240 rgb = get_rgb(tex_xpos,tex_ypos); | |
241 //viewer->write_pixel(j + x, y, tex_z, rgb); | |
242 } | |
243 } | |
244 } | |
245 } | |
246 | |
247 void Polygon::add_next() | |
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 | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
265 for (list=this; list; list=list->next) { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
266 if (xmlStrcmp((xmlChar *)list->parent_name, (xmlChar *)"NULL")) { |
109 | 267 p = this; |
268 | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
269 while (p) { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
270 if (!xmlStrcmp((xmlChar *)p->name,(xmlChar *)list->parent_name)) |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
271 { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
272 list->parent = p; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
273 if(p->children == NULL) |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
274 { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
275 p->children = list; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
276 } |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
277 else |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
278 { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
279 bros = p->children; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
280 while(bros->brother != NULL) |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
281 { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
282 bros = bros->brother; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
283 } |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
284 bros->brother = list; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
285 } |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
286 break; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
287 } |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
288 p = p->next; |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
289 } |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
290 } |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
291 } |
109 | 292 } |
293 | |
294 void Polygon::pickup_coordinate(char *cont) | |
295 { | |
296 for(int n=0; n<size*3; n+=3) | |
297 { | |
298 cont = pickup_float(cont, data+n); | |
299 cont = pickup_float(cont, data+n+1); | |
300 cont = pickup_float(cont, data+n+2); | |
301 | |
302 if (cont == NULL) | |
303 { | |
304 cout << "Analyzing obj data failed coordinate\n"; | |
305 } | |
306 } | |
307 } | |
308 | |
309 void Polygon::pickup_normal(char *cont) | |
310 { | |
311 for(int n=size*3;n<size*6;n+=3) | |
312 { | |
313 cont = pickup_float(cont,data+n); | |
314 cont = pickup_float(cont,data+n+1); | |
315 cont = pickup_float(cont,data+n+2); | |
316 | |
317 if (cont == NULL) | |
318 { | |
319 cout << "Analyzing obj data failed normal\n"; | |
320 } | |
321 } | |
322 } | |
323 | |
324 void Polygon::pickup_model(char *cont) | |
325 { | |
326 cont = pickup_float(cont,c_xyz); | |
327 cont = pickup_float(cont,c_xyz+1); | |
328 cont = pickup_float(cont,c_xyz+2); | |
329 | |
330 if (cont == NULL) | |
331 { | |
332 cout << "Analyzing obj data failed model\n"; | |
333 } | |
334 } | |
335 | |
336 void Polygon::pickup_texture(char *cont) | |
337 { | |
338 for(int n=size*6; n<size*9; n+=3) | |
339 { | |
340 cont = pickup_float(cont,data+n); | |
341 cont = pickup_float(cont,data+n+1); | |
342 data[n+2] = 1.0; | |
343 | |
344 if (cont == NULL) | |
345 { | |
346 cout << "Analyzing obj data failed texture\n"; | |
347 } | |
348 } | |
349 } | |
350 | |
351 char *get_pixel(int tx, int ty, SDL_Surface *texture_image) | |
352 { | |
353 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx)); | |
354 } | |
355 | |
356 unsigned my_ntohl(unsigned u) { | |
357 // rr gg bb 00 | |
358 // rr | |
359 // bb gg rr | |
360 //unsigned u1 = ((u&0xff)<<24) + | |
361 // ((u&0xff00)<<8) + | |
362 // ((u&0xff0000)>>8) + | |
363 // ((u&0xff000000)>>24); | |
364 unsigned u1; | |
365 unsigned b = (u&0xff000000)>>24; | |
366 unsigned g = (u&0xff0000)>>16; | |
367 unsigned r = (u&0xff00)>>8; | |
368 u1 = r + (g<<8) + (b<<16); | |
369 //printf("pixel %x->%x\n",u,u1); | |
370 return u1; | |
371 } | |
372 | |
373 Uint32 Polygon::get_rgb(int tx, int ty) | |
374 { | |
375 SDL_PixelFormat *fmt; | |
376 //Uint32 temp, pixel; | |
377 Uint8 red, green, blue; | |
378 | |
379 fmt = texture_image->format; | |
380 | |
381 if (tx<0) tx = 0; | |
382 if (texture_image->w-1< tx) tx = texture_image->w-1 ; | |
383 if (ty<0) ty = 0; | |
384 if (texture_image->h-1< ty) ty = texture_image->h-1 ; | |
385 | |
386 | |
387 | |
388 //SDL_LockSurface(texture_image); | |
389 char *p = get_pixel(tx,ty,texture_image); | |
390 #if 0 | |
391 pixel = my_ntohl(*(Uint32*)p); | |
392 //printf("pixel = %d\n", pixel); | |
393 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); | |
394 //SDL_UnlockSurface(texture_image); | |
395 | |
396 temp = pixel&fmt->Rmask; | |
397 temp = temp>>fmt->Rshift; | |
398 temp = temp<<fmt->Rloss; | |
399 red = (Uint8)temp; | |
400 | |
401 temp = pixel&fmt->Gmask; | |
402 temp = temp>>fmt->Gshift; | |
403 temp = temp<<fmt->Gloss; | |
404 green = (Uint8)temp; | |
405 | |
406 temp = pixel&fmt->Bmask; | |
407 temp = temp>>fmt->Bshift; | |
408 temp = temp<<fmt->Bloss; | |
409 blue = (Uint8)temp; | |
410 #endif | |
411 blue = (Uint8) p[0]; | |
412 green = (Uint8) p[1]; | |
413 red = (Uint8) p[2]; | |
414 | |
415 //printf("tx = %d ty = %d ", tx,ty); | |
416 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue); | |
417 | |
418 SDL_PixelFormat *pf; | |
419 //pf = viewer->screen->format; | |
420 | |
421 //cout << SDL_MapRGB(pf, red, green, blue) << endl; | |
422 return SDL_MapRGB(pf, red, green, blue); | |
423 } | |
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); | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
471 |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
472 /** |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
473 * image を 32bit(RGBA) に変換する |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
474 */ |
120 | 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 } | |
109 | 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); | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
686 if ( tmp->parent_name==NULL || 0==strcmp(tmp->parent_name, "NULL")) { |
109 | 687 /* このsurfaceがroot */ |
688 root = tmp; | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
689 } else { |
109 | 690 /* 親はこのsurfaceより前に定義されているものとする (していい?) */ |
691 // ここで parent_name を用いるのは間違っていて、 | |
692 // *cur->properties->children から探すべきらしい kono | |
693 parent = root->searchPolygon(tmp->parent_name); | |
694 if (parent==NULL) { | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
695 fprintf(stderr, "[%s] No such parent %s\n", |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
696 tmp->name, tmp->parent_name); |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
697 root->addChild(tmp); |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
698 } else { |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
699 parent->addChild(tmp); |
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
700 } |
109 | 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; | |
123
f515436feb71
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
716 tmp->children = child; |
109 | 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 |