Mercurial > hg > Game > Cerium
annotate Renderer/Engine/Collada.cc @ 1398:e19296785c07 draft
collada can move
author | e095732 <e095732@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 05 Feb 2012 23:34:16 +0900 |
parents | ad841dcdbe67 |
children | d6170e1200a0 |
rev | line source |
---|---|
1376 | 1 #include "polygon.h" |
2 #include "SceneGraph.h" | |
3 #include "SceneGraph.cc" | |
4 #include "TextureHash.h" | |
5 #include "xml.h" | |
6 #include "SceneGraphRoot.h" | |
7 #include <iostream> | |
8 #include <SDL_image.h> | |
9 | |
10 using namespace std; | |
11 static TextureHash sgid_hash; | |
12 | |
13 extern int is_bmp(const char* name); | |
14 extern void make_black_alpha(SDL_Surface *texture_image); | |
15 | |
1398 | 16 typedef struct p_info { |
17 p_info(){ | |
18 x = 0; | |
19 y = 0; | |
20 z = 0; | |
21 } | |
22 float x; | |
23 float y; | |
24 float z; | |
25 }; | |
26 | |
1376 | 27 typedef struct source { |
28 char *id; | |
29 union { | |
30 float *array; | |
31 char *alias; | |
32 }u; | |
33 int count; | |
34 struct source *next; | |
35 } SOURCE; | |
36 typedef SOURCE *SOURCE_P; | |
37 | |
38 typedef struct list { | |
39 SOURCE_P first; | |
40 SOURCE_P end; | |
41 } LIST; | |
42 typedef LIST *LIST_P; | |
43 | |
44 struct collada_state { | |
45 collada_state(){ | |
46 polylist = 0; | |
47 | |
48 vertex_offset = -1; | |
49 vertex_count = 0; | |
50 | |
51 normal_offset = -1; | |
52 normal_count = 0; | |
53 | |
54 texcoord_offset = -1; | |
55 texcoord_count = 0; | |
56 | |
57 polylist_count = 0; | |
58 vcsum = 0; | |
59 | |
60 limit = 0; | |
61 vmember = 0; | |
1390 | 62 vtable_size =0; |
1376 | 63 |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
64 images_flag=0; |
1376 | 65 } |
66 int polylist; | |
67 int library_images; | |
68 xmlChar *vertices_id; | |
69 | |
70 char *vertex_src; | |
71 int vertex_offset; | |
72 int vertex_count; | |
73 | |
74 char *normal_src; | |
75 int normal_offset; | |
76 int normal_count; | |
77 | |
78 char *texcoord_src; | |
79 int texcoord_offset; | |
80 int texcoord_count; | |
81 | |
82 float *vcount; | |
83 float *pcount; | |
84 | |
85 SOURCE_P normal_float; | |
86 SOURCE_P vertex_float; | |
87 SOURCE_P texcoord_float; | |
88 | |
89 char *vertices_src; | |
90 int polylist_count; | |
91 | |
92 char *name; | |
93 char *tex_picname; | |
94 | |
95 int images_flag; | |
96 | |
97 int vcsum; | |
98 int limit; | |
99 int vmember; | |
1390 | 100 int vtable_size; |
1376 | 101 }; |
102 | |
103 /*static const char* | |
104 get_property(const char *name, xmlNodePtr cur){ | |
105 xmlAttr *p=cur->properties; | |
106 if (p==0) return ""; | |
107 for ( ;p; p=p->next) { | |
108 if ( xmlStrcmp(p->name, (xmlChar*)name) !=0 ) { | |
109 xmlNode* n=p->children; | |
110 if ( n==NULL ) return ""; | |
111 const char * v=(const char*)n->content; | |
112 if ( v==NULL ) return ""; | |
113 return v; | |
114 } | |
115 } | |
116 return ""; | |
117 }*/ | |
118 | |
119 SDL_Surface* | |
120 load_image(const char *file_name, const char *image_name) | |
121 { | |
122 int alpha_black = is_bmp(file_name); | |
123 /** | |
124 * image を 32bit(RGBA) に変換する | |
125 */ | |
126 SDL_Surface *texture_image = IMG_Load(file_name); | |
127 if (!texture_image) return 0; | |
128 SDL_Surface *tmpImage | |
129 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w, | |
130 texture_image->h, 32, redMask, | |
131 greenMask, blueMask, alphaMask); | |
132 //= SDL_CreateRGBSurface(SDL_HWSURFACE, 0, | |
133 // 0, 32, redMask, | |
134 // greenMask, blueMask, alphaMask); | |
135 SDL_Surface *converted; | |
136 converted = SDL_ConvertSurface(texture_image, tmpImage->format, | |
137 SDL_HWSURFACE); | |
138 //SDL_SetAlpha(converted, 0, 0); | |
139 | |
140 if (converted != NULL) { | |
141 SDL_FreeSurface(texture_image); | |
142 texture_image = converted; | |
143 } | |
144 if (alpha_black) { | |
145 make_black_alpha(texture_image); | |
146 } | |
147 // this->gl_tex = SDL_GL_LoadTexture(texture_image); | |
148 return texture_image; | |
149 } | |
150 | |
151 /* add source list */ | |
152 static void | |
153 addSource(LIST_P list, SOURCE_P src) { | |
154 if (list->first == NULL && list->end == NULL) { | |
155 list->first = list->end = src; | |
156 return; | |
157 } | |
158 list->end->next = src; | |
159 list->end = src; | |
160 } | |
161 | |
162 /* compare a with b. Using to compare id */ | |
163 /* | |
164 static int | |
165 strcmp_a(const char *a, const char *b) | |
166 { | |
167 while (*a && *a++ == *b++); | |
168 if (*a) return 0; | |
169 return a[-1] > b[-1] ? 1:-1; | |
170 } | |
171 static float | |
172 get_point(char *id, int position, LIST_P list) | |
173 { | |
174 SOURCE_P cur = list->first; | |
175 for (;cur ; cur=cur->next) { | |
176 if (!strcmp_a(id, cur->id)) { | |
177 if (cur->count == 0) //alias | |
178 return get_point(cur->u.alias, position, list); | |
179 float *a = cur->u.array; | |
180 if (position <= cur->count) { | |
181 return a[position]; | |
182 } | |
183 } | |
184 } | |
185 } | |
186 */ | |
187 | |
188 static SOURCE_P | |
189 most_match(const char *id , LIST_P list) | |
190 { | |
191 SOURCE_P src = NULL; | |
192 SOURCE_P cur = NULL; | |
193 int tmplength = 0; | |
194 int strlength; | |
195 | |
196 for (cur=list->first ;cur!=list->end ;cur=cur->next) { | |
197 for (strlength=0;id[strlength]==cur->id[strlength];strlength++); | |
198 if (tmplength < strlength) { | |
199 tmplength = strlength; | |
200 src = cur; | |
201 } | |
202 } | |
1378 | 203 if (src == NULL){ |
204 fprintf(stderr,"not match"); | |
205 } | |
1376 | 206 return src; |
207 } | |
208 | |
209 void get_texture_image(char *filename, SceneGraphPtr sg, xmlNodePtr cur, TaskManager *manager) | |
210 { | |
211 char image_name[20] = "/tmp/image_XXXXXX"; | |
212 if (filename == NULL || filename[0] == 0) { | |
213 return; | |
214 } | |
215 /** | |
216 * image_name を既に Load していれば何もしない | |
217 */ | |
1391 | 218 int tex_id; |
1376 | 219 /* ball test */ |
1382 | 220 if (sgid_hash.sg_hash_regist(filename, tex_id) == -1) { |
221 SDL_Surface *texture_image = load_image(filename, image_name); | |
222 if (texture_image==0) { | |
1376 | 223 printf("Can't load image %s\n",filename); |
224 exit(0); | |
1382 | 225 } |
1376 | 226 sg->texture_info->texture_id = sg->makeTapestries(manager, texture_image, tex_id); |
1382 | 227 tex_id = sg->texture_info->texture_id; |
1376 | 228 if (unlink(image_name)) { |
1382 | 229 printf("unlink error\n"); |
1376 | 230 } |
231 } else { | |
232 /** | |
233 * 以前に Load されている Texture を共用 | |
234 */ | |
235 sg->texture_info->texture_id = tex_id; | |
236 } | |
1382 | 237 // 微妙に思う、自分で書き換えた感想 by gongo |
238 sg->texture_info->t_w = list[tex_id].t_w; | |
239 sg->texture_info->t_h = list[tex_id].t_h;; | |
240 sg->texture_info->pixels_orig = list[tex_id].pixels_orig; | |
241 sg->texture_info->pixels = list[tex_id].pixels; | |
242 sg->texture_info->scale_max = list[tex_id].scale_max; | |
243 sg->texture_info->texture_image = list[tex_id].texture_image; | |
1376 | 244 } |
245 | |
246 void decode_float_array(xmlNodePtr cur,LIST_P list){ | |
247 SOURCE_P src = (SOURCE_P)malloc(sizeof(SOURCE)); | |
248 src->id = (char*)xmlGetProp(cur, (xmlChar*)"id"); | |
249 | |
250 src->count = atoi((char*)xmlGetProp(cur, (xmlChar*)"count")); | |
251 src->u.array = new float[src->count]; | |
252 char *cont =(char*)xmlNodeGetContent(cur); | |
1384 | 253 |
1376 | 254 /* store float inpoint list */ |
255 for (int i = 0; cont != NULL; i++) { | |
1398 | 256 cont = pickup_float(cont, src->u.array+i); |
1376 | 257 } |
258 | |
259 src->next = NULL; | |
260 addSource(list, src); | |
261 } | |
262 | |
263 | |
264 void | |
265 get_points(xmlNodePtr cur, collada_state *s, TaskManager *manager){ | |
266 char *pcont = (char*)xmlNodeGetContent(cur); | |
267 for (int i = 0;i < s->polylist_count;i++){ | |
268 s->vcsum += s->vcount[i]; | |
269 s->vmember = i; | |
1390 | 270 if (s->vcount[i]==4){ |
271 s->vtable_size +=6; | |
272 } else { | |
273 s->vtable_size +=3; | |
274 } | |
1376 | 275 } |
276 s->limit = s->vcsum * 2; | |
277 if (s->texcoord_offset == 2){ | |
278 s->limit = s->vcsum * 3; | |
279 } | |
280 s->pcount = new float[s->limit]; | |
281 for (int i=0;i<s->limit;i++){ | |
282 s->pcount[i] = 0; | |
283 } | |
284 for (int i=0; pcont != NULL; i++) { | |
285 pcont = pickup_float(pcont, s->pcount+i); | |
286 } | |
287 } | |
288 SceneGraph* | |
289 decode_points(collada_state *s, TaskManager *manager, SceneGraphPtr sg){ | |
1390 | 290 |
1376 | 291 /* |
292 * vertex_tableだけはpolygonを作る際にvcountが4の場合重複する点が | |
293 * 出てくるのでサイズを2倍用意しておく | |
294 */ | |
1398 | 295 /* |
1376 | 296 float *vertex_table; |
297 float *normal_table; | |
1398 | 298 float *texcoord_table; |
1390 | 299 vertex_table = new float[s->vtable_size*3]; |
300 normal_table = new float[s->vtable_size*3]; | |
301 texcoord_table = new float[s->vtable_size*2]; | |
302 bzero(vertex_table,sizeof(float)*s->vtable_size*3); | |
303 bzero(normal_table,sizeof(float)*s->vtable_size*3); | |
304 bzero(texcoord_table,sizeof(float)*s->vtable_size*2); | |
1398 | 305 */ |
306 p_info *vertex_p; | |
307 p_info *normal_p; | |
308 p_info *texcoord_p; | |
309 vertex_p = new p_info[s->vtable_size]; | |
310 normal_p = new p_info[s->vtable_size]; | |
311 texcoord_p = new p_info[s->vtable_size]; | |
1376 | 312 |
1392 | 313 /* p separate vertex position and nomal position. */ |
1376 | 314 /* make triangle */ |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
315 int k=0,j=0,l=0,count=0; |
1392 | 316 if (s->texcoord_offset == 2){ |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
317 for (int i=0;i<s->polylist_count;i++) { |
1392 | 318 if (s->vcount[i] == 4) { |
1398 | 319 vertex_p[k].x = s->vertex_float->u.array[(int)s->pcount[l]*3]; |
320 vertex_p[k].y = s->vertex_float->u.array[(int)s->pcount[l]*3+1]; | |
321 vertex_p[k].z = s->vertex_float->u.array[(int)s->pcount[l]*3+2]; | |
322 vertex_p[k+1].x = s->vertex_float->u.array[(int)s->pcount[l+3]*3]; | |
323 vertex_p[k+1].y = s->vertex_float->u.array[(int)s->pcount[l+3]*3+1]; | |
324 vertex_p[k+1].z = s->vertex_float->u.array[(int)s->pcount[l+3]*3+2]; | |
325 vertex_p[k+2].x = s->vertex_float->u.array[(int)s->pcount[l+6]*3]; | |
326 vertex_p[k+2].y = s->vertex_float->u.array[(int)s->pcount[l+6]*3+1]; | |
327 vertex_p[k+2].z = s->vertex_float->u.array[(int)s->pcount[l+6]*3+2]; | |
328 | |
329 vertex_p[k+3].x = s->vertex_float->u.array[(int)s->pcount[l+6]*3]; | |
330 vertex_p[k+3].y = s->vertex_float->u.array[(int)s->pcount[l+6]*3+1]; | |
331 vertex_p[k+3].z = s->vertex_float->u.array[(int)s->pcount[l+6]*3+2]; | |
332 vertex_p[k+4].x = s->vertex_float->u.array[(int)s->pcount[l+9]*3]; | |
333 vertex_p[k+4].y = s->vertex_float->u.array[(int)s->pcount[l+9]*3+1]; | |
334 vertex_p[k+4].z = s->vertex_float->u.array[(int)s->pcount[l+9]*3+2]; | |
335 vertex_p[k+5].x = s->vertex_float->u.array[(int)s->pcount[l]*3]; | |
336 vertex_p[k+5].y = s->vertex_float->u.array[(int)s->pcount[l]*3+1]; | |
337 vertex_p[k+5].z = s->vertex_float->u.array[(int)s->pcount[l]*3+2]; | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
338 |
1398 | 339 normal_p[k].x = s->normal_float->u.array[(int)s->pcount[l+1]*3]; |
340 normal_p[k].y = s->normal_float->u.array[(int)s->pcount[l+1]*3+1]; | |
341 normal_p[k].z = s->normal_float->u.array[(int)s->pcount[l+1]*3+2]; | |
342 normal_p[k+1].x = s->normal_float->u.array[(int)s->pcount[l+4]*3]; | |
343 normal_p[k+1].y = s->normal_float->u.array[(int)s->pcount[l+4]*3+1]; | |
344 normal_p[k+1].z = s->normal_float->u.array[(int)s->pcount[l+4]*3+2]; | |
345 normal_p[k+2].x = s->normal_float->u.array[(int)s->pcount[l+7]*3]; | |
346 normal_p[k+2].y = s->normal_float->u.array[(int)s->pcount[l+7]*3+1]; | |
347 normal_p[k+2].z = s->normal_float->u.array[(int)s->pcount[l+7]*3+2]; | |
1392 | 348 |
1398 | 349 normal_p[k+3].x = s->normal_float->u.array[(int)s->pcount[l+7]*3]; |
350 normal_p[k+3].y = s->normal_float->u.array[(int)s->pcount[l+7]*3+1]; | |
351 normal_p[k+3].z = s->normal_float->u.array[(int)s->pcount[l+7]*3+2]; | |
352 normal_p[k+4].x = s->normal_float->u.array[(int)s->pcount[l+10]*3]; | |
353 normal_p[k+4].y = s->normal_float->u.array[(int)s->pcount[l+10]*3+1]; | |
354 normal_p[k+4].z = s->normal_float->u.array[(int)s->pcount[l+10]*3+2]; | |
355 normal_p[k+5].x = s->normal_float->u.array[(int)s->pcount[l+1]*3]; | |
356 normal_p[k+5].y = s->normal_float->u.array[(int)s->pcount[l+1]*3+1]; | |
357 normal_p[k+5].z = s->normal_float->u.array[(int)s->pcount[l+1]*3+2]; | |
358 | |
359 texcoord_p[j].x = s->texcoord_float->u.array[(int)s->pcount[l+2]*2]; | |
360 texcoord_p[j].y = s->texcoord_float->u.array[(int)s->pcount[l+2]*2]+1; | |
361 texcoord_p[j+1].x = s->texcoord_float->u.array[(int)s->pcount[l+5]*2]; | |
362 texcoord_p[j+1].y = s->texcoord_float->u.array[(int)s->pcount[l+5]*2]+1; | |
363 texcoord_p[j+2].x = s->texcoord_float->u.array[(int)s->pcount[l+8]*2]; | |
364 texcoord_p[j+2].y = s->texcoord_float->u.array[(int)s->pcount[l+8]*2]+1; | |
365 texcoord_p[j+3].x = s->texcoord_float->u.array[(int)s->pcount[l+11]*2]; | |
366 texcoord_p[j+3].y = s->texcoord_float->u.array[(int)s->pcount[l+11]*2]+1; | |
367 | |
368 l+=12;j+=4;k+=6; | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
369 count +=2; |
1392 | 370 } else if (s->vcount[i]==3) { |
1398 | 371 vertex_p[k].x = s->vertex_float->u.array[(int)s->pcount[l]*3]; |
372 vertex_p[k].y = s->vertex_float->u.array[(int)s->pcount[l]*3+1]; | |
373 vertex_p[k].z = s->vertex_float->u.array[(int)s->pcount[l]*3+2]; | |
374 vertex_p[k+1].x = s->vertex_float->u.array[(int)s->pcount[l+3]*3]; | |
375 vertex_p[k+1].y = s->vertex_float->u.array[(int)s->pcount[l+3]*3+1]; | |
376 vertex_p[k+1].z = s->vertex_float->u.array[(int)s->pcount[l+3]*3+2]; | |
377 vertex_p[k+2].x = s->vertex_float->u.array[(int)s->pcount[l+6]*3]; | |
378 vertex_p[k+2].y = s->vertex_float->u.array[(int)s->pcount[l+6]*3+1]; | |
379 vertex_p[k+2].z = s->vertex_float->u.array[(int)s->pcount[l+6]*3+2]; | |
1392 | 380 |
1398 | 381 normal_p[k].x = s->normal_float->u.array[(int)s->pcount[l+1]*3]; |
382 normal_p[k].y = s->normal_float->u.array[(int)s->pcount[l+1]*3+1]; | |
383 normal_p[k].z = s->normal_float->u.array[(int)s->pcount[l+1]*3+2]; | |
384 normal_p[k+1].x = s->normal_float->u.array[(int)s->pcount[l+4]*3]; | |
385 normal_p[k+1].y = s->normal_float->u.array[(int)s->pcount[l+4]*3+1]; | |
386 normal_p[k+1].z = s->normal_float->u.array[(int)s->pcount[l+4]*3+2]; | |
387 normal_p[k+2].x = s->normal_float->u.array[(int)s->pcount[l+7]*3]; | |
388 normal_p[k+2].y = s->normal_float->u.array[(int)s->pcount[l+7]*3+1]; | |
389 normal_p[k+2].z = s->normal_float->u.array[(int)s->pcount[l+7]*3+2]; | |
390 | |
391 texcoord_p[j].x = s->texcoord_float->u.array[(int)s->pcount[l+2]*2]; | |
392 texcoord_p[j].y = s->texcoord_float->u.array[(int)s->pcount[l+2]*2]+1; | |
393 texcoord_p[j+1].x = s->texcoord_float->u.array[(int)s->pcount[l+5]*2]; | |
394 texcoord_p[j+1].y = s->texcoord_float->u.array[(int)s->pcount[l+5]*2]+1; | |
395 texcoord_p[j+2].x = s->texcoord_float->u.array[(int)s->pcount[l+8]*2]; | |
396 texcoord_p[j+2].y = s->texcoord_float->u.array[(int)s->pcount[l+8]*2]+1; | |
397 | |
398 l+=9;j+=3;k+=3; | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
399 count++; |
1392 | 400 } |
401 } | |
402 }else{ | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
403 for (int i=0;i<s->polylist_count;i++) { |
1392 | 404 if (s->vcount[i] == 4) { |
1398 | 405 vertex_p[k].x = s->vertex_float->u.array[(int)s->pcount[l]*3]; |
406 vertex_p[k].y = s->vertex_float->u.array[(int)s->pcount[l]*3+1]; | |
407 vertex_p[k].z = s->vertex_float->u.array[(int)s->pcount[l]*3+2]; | |
408 vertex_p[k+1].x = s->vertex_float->u.array[(int)s->pcount[l+2]*3]; | |
409 vertex_p[k+1].y = s->vertex_float->u.array[(int)s->pcount[l+2]*3+1]; | |
410 vertex_p[k+1].z = s->vertex_float->u.array[(int)s->pcount[l+2]*3+2]; | |
411 vertex_p[k+2].x = s->vertex_float->u.array[(int)s->pcount[l+4]*3]; | |
412 vertex_p[k+2].y = s->vertex_float->u.array[(int)s->pcount[l+4]*3+1]; | |
413 vertex_p[k+2].z = s->vertex_float->u.array[(int)s->pcount[l+4]*3+2]; | |
414 | |
415 vertex_p[k+3].x = s->vertex_float->u.array[(int)s->pcount[l+4]*3]; | |
416 vertex_p[k+3].y = s->vertex_float->u.array[(int)s->pcount[l+4]*3+1]; | |
417 vertex_p[k+3].z = s->vertex_float->u.array[(int)s->pcount[l+4]*3+2]; | |
418 vertex_p[k+4].x = s->vertex_float->u.array[(int)s->pcount[l+6]*3]; | |
419 vertex_p[k+4].y = s->vertex_float->u.array[(int)s->pcount[l+6]*3+1]; | |
420 vertex_p[k+4].z = s->vertex_float->u.array[(int)s->pcount[l+6]*3+2]; | |
421 vertex_p[k+5].x = s->vertex_float->u.array[(int)s->pcount[l]*3]; | |
422 vertex_p[k+5].y = s->vertex_float->u.array[(int)s->pcount[l]*3+1]; | |
423 vertex_p[k+5].z = s->vertex_float->u.array[(int)s->pcount[l]*3+2]; | |
1392 | 424 |
1398 | 425 normal_p[k].x = s->normal_float->u.array[(int)s->pcount[l+1]*3]; |
426 normal_p[k].y = s->normal_float->u.array[(int)s->pcount[l+1]*3+1]; | |
427 normal_p[k].z = s->normal_float->u.array[(int)s->pcount[l+1]*3+2]; | |
428 normal_p[k+1].x = s->normal_float->u.array[(int)s->pcount[l+3]*3]; | |
429 normal_p[k+1].y = s->normal_float->u.array[(int)s->pcount[l+3]*3+1]; | |
430 normal_p[k+1].z = s->normal_float->u.array[(int)s->pcount[l+3]*3+2]; | |
431 normal_p[k+2].x = s->normal_float->u.array[(int)s->pcount[l+5]*3]; | |
432 normal_p[k+2].y = s->normal_float->u.array[(int)s->pcount[l+5]*3+1]; | |
433 normal_p[k+2].z = s->normal_float->u.array[(int)s->pcount[l+5]*3+2]; | |
1392 | 434 |
1398 | 435 normal_p[k+3].x = s->normal_float->u.array[(int)s->pcount[l+5]*3]; |
436 normal_p[k+3].y = s->normal_float->u.array[(int)s->pcount[l+5]*3+1]; | |
437 normal_p[k+3].z = s->normal_float->u.array[(int)s->pcount[l+5]*3+2]; | |
438 normal_p[k+4].x = s->normal_float->u.array[(int)s->pcount[l+7]*3]; | |
439 normal_p[k+4].y = s->normal_float->u.array[(int)s->pcount[l+7]*3+1]; | |
440 normal_p[k+4].z = s->normal_float->u.array[(int)s->pcount[l+7]*3+2]; | |
441 normal_p[k+5].x = s->normal_float->u.array[(int)s->pcount[l+1]*3]; | |
442 normal_p[k+5].y = s->normal_float->u.array[(int)s->pcount[l+1]*3+1]; | |
443 normal_p[k+5].z = s->normal_float->u.array[(int)s->pcount[l+1]*3+2]; | |
444 | |
445 l+=8;k+=6; | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
446 count +=2; |
1392 | 447 } else if (s->vcount[i]==3) { |
1398 | 448 vertex_p[k].x = s->vertex_float->u.array[(int)s->pcount[l]*3]; |
449 vertex_p[k].y = s->vertex_float->u.array[(int)s->pcount[l]*3+1]; | |
450 vertex_p[k].z = s->vertex_float->u.array[(int)s->pcount[l]*3+2]; | |
451 vertex_p[k+1].x = s->vertex_float->u.array[(int)s->pcount[l+2]*3]; | |
452 vertex_p[k+1].y = s->vertex_float->u.array[(int)s->pcount[l+2]*3+1]; | |
453 vertex_p[k+1].z = s->vertex_float->u.array[(int)s->pcount[l+2]*3+2]; | |
454 vertex_p[k+2].x = s->vertex_float->u.array[(int)s->pcount[l+4]*3]; | |
455 vertex_p[k+2].y = s->vertex_float->u.array[(int)s->pcount[l+4]*3+1]; | |
456 vertex_p[k+2].z = s->vertex_float->u.array[(int)s->pcount[l+4]*3+2]; | |
1392 | 457 |
1398 | 458 normal_p[k].x = s->normal_float->u.array[(int)s->pcount[l+1]*3]; |
459 normal_p[k].y = s->normal_float->u.array[(int)s->pcount[l+1]*3+1]; | |
460 normal_p[k].z = s->normal_float->u.array[(int)s->pcount[l+1]*3+2]; | |
461 normal_p[k+1].x = s->normal_float->u.array[(int)s->pcount[l+3]*3]; | |
462 normal_p[k+1].y = s->normal_float->u.array[(int)s->pcount[l+3]*3+1]; | |
463 normal_p[k+1].z = s->normal_float->u.array[(int)s->pcount[l+3]*3+2]; | |
464 normal_p[k+2].x = s->normal_float->u.array[(int)s->pcount[l+5]*3]; | |
465 normal_p[k+2].y = s->normal_float->u.array[(int)s->pcount[l+5]*3+1]; | |
466 normal_p[k+2].z = s->normal_float->u.array[(int)s->pcount[l+5]*3+2]; | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
467 |
1398 | 468 l+=6;k+=3; |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
469 count++; |
1392 | 470 } |
471 } | |
1376 | 472 } |
473 | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
474 |
1376 | 475 /** |
476 * (SceneGraph.cc) | |
477 * pickup_normal,pickup_coordinate,pickup_textureの処理 | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
478 * sg->sizeは頂点の数,countは面の数 |
1376 | 479 */ |
1384 | 480 |
1376 | 481 //polygonの作成 |
482 sg->pp_num = (count + MAX_SIZE_TRIANGLE - 1) / MAX_SIZE_TRIANGLE; | |
483 sg->pp = new PolygonPack[sg->pp_num]; | |
1379
13065ad17328
collada moved but only my mac.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1378
diff
changeset
|
484 sg->name = s->name; |
13065ad17328
collada moved but only my mac.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1378
diff
changeset
|
485 sg->parent_name = "NULL"; |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
486 sg->size = count*3; |
1398 | 487 k = 0; |
1376 | 488 for (int i = 0;i < sg->pp_num; i++ ){ |
1398 | 489 PolygonPackPtr pp = sg->pp; |
1376 | 490 TrianglePackPtr tri = pp[i].tri; |
491 // TrianglePack の size のチェック | |
492 int tri_size = (count < MAX_SIZE_TRIANGLE) ? count : MAX_SIZE_TRIANGLE ; | |
493 pp[i].info.size = tri_size; | |
1398 | 494 /* default texture peste */ |
495 if (s->images_flag==0) { | |
496 char *default_image = "../Test/xml_file/blend/images/ball.jpg"; | |
497 get_texture_image(default_image, sg, (xmlNodePtr)NULL, manager); | |
498 } | |
499 for (int j = 0; j < tri_size; j++,k+=3) { | |
500 tri[j].ver1.x = vertex_p[k].x; | |
501 tri[j].ver1.y = vertex_p[k].y; | |
502 tri[j].ver1.z = vertex_p[k].z; | |
1376 | 503 |
1398 | 504 tri[j].ver2.x = vertex_p[k+1].x; |
505 tri[j].ver2.y = vertex_p[k+1].y; | |
506 tri[j].ver2.z = vertex_p[k+1].z; | |
1376 | 507 |
1398 | 508 tri[j].ver3.x = vertex_p[k+2].x; |
509 tri[j].ver3.y = vertex_p[k+2].y; | |
510 tri[j].ver3.z = vertex_p[k+2].z; | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
511 |
1398 | 512 tri[j].normal1.x = normal_p[k].x; |
513 tri[j].normal1.y = normal_p[k].y; | |
514 tri[j].normal1.z = normal_p[k].z; | |
1376 | 515 |
1398 | 516 tri[j].normal2.x = normal_p[k+1].x; |
517 tri[j].normal2.y = normal_p[k+1].y; | |
518 tri[j].normal2.z = normal_p[k+1].z; | |
1376 | 519 |
1398 | 520 tri[j].normal3.x = normal_p[k+2].x; |
521 tri[j].normal3.y = normal_p[k+2].y; | |
522 tri[j].normal3.z = normal_p[k+2].z; | |
523 /* | |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
524 tri[j].ver1.tex_x = texcoord_table[m]; |
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
525 tri[j].ver1.tex_y = texcoord_table[m+1]; |
1376 | 526 |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
527 tri[j].ver2.tex_x = texcoord_table[m+2]; |
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
528 tri[j].ver2.tex_y = texcoord_table[m+3]; |
1376 | 529 |
1396
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
530 tri[j].ver3.tex_x = texcoord_table[m+4]; |
ad841dcdbe67
collada moved. But only cube.
e095732 <e095732@ie.u-ryukyu.ac.jp>
parents:
1392
diff
changeset
|
531 tri[j].ver3.tex_y = texcoord_table[m+5]; |
1398 | 532 */ |
1376 | 533 } |
534 | |
535 } | |
1398 | 536 |
1376 | 537 sg->c_xyz[0] = sg->c_xyz[1] = sg->c_xyz[2] = 0; |
1398 | 538 /* |
1376 | 539 delete []vertex_table; |
540 delete []normal_table; | |
541 delete []texcoord_table; | |
1398 | 542 */ |
1376 | 543 /* got out of polylist */ |
544 s->polylist = 0; | |
545 return sg; | |
546 } | |
547 | |
548 static void | |
549 xml_walk(xmlNodePtr cur, struct collada_state *s, LIST_P list,SceneGraphPtr sg, SceneGraphRoot *root) | |
550 { | |
551 int in_polylist=0; | |
552 if (!xmlStrcmp(cur->name, (xmlChar*)"polylist")) { | |
553 s->polylist_count = atoi((char*)xmlGetProp(cur, (xmlChar*)"count")); | |
554 s->polylist=1; | |
555 in_polylist=1; | |
556 } else if (!xmlStrcmp(cur->name, (xmlChar*)"vertices")) { | |
557 s->vertices_id = xmlGetProp(cur, (xmlChar*)"id"); | |
558 } else if (!xmlStrcmp(cur->name, (xmlChar*)"library_images")) { | |
1378 | 559 s->library_images=1; |
560 //library_images is wrote at texture image name. only use one image file | |
1376 | 561 } else if (s->library_images && !xmlStrcmp(cur->name, (xmlChar*)"init_from")) { |
562 s->tex_picname = (char*)xmlGetProp(cur, (xmlChar*)"init_from"); | |
563 get_texture_image(s->tex_picname, sg, cur , root->tmanager); | |
564 s->library_images=0; | |
565 s->images_flag=1; | |
566 } else if (!s->polylist && !xmlStrcmp(cur->name, (xmlChar*)"input")) { | |
567 char *semantic = (char*)xmlGetProp(cur, (xmlChar*)"semantic"); | |
568 if (!xmlStrcmp((xmlChar*)semantic, (xmlChar*)"POSITION")) { | |
569 s->vertices_src = (char*)xmlGetProp(cur, (xmlChar*)"source"); | |
570 } | |
571 } else if (s->polylist && !xmlStrcmp(cur->name, (xmlChar*)"input")) { | |
572 char *semantic = (char*)xmlGetProp(cur, (xmlChar*)"semantic"); | |
573 if (!xmlStrcmp((xmlChar*)semantic, (xmlChar*)"VERTEX")) { | |
574 s->vertex_src = (char*)xmlGetProp(cur, (xmlChar*)"source"); | |
575 s->vertex_float = most_match(s->vertices_src+1, list); | |
576 } else if (!xmlStrcmp((xmlChar*)semantic, (xmlChar*)"NORMAL")) { | |
577 s->normal_src = (char*)xmlGetProp(cur, (xmlChar*)"source"); | |
578 s->normal_offset = atoi((char*)xmlGetProp(cur, (xmlChar*)"offset")); | |
579 s->normal_float = most_match(s->normal_src+1, list); | |
580 } else if (!xmlStrcmp((xmlChar*)semantic, (xmlChar*)"TEXCOORD")) { | |
581 s->texcoord_src = (char*)xmlGetProp(cur, (xmlChar*)"source"); | |
582 s->texcoord_offset = atoi((char*)xmlGetProp(cur, (xmlChar*)"offset")); | |
583 s->texcoord_float = most_match(s->texcoord_src+1, list); | |
584 } | |
585 } else if (!xmlStrcmp(cur->name, (xmlChar*)"vcount")) { | |
586 char *vcont = (char*)xmlNodeGetContent(cur); | |
587 s->vcount = new float[s->polylist_count]; | |
588 for (int i=0; vcont!=NULL; i++) { | |
589 /* store vcount list */ | |
590 vcont = pickup_float(vcont, s->vcount+i); | |
591 } | |
592 } else if (!xmlStrcmp(cur->name, (xmlChar*)"p")) { | |
593 get_points(cur,s,root->tmanager); | |
594 in_polylist = 0; | |
595 } else if (!xmlStrcmp(cur->name, (xmlChar*)"float_array")) { | |
596 decode_float_array(cur,list); | |
597 } else if (!xmlStrcmp(cur->name, (xmlChar*)"node" )) { | |
598 s->name = (char*)xmlGetProp(cur, (xmlChar*)"id"); | |
599 } | |
600 for (cur=cur->children; cur; cur=cur->next){ | |
601 xml_walk(cur,s,list,sg,root); | |
602 } | |
603 } | |
604 void | |
605 init_list(LIST_P list) { | |
606 list->first = NULL; | |
607 list->end = NULL; | |
608 } | |
609 | |
610 | |
611 void | |
612 SceneGraphRoot::createFromCOLLADAfile(TaskManager *manager, const char *xmlColladafile) | |
613 { | |
614 /*make parse dom*/ | |
615 xmlDocPtr doc; | |
616 xmlNodePtr cur; | |
617 | |
618 doc = xmlParseFile(xmlColladafile); | |
619 cur = xmlDocGetRootElement(doc); | |
620 | |
621 if (xmlStrcmp(cur->name, (xmlChar*)"COLLADA")){ | |
622 return ; | |
623 } | |
624 | |
625 /* node analyze */ | |
626 struct collada_state s; | |
627 SceneGraphPtr sg = new SceneGraph(manager); | |
628 for (cur=cur->children; cur; cur=cur->next){ | |
629 LIST list; | |
630 init_list(&list); | |
631 xml_walk(cur,&s,&list,sg,this); | |
632 } | |
633 registSceneGraph(decode_points(&s,manager,sg)); | |
634 xmlFreeDoc(doc); | |
635 } |