changeset 1324:ada0b6ff6575 draft

add diffuse parameter
author Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
date Mon, 19 Dec 2011 17:45:00 +0900
parents b606adda63fe
children d4f83f5d2d32
files Renderer/Engine/SceneGraphRoot.cc Renderer/Engine/SceneGraphRoot.h Renderer/Engine/polygon_pack.h Renderer/Engine/task/CreatePolygonFromSceneGraph.cc Renderer/Engine/task/CreateSpan.cc Renderer/Engine/task/DrawSpan.cc
diffstat 6 files changed, 129 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/SceneGraphRoot.cc	Tue Dec 13 19:01:32 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Mon Dec 19 17:45:00 2011 +0900
@@ -515,8 +515,9 @@
 }
 
 
+// light Object も SceneGraph の一部としてしまえば、別個に計算しなくていい
 void
-SceneGraphRoot::lightCalc()
+SceneGraphRoot::lightCalc(SceneGraphPtr cur_parent)
 {
     int light_num = 4;
     float light_vector_tmp[16];
@@ -531,7 +532,7 @@
 
     for (int i = 0; i < light_num; i++) {
 
-        get_matrix(light[i]->matrix, light[i]->angle, light[i]->xyz, camera->matrix);
+        get_matrix(light[i]->matrix, light[i]->angle, light[i]->xyz, cur_parent->matrix);
       	ApplyMatrix(&light_vector_tmp[i*4], light[i]->matrix);
 
         light_vector_tmp[i*4] /= light_vector_tmp[i*4+3];
@@ -594,8 +595,6 @@
         list = list->next;
     }
 
-    lightCalc();
-
     if(sg_exec_tree != NULL) {
 	return;
     }
@@ -635,6 +634,7 @@
      */
 
     matrix4x4(camera->matrix, camera->m_view, camera->m_pers);
+    lightCalc(camera);
     copyTree(sg_draw_tree, camera);
 
     // 現在、allExecute が終わった時点では
--- a/Renderer/Engine/SceneGraphRoot.h	Tue Dec 13 19:01:32 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.h	Mon Dec 19 17:45:00 2011 +0900
@@ -125,7 +125,7 @@
     /* Other System API */
     void allExecute(int screen_w, int screen_h);
     void treeApply(int screen_w, int screen_h);
-    void lightCalc();
+    void lightCalc(SceneGraphPtr cur_parent);
     void flip();
     void copyTree(SceneGraphPtr from, SceneGraphPtr to);
     void transTree(SceneGraphPtr t, SceneGraphPtr cur_parent);
--- a/Renderer/Engine/polygon_pack.h	Tue Dec 13 19:01:32 2011 +0900
+++ b/Renderer/Engine/polygon_pack.h	Mon Dec 19 17:45:00 2011 +0900
@@ -14,7 +14,8 @@
     float z;
     float tex_x;
     float tex_y;
-} VertexPack, *VertexPackPtr; // 20
+    float diffuse; // 拡散成分
+} VertexPack, *VertexPackPtr; // 24
 
 typedef struct NormalPack {
   float x;
@@ -29,16 +30,25 @@
     int scale_max;
 } TriangleTexInfo, *TriangleTexInfoPtr; // 16
 
+
 typedef struct TrianglePack {
-    TriTexInfo tex_info; // 16
-    VertexPack ver1;     // 20
-    VertexPack ver2;     // 20
-    VertexPack ver3;     // 20
-    NormalPack normal1;  // 12
-    NormalPack normal2;  // 12
-    NormalPack normal3;  // 12
-} TrianglePack, *TrianglePackPtr; // 112 (16 * 7)
+    TriTexInfo  tex_info;   // 16
+    VertexPack  ver1;       // 20
+    VertexPack  ver2;       // 20
+    VertexPack  ver3;       // 20
+    NormalPack  normal1;    // 12
+    NormalPack  normal2;    // 12
+    NormalPack  normal3;    // 12
+} TrianglePack, *TrianglePackPtr; 
 
+/*
+ * PolygonPackは3Dモデルの情報を格納し、CreatePolygonTask の input, output として扱われる
+ * Task内で、各頂点にmatrixが乗算され、拡散成分が計算される
+ * 冗長なところがあるから、どうにかならんものか。 NormalPack は CPTask
+ * の output としてはいらないデータ。 Task の input と output で被っているデータとそうで
+ * ないところがあるから、ひとつの構造体として送るのはどうかな
+ * DS はそこらへんの、データの結合と分解をできればいいか 
+ */
 
 typedef struct PolygonPack {
 
@@ -48,17 +58,14 @@
 	int size;
 	int light_pos[3];
 	int light_rgb[3];
-
         int span_num;
-        int pad[3];
-
     }info;
-
     PolygonPack* next;
 
     void init(void) {
 	info.size = 0;
 	info.span_num = 0;
+
 	next = 0;
     }
 
--- a/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Tue Dec 13 19:01:32 2011 +0900
+++ b/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Mon Dec 19 17:45:00 2011 +0900
@@ -11,6 +11,7 @@
 #include "polygon_pack.h"
 #include "texture.h"
 #include "matrix_calc.h"
+#include "Func.h"
 
 #define  STATUS_NUM 3
 
@@ -44,7 +45,6 @@
 
     float max = 0;
     float min = 0;
-
     max = val[0];
     min = val[0];
 
@@ -57,14 +57,43 @@
     }
 
     return (max - min);
+}
 
+/**
+ * 輝度の計算
+ * @param[in] vertexs polygon vertex position
+ * @param[in] normal normal vector
+ * @param[in] light light object position
+ * @return diffuse
+ */
+
+static float
+lighting(const float *vertex, const float *normal, const float *light) {
+    
+    float light_vector[4];
+    float normal_vector[4];
+
+    light_vector[0] = vertex[0] - light[0];
+    light_vector[1] = vertex[1] - light[1];
+    light_vector[2] = vertex[2] - light[3];
+    light_vector[3] = 0;
+
+    normal_vector[0] = vertex[0] - light[0];
+    normal_vector[1] = vertex[1] - light[1];
+    normal_vector[2] = vertex[2] - light[3];
+    normal_vector[3] = 0;
+
+    normalize(light_vector, light_vector);
+    normalize(normal_vector, normal_vector);
+
+    return innerProduct(light_vector, normal_vector);
 }
 
+
 static int 
 createPolygon(SchedTask *smanager, void *rbuf, void *wbuf)
 {
 
-
     float xyz1[4], xyz2[4], xyz3[4];
     float normal1[4],normal2[4],normal3[4];
 
@@ -81,7 +110,7 @@
     normal_matrix[4*3] = normal_matrix[4*3+1] = normal_matrix[4*3+2] = 0;
     normal_matrix[4*3+3] = 1;
 
-    float matrix[16];
+    float matrix[16]; // wvpm matrix
     matrix4x4(matrix, wvp_matrix, m_screen);
 
     texture_list *tritexinfo  = (texture_list*)smanager->get_input(rbuf, 3);
@@ -96,6 +125,13 @@
       printf("in_pp->info.size = 0\n");
     }
 
+    float *light_xyz = (float*)smanager->global_get(Light);
+    int *light_switch = (int*)smanager->global_get(LightSwitch);
+    int light_num = 4;
+    float diffuse1 = 1;
+    float diffuse2 = 1;
+    float diffuse3 = 1;
+
     for (int i = 0; i < in_pp->info.size; i++) {
 
       TrianglePack tri = in_pp->tri[i];
@@ -114,14 +150,47 @@
       xyz3[1] = tri.ver3.y;
       xyz3[2] = tri.ver3.z * -1.0f;
       xyz3[3] = 1.0f;
+
+      normal1[0] = tri.normal1.x;
+      normal1[1] = tri.normal1.y;
+      normal1[2] = tri.normal1.z * -1.0f;
+      //normal1[3] = 1.0f;
+      normal1[3] = 0.0f;
       
+      normal2[0] = tri.normal2.x;
+      normal2[1] = tri.normal2.y;
+      normal2[2] = tri.normal2.z * -1.0f;
+      //normal2[3] = 1.0f;
+      normal2[3] = 0.0f;
+      
+      normal3[0] = tri.normal3.x;
+      normal3[1] = tri.normal3.y;
+      normal3[2] = tri.normal3.z * -1.0f;
+      //normal3[3] = 1.0f;
+      normal3[3] = 0.0f;
+
       // matrix = ビュー座標変換行列*射影変換行列
       ApplyMatrix(xyz1, matrix);
       ApplyMatrix(xyz2, matrix);
       ApplyMatrix(xyz3, matrix);
 
+      ApplyMatrix(normal1, normal_matrix);
+      ApplyMatrix(normal2, normal_matrix);
+      ApplyMatrix(normal3, normal_matrix);
+
+      for (int j = 0; j < light_num; j++) {
+          // 光源のスイッチが入ってたら 
+          if (light_switch[j] == 1) {
+              // 複数の光源の計算, 全部足し合わせてみる.. 
+              // あとで255を超えた値は、255にされるからここではいくら足してもいい
+              diffuse1 += lighting(xyz1, normal1, &light_xyz[j*4]);
+              diffuse2 += lighting(xyz2, normal2, &light_xyz[j*4]);
+              diffuse3 += lighting(xyz3, normal3, &light_xyz[j*4]);
+          }
+      }
+
       // このif文は、視錐台カリングに変えられる. 違うやり方があるはず
-      //if (xyz1[2] > 100 && xyz2[2] > 100 && xyz3[2] > 100) {
+      if (xyz1[2] > 100 && xyz2[2] > 100 && xyz3[2] > 100) {
 
           /*
            * 同次座標で除算、同次クリップ空間に変換する
@@ -141,21 +210,21 @@
           xyz3[2] /= xyz3[3];
 
 
-          /*} else {
+      } else {
           
           xyz1[0] = 0; 
           xyz1[1] = 0;
           xyz1[2] = 0;
-
+          
           xyz2[0] = 0;
           xyz2[1] = 0; 
           xyz2[2] = 0;
-
+          
           xyz3[0] = 0; 
           xyz3[1] = 0;
           xyz3[2] = 0;
           
-          }*/
+      }
 
    
       TrianglePackPtr triangle = &out_pp->tri[i];
@@ -165,40 +234,21 @@
       triangle->ver1.z = xyz1[2];
       triangle->ver1.tex_x = tri.ver1.tex_x;
       triangle->ver1.tex_y = tri.ver1.tex_y;
-      
+      triangle->ver1.diffuse = diffuse1;      
+
       triangle->ver2.x = xyz2[0];
       triangle->ver2.y = xyz2[1];
       triangle->ver2.z = xyz2[2];
       triangle->ver2.tex_x = tri.ver2.tex_x;
       triangle->ver2.tex_y = tri.ver2.tex_y;
+      triangle->ver2.diffuse = diffuse2;      
       
       triangle->ver3.x = xyz3[0];
       triangle->ver3.y = xyz3[1];
       triangle->ver3.z = xyz3[2];
       triangle->ver3.tex_x = tri.ver3.tex_x;
       triangle->ver3.tex_y = tri.ver3.tex_y;
-      
-      normal1[0] = tri.normal1.x;
-      normal1[1] = tri.normal1.y;
-      normal1[2] = tri.normal1.z * -1.0f;
-      //normal1[3] = 1.0f;
-      normal1[3] = 0.0f;
-      
-      normal2[0] = tri.normal2.x;
-      normal2[1] = tri.normal2.y;
-      normal2[2] = tri.normal2.z * -1.0f;
-      //normal2[3] = 1.0f;
-      normal2[3] = 0.0f;
-      
-      normal3[0] = tri.normal3.x;
-      normal3[1] = tri.normal3.y;
-      normal3[2] = tri.normal3.z * -1.0f;
-      //normal3[3] = 1.0f;
-      normal3[3] = 0.0f;
-
-      ApplyMatrix(normal1,normal_matrix);
-      ApplyMatrix(normal2,normal_matrix);
-      ApplyMatrix(normal3,normal_matrix);
+      triangle->ver3.diffuse = diffuse3;      
 
       triangle->normal1.x = normal1[0];
       triangle->normal1.y = normal1[1];
--- a/Renderer/Engine/task/CreateSpan.cc	Tue Dec 13 19:01:32 2011 +0900
+++ b/Renderer/Engine/task/CreateSpan.cc	Mon Dec 19 17:45:00 2011 +0900
@@ -29,7 +29,6 @@
     return ans;
 }
 
-
 /**
  * TrianglePack から、vMin, vMid, vMax を求める
  *
@@ -41,6 +40,25 @@
 	    VertexPackPtr *vMin, VertexPackPtr *vMid, VertexPackPtr *vMax,
             NormalPackPtr *normal1, NormalPackPtr *normal2, NormalPackPtr *normal3)
 {
+
+    /**
+     *  y座標から、vMax, vMid, vMin, を求める
+     *
+     *      vMax
+     *        |\
+     *        | \
+     *        |  \
+     *        |   \
+     *         ------ vMid
+     *        |   /
+     *        |  /
+     *        | /
+     *        |/
+     *      vMin
+     *
+     */
+
+
     if (triPack->ver1.y <= triPack->ver2.y) {
 	if (triPack->ver2.y <= triPack->ver3.y) {
 	    *vMin = &triPack->ver1;
@@ -93,6 +111,8 @@
     v->x      = calc(vMax->x - vMin->x, d, d1, vMin->x);
     v->y      = vMid->y;
     v->z      = calc(vMax->z - vMin->z, d, d1, vMin->z);
+    v->diffuse = calc(vMax->diffuse - vMin->diffuse, d, d1, vMin->diffuse);
+
 }
 
 /**
--- a/Renderer/Engine/task/DrawSpan.cc	Tue Dec 13 19:01:32 2011 +0900
+++ b/Renderer/Engine/task/DrawSpan.cc	Mon Dec 19 17:45:00 2011 +0900
@@ -16,7 +16,7 @@
 #define LITTLEENDIAN 0
 #endif
 
-SchedDefineTask(DrawSpan);
+SchedDefineTask1(DrawSpan, drawSpan);
 
 #define TEX_LOAD1      0
 #define TEX_LOAD2      1
@@ -575,10 +575,6 @@
 
       // 光源のスイッチが入ってたら 
       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] = 0;
         light_vector[1] = 0;
@@ -629,7 +625,7 @@
 
 
 static int
-run(SchedTask *smanager, void *rbuf, void *wbuf)
+drawSpan(SchedTask *smanager, void *rbuf, void *wbuf)
 {
 
   //get_param(5) is spack->info.size