changeset 1311:ad17daa18d63 draft

merge
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Thu, 15 Dec 2011 16:49:23 +0900
parents 26c155523861 (current diff) 78248082c56d (diff)
children d0c14093e019
files
diffstat 8 files changed, 196 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/Camera.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/Camera.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -141,32 +141,33 @@
     //xyz[2] = -200.0f;
     xyz[3] = 1.0f;
 
+/*
     m_view = new float[16];
     m_pers = new float[16];
     m_screen = new float[16];
-
-    //matrix = (float*)malloc(sizeof(float)*16);
-    //real_matrix = (float*)malloc(sizeof(float)*16);
-    //texture_info = (texture_list*)malloc(sizeof(texture_list));
-    
-    this->set_move_collision(camera_move, camera_collision, (void *)sgroot);
+*/  
 
-    for(int i = 0; i < 16; i++) {
-      real_matrix[i] = 0;
-      if (i % 5 == 0) {
-        real_matrix[i] = 1;
-      }
-    }
-    
+    m_view = (float*)manager->allocate(sizeof(float)*16);
+    m_pers = (float*)manager->allocate(sizeof(float)*16);
+    m_screen = (float*)manager->allocate(sizeof(float)*16);
+
+    this->set_move_collision(camera_move, camera_collision, (void *)sgroot);
 
     update(w,h); // to make matrix[]
 }
 
 Camera::~Camera(void)
 {
+
+/*
     delete [] m_view;
     delete [] m_pers;
     delete [] m_screen;
+*/
+
+    free(m_view);
+    free(m_pers);
+    free(m_screen);
 }
 
 void
@@ -268,6 +269,8 @@
     m_pers[13] = 0.0f;
     m_pers[14] = -near*sz;
     m_pers[15] = 0.0f;
+
+
 }
 
 void
@@ -307,17 +310,14 @@
 Camera::update(float w, float h)
 {
 #if 1
-    float tmp[16];
+    //float tmp[16];
 
     updateView();
     updatePerspective(w, h);
     updateScreen(w, h);
 
-    matrix4x4(tmp, this->m_pers, this->m_screen);
-    matrix4x4(this->matrix, this->m_view, tmp);
-
-    /* 法線ベクトル、光源ベクトル に使うビュー座標変換までの行列 */
-    for (int i = 0; i < 16; i++) this->real_matrix[i] = this->m_view[i];
+    //matrix4x4(tmp, this->m_pers, this->m_screen);
+    //matrix4x4(this->matrix, this->m_view, tmp);
 
 #else
     get_matrix(matrix, angle, xyz, NULL);
--- a/Renderer/Engine/SceneGraph.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/SceneGraph.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -126,19 +126,16 @@
     init();
 
     matrix = (float*)manager->allocate(sizeof(float)*16);
-    real_matrix = (float*)manager->allocate(sizeof(float)*16);
     texture_info = (texture_list*)manager->allocate(sizeof(texture_list));
 
     texture_info->texture_id = -1;
 
     for (int i = 0; i < 16; i++) {
       matrix[i]      = 0;
-      real_matrix[i] = 0;
     }
 
     for (int i = 0; i < 4; i++) {
       matrix[i*4+i]      = 1;
-      real_matrix[i*4+i] = 1;
     }
 
 
@@ -159,12 +156,10 @@
     memcpy(this, orig, sizeof(SceneGraph));
 
     matrix = (float*)manager->allocate(sizeof(float)*16);
-    real_matrix = (float*)manager->allocate(sizeof(float)*16);
     texture_info = (texture_list*)manager->allocate(sizeof(texture_list));
 
     for (int i = 0; i < 16; i++) {
       matrix[i] = orig->matrix[i];
-      real_matrix[i] = orig->real_matrix[i];
     }
 
     memcpy(texture_info, orig->texture_info, sizeof(texture_list));
@@ -194,7 +189,6 @@
     init();
 
     matrix = (float*)manager->allocate(sizeof(float)*16*2);
-    real_matrix = matrix+16;
     texture_info = (texture_list*)manager->allocate(sizeof(texture_list));
     texture_info->texture_id = -1;
     
@@ -205,11 +199,9 @@
 
     for (int i = 0; i < 16; i++) {
       matrix[i]      = 0;
-      real_matrix[i] = 0;
     }
     for (int i = 0; i < 4; i++) {
       matrix[i*4+i]      = 1;
-      real_matrix[i*4+i] = 1;
     }
 
     if (size % 3 != 0) {
@@ -247,7 +239,6 @@
     init();
 
     this->matrix = (float*)manager->allocate(sizeof(float)*16);
-    this->real_matrix = this->matrix+16;
     this->texture_info = (texture_list*)manager->allocate(sizeof(texture_list));
     texture_info->texture_id = -1;
     
@@ -257,11 +248,9 @@
     name = string_name;
     for (int i = 0; i < 16; i++) {
       matrix[i]      = 0;
-      real_matrix[i] = 0;
     }
     for (int i = 0; i < 4; i++) {
       matrix[i*4+i]      = 1;
-      real_matrix[i*4+i] = 1;
     }
 
     if (size % 3 != 0) {
@@ -489,7 +478,6 @@
 
     free(pp);
     free(matrix);
-    free(real_matrix);
     free(texture_info);
 
 }
@@ -503,7 +491,6 @@
 {
 
     free(matrix);
-    free(real_matrix);
     free(texture_info);
 }
 
@@ -1129,7 +1116,6 @@
     float t[4] = {0,0,0,0};
     for(int i=0;i<16;i++) m[i] = matrix[i];
     get_matrix(matrix,      angle, t, m);
-    get_matrix(real_matrix, angle, t, m);
 }
 
 void
@@ -1138,8 +1124,6 @@
     for(int i=0;i<4;i++) {
 	for(int j=0;i<3;j++) {
 	    matrix[i*4+j] = matrix[i*4+j]*scale[i]; 
-            /* real_matrix は スケールする必要はないかも */
-	    real_matrix[i*4+j] = real_matrix[i*4+j]*scale[i]; 
         }
     }
 }
--- a/Renderer/Engine/SceneGraphRoot.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -192,7 +192,6 @@
     width_shift += text->seq;
     root->addChild(text);
     get_matrix_scale(text->matrix, text->angle, text->xyz, scale, root->matrix);
-    // get_matrix(text->real_matrix, text->angle, text->xyz, root->real_matrix);
     i += len;
   }
 }
@@ -279,8 +278,11 @@
 
 typedef struct source {
     char *id;
+    union {
+        float *array;
+        char *alias;
+    }u;
     int count;
-    float *array;
     struct source *next;
 } SOURCE;
 typedef SOURCE *SOURCE_P;
@@ -291,6 +293,7 @@
 } LIST;
 typedef LIST *LIST_P;
 
+/* add source list */
 static void
 addSource(LIST_P list, SOURCE_P src) {
     if (list->first == NULL && list->end == NULL) {
@@ -301,12 +304,69 @@
     list->end = src;
 }
 
+/* compare a with b. Using to compare id */
+static int
+strcmp_a(const char *a, const char *b)
+{
+    while (*a && *a++ = *b++);
+    if (*a) return 0;
+    return a[-1] > b[-1] ? 1:-1;
+}
+
+static float
+get_point(char *id, int position, LIST_P list)
+{
+    SOURCE_P cur = list->first;
+    for (;cur ; cur=cur->next) {
+        if (!strcmp_a(id, cur->name)) {
+            if (list->count == 0) //alias
+                return get_point(list->u.alias, position, list);
+            float *a = cur->u.array;
+            if (position <= list->count) {
+                return a[position];
+            }
+        }
+    }
+}
+
+static int polylist=0;
+static const char *polylist_normal=0;
+static const char *polylist_vertex=0;
+
+
 static void 
-xml_walk( SceneGraphRoot* self, xmlNodePtr cur, LIST_P list)
+xml_walk(SceneGraphRoot* self, xmlNodePtr cur, LIST_P list)
 {
 
+    int in_polylist=0;
     /*get float array.*/
     printf("name = %s, child:%s\n", (char *)cur->name, (char *)cur->children);
+
+    if (!xmlStrcmp(cur->name, (xmlChar*)"polylist")) {
+        polylist=1;
+        in_polylist=1;
+    }
+
+    if (polylist && !xmlStrcmp(cur->name, (xmlChar*)"input") {
+            char *semantic = (char*)xmlGetProp(cur, (xmlChar*)"semantic");
+            if (!xmlStrcmp(semantic, (xmlChar*)"VERTEX") {
+                char *vertex_id = (char*)xmlGetProp(cur, (xmlChar*)"source");
+                int *vertex_offset = atoi((char*)xmlGetProp(cur, (xmlChar*)"offset"));
+            }
+
+            if (!xmlStrcmp(semantic, (xmlChar*)"NORMAL") {
+                char *normal_id = (char*)xmlGetProp(cur, (xmlChar*)"id");
+                int *normal_offset = atoi((char*)xmlGetProp(cur, (xmlChar*)"offset"));
+            }
+            /*
+            if (!xmlStrcmp(semantic, (xmlChar*)"TEXTURE") {
+                char *normal_id = (char*)xmlGetProp(cur, (xmlChar*)"id");
+                int *normal_offset = atoi((char*)xmlGetProp(cur, (xmlChar*)"offset"));
+            }
+            */
+
+    }
+
     if (!xmlStrcmp(cur->name, (xmlChar*)"float_array")) {
 
         SOURCE_P src = (SOURCE_P)malloc(sizeof(SOURCE));
@@ -323,13 +383,9 @@
         //int count = atoi(get_property("count", cur));
 
         for (int i = 0; cont != NULL; i++) {
-
-            //cont = pickup_float(cont, src->array+1);
-            // ここ+1じゃなくて、+iじゃない?
             cont = pickup_float(cont, src->array+i);
         }
 
-
         src->next = NULL;
         addSource(list, src);
         printf("id:%s count:%d cont:%s\n", id, count, cont);
@@ -339,7 +395,42 @@
             printf("p:%s", p);
         }
         
+    } else if (!xmlStrcmp(cur->name, (xmlChar*)"vertices") {
+
+            char *pid = (char*)xmlGetProp(cur, (xmlChar*)"id");
+            float *vcount = malloc(sizeof(int)*count);
+            float *pcount = malloc(sizeof(float)*sum); 
+
+            if (!xmlStrcmp(cur->name, (xmlChar*)"vcount")) {
+                char *vcont = (char*)xmlNodeGetContent(cur);
+                    for (int i=0; vcount!=NULL; i++) {
+                        vcont = pickup_float(vcont, vcount+i);
+                    }
+            }
+
+            if (!xmlStrcmp(cur->name, (xmlChar*)"p")) {
+                char *pcont = (char*)xmlNodeGetContent(cur);
+                for (int i=0; pcont != NULL; i++) {
+                    pcont = pickup_float(pcont, pcount+i);
+                }
+            }
+        }
+
+        get_point(pid, , cur);
+
+
+        for (SOURCE *iliner = list->first; list->end != iliner; iliner = iliner->next) {
+            if (!strcmp_a(pid, iliner->id)) {
+                if (vcount == 4) {
+
+
+                } else if (vcount == 3) {
+
+                }
+            }
+        }
     }
+
     
     for (cur=cur->children; cur; cur=cur->next){
         xml_walk(self, cur, list);
@@ -367,7 +458,7 @@
 	/*エラー処理……だけど何書けばいいのか謎。とりあえず-1返してみる*/
 	if(xmlStrcmp(cur->name, (xmlChar*)"COLLADA")){
 		return ;
-       };
+     }
 
 	/* node analyze */
 	for(cur=cur->children; cur; cur=cur->next){
@@ -601,6 +692,41 @@
 	return;
     }
 
+    /* ここで、scenegraph node の matrix に演算する座標変換は、
+     * world->view->perspective まで。 
+     *
+     * CreatePolygonFromSceneGraph で perspective の座標系で lighting の演算を行い、その後
+     * screen 変換をするので。
+     *
+     * その際に、camera がもつ screen matrix を Task に渡す必要がある
+     * Task に screen matrix を渡す部分は viewer.cc にある
+     *
+     * world 変換は node が持つ matrix で行う
+     * view->perspective 変換は camera が持つ matrix で行う
+     *
+
+     *
+     * (w) = world matrix
+     * (v) = view matrix
+     * (p) = perspective matrix
+     * (s) = screen matrix
+     *
+     * --- copyTree ---
+     *
+     * node->(wvp) = node->(w) * node->parent->(w) * ..... camera->(v) * camera->(p)
+     *
+     * --- CreatePolygonFromSceneGraph ---
+     * 
+     * (polygon_vertex) * node->(wvp)
+     * (light_position) * node->(wvp)
+     * (normal_vector) * normal_matrix
+     *
+     * lighting(polygon_vertex, light_position, normal_vector)
+     *
+     * (polygon_vertex) * camera->(s)
+     */
+
+    matrix4x4(camera->matrix, camera->m_view, camera->m_pers);
     copyTree(sg_draw_tree, camera);
 
     // 現在、allExecute が終わった時点では
@@ -620,6 +746,7 @@
     // SceneGraphPtr t = sg_draw_tree;
 
     /*removeのflagをもとにtreeを形成*/
+
     while (t) {
 	SceneGraphPtr c = NULL;
 	if (!t->isRemoved()) {
@@ -629,11 +756,8 @@
 	    c->frame = t->frame;
 	    /*親の回転、座標から、子の回転、座標を算出*/
 	    get_matrix(c->matrix, c->angle, c->xyz, cur_parent->matrix);
-	    /*法線用の行列。Cameraの行列を抜いている(Cameraのコンストラクタで、単位行列にしている)*/
-	    get_matrix(c->real_matrix, c->angle, c->xyz, cur_parent->real_matrix);
-	    //get_matrix(c->real_matrix, c->angle, c->xyz, camera->real_matrix);
+	}
 
-	}
 
 	if (t->children != NULL && c != NULL) {
 	    cur_parent = c;
@@ -683,8 +807,6 @@
 	if (!t->isRemoved()) {
 	    /*親の回転、座標から、子の回転、座標を算出*/
 	    matrix4x4(t->matrix,t->matrix,cur_parent->matrix);
-	    /*法線用の行列。Cameraの行列を抜いている(Cameraのコンストラクタで、単位行列にしている)*/
-	    // matrix4x4(t->real_matrix,t->real_matrix,cur_parent->real_matrix);
 	} 
 	if (t->children != NULL && c != NULL) {
 	    cur_parent = t;
--- a/Renderer/Engine/polygon.h	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/polygon.h	Thu Dec 15 16:49:23 2011 +0900
@@ -16,7 +16,6 @@
     const char *parent_name;
 
     float *matrix;
-    float *real_matrix;
     texture_list *texture_info;
        
     PolygonPackPtr pp;
--- a/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -68,21 +68,26 @@
     float xyz1[4], xyz2[4], xyz3[4];
     float normal1[4],normal2[4],normal3[4];
 
-    //pp, matrix, real_matrix を受け取る
+    //pp, matrix, を受け取る
     PolygonPackPtr in_pp      = (PolygonPackPtr)smanager->get_input(rbuf, 0);
-    float *matrix             = (float*)smanager->get_input(rbuf, 1);
+    // w = world, v = view, p = perspective
+    float *wvp_matrix         = (float*)smanager->get_input(rbuf, 1);
+    float *m_screen           = (float*)smanager->get_input(rbuf, 2);
 
-    float real_matrix[16]; //        = (float*)smanager->get_input(rbuf, 2);
-    //float *real_matrix = (float*)smanager->get_input(rbuf, 2);
+    float normal_matrix[16]; 
   
-    for (int i = 0; i<16; i++) real_matrix[i]=matrix[i];
-    real_matrix[4*0+3]=real_matrix[4*1+3]=real_matrix[4*2+3]=0;
+    for (int i = 0; i<16; i++) normal_matrix[i] = wvp_matrix[i];
+    normal_matrix[4*0+3] = normal_matrix[4*1+3] = normal_matrix[4*2+3] = 0;
+    normal_matrix[4*3] = normal_matrix[4*3+1] = normal_matrix[4*3+2] = 0;
+    normal_matrix[4*3+3] = 1;
+
+    float matrix[16];
+    matrix4x4(matrix, wvp_matrix, m_screen);
 
     texture_list *tritexinfo  = (texture_list*)smanager->get_input(rbuf, 3);
     
     PolygonPackPtr next = (PolygonPackPtr)smanager->get_param(0);
 
-
     PolygonPackPtr out_pp = (PolygonPackPtr)smanager->get_output(wbuf, 0);
     out_pp->info.size = in_pp->info.size;
     out_pp->next = next;
@@ -110,13 +115,13 @@
       xyz3[2] = tri.ver3.z * -1.0f;
       xyz3[3] = 1.0f;
       
-      // matrix = ビュー座標変換行列*射影変換行列*スクリーン変換行列
+      // matrix = ビュー座標変換行列*射影変換行列
       ApplyMatrix(xyz1, matrix);
       ApplyMatrix(xyz2, matrix);
       ApplyMatrix(xyz3, matrix);
 
       // このif文は、視錐台カリングに変えられる. 違うやり方があるはず
-      if (xyz1[2] > 100 && xyz2[2] > 100 && xyz3[2] > 100) {
+      //if (xyz1[2] > 100 && xyz2[2] > 100 && xyz3[2] > 100) {
 
           /*
            * 同次座標で除算、同次クリップ空間に変換する
@@ -136,7 +141,7 @@
           xyz3[2] /= xyz3[3];
 
 
-      } else {
+          /*} else {
           
           xyz1[0] = 0; 
           xyz1[1] = 0;
@@ -150,7 +155,8 @@
           xyz3[1] = 0;
           xyz3[2] = 0;
           
-      }
+          }*/
+
    
       TrianglePackPtr triangle = &out_pp->tri[i];
 
@@ -190,9 +196,9 @@
       //normal3[3] = 1.0f;
       normal3[3] = 0.0f;
 
-      ApplyMatrix(normal1,real_matrix);
-      ApplyMatrix(normal2,real_matrix);
-      ApplyMatrix(normal3,real_matrix);
+      ApplyMatrix(normal1,normal_matrix);
+      ApplyMatrix(normal2,normal_matrix);
+      ApplyMatrix(normal3,normal_matrix);
 
       triangle->normal1.x = normal1[0];
       triangle->normal1.y = normal1[1];
--- a/Renderer/Engine/task/DrawSpan.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/task/DrawSpan.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -576,16 +576,21 @@
       // 光源のスイッチが入ってたら 
       if (light_switch[i] == 1) {
 
-        light_vector[0] = world_x - light_xyz[i*4];
-        light_vector[1] = world_y - light_xyz[i*4+1];
-        light_vector[2] = light_xyz[i*4+2] - world_z;
+          //light_vector[0] = world_x - light_xyz[i*4];
+          //light_vector[1] = world_y - light_xyz[i*4+1];
+          //light_vector[2] = light_xyz[i*4+2] - world_z;
         
+        light_vector[0] = 0;
+        light_vector[1] = 0;
+        light_vector[2] = -1;
+        light_vector[3] = 0;
+
         normalize(light_vector, light_vector);
         
         float tmp_inner_product = 0;
         
         // 法線ベクトルと光源ベクトルとの内積をとる
-        tmp_inner_product = innerProduct1(normal_vector,light_vector);
+        inner_product = innerProduct1(normal_vector,light_vector);
         
         // 内積がマイナスの場合は色がない。
         if (inner_product < tmp_inner_product) {
--- a/Renderer/Engine/viewer.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/viewer.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -468,6 +468,8 @@
 
     PolygonPackPtr out_pp = r[ppi].ppack;
     out_pp->init();
+
+    CameraPtr camera = sgroot->getCamera();
     
     //多分このsg_remove_listであってる?。チェック対象かも
     for (SceneGraphPtr t = sgroot->sg_remove_list; t != NULL; t = t->next) {
@@ -479,17 +481,13 @@
 	  
 	  create_pp->add_inData(&t->pp[i], sizeof(PolygonPack));
 	  create_pp->add_inData(t->matrix, sizeof(float)*16);
-	  create_pp->add_inData(t->real_matrix, sizeof(float)*16);
+	  create_pp->add_inData(camera->m_screen, sizeof(float)*16);
 	  create_pp->add_inData(t->texture_info, sizeof(texture_list));
 	  
 	  if ( (unsigned long)t->matrix % 16) {
 	    printf("marix not aligned\n");
 	  }
 	  
-	  if ((unsigned long)t->real_matrix % 16) {
-	    printf("real_matrix not aligned\n");
-	  }
-	  
 	  if ((unsigned long)t->texture_info % 16) {
 	    printf("texture_info not aligned\n");
 	  }
--- a/Renderer/Engine/viewerGL.cc	Thu Dec 15 16:48:55 2011 +0900
+++ b/Renderer/Engine/viewerGL.cc	Thu Dec 15 16:49:23 2011 +0900
@@ -204,6 +204,12 @@
 	if (!sg->texture_info->gl_tex) {
 	  sg->texture_info->gl_tex = SDL_GL_LoadTexture(sg->texture_info->texture_image);
 	}
+
+        float normal_matrix[16]; 
+  
+        for (int i = 0; i<16; i++) normal_matrix[i]=sg->matrix[i];
+        normal_matrix[4*0+3]=normal_matrix[4*1+3]=normal_matrix[4*2+3]=0;
+
 	texture = sg->texture_info->gl_tex;
 	glBindTexture(GL_TEXTURE_2D, texture);
 	
@@ -267,9 +273,9 @@
 
 	  normal3[3] = 0.0f;
 	  
-	  ApplyMatrix(normal1,sg->real_matrix);
-	  ApplyMatrix(normal2,sg->real_matrix);
-	  ApplyMatrix(normal3,sg->real_matrix);
+	  ApplyMatrix(normal1, normal_matrix);
+	  ApplyMatrix(normal2, normal_matrix);
+	  ApplyMatrix(normal3, normal_matrix);
 	  
 	  obj_draw(xyz1, tex_xy1, normal1);
 	  obj_draw(xyz2, tex_xy2, normal2);