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