changeset 1363:38ee695f772c draft

merge
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Fri, 20 Jan 2012 05:46:56 +0900
parents 6b4a0846afcb (current diff) 6dc0176a47d6 (diff)
children f53e986715fa
files
diffstat 1 files changed, 83 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/SceneGraphRoot.cc	Fri Jan 20 05:46:00 2012 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Fri Jan 20 05:46:56 2012 +0900
@@ -232,9 +232,6 @@
     doc = xmlParseFile(xmlfile);
     cur = xmlDocGetRootElement(doc);
 
-    /* ??  */
-    xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
-
     /* XMLのノードを一つずつ解析  */
     for (cur=cur->children; cur; cur=cur->next) {
       /*初期化:curをドキュメントルートの一個下に設定
@@ -369,6 +366,8 @@
 	
 	polylist_count = 0;
 	vcsum = 0;
+	limit = 0;
+	vmember = 0;
 
     }
     int polylist;
@@ -400,10 +399,11 @@
     char *name;
     char *tex_picname;
     int vcsum;
+    int limit;
+    int vmember;
 }; 
 
 static texture_list list[TABLE_SIZE];
-static texture_list *texture_info;
 
 void get_texpic(char *filename, SceneGraphPtr sg,  xmlNodePtr cur, TaskManager *manager)
 {
@@ -411,22 +411,19 @@
     if (filename == NULL || filename[0] == 0) {
         return;
     }
-
     /**
      * image_name を既に Load していれば何もしない
      */
     int tex_id;
     /* ball test */
     if (sgid_hash.sg_hash_regist(/*filename*/"Ball", tex_id) == -1) {
-
         SDL_Surface *texture_image = sg->load_decode_image(filename, image_name, cur);
 	    if (texture_image==0) {
 	    printf("Can't load image %s\n",filename);
 	    exit(0);
 	    }
-        texture_info->texture_id = sg->makeTapestries(manager, texture_image, tex_id);
-	    tex_id = texture_info->texture_id;
-
+        sg->texture_info->texture_id = sg->makeTapestries(manager, texture_image, tex_id);
+	    tex_id = sg->texture_info->texture_id;
         if (unlink(image_name)) {
             printf("unlink error\n");
         }
@@ -434,28 +431,24 @@
         /**
          * 以前に Load されている Texture を共用
          */
-        texture_info->texture_id = tex_id;
+        sg->texture_info->texture_id = tex_id;
     }
-
-      // こんなことすると list[] のいみあるのかなーと
       // 微妙に思う、自分で書き換えた感想 by gongo
-      texture_info->t_w = list[tex_id].t_w;
-      texture_info->t_h = list[tex_id].t_h;;
-      texture_info->pixels_orig = list[tex_id].pixels_orig;
-      texture_info->pixels = list[tex_id].pixels;
-      texture_info->scale_max = list[tex_id].scale_max;
-      texture_info->texture_image = list[tex_id].texture_image;
-
+      sg->texture_info->t_w = list[tex_id].t_w;
+      sg->texture_info->t_h = list[tex_id].t_h;;
+      sg->texture_info->pixels_orig = list[tex_id].pixels_orig;
+      sg->texture_info->pixels = list[tex_id].pixels;
+      sg->texture_info->scale_max = list[tex_id].scale_max;
+      sg->texture_info->texture_image = list[tex_id].texture_image;
 }
  
-void decode_float_array(xmlNodePtr cur,LIST_P list ){ 
+void decode_float_array(xmlNodePtr cur,LIST_P list){ 
     SOURCE_P src = (SOURCE_P)malloc(sizeof(SOURCE));
     char *id = (char*)xmlGetProp(cur, (xmlChar*)"id");
     src->id = (char*)xmlGetProp(cur, (xmlChar*)"id");
     
     int count = atoi((char*)xmlGetProp(cur, (xmlChar*)"count"));
     src->count = atoi((char*)xmlGetProp(cur, (xmlChar*)"count"));
-    //src->u.array = (float*)malloc(sizeof(float)*src->count);
     src->u.array = new float[src->count];
     char *cont =(char*)xmlNodeGetContent(cur);
     //const char *id = get_property("id", cur);
@@ -474,67 +467,84 @@
 
 void 
 get_points(xmlNodePtr cur, collada_state *s, TaskManager *manager){
-    printf("start decode points\n");
     char *pcont = (char*)xmlNodeGetContent(cur);
     for (int i = 0;i < s->polylist_count;i++){
        s->vcsum += s->vcount[i];
+       s->vmember = i;
     }
-
-    //s->pcount = (float*)malloc(sizeof(float)*vcsum);
-    s->pcount = new float[s->vcsum];
+    s->limit = s->vcsum * 2;
+    if (s->texcoord_offset == 2){
+        s->limit = s->vcsum * 3;
+    }
+    s->pcount = new float[s->limit];
+    for (int i=0;i<s->limit;i++){
+        s->pcount[i] = 0;
+    }
     for (int i=0; pcont != NULL; i++) {
         pcont = pickup_float(pcont, s->pcount+i);
     }
 }
-void
-decode_points(xmlNodePtr cur, collada_state *s, TaskManager *manager){
-    int vertexp[s->vcsum];
+
+SceneGraph*
+decode_points(collada_state *s, TaskManager *manager){
+    int *vertexp;
+    vertexp = new int[s->vcsum];
     for (int i=0;i<s->vcsum;i++){
        vertexp[i]=0;
     }
-    //float *vertex_table;
-    //float *normal_table;
-    //float *texcoord_table;
-
-    float vertex_table[s->vertex_float->count];
-    //vertex_table = new float[s->vertex_float->count];
-    //float *vertex_table = (float*)malloc(sizeof(float)*s->vertex_float->count) ;
-
-    float normal_table[s->normal_float->count];
-    //normal_table = new float[s->normal_float->count];
-    //float *normal_table = (float*)malloc(sizeof(float)*s->normal_float->count);
-
-    float texcoord_table[s->texcoord_float->count];
-    //texcoord_table = new float[s->texcoord_float->count];
-    //float *texcoord_table = (float*)malloc(sizeof(float)*s->texcoord_float->count) ;
-
-    int limit = s->vcsum * 2;
-    if (s->texcoord_offset == 2){
-        limit = s->vcsum * 3;
+    /*
+     * vertex_tableだけはpolygonを作る際にvcountが4の場合重複する点が
+     * 出てくるのでサイズを2倍用意しておく
+     */
+    float *vertex_table;
+    float *normal_table;
+    float *texcoord_table; 
+    vertex_table = new float[(s->vcsum)*2];
+    normal_table = new float[s->vcsum];
+    texcoord_table = new float[s->vcsum];
+    for (int i=0;i < s->vcsum;i++){
+        vertex_table[i] = 0;
+	normal_table[i] = 0;
+	texcoord_table[i] = 0;
     }
 
+    /**
+     * s->vcsum と s->vertex_float->countの値が違うので大きい方をとりあえず使っておく
+     */
+
     /* p separate vertex position and nomal position. */
-    for (int i=0,j=0; i < limit; i+=2,j++) {
-        vertexp[j] = s->pcount[i];
-	normal_table[j] = s->normal_float->u.array[(int)s->pcount[i+1]];
-	if (s->texcoord_offset == 2) {
+    if (s->texcoord_offset == 2){
+        for (int i=0,j=0; i < s->limit; i+=3,j++) {
+	    vertexp[j] = (int)s->pcount[i];
+	    normal_table[j] = s->normal_float->u.array[(int)s->pcount[i+1]];
 	    texcoord_table[j] = s->texcoord_float->u.array[(int)s->pcount[i+2]];
-	    i++;
+	}
+    } else{
+        for (int i=0,j=0; i < s->limit; i+=2,j++) {
+	    vertexp[j] = (int)s->pcount[i];
+	    normal_table[j] = s->normal_float->u.array[(int)s->pcount[i+1]];
 	}
     }
 
-    for (int i=0;i<s->vcsum;i++) {
+    /* make triangle */
+    int k=0,l=0;
+    for (int i=0;i<s->vmember;i++) {
         if (s->vcount[i] == 4) {
-	    for (int j=0; j < s->vcount[i]; j++) {
-	        vertex_table[i] = s->vertex_float->u.array[vertexp[i]];
-		vertex_table[i+3] = s->vertex_float->u.array[vertexp[i+1]];
-		i += 2;
-	    }
-	}else if (s->vcount[i]==3) {
-	    vertex_table[i] = s->vertex_float->u.array[vertexp[i]];
-	}
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l+1]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l+2]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l+1]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l+2]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l+3]];
+            l+=4;
+        } else if (s->vcount[i]==3) {
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l++]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l++]];
+            vertex_table[k++] = s->vertex_float->u.array[vertexp[l++]];
+        }
     }
-    /**
+
+   /**
      * (SceneGraph.cc)
      * pickup_normal,pickup_coordinate,pickup_textureの処理
      * vcsumは頂点の数,countは面の数
@@ -543,14 +553,13 @@
     //polygonの作成
     SceneGraphPtr sg = new SceneGraph(manager);
     sg->pp_num = (count + MAX_SIZE_TRIANGLE - 1) / MAX_SIZE_TRIANGLE;
-    sg->pp = (PolygonPack*)malloc(sizeof(PolygonPack)*sg->pp_num);
-    //sg->pp = new PolygonPack[sg->pp_num];
+    sg->pp = new PolygonPack[sg->pp_num];
     
     for (int i = 0;i < sg->pp_num; i++ ){
         PolygonPackPtr pp = sg->pp;
 	TrianglePackPtr tri =  pp[i].tri;
 	// TrianglePack の size のチェック
-	int tri_size = (count < MAX_SIZE_TRIANGLE) ? count : MAX_SIZE_TRIANGLE ;
+        int tri_size = (count < MAX_SIZE_TRIANGLE) ? count : MAX_SIZE_TRIANGLE ;
 	pp[i].info.size = tri_size;
 	int k = 0;
 	int m = 0;
@@ -594,25 +603,18 @@
     }
     sg->c_xyz[0] = sg->c_xyz[1] = sg->c_xyz[2] = 0;
     
-    /*TEST*/
-    for (int i=0; i<s->vcsum; i++) {
-        printf("vertexp = %d\n", vertexp[i]);
-        //printf("vertex_table= %f\n", s->vertex_table[i]);
-    } 
     int tex_id = 0;
-    sgid_hash.sg_hash_regist("Ball", tex_id);
+    sgid_hash.sg_hash_regist(s->name, tex_id);
     //get_texpic(s->tex_picname, sg, cur , manager);
-    //free(vertexp);
-    //free(vertex_table);
-    //free(normal_table);
-    //free(texcoord_table);
-    
-    //delete []vertex_table;
-    //delete []normal_table;
-    //delete []texcoord_table;
+ 
+    delete []vertexp;
+    delete []vertex_table;
+    delete []normal_table;
+    delete []texcoord_table;
 
     /* got out of polylist */
     s->polylist = 0;
+    return sg;
 }
 
 static void 
@@ -655,7 +657,6 @@
 	}
     } else if (!xmlStrcmp(cur->name, (xmlChar*)"vcount")) {
         char *vcont = (char*)xmlNodeGetContent(cur);
-        //s->vcount = (float*)malloc(sizeof(float)*s->polylist_count);
 	s->vcount = new float[s->polylist_count];
         for (int i=0; vcont!=NULL; i++) {
 	    /* store vcount list */
@@ -668,7 +669,6 @@
         decode_float_array(cur,list);
     } else if (!xmlStrcmp(cur->name, (xmlChar*)"node" )) {
         s->name = (char*)xmlGetProp(cur, (xmlChar*)"id");
-	decode_points(cur,s,root->tmanager);
     }
     for (cur=cur->children; cur; cur=cur->next){
         xml_walk(cur,s,list,root);
@@ -704,7 +704,8 @@
         init_list(&list);
         xml_walk(cur,&s,&list,this);
     }
-    //xmlFreeDoc(doc);
+    registSceneGraph(decode_points(&s,manager));
+    xmlFreeDoc(doc);
 }
 
 void
@@ -718,9 +719,6 @@
     doc = xmlParseMemory(data, len);
     cur = xmlDocGetRootElement(doc);
 
-    /* ??  */
-    xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
-
     /* XMLのノードを一つずつ解析  */
     for (cur=cur->children; cur; cur=cur->next) {
 	/* 扱うのはsurfaceオンリー  */
@@ -858,9 +856,6 @@
         light_vector[i*4+3] = light_vector_tmp[i*4+3];
     }
 
-
-
-
     }
 }