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