Mercurial > hg > Members > kono > Cerium
comparison old/simple_render/polygon.cpp @ 507:735f76483bb2
Reorganization..
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 12 Oct 2009 09:39:35 +0900 |
parents | TaskManager/Test/simple_render/polygon.cpp@5c194c71eca8 |
children |
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 "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 using namespace std; | |
16 | |
17 //extern int decode(char *cont, char *file_name); | |
18 extern int decode(char *cont, FILE *outfile); | |
19 | |
20 | |
21 SDL_Surface* Polygon::texture_image; | |
22 | |
23 Polygon::Polygon() | |
24 { | |
25 xyz[0] = 0; | |
26 xyz[1] = 0; | |
27 xyz[2] = 0; | |
28 xyz[3] = 1; | |
29 c_xyz[0] = 0; | |
30 c_xyz[1] = 0; | |
31 c_xyz[2] = 0; | |
32 c_xyz[3] = 1; | |
33 angle[0] = 0; | |
34 angle[1] = 0; | |
35 angle[2] = 0; | |
36 angle[3] = 1; | |
37 next = NULL; | |
38 child = NULL; | |
39 parent = NULL; | |
40 brother = NULL; | |
41 children = NULL; | |
42 lastChild = NULL; | |
43 } | |
44 | |
45 | |
46 | |
47 void Polygon::tree_check() | |
48 { | |
49 Polygon *t; | |
50 t = this; | |
51 | |
52 while(t) | |
53 { | |
54 cout << "my_name : " << t->name << endl; | |
55 if(t->child != NULL) | |
56 { | |
57 cout << "--move child : " << t->child->name << endl; | |
58 t = t->child; | |
59 } | |
60 else if(t->brother != NULL) | |
61 { | |
62 cout << "--move brother : " << t->brother->name << endl; | |
63 t = t->brother; | |
64 } | |
65 else | |
66 { | |
67 while(t) | |
68 { | |
69 if(t->brother != NULL) | |
70 { | |
71 cout << "--move brother : " << t->brother->name << endl; | |
72 t = t->brother; | |
73 break; | |
74 } | |
75 else | |
76 { | |
77 if(t->parent) | |
78 { | |
79 cout << "--move parent : " << t->parent->name << endl; | |
80 } | |
81 t = t->parent; | |
82 } | |
83 } | |
84 } | |
85 } | |
86 } | |
87 | |
88 void Polygon::print_member() | |
89 { | |
90 //int n; | |
91 | |
92 cout << "size = " << size << endl; | |
93 cout << "name = " << name << endl; | |
94 cout << "parent_name = " << parent_name << endl; | |
95 /* | |
96 for(n=0; n<size*3; n+=3) | |
97 { | |
98 cout<<"coordinate = "<<data[n]<<" "<<data[n+1]<<" "<< data[n+2]<<endl; | |
99 } | |
100 */ | |
101 /* | |
102 for(n=size*3; n<size*6; n+=3) | |
103 { | |
104 cout<<"narmal = "<<data[n]<<" "<<data[n+1]<<" "<<data[n+2]<<endl; | |
105 } | |
106 */ | |
107 cout<<"c_xyz = "<<c_xyz[0]<<" "<<c_xyz[1]<<" "<<c_xyz[2]<<endl; | |
108 /* | |
109 for(n=size*6; n<size*9; n+=3) | |
110 { | |
111 cout<<"texture = "<<data[n]<<" "<<data[n+1]<<" "<<data[n+2]<<endl; | |
112 } | |
113 */ | |
114 /* | |
115 for(n=0; n<size*3; n+=3) | |
116 { | |
117 cout<<"coordinate = "<<data[n]<<" "<<data[n+1]<<" "<<data[n+2]<<endl; | |
118 cout<<"normal = "<<data[n+size*3]<<" "<<data[n+size*3+1]<<" "<<data[n+size*3+2]<<endl; | |
119 cout<<"texture = "<<data[n+size*6]<<" "<<1-data[n+size*6+1]<<endl; | |
120 } | |
121 */ | |
122 //cout << "image_name = " << image_name << endl; | |
123 if(parent != NULL) | |
124 { | |
125 cout << "parent->name = " << parent->name << endl; | |
126 } | |
127 if(child != NULL) | |
128 { | |
129 cout << "child->name = " << child->name << endl; | |
130 } | |
131 } | |
132 | |
133 void Polygon::draw(SceneGraphPack *sgp) | |
134 { | |
135 float xyz1[4],xyz2[4],xyz3[4]; | |
136 | |
137 /***SceneGraphUpdate***/ | |
138 //sgp_update(); | |
139 for (int i = 0; i < sgp->info.size; i++) { | |
140 SceneGraphNode node = sgp->node[i]; | |
141 | |
142 /***draw***/ | |
143 int n,nt; | |
144 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) { | |
145 xyz1[0] = node.vertex[n]; | |
146 xyz1[1] = node.vertex[n+1]; | |
147 xyz1[2] = node.vertex[n+2]*-1; | |
148 xyz1[3] = 1; | |
149 xyz2[0] = node.vertex[n+3]; | |
150 xyz2[1] = node.vertex[n+3+1]; | |
151 xyz2[2] = node.vertex[n+3+2]*-1; | |
152 xyz2[3] = 1; | |
153 xyz3[0] = node.vertex[n+6]; | |
154 xyz3[1] = node.vertex[n+6+1]; | |
155 xyz3[2] = node.vertex[n+6+2]*-1; | |
156 xyz3[3] = 1; | |
157 | |
158 rotate(xyz1, node.translation); | |
159 rotate(xyz2, node.translation); | |
160 rotate(xyz3, node.translation); | |
161 | |
162 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]); | |
163 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]); | |
164 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]); | |
165 | |
166 Triangle *tri = new Triangle(ver1,ver2,ver3); | |
167 Span_c *span = new Span_c(); | |
168 span->viewer = viewer; | |
169 span->p = this; | |
170 span->create_span(tri,texture_image); | |
171 delete ver1; | |
172 delete ver2; | |
173 delete ver3; | |
174 delete tri; | |
175 delete span; | |
176 } | |
177 } | |
178 } | |
179 | |
180 | |
181 void Polygon::draw(PolygonPack *pp) | |
182 { | |
183 for(int n=0; n<pp->info.size; n++) | |
184 { | |
185 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); | |
186 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); | |
187 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); | |
188 | |
189 Triangle *tri = new Triangle(ver1,ver2,ver3); | |
190 Span_c *span = new Span_c(); | |
191 span->viewer = viewer; | |
192 span->p = this; | |
193 span->create_span(tri,texture_image); | |
194 delete ver1; | |
195 delete ver2; | |
196 delete ver3; | |
197 delete tri; | |
198 delete span; | |
199 } | |
200 } | |
201 | |
202 void Polygon::draw(SpanPack *sp) | |
203 { | |
204 Span *span; | |
205 | |
206 for (int n = 0; n < sp->info.size; n++) { | |
207 span = &sp->span[n]; | |
208 | |
209 int x = span->x; | |
210 int y = span->y; | |
211 float z = span->start_z; | |
212 int end = span->length_x; | |
213 float zpos = span->end_z; | |
214 float tex1 = span->tex_x1; | |
215 float tex2 = span->tex_x2; | |
216 float tey1 = span->tex_y1; | |
217 float tey2 = span->tex_y2; | |
218 Uint32 rgb; | |
219 int tex_xpos; | |
220 int tex_ypos; | |
221 int tex_zpos; | |
222 float tex_x, tex_y, tex_z; | |
223 | |
224 if (end == 1) { | |
225 tex_xpos = (int)((span->tex_height-1) * tex1); | |
226 tex_ypos = (int)((span->tex_width-1) * tey1); | |
227 tex_zpos = (int)z; | |
228 rgb = get_rgb(tex_xpos, tex_ypos); | |
229 viewer->write_pixel(x, y, zpos, rgb); | |
230 } else { | |
231 for (int j = 0; j < end; j++) { | |
232 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); | |
233 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); | |
234 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); | |
235 if (tex_x > 1) tex_x = 1; | |
236 if (tex_y > 1) tex_y = 1; | |
237 tex_xpos = (int)((span->tex_height-1) * tex_x); | |
238 tex_ypos = (int)((span->tex_width-1) * tex_y); | |
239 rgb = get_rgb(tex_xpos,tex_ypos); | |
240 viewer->write_pixel(j + x, y, tex_z, rgb); | |
241 } | |
242 } | |
243 } | |
244 } | |
245 | |
246 void | |
247 Polygon::draw(SpanPackList *ss) | |
248 { | |
249 SpanPack *spanPack; | |
250 Span *span; | |
251 | |
252 //for (int i = 0; i < ss->length; i++) { | |
253 for (int i = 0; i < 10; i++) { | |
254 spanPack = &ss->packs[i]; | |
255 | |
256 for(int n = 0; n < spanPack->info.size; n++) { | |
257 span = &spanPack->span[n]; | |
258 | |
259 int end = span->length_x; | |
260 Uint32 rgb; | |
261 float tex1 = span->tex_x1; | |
262 float tex2 = span->tex_x2; | |
263 float tey1 = span->tex_y1; | |
264 float tey2 = span->tex_y2; | |
265 int tex_xpos; | |
266 int tex_ypos; | |
267 int tex_zpos; | |
268 int x = span->x; | |
269 int y = span->y; | |
270 float z = span->start_z; | |
271 float zpos = span->end_z; | |
272 float tex_x,tex_y,tex_z; | |
273 | |
274 if(end == 1) { | |
275 //printf("end == 1\n"); | |
276 //printf("tex_x:%f tex_y:%f\n",tex1,tex2); | |
277 //if(tex1 > 1) tex1 = 1; | |
278 //if(tey1 > 1) tey1 = 1; | |
279 tex_xpos = (int)((span->tex_height-1) * tex1); | |
280 tex_ypos = (int)((span->tex_width-1) * tey1); | |
281 tex_zpos = (int)z; | |
282 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos); | |
283 //printf("image->h:%d tex_x:%f\n",(int)span->tex_height,tex1); | |
284 rgb = get_rgb(tex_xpos,tex_ypos); | |
285 viewer->write_pixel(x,y,zpos,rgb); | |
286 }else { | |
287 //printf("end != 1\n"); | |
288 for(int j = 0; j < end; j++) { | |
289 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); | |
290 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); | |
291 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); | |
292 if(tex_x > 1) tex_x = 1; | |
293 if(tex_y > 1) tex_y = 1; | |
294 tex_xpos = (int)((span->tex_height-1) * tex_x); | |
295 tex_ypos = (int)((span->tex_width-1) * tex_y); | |
296 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos); | |
297 //printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z); | |
298 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y); | |
299 rgb = get_rgb(tex_xpos,tex_ypos); | |
300 viewer->write_pixel(j+x,y,tex_z,rgb); | |
301 } | |
302 } | |
303 } | |
304 } | |
305 } | |
306 | |
307 | |
308 /* | |
309 void Polygon::create_scene_graph_pack() | |
310 { | |
311 //SceneGraphPack *sgp = new SceneGraphPack; | |
312 sgp = new SceneGraphPack; | |
313 int i = 0; | |
314 int nnpn = -1; | |
315 | |
316 Polygon *t; | |
317 t = this; | |
318 | |
319 while(t) | |
320 { | |
321 //SceneGraphNode node; | |
322 sgp->node[i].size = t->size; | |
323 int d,tex; | |
324 for(d=0,tex=0; d<t->size*3; d+=3,tex+=2) | |
325 { | |
326 sgp->node[i].vertex[d] = t->data[d]; | |
327 sgp->node[i].vertex[d+1] = t->data[d+1]; | |
328 sgp->node[i].vertex[d+2] = t->data[d+2]; | |
329 sgp->node[i].texture[tex] = t->data[d+t->size*6]; | |
330 sgp->node[i].texture[tex+1] = t->data[d+t->size*6+1]; | |
331 } | |
332 | |
333 sgp->node[i].obj_pos[0] = 0; | |
334 sgp->node[i].obj_pos[1] = 0; | |
335 sgp->node[i].obj_pos[2] = 0; | |
336 sgp->node[i].obj_pos[3] = 1; | |
337 sgp->node[i].angle[0] = 0; | |
338 sgp->node[i].angle[1] = 0; | |
339 sgp->node[i].angle[2] = 0; | |
340 sgp->node[i].angle[3] = 1; | |
341 | |
342 for(int tm=0; tm<16; tm++) | |
343 { | |
344 sgp->node[i].translation[tm] = 0; | |
345 } | |
346 sgp->node[i].id = 0; | |
347 sgp->node[i].move = 0; | |
348 sgp->node[i].interaction = 0; | |
349 sgp->node[i].pn = nnpn; | |
350 | |
351 if(t->child != NULL) | |
352 { | |
353 nnpn = i; | |
354 t = t->child; | |
355 } | |
356 else if(t->brother != NULL) | |
357 { | |
358 nnpn = sgp->node[i].pn; | |
359 t = t->brother; | |
360 } | |
361 else | |
362 { | |
363 while(t) | |
364 { | |
365 if(t->brother != NULL) | |
366 { | |
367 t = t->brother; | |
368 break; | |
369 } | |
370 else | |
371 { | |
372 if(t->parent == NULL) | |
373 { | |
374 t = NULL; | |
375 break; | |
376 } | |
377 nnpn = sgp->node[nnpn].pn; | |
378 t = t->parent; | |
379 } | |
380 } | |
381 } | |
382 //printf("sgp->node[%d].pn = %d\n", i, sgp->node[i].pn); | |
383 i++; | |
384 } | |
385 sgp->info.size = i; | |
386 | |
387 | |
388 //for(int s=0; s<i; s++) | |
389 //{ | |
390 //printf("sgp.node[%d].pn = %d\n",s,sgp.node[s].pn); | |
391 //} | |
392 | |
393 //delete sgp; | |
394 } | |
395 */ | |
396 | |
397 | |
398 void Polygon::add_next() | |
399 { | |
400 Polygon *p; | |
401 p = new Polygon; | |
402 | |
403 next = p; | |
404 } | |
405 | |
406 | |
407 void Polygon::create_tree() | |
408 { | |
409 Polygon *list; | |
410 Polygon *check_list; | |
411 Polygon *p; | |
412 Polygon *bros; | |
413 | |
414 check_list = this; | |
415 | |
416 for(list=this; list; list=list->next) | |
417 { | |
418 if(xmlStrcmp((xmlChar *)list->parent_name, (xmlChar *)"NULL")) | |
419 { | |
420 p = this; | |
421 while(p) | |
422 { | |
423 if(!xmlStrcmp((xmlChar *)p->name,(xmlChar *)list->parent_name)) | |
424 { | |
425 list->parent = p; | |
426 if(p->child == NULL) | |
427 { | |
428 p->child = list; | |
429 } | |
430 else | |
431 { | |
432 bros = p->child; | |
433 while(bros->brother != NULL) | |
434 { | |
435 bros = bros->brother; | |
436 } | |
437 bros->brother = list; | |
438 } | |
439 break; | |
440 } | |
441 p = p->next; | |
442 } | |
443 } | |
444 } | |
445 } | |
446 | |
447 /* | |
448 void Polygon::create_scene_graph_pack() | |
449 { | |
450 SceneGraphPack sgp; | |
451 int i = 0; | |
452 int nnpn = -1; | |
453 | |
454 Polygon *t; | |
455 t = this; | |
456 while(t) | |
457 { | |
458 //t->draw(); | |
459 SceneGraphNode node; | |
460 node.obj_pos[0] = 0; | |
461 node.obj_pos[1] = 0; | |
462 node.obj_pos[2] = 0; | |
463 node.obj_pos[3] = 1; | |
464 for(int tm=0; tm<16; tm++) | |
465 { | |
466 node.translation[tm] = 0; | |
467 } | |
468 node.id = 0; | |
469 node.move = 0; | |
470 node.interaction = 0; | |
471 for(int a=0; a<10; a++) | |
472 { | |
473 node.op[a] = -1; | |
474 } | |
475 node.pn = nnpn; | |
476 int p = 0; | |
477 | |
478 if(t->child != NULL) | |
479 { | |
480 //glPushMatrix(); | |
481 //node.op[p] = PUSH; | |
482 //nnpn = i; | |
483 t = t->child; | |
484 } | |
485 else if(t->brother != NULL) | |
486 { | |
487 //glPopMatrix(); | |
488 //glPushMatrix(); | |
489 //node.op[p] = SHIFT; | |
490 //nnpn = node.pn; | |
491 t = t->brother; | |
492 } | |
493 else | |
494 { | |
495 while(t) | |
496 { | |
497 if(t->brother != NULL) | |
498 { | |
499 //glPopMatrix(); | |
500 //glPushMatrix(); | |
501 //node.op[p] = SHIFT; | |
502 t = t->brother; | |
503 break; | |
504 } | |
505 else | |
506 { | |
507 t = t->parent; | |
508 if(t) | |
509 { | |
510 //glPopMatrix(); | |
511 //node.op[p] = POP; | |
512 //nnpn = sgp.node[nnpn].pn; | |
513 p++; | |
514 } | |
515 } | |
516 } | |
517 } | |
518 sgp.node[i] = node; | |
519 i++; | |
520 } | |
521 | |
522 sgp.info.size = i; | |
523 | |
524 for(int s=0; s<4; s++) | |
525 { | |
526 for(int a=0; sgp.node[s].op[a]!=-1; a++) | |
527 { | |
528 printf("sgp.node[%d].op[%d] = %d\n",s, a, sgp.node[s].op[a]); | |
529 } | |
530 //printf("sgp.node[%d].pn = %d\n",s,sgp.node[s].pn); | |
531 } | |
532 } | |
533 */ | |
534 | |
535 | |
536 | |
537 void Polygon::pickup_coordinate(char *cont) | |
538 { | |
539 for(int n=0; n<size*3; n+=3) | |
540 { | |
541 cont = pickup_float(cont, data+n); | |
542 cont = pickup_float(cont, data+n+1); | |
543 cont = pickup_float(cont, data+n+2); | |
544 | |
545 if (cont == NULL) | |
546 { | |
547 cout << "Analyzing obj data failed coordinate\n"; | |
548 } | |
549 } | |
550 } | |
551 | |
552 void Polygon::pickup_normal(char *cont) | |
553 { | |
554 for(int n=size*3;n<size*6;n+=3) | |
555 { | |
556 cont = pickup_float(cont,data+n); | |
557 cont = pickup_float(cont,data+n+1); | |
558 cont = pickup_float(cont,data+n+2); | |
559 | |
560 if (cont == NULL) | |
561 { | |
562 cout << "Analyzing obj data failed normal\n"; | |
563 } | |
564 } | |
565 } | |
566 | |
567 void Polygon::pickup_model(char *cont) | |
568 { | |
569 cont = pickup_float(cont,c_xyz); | |
570 cont = pickup_float(cont,c_xyz+1); | |
571 cont = pickup_float(cont,c_xyz+2); | |
572 | |
573 if (cont == NULL) | |
574 { | |
575 cout << "Analyzing obj data failed model\n"; | |
576 } | |
577 } | |
578 | |
579 void Polygon::pickup_texture(char *cont) | |
580 { | |
581 for(int n=size*6; n<size*9; n+=3) | |
582 { | |
583 cont = pickup_float(cont,data+n); | |
584 cont = pickup_float(cont,data+n+1); | |
585 data[n+2] = 1.0; | |
586 | |
587 if (cont == NULL) | |
588 { | |
589 cout << "Analyzing obj data failed texture\n"; | |
590 } | |
591 } | |
592 } | |
593 | |
594 char *get_pixel(int tx, int ty, SDL_Surface *texture_image) | |
595 { | |
596 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx)); | |
597 } | |
598 | |
599 unsigned my_ntohl(unsigned u) { | |
600 // rr gg bb 00 | |
601 // rr | |
602 // bb gg rr | |
603 //unsigned u1 = ((u&0xff)<<24) + | |
604 // ((u&0xff00)<<8) + | |
605 // ((u&0xff0000)>>8) + | |
606 // ((u&0xff000000)>>24); | |
607 unsigned u1; | |
608 unsigned b = (u&0xff000000)>>24; | |
609 unsigned g = (u&0xff0000)>>16; | |
610 unsigned r = (u&0xff00)>>8; | |
611 u1 = r + (g<<8) + (b<<16); | |
612 //printf("pixel %x->%x\n",u,u1); | |
613 return u1; | |
614 } | |
615 | |
616 Uint32 Polygon::get_rgb(int tx, int ty) | |
617 { | |
618 SDL_PixelFormat *fmt; | |
619 //Uint32 temp, pixel; | |
620 Uint8 red, green, blue; | |
621 | |
622 fmt = texture_image->format; | |
623 | |
624 if (tx<0) tx = 0; | |
625 if (texture_image->w-1< tx) tx = texture_image->w-1 ; | |
626 if (ty<0) ty = 0; | |
627 if (texture_image->h-1< ty) ty = texture_image->h-1 ; | |
628 | |
629 | |
630 | |
631 //SDL_LockSurface(texture_image); | |
632 char *p = get_pixel(tx,ty,texture_image); | |
633 #if 0 | |
634 pixel = my_ntohl(*(Uint32*)p); | |
635 //printf("pixel = %d\n", pixel); | |
636 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); | |
637 //SDL_UnlockSurface(texture_image); | |
638 | |
639 temp = pixel&fmt->Rmask; | |
640 temp = temp>>fmt->Rshift; | |
641 temp = temp<<fmt->Rloss; | |
642 red = (Uint8)temp; | |
643 | |
644 temp = pixel&fmt->Gmask; | |
645 temp = temp>>fmt->Gshift; | |
646 temp = temp<<fmt->Gloss; | |
647 green = (Uint8)temp; | |
648 | |
649 temp = pixel&fmt->Bmask; | |
650 temp = temp>>fmt->Bshift; | |
651 temp = temp<<fmt->Bloss; | |
652 blue = (Uint8)temp; | |
653 #endif | |
654 blue = (Uint8) p[0]; | |
655 green = (Uint8) p[1]; | |
656 red = (Uint8) p[2]; | |
657 | |
658 //printf("tx = %d ty = %d ", tx,ty); | |
659 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue); | |
660 | |
661 SDL_PixelFormat *pf; | |
662 pf = viewer->screen->format; | |
663 | |
664 //cout << SDL_MapRGB(pf, red, green, blue) << endl; | |
665 return SDL_MapRGB(pf, red, green, blue); | |
666 } | |
667 | |
668 | |
669 /* | |
670 * surface nodeからポリゴンの情報を読み出す 再帰しない | |
671 */ | |
672 void Polygon::get_data(xmlNodePtr cur) | |
673 { | |
674 char *cont; | |
675 //char *image_name; | |
676 | |
677 for(;cur;cur=cur->next) | |
678 { | |
679 if(!xmlStrcmp(cur->name,(xmlChar*)"coordinate")) | |
680 { | |
681 cont = (char *)xmlNodeGetContent(cur); | |
682 pickup_coordinate(cont); | |
683 } | |
684 else if(!xmlStrcmp(cur->name,(xmlChar*)"normal")) | |
685 { | |
686 cont = (char *)xmlNodeGetContent(cur); | |
687 pickup_normal(cont); | |
688 } | |
689 else if(!xmlStrcmp(cur->name,(xmlChar*)"model")) | |
690 { | |
691 cont = (char *)xmlNodeGetContent(cur); | |
692 pickup_model(cont); | |
693 } | |
694 else if(!xmlStrcmp(cur->name,(xmlChar*)"texture")) | |
695 { | |
696 cont = (char *)xmlNodeGetContent(cur); | |
697 pickup_texture(cont); | |
698 } | |
699 else if(!xmlStrcmp(cur->name,(xmlChar*)"image")) | |
700 { | |
701 char image_name[20] = "/tmp/image_XXXXXX"; | |
702 int fd = mkstemp(image_name); | |
703 FILE *outfile = fdopen(fd, "wb"); | |
704 if(NULL == outfile) | |
705 { | |
706 cout << "error open file\n"; | |
707 } | |
708 cont = (char *)xmlNodeGetContent(cur); | |
709 //decode(cont, image_name); | |
710 decode(cont, outfile); | |
711 fclose(outfile); | |
712 | |
713 texture_image = IMG_Load(image_name); | |
714 | |
715 //load_texture(image_name); | |
716 if(unlink(image_name)) | |
717 { | |
718 cout << "unlink error\n"; | |
719 } | |
720 } | |
721 } | |
722 } | |
723 | |
724 | |
725 /* | |
726 * ノードを受け取って、そこからポリゴンを生成 | |
727 * リークしてない? data上書きしちゃだめ | |
728 */ | |
729 void Polygon::create_data(xmlNodePtr cur) | |
730 { | |
731 size = atoi((char *)xmlGetProp(cur,(xmlChar *)"size")); | |
732 name = (char *)xmlGetProp(cur,(xmlChar *)"name"); | |
733 parent_name = (char *)xmlGetProp(cur,(xmlChar *)"parent"); | |
734 next = NULL; | |
735 | |
736 data = new float[size*3*3]; | |
737 | |
738 get_data(cur->children); | |
739 } | |
740 | |
741 /* | |
742 * xmlをオープンして、Surfaceノードからポリゴンを作る | |
743 * | |
744 */ | |
745 void Polygon::set_data(char *file_name) | |
746 { | |
747 xmlDocPtr doc; | |
748 xmlNodePtr cur; | |
749 Polygon *tmp; | |
750 | |
751 doc = xmlParseFile(file_name); | |
752 | |
753 cur = xmlDocGetRootElement(doc); | |
754 | |
755 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"); | |
756 | |
757 tmp = this; | |
758 | |
759 for (cur=cur->children; cur; cur=cur->next) | |
760 { | |
761 if (!xmlStrcmp(cur->name,(xmlChar*)"surface")) | |
762 { | |
763 tmp->create_data(cur); | |
764 if(cur->next->next) | |
765 { | |
766 tmp->add_next(); | |
767 tmp = tmp->next; | |
768 } | |
769 } | |
770 } | |
771 | |
772 create_tree(); | |
773 //tree_check(); | |
774 //create_scene_graph_pack(); | |
775 | |
776 xmlFreeDoc(doc); | |
777 | |
778 /* | |
779 for(int s=0; s<sgp->info.size; s++) | |
780 { | |
781 printf("sgp->node[%d].pn = %d\n",s,sgp->node[s].pn); | |
782 } | |
783 */ | |
784 //delete sgp; | |
785 } | |
786 | |
787 void Polygon::delete_data() | |
788 { | |
789 Polygon *n,*m; | |
790 | |
791 n = this; | |
792 delete [] n->data; | |
793 | |
794 if(next) | |
795 { | |
796 for(n = this->next; n; n=m) | |
797 { | |
798 m = n->next; | |
799 delete [] n->data; | |
800 delete n; | |
801 } | |
802 } | |
803 } | |
804 | |
805 | |
806 | |
807 /* construct polygon from xml. */ | |
808 /* | |
809 Polygon::Polygon(char *xmlfile) | |
810 { | |
811 Polygon(); | |
812 xmlDocPtr doc; | |
813 xmlNodePtr cur; | |
814 Polygon *tmp; | |
815 | |
816 doc = xmlParseFile(file_name); | |
817 | |
818 cur = xmlDocGetRootElement(doc); | |
819 | |
820 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"); | |
821 | |
822 tmp = this; | |
823 | |
824 for (cur=cur->children; cur; cur=cur->next) | |
825 { | |
826 if ( xmlStrcmp(cur->name,(xmlChar*)"surface")!=0 ) | |
827 continue; | |
828 | |
829 if ( xmlStrcmp(cur->parent )) | |
830 tmp->create_data(cur); | |
831 if(cur->next->next) | |
832 { | |
833 tmp->add_next(); | |
834 tmp = tmp->next; | |
835 } | |
836 } | |
837 | |
838 create_tree(); | |
839 //tree_check(); | |
840 //create_scene_graph_pack(); | |
841 | |
842 xmlFreeDoc(doc); | |
843 } | |
844 */ | |
845 | |
846 /* construct polygon from xmlNode. */ | |
847 Polygon::Polygon(xmlNodePtr surface) | |
848 { | |
849 #if 1 | |
850 xyz[0] = 0; | |
851 xyz[1] = 0; | |
852 xyz[2] = 0; | |
853 xyz[3] = 1; | |
854 c_xyz[0] = 0; | |
855 c_xyz[1] = 0; | |
856 c_xyz[2] = 0; | |
857 c_xyz[3] = 1; | |
858 angle[0] = 0; | |
859 angle[1] = 0; | |
860 angle[2] = 0; | |
861 angle[3] = 1; | |
862 next = NULL; | |
863 child = NULL; | |
864 parent = NULL; | |
865 brother = NULL; | |
866 children = NULL; | |
867 lastChild = NULL; | |
868 #else | |
869 /* これ使うにはどうすればいいの? */ | |
870 this->Polygon(); | |
871 #endif | |
872 size = atoi((char *)xmlGetProp(surface,(xmlChar *)"size")); | |
873 name = (char *)xmlGetProp(surface,(xmlChar *)"name"); | |
874 parent_name = (char *)xmlGetProp(surface,(xmlChar *)"parent"); | |
875 next = NULL; | |
876 | |
877 data = new float[size*3*3]; | |
878 | |
879 get_data(surface->children); | |
880 } | |
881 | |
882 /* XMLファイルからポリゴンを作成 */ | |
883 Polygon *Polygon::createFromXMLfile(char *xmlfile) | |
884 { | |
885 xmlDocPtr doc; | |
886 xmlNodePtr cur; | |
887 Polygon *root=NULL, *tmp, *parent; | |
888 | |
889 /* パース DOM生成 */ | |
890 doc = xmlParseFile(xmlfile); | |
891 cur = xmlDocGetRootElement(doc); | |
892 | |
893 /* ?? */ | |
894 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"); | |
895 | |
896 /* XMLのノードを一つずつ解析 */ | |
897 for (cur=cur->children; cur; cur=cur->next) | |
898 { | |
899 /* 扱うのはsurfaceオンリー */ | |
900 if ( xmlStrcmp(cur->name,(xmlChar*)"surface")!=0 ) | |
901 continue; | |
902 | |
903 /* ポリゴン生成 */ | |
904 tmp = new Polygon(cur); | |
905 if( tmp->parent_name==NULL || 0==strcmp(tmp->parent_name, "NULL")) | |
906 /* このsurfaceがroot */ | |
907 root = tmp; | |
908 else | |
909 { | |
910 /* 親はこのsurfaceより前に定義されているものとする (していい?) */ | |
911 parent = root->searchPolygon(tmp->parent_name); | |
912 parent->addChild( tmp ); | |
913 } | |
914 } | |
915 | |
916 xmlFreeDoc(doc); | |
917 return root; | |
918 } | |
919 | |
920 /* 子供を追加 */ | |
921 Polygon *Polygon::addChild(Polygon *child) | |
922 { | |
923 Polygon *tmp; | |
924 | |
925 /* childrenのリストの最後に加える */ | |
926 if (this->lastChild != NULL) { | |
927 tmp = this->lastChild; | |
928 tmp->next = child; | |
929 } | |
930 | |
931 this->lastChild = child; | |
932 | |
933 return child; | |
934 } | |
935 | |
936 /* thisの子や子孫にnameのものが存在すればそいつを返す なければNULL. */ | |
937 Polygon *Polygon::searchPolygon(char *name) | |
938 { | |
939 Polygon *tmp, *result; | |
940 | |
941 /* 本人か */ | |
942 if( 0==strcmp(this->name, name) ) return this; | |
943 | |
944 /* 子供から再帰的に探す */ | |
945 for( tmp=this->children; tmp; tmp=tmp->next ) | |
946 { | |
947 if( (result=tmp->searchPolygon(name))!=NULL ) | |
948 return result; | |
949 } | |
950 | |
951 /* 無かったら NULL. */ | |
952 return NULL; | |
953 } | |
954 |