53
|
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);
|
68
|
190 Span_c *span = new Span_c();
|
53
|
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);
|
68
|
320 Span_c *span = new Span_c();
|
53
|
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 {
|
|
336 for(int n=0; n<pp->info.size; n++)
|
|
337 {
|
68
|
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);
|
53
|
341
|
|
342 Triangle *tri = new Triangle(ver1,ver2,ver3);
|
68
|
343 Span_c *span = new Span_c();
|
53
|
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;
|
|
352 }
|
|
353 }
|
|
354
|
|
355
|
68
|
356 void Polygon::draw(SPANPACK *sp)
|
|
357 {
|
|
358 for(int n=0; n<sp->info.size; n++)
|
|
359 {
|
|
360 int end = sp->span[n].length_x;
|
|
361 Uint32 rgb;
|
|
362 float tex1 = sp->span[n].tex_x1;
|
|
363 float tex2= sp->span[n].tex_x2;
|
|
364 float tey1 = sp->span[n].tex_y1;
|
|
365 float tey2= sp->span[n].tex_y2;
|
|
366 int tex_xpos;
|
|
367 int tex_ypos;
|
|
368 int tex_zpos;
|
|
369 int x = sp->span[n].x;
|
|
370 int y = sp->span[n].y;
|
|
371 float z = sp->span[n].start_z;
|
|
372 float zpos = sp->span[n].end_z;
|
|
373 float tex_x,tex_y,tex_z;
|
|
374
|
|
375 if(end == 1) {
|
|
376 //printf("end == 1\n");
|
|
377 //printf("tex_x:%f tex_y:%f\n",tex1,tex2);
|
|
378 //if(tex1 > 1) tex1 = 1;
|
|
379 //if(tey1 > 1) tey1 = 1;
|
|
380 tex_xpos = (int)((sp->span[n].tex_height-1) * tex1);
|
|
381 tex_ypos = (int)((sp->span[n].tex_width-1) * tey1);
|
|
382 tex_zpos = (int)z;
|
|
383 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
384 //printf("image->h:%d tex_x:%f\n",(int)sp->span[n].tex_height,tex1);
|
|
385 rgb = get_rgb(tex_xpos,tex_ypos);
|
|
386 viewer->write_pixel(x,y,zpos,rgb);
|
|
387 }else {
|
|
388 //printf("end != 1\n");
|
|
389 for(int j = 0; j < end; j++) {
|
|
390 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
391 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
392 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
393 if(tex_x > 1) tex_x = 1;
|
|
394 if(tex_y > 1) tex_y = 1;
|
|
395 tex_xpos = (int)((sp->span[n].tex_height-1) * tex_x);
|
|
396 tex_ypos = (int)((sp->span[n].tex_width-1) * tex_y);
|
|
397 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
398 //printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
|
|
399 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
400 rgb = get_rgb(tex_xpos,tex_ypos);
|
|
401 viewer->write_pixel(j+x,y,tex_z,rgb);
|
|
402 }
|
|
403 }
|
|
404 }
|
|
405 }
|
|
406
|
|
407
|
53
|
408 /*
|
|
409 void Polygon::create_scene_graph_pack()
|
|
410 {
|
|
411 //SceneGraphPack *sgp = new SceneGraphPack;
|
|
412 sgp = new SceneGraphPack;
|
|
413 int i = 0;
|
|
414 int nnpn = -1;
|
|
415
|
|
416 Polygon *t;
|
|
417 t = this;
|
|
418
|
|
419 while(t)
|
|
420 {
|
|
421 //SceneGraphNode node;
|
|
422 sgp->node[i].size = t->size;
|
|
423 int d,tex;
|
|
424 for(d=0,tex=0; d<t->size*3; d+=3,tex+=2)
|
|
425 {
|
|
426 sgp->node[i].vertex[d] = t->data[d];
|
|
427 sgp->node[i].vertex[d+1] = t->data[d+1];
|
|
428 sgp->node[i].vertex[d+2] = t->data[d+2];
|
|
429 sgp->node[i].texture[tex] = t->data[d+t->size*6];
|
|
430 sgp->node[i].texture[tex+1] = t->data[d+t->size*6+1];
|
|
431 }
|
|
432
|
|
433 sgp->node[i].obj_pos[0] = 0;
|
|
434 sgp->node[i].obj_pos[1] = 0;
|
|
435 sgp->node[i].obj_pos[2] = 0;
|
|
436 sgp->node[i].obj_pos[3] = 1;
|
|
437 sgp->node[i].angle[0] = 0;
|
|
438 sgp->node[i].angle[1] = 0;
|
|
439 sgp->node[i].angle[2] = 0;
|
|
440 sgp->node[i].angle[3] = 1;
|
|
441
|
|
442 for(int tm=0; tm<16; tm++)
|
|
443 {
|
|
444 sgp->node[i].translation[tm] = 0;
|
|
445 }
|
|
446 sgp->node[i].id = 0;
|
|
447 sgp->node[i].move = 0;
|
|
448 sgp->node[i].interaction = 0;
|
|
449 sgp->node[i].pn = nnpn;
|
|
450
|
|
451 if(t->child != NULL)
|
|
452 {
|
|
453 nnpn = i;
|
|
454 t = t->child;
|
|
455 }
|
|
456 else if(t->brother != NULL)
|
|
457 {
|
|
458 nnpn = sgp->node[i].pn;
|
|
459 t = t->brother;
|
|
460 }
|
|
461 else
|
|
462 {
|
|
463 while(t)
|
|
464 {
|
|
465 if(t->brother != NULL)
|
|
466 {
|
|
467 t = t->brother;
|
|
468 break;
|
|
469 }
|
|
470 else
|
|
471 {
|
|
472 if(t->parent == NULL)
|
|
473 {
|
|
474 t = NULL;
|
|
475 break;
|
|
476 }
|
|
477 nnpn = sgp->node[nnpn].pn;
|
|
478 t = t->parent;
|
|
479 }
|
|
480 }
|
|
481 }
|
|
482 //printf("sgp->node[%d].pn = %d\n", i, sgp->node[i].pn);
|
|
483 i++;
|
|
484 }
|
|
485 sgp->info.size = i;
|
|
486
|
|
487
|
|
488 //for(int s=0; s<i; s++)
|
|
489 //{
|
|
490 //printf("sgp.node[%d].pn = %d\n",s,sgp.node[s].pn);
|
|
491 //}
|
|
492
|
|
493 //delete sgp;
|
|
494 }
|
|
495 */
|
|
496
|
|
497
|
|
498 void Polygon::add_next()
|
|
499 {
|
|
500 Polygon *p;
|
|
501 p = new Polygon;
|
|
502
|
|
503 next = p;
|
|
504 }
|
|
505
|
|
506
|
|
507 void Polygon::create_tree()
|
|
508 {
|
|
509 Polygon *list;
|
|
510 Polygon *check_list;
|
|
511 Polygon *p;
|
|
512 Polygon *bros;
|
|
513
|
|
514 check_list = this;
|
|
515
|
|
516 for(list=this; list; list=list->next)
|
|
517 {
|
|
518 if(xmlStrcmp((xmlChar *)list->parent_name, (xmlChar *)"NULL"))
|
|
519 {
|
|
520 p = this;
|
|
521 while(p)
|
|
522 {
|
|
523 if(!xmlStrcmp((xmlChar *)p->name,(xmlChar *)list->parent_name))
|
|
524 {
|
|
525 list->parent = p;
|
|
526 if(p->child == NULL)
|
|
527 {
|
|
528 p->child = list;
|
|
529 }
|
|
530 else
|
|
531 {
|
|
532 bros = p->child;
|
|
533 while(bros->brother != NULL)
|
|
534 {
|
|
535 bros = bros->brother;
|
|
536 }
|
|
537 bros->brother = list;
|
|
538 }
|
|
539 break;
|
|
540 }
|
|
541 p = p->next;
|
|
542 }
|
|
543 }
|
|
544 }
|
|
545 }
|
|
546
|
|
547 /*
|
|
548 void Polygon::create_scene_graph_pack()
|
|
549 {
|
|
550 SceneGraphPack sgp;
|
|
551 int i = 0;
|
|
552 int nnpn = -1;
|
|
553
|
|
554 Polygon *t;
|
|
555 t = this;
|
|
556 while(t)
|
|
557 {
|
|
558 //t->draw();
|
|
559 SceneGraphNode node;
|
|
560 node.obj_pos[0] = 0;
|
|
561 node.obj_pos[1] = 0;
|
|
562 node.obj_pos[2] = 0;
|
|
563 node.obj_pos[3] = 1;
|
|
564 for(int tm=0; tm<16; tm++)
|
|
565 {
|
|
566 node.translation[tm] = 0;
|
|
567 }
|
|
568 node.id = 0;
|
|
569 node.move = 0;
|
|
570 node.interaction = 0;
|
|
571 for(int a=0; a<10; a++)
|
|
572 {
|
|
573 node.op[a] = -1;
|
|
574 }
|
|
575 node.pn = nnpn;
|
|
576 int p = 0;
|
|
577
|
|
578 if(t->child != NULL)
|
|
579 {
|
|
580 //glPushMatrix();
|
|
581 //node.op[p] = PUSH;
|
|
582 //nnpn = i;
|
|
583 t = t->child;
|
|
584 }
|
|
585 else if(t->brother != NULL)
|
|
586 {
|
|
587 //glPopMatrix();
|
|
588 //glPushMatrix();
|
|
589 //node.op[p] = SHIFT;
|
|
590 //nnpn = node.pn;
|
|
591 t = t->brother;
|
|
592 }
|
|
593 else
|
|
594 {
|
|
595 while(t)
|
|
596 {
|
|
597 if(t->brother != NULL)
|
|
598 {
|
|
599 //glPopMatrix();
|
|
600 //glPushMatrix();
|
|
601 //node.op[p] = SHIFT;
|
|
602 t = t->brother;
|
|
603 break;
|
|
604 }
|
|
605 else
|
|
606 {
|
|
607 t = t->parent;
|
|
608 if(t)
|
|
609 {
|
|
610 //glPopMatrix();
|
|
611 //node.op[p] = POP;
|
|
612 //nnpn = sgp.node[nnpn].pn;
|
|
613 p++;
|
|
614 }
|
|
615 }
|
|
616 }
|
|
617 }
|
|
618 sgp.node[i] = node;
|
|
619 i++;
|
|
620 }
|
|
621
|
|
622 sgp.info.size = i;
|
|
623
|
|
624 for(int s=0; s<4; s++)
|
|
625 {
|
|
626 for(int a=0; sgp.node[s].op[a]!=-1; a++)
|
|
627 {
|
|
628 printf("sgp.node[%d].op[%d] = %d\n",s, a, sgp.node[s].op[a]);
|
|
629 }
|
|
630 //printf("sgp.node[%d].pn = %d\n",s,sgp.node[s].pn);
|
|
631 }
|
|
632 }
|
|
633 */
|
|
634
|
|
635
|
|
636
|
|
637 void Polygon::pickup_coordinate(char *cont)
|
|
638 {
|
|
639 for(int n=0; n<size*3; n+=3)
|
|
640 {
|
|
641 cont = pickup_float(cont, data+n);
|
|
642 cont = pickup_float(cont, data+n+1);
|
|
643 cont = pickup_float(cont, data+n+2);
|
|
644
|
|
645 if (cont == NULL)
|
|
646 {
|
|
647 cout << "Analyzing obj data failed coordinate\n";
|
|
648 }
|
|
649 }
|
|
650 }
|
|
651
|
|
652 void Polygon::pickup_normal(char *cont)
|
|
653 {
|
|
654 for(int n=size*3;n<size*6;n+=3)
|
|
655 {
|
|
656 cont = pickup_float(cont,data+n);
|
|
657 cont = pickup_float(cont,data+n+1);
|
|
658 cont = pickup_float(cont,data+n+2);
|
|
659
|
|
660 if (cont == NULL)
|
|
661 {
|
|
662 cout << "Analyzing obj data failed normal\n";
|
|
663 }
|
|
664 }
|
|
665 }
|
|
666
|
|
667 void Polygon::pickup_model(char *cont)
|
|
668 {
|
|
669 cont = pickup_float(cont,c_xyz);
|
|
670 cont = pickup_float(cont,c_xyz+1);
|
|
671 cont = pickup_float(cont,c_xyz+2);
|
|
672
|
|
673 if (cont == NULL)
|
|
674 {
|
|
675 cout << "Analyzing obj data failed model\n";
|
|
676 }
|
|
677 }
|
|
678
|
|
679 void Polygon::pickup_texture(char *cont)
|
|
680 {
|
|
681 for(int n=size*6; n<size*9; n+=3)
|
|
682 {
|
|
683 cont = pickup_float(cont,data+n);
|
|
684 cont = pickup_float(cont,data+n+1);
|
|
685 data[n+2] = 1.0;
|
|
686
|
|
687 if (cont == NULL)
|
|
688 {
|
|
689 cout << "Analyzing obj data failed texture\n";
|
|
690 }
|
|
691 }
|
|
692 }
|
|
693
|
|
694 char *get_pixel(int tx, int ty, SDL_Surface *texture_image)
|
|
695 {
|
|
696 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx));
|
|
697 }
|
|
698
|
|
699 unsigned my_ntohl(unsigned u) {
|
|
700 // rr gg bb 00
|
|
701 // rr
|
|
702 // bb gg rr
|
|
703 //unsigned u1 = ((u&0xff)<<24) +
|
|
704 // ((u&0xff00)<<8) +
|
|
705 // ((u&0xff0000)>>8) +
|
|
706 // ((u&0xff000000)>>24);
|
|
707 unsigned u1;
|
|
708 unsigned b = (u&0xff000000)>>24;
|
|
709 unsigned g = (u&0xff0000)>>16;
|
|
710 unsigned r = (u&0xff00)>>8;
|
|
711 u1 = r + (g<<8) + (b<<16);
|
|
712 //printf("pixel %x->%x\n",u,u1);
|
|
713 return u1;
|
|
714 }
|
|
715
|
|
716 Uint32 Polygon::get_rgb(int tx, int ty)
|
|
717 {
|
|
718 SDL_PixelFormat *fmt;
|
|
719 //Uint32 temp, pixel;
|
|
720 Uint8 red, green, blue;
|
|
721
|
|
722 fmt = texture_image->format;
|
|
723
|
|
724 if (tx<0) tx = 0;
|
|
725 if (texture_image->w-1< tx) tx = texture_image->w-1 ;
|
|
726 if (ty<0) ty = 0;
|
|
727 if (texture_image->h-1< ty) ty = texture_image->h-1 ;
|
|
728
|
|
729
|
|
730
|
|
731 //SDL_LockSurface(texture_image);
|
|
732 char *p = get_pixel(tx,ty,texture_image);
|
|
733 #if 0
|
|
734 pixel = my_ntohl(*(Uint32*)p);
|
|
735 //printf("pixel = %d\n", pixel);
|
|
736 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel);
|
|
737 //SDL_UnlockSurface(texture_image);
|
|
738
|
|
739 temp = pixel&fmt->Rmask;
|
|
740 temp = temp>>fmt->Rshift;
|
|
741 temp = temp<<fmt->Rloss;
|
|
742 red = (Uint8)temp;
|
|
743
|
|
744 temp = pixel&fmt->Gmask;
|
|
745 temp = temp>>fmt->Gshift;
|
|
746 temp = temp<<fmt->Gloss;
|
|
747 green = (Uint8)temp;
|
|
748
|
|
749 temp = pixel&fmt->Bmask;
|
|
750 temp = temp>>fmt->Bshift;
|
|
751 temp = temp<<fmt->Bloss;
|
|
752 blue = (Uint8)temp;
|
|
753 #endif
|
|
754 blue = (Uint8) p[0];
|
|
755 green = (Uint8) p[1];
|
|
756 red = (Uint8) p[2];
|
|
757
|
|
758 //printf("tx = %d ty = %d ", tx,ty);
|
|
759 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue);
|
|
760
|
|
761 SDL_PixelFormat *pf;
|
|
762 pf = viewer->screen->format;
|
|
763
|
|
764 //cout << SDL_MapRGB(pf, red, green, blue) << endl;
|
|
765 return SDL_MapRGB(pf, red, green, blue);
|
|
766 }
|
|
767
|
|
768 void Polygon::get_data(xmlNodePtr cur)
|
|
769 {
|
|
770 char *cont;
|
|
771 //char *image_name;
|
|
772
|
|
773 for(;cur;cur=cur->next)
|
|
774 {
|
|
775 if(!xmlStrcmp(cur->name,(xmlChar*)"coordinate"))
|
|
776 {
|
|
777 cont = (char *)xmlNodeGetContent(cur);
|
|
778 pickup_coordinate(cont);
|
|
779 }
|
|
780 else if(!xmlStrcmp(cur->name,(xmlChar*)"normal"))
|
|
781 {
|
|
782 cont = (char *)xmlNodeGetContent(cur);
|
|
783 pickup_normal(cont);
|
|
784 }
|
|
785 else if(!xmlStrcmp(cur->name,(xmlChar*)"model"))
|
|
786 {
|
|
787 cont = (char *)xmlNodeGetContent(cur);
|
|
788 pickup_model(cont);
|
|
789 }
|
|
790 else if(!xmlStrcmp(cur->name,(xmlChar*)"texture"))
|
|
791 {
|
|
792 cont = (char *)xmlNodeGetContent(cur);
|
|
793 pickup_texture(cont);
|
|
794 }
|
|
795 else if(!xmlStrcmp(cur->name,(xmlChar*)"image"))
|
|
796 {
|
|
797 char image_name[20] = "/tmp/image_XXXXXX";
|
|
798 int fd = mkstemp(image_name);
|
|
799 FILE *outfile = fdopen(fd, "wb");
|
|
800 if(NULL == outfile)
|
|
801 {
|
|
802 cout << "error open file\n";
|
|
803 }
|
|
804 cont = (char *)xmlNodeGetContent(cur);
|
|
805 //decode(cont, image_name);
|
|
806 decode(cont, outfile);
|
|
807 fclose(outfile);
|
|
808
|
|
809 texture_image = IMG_Load(image_name);
|
|
810
|
|
811 //load_texture(image_name);
|
|
812 if(unlink(image_name))
|
|
813 {
|
|
814 cout << "unlink error\n";
|
|
815 }
|
|
816 }
|
|
817 }
|
|
818 }
|
|
819
|
|
820
|
|
821 void Polygon::create_data(xmlNodePtr cur)
|
|
822 {
|
|
823 size = atoi((char *)xmlGetProp(cur,(xmlChar *)"size"));
|
|
824 name = (char *)xmlGetProp(cur,(xmlChar *)"name");
|
|
825 parent_name = (char *)xmlGetProp(cur,(xmlChar *)"parent");
|
|
826 next = NULL;
|
|
827
|
|
828 data = new float[size*3*3];
|
|
829
|
|
830 get_data(cur->children);
|
|
831 }
|
|
832
|
|
833 void Polygon::set_data(char *file_name)
|
|
834 {
|
|
835 xmlDocPtr doc;
|
|
836 xmlNodePtr cur;
|
|
837 Polygon *tmp;
|
|
838
|
|
839 doc = xmlParseFile(file_name);
|
|
840
|
|
841 cur = xmlDocGetRootElement(doc);
|
|
842
|
|
843 xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
|
|
844
|
|
845 tmp = this;
|
|
846
|
|
847 for (cur=cur->children; cur; cur=cur->next)
|
|
848 {
|
|
849 if (!xmlStrcmp(cur->name,(xmlChar*)"surface"))
|
|
850 {
|
|
851 tmp->create_data(cur);
|
|
852 if(cur->next->next)
|
|
853 {
|
|
854 tmp->add_next();
|
|
855 tmp = tmp->next;
|
|
856 }
|
|
857 }
|
|
858 }
|
|
859
|
|
860 create_tree();
|
|
861 //tree_check();
|
|
862 //create_scene_graph_pack();
|
|
863
|
|
864 xmlFreeDoc(doc);
|
|
865
|
|
866 /*
|
|
867 for(int s=0; s<sgp->info.size; s++)
|
|
868 {
|
|
869 printf("sgp->node[%d].pn = %d\n",s,sgp->node[s].pn);
|
|
870 }
|
|
871 */
|
|
872 //delete sgp;
|
|
873 }
|
|
874
|
|
875 void Polygon::delete_data()
|
|
876 {
|
|
877 Polygon *n,*m;
|
|
878
|
|
879 n = this;
|
|
880 delete [] n->data;
|
|
881
|
|
882 if(next)
|
|
883 {
|
|
884 for(n = this->next; n; n=m)
|
|
885 {
|
|
886 m = n->next;
|
|
887 delete [] n->data;
|
|
888 delete n;
|
|
889 }
|
|
890 }
|
|
891 }
|