Mercurial > hg > Game > Cerium
annotate TaskManager/Test/test_render/polygon.cpp @ 123:e9e12e2088bd draft
delete scene_graph->child (instead use "children")
author | gongo@charles.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Tue, 25 Nov 2008 10:32:23 +0900 |
parents | 13b43de5ef5d |
children | 40978d4b608a |
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
e9e12e2088bd
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
e9e12e2088bd
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; |
e9e12e2088bd
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
e9e12e2088bd
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
e9e12e2088bd
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
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
210 //int x = span->x; |
e9e12e2088bd
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
e9e12e2088bd
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) { |
e9e12e2088bd
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
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
269 while (p) { |
e9e12e2088bd
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)) |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
271 { |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
272 list->parent = p; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
273 if(p->children == NULL) |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
274 { |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
275 p->children = list; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
276 } |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
277 else |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
278 { |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
279 bros = p->children; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
280 while(bros->brother != NULL) |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
281 { |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
282 bros = bros->brother; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
283 } |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
284 bros->brother = list; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
285 } |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
286 break; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
287 } |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
288 p = p->next; |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
289 } |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
290 } |
e9e12e2088bd
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); | |
120 | 471 |
123
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
472 |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
473 /** |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
474 * image を 32bit(RGBA) に変換する |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
475 */ |
120 | 476 SDL_Surface *tmpImage |
477 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w, | |
478 texture_image->h, 32, | |
479 redMask, greenMask, blueMask, alphaMask); | |
480 SDL_Surface *converted; | |
481 converted = SDL_ConvertSurface(texture_image, | |
482 tmpImage->format, SDL_HWSURFACE); | |
483 if( converted != NULL){ | |
484 SDL_FreeSurface(texture_image); | |
485 texture_image = converted; | |
486 } | |
109 | 487 |
488 //load_texture(image_name); | |
489 if(unlink(image_name)) | |
490 { | |
491 cout << "unlink error\n"; | |
492 } | |
493 } | |
494 } | |
495 } | |
496 | |
497 | |
498 /* | |
499 * ノードを受け取って、そこからポリゴンを生成 | |
500 * リークしてない? data上書きしちゃだめ | |
501 */ | |
502 void Polygon::create_data(xmlNodePtr cur) | |
503 { | |
504 char *p = (char *)xmlGetProp(cur,(xmlChar *)"size"); | |
505 //error 出力? | |
506 | |
507 #ifndef _DEBUG | |
508 if(strlen(p)>8){ | |
509 __debug("Polygon size is too large.\n"); | |
510 exit(1); | |
511 } | |
512 #endif | |
513 | |
514 size = atoi((char *)xmlGetProp(cur,(xmlChar *)"size")); | |
515 name = (char *)xmlGetProp(cur,(xmlChar *)"name"); | |
516 parent_name = (char *)xmlGetProp(cur,(xmlChar *)"parent"); | |
517 next = NULL; | |
518 | |
519 data = new float[size*3*3]; | |
520 | |
521 get_data(cur->children); | |
522 } | |
523 | |
524 /* | |
525 * xmlをオープンして、Surfaceノードからポリゴンを作る | |
526 * | |
527 */ | |
528 void Polygon::set_data(char *file_name) | |
529 { | |
530 xmlDocPtr doc; | |
531 xmlNodePtr cur; | |
532 Polygon *tmp; | |
533 | |
534 doc = xmlParseFile(file_name); | |
535 | |
536 cur = xmlDocGetRootElement(doc); | |
537 | |
538 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"); | |
539 | |
540 tmp = this; | |
541 | |
542 for (cur=cur->children; cur; cur=cur->next) | |
543 { | |
544 if (!xmlStrcmp(cur->name,(xmlChar*)"surface")) | |
545 { | |
546 tmp->create_data(cur); | |
547 if(cur->next->next) | |
548 { | |
549 tmp->add_next(); | |
550 tmp = tmp->next; | |
551 } | |
552 } | |
553 } | |
554 | |
555 create_tree(); | |
556 //tree_check(); | |
557 //create_scene_graph_pack(); | |
558 | |
559 xmlFreeDoc(doc); | |
560 | |
561 /* | |
562 for(int s=0; s<sgp->info.size; s++) | |
563 { | |
564 printf("sgp->node[%d].pn = %d\n",s,sgp->node[s].pn); | |
565 } | |
566 */ | |
567 //delete sgp; | |
568 } | |
569 | |
570 void Polygon::delete_data() | |
571 { | |
572 Polygon *n,*m; | |
573 | |
574 n = this; | |
575 delete [] n->data; | |
576 | |
577 if(next) | |
578 { | |
579 for(n = this->next; n; n=m) | |
580 { | |
581 m = n->next; | |
582 delete [] n->data; | |
583 delete n; | |
584 } | |
585 } | |
586 } | |
587 | |
588 | |
589 | |
590 /* construct polygon from xml. */ | |
591 /* | |
592 Polygon::Polygon(char *xmlfile) | |
593 { | |
594 Polygon(); | |
595 xmlDocPtr doc; | |
596 xmlNodePtr cur; | |
597 Polygon *tmp; | |
598 | |
599 doc = xmlParseFile(file_name); | |
600 | |
601 cur = xmlDocGetRootElement(doc); | |
602 | |
603 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"); | |
604 | |
605 tmp = this; | |
606 | |
607 for (cur=cur->children; cur; cur=cur->next) | |
608 { | |
609 if ( xmlStrcmp(cur->name,(xmlChar*)"surface")!=0 ) | |
610 continue; | |
611 | |
612 if ( xmlStrcmp(cur->parent )) | |
613 tmp->create_data(cur); | |
614 if(cur->next->next) | |
615 { | |
616 tmp->add_next(); | |
617 tmp = tmp->next; | |
618 } | |
619 } | |
620 | |
621 create_tree(); | |
622 //tree_check(); | |
623 //create_scene_graph_pack(); | |
624 | |
625 xmlFreeDoc(doc); | |
626 } | |
627 */ | |
628 | |
629 /* construct polygon from xmlNode. */ | |
630 Polygon::Polygon(xmlNodePtr surface) | |
631 { | |
632 #if 1 | |
633 xyz[0] = 0; | |
634 xyz[1] = 0; | |
635 xyz[2] = 0; | |
636 xyz[3] = 1; | |
637 c_xyz[0] = 0; | |
638 c_xyz[1] = 0; | |
639 c_xyz[2] = 0; | |
640 c_xyz[3] = 1; | |
641 angle[0] = 0; | |
642 angle[1] = 0; | |
643 angle[2] = 0; | |
644 angle[3] = 1; | |
645 next = NULL; | |
646 parent = NULL; | |
647 brother = NULL; | |
648 children = NULL; | |
649 lastChild = NULL; | |
650 #else | |
651 /* これ使うにはどうすればいいの? */ | |
652 this->Polygon(); | |
653 #endif | |
654 size = atoi((char *)xmlGetProp(surface,(xmlChar *)"size")); | |
655 name = (char *)xmlGetProp(surface,(xmlChar *)"name"); | |
656 parent_name = (char *)xmlGetProp(surface,(xmlChar *)"parent"); | |
657 next = NULL; | |
658 | |
659 data = new float[size*3*3]; | |
660 | |
661 get_data(surface->children); | |
662 } | |
663 | |
664 /* XMLファイルからポリゴンを作成 */ | |
665 Polygon *Polygon::createFromXMLfile(char *xmlfile) | |
666 { | |
667 xmlDocPtr doc; | |
668 xmlNodePtr cur; | |
669 Polygon *root=NULL, *tmp, *parent; | |
670 | |
671 /* パース DOM生成 */ | |
672 doc = xmlParseFile(xmlfile); | |
673 cur = xmlDocGetRootElement(doc); | |
674 | |
675 /* ?? */ | |
676 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"); | |
677 | |
678 /* XMLのノードを一つずつ解析 */ | |
679 for (cur=cur->children; cur; cur=cur->next) | |
680 { | |
681 /* 扱うのはsurfaceオンリー */ | |
682 if ( xmlStrcmp(cur->name,(xmlChar*)"surface")!=0 ) | |
683 continue; | |
684 | |
685 /* ポリゴン生成 */ | |
686 tmp = new Polygon(cur); | |
123
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
687 if ( tmp->parent_name==NULL || 0==strcmp(tmp->parent_name, "NULL")) { |
109 | 688 /* このsurfaceがroot */ |
689 root = tmp; | |
123
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
690 } else { |
109 | 691 /* 親はこのsurfaceより前に定義されているものとする (していい?) */ |
692 // ここで parent_name を用いるのは間違っていて、 | |
693 // *cur->properties->children から探すべきらしい kono | |
694 parent = root->searchPolygon(tmp->parent_name); | |
695 if (parent==NULL) { | |
123
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
696 fprintf(stderr, "[%s] No such parent %s\n", |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
697 tmp->name, tmp->parent_name); |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
698 root->addChild(tmp); |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
699 } else { |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
700 parent->addChild(tmp); |
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
701 } |
109 | 702 } |
703 } | |
704 | |
705 xmlFreeDoc(doc); | |
706 return root; | |
707 } | |
708 | |
709 /* 子供を追加 */ | |
710 Polygon *Polygon::addChild(Polygon *child) | |
711 { | |
712 Polygon *tmp; | |
713 | |
714 /* childrenのリストの最後に加える */ | |
715 if (this->lastChild != NULL) { | |
716 tmp = this->lastChild; | |
123
e9e12e2088bd
delete scene_graph->child (instead use "children")
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
120
diff
changeset
|
717 tmp->children = child; |
109 | 718 } |
719 | |
720 this->lastChild = child; | |
721 | |
722 if (this->children == NULL) { | |
723 this->children = child; | |
724 } | |
725 | |
726 return child; | |
727 } | |
728 | |
729 /* thisの子や子孫にnameのものが存在すればそいつを返す なければNULL. */ | |
730 Polygon *Polygon::searchPolygon(char *name) | |
731 { | |
732 Polygon *tmp, *result; | |
733 | |
734 /* 本人か */ | |
735 if( 0==strcmp(this->name, name) ) return this; | |
736 | |
737 /* 子供から再帰的に探す */ | |
738 for( tmp=this->children; tmp; tmp=tmp->next ) | |
739 { | |
740 if( (result=tmp->searchPolygon(name))!=NULL ) | |
741 return result; | |
742 } | |
743 | |
744 /* 無かったら NULL. */ | |
745 return NULL; | |
746 } | |
747 |