changeset 1302:ab9b7d21b32b draft

removed real_matrix. sparated screen matrix from camera matrix.
author Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
date Sat, 10 Dec 2011 17:53:32 +0900
parents 3a04b2e24df1
children 0229c8ce0327
files Renderer/Engine/Camera.cc Renderer/Engine/SceneGraph.cc Renderer/Engine/SceneGraphRoot.cc Renderer/Engine/polygon.h Renderer/Engine/task/CreatePolygonFromSceneGraph.cc Renderer/Engine/task/DrawSpan.cc Renderer/Engine/viewer.cc Renderer/Engine/viewerGL.cc
diffstat 8 files changed, 96 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/Camera.cc	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/Camera.cc	Sat Dec 10 17:53:32 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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/SceneGraph.cc	Sat Dec 10 17:53:32 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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Sat Dec 10 17:53:32 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;
   }
 }
@@ -601,6 +600,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 +654,7 @@
     // SceneGraphPtr t = sg_draw_tree;
 
     /*removeのflagをもとにtreeを形成*/
+
     while (t) {
 	SceneGraphPtr c = NULL;
 	if (!t->isRemoved()) {
@@ -629,11 +664,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 +715,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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/polygon.h	Sat Dec 10 17:53:32 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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Sat Dec 10 17:53:32 2011 +0900
@@ -68,21 +68,22 @@
     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);
+    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]=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;
 
     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 +111,17 @@
       xyz3[2] = tri.ver3.z * -1.0f;
       xyz3[3] = 1.0f;
       
-      // matrix = ビュー座標変換行列*射影変換行列*スクリーン変換行列
+      // matrix = ビュー座標変換行列*射影変換行列
       ApplyMatrix(xyz1, matrix);
       ApplyMatrix(xyz2, matrix);
       ApplyMatrix(xyz3, matrix);
 
+      ApplyMatrix(xyz1, m_screen);
+      ApplyMatrix(xyz2, m_screen);
+      ApplyMatrix(xyz3, m_screen);
+
       // この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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/task/DrawSpan.cc	Sat Dec 10 17:53:32 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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/viewer.cc	Sat Dec 10 17:53:32 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	Tue Dec 06 18:01:33 2011 +0900
+++ b/Renderer/Engine/viewerGL.cc	Sat Dec 10 17:53:32 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);