changeset 1328:0f41ff4ca2ab draft

minor changes.
author Taiki TAIRA <e095767@ie.u-ryukyu.ac.jp>
date Tue, 20 Dec 2011 18:54:19 +0900
parents c2f9b2492a31 (current diff) 64b54c99d895 (diff)
children e5894cf1d2ca
files Renderer/Engine/SceneGraphRoot.cc
diffstat 7 files changed, 179 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/SceneGraphRoot.cc	Tue Dec 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Tue Dec 20 18:54:19 2011 +0900
@@ -672,8 +672,9 @@
 }
 
 
+// light Object も SceneGraph の一部としてしまえば、別個に計算しなくていい
 void
-SceneGraphRoot::lightCalc()
+SceneGraphRoot::lightCalc(SceneGraphPtr cur_parent)
 {
     int light_num = 4;
     float light_vector_tmp[16];
@@ -688,7 +689,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];
@@ -751,8 +752,6 @@
         list = list->next;
     }
 
-    lightCalc();
-
     if(sg_exec_tree != NULL) {
 	return;
     }
@@ -792,6 +791,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 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.h	Tue Dec 20 18:54:19 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/Span.h	Tue Dec 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/Span.h	Tue Dec 20 18:54:19 2011 +0900
@@ -44,6 +44,12 @@
     float normal_y;
     float normal_z;
 
+    //test
+    float diffuse_l;
+    float diffuse_r;
+    int pad[2];
+
+
 #if 0
     TileInfoListPtr tilelist;
 #endif
--- a/Renderer/Engine/polygon_pack.h	Tue Dec 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/polygon_pack.h	Tue Dec 20 18:54:19 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 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Tue Dec 20 18:54:19 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,44 @@
     }
 
     return (max - min);
+}
 
+/**
+ * 輝度の計算
+ * @param[in] vertexs polygon vertex position
+ * @param[in] normal normal vector
+ * @param[in] light light object position
+ * @return diffuse
+ */
+
+// test
+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 +111,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 +126,14 @@
       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;
+    // test
+    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 +152,48 @@
       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);
+
+      // test
+      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 +213,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 +237,24 @@
       triangle->ver1.z = xyz1[2];
       triangle->ver1.tex_x = tri.ver1.tex_x;
       triangle->ver1.tex_y = tri.ver1.tex_y;
-      
+      // test
+      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;
+      // test
+      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);
+      // test
+      triangle->ver3.diffuse = diffuse3;      
 
       triangle->normal1.x = normal1[0];
       triangle->normal1.y = normal1[1];
--- a/Renderer/Engine/task/CreateSpan.cc	Tue Dec 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/task/CreateSpan.cc	Tue Dec 20 18:54:19 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,9 @@
     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);
+    // test
+    v->diffuse = calc(vMax->diffuse - vMin->diffuse, d, d1, vMin->diffuse);
+
 }
 
 /**
@@ -197,14 +218,20 @@
 
     float tmp_z,tmp_tex1, tmp_tex2 ,tmp_tey1,tmp_tey2;
     float tmp_xpos,tmp_end,tmp_zpos;
+    float tmp_diffuse_l,tmp_diffuse_r;
     float start_z, end_z;
     float start_tex_x, end_tex_x, start_tex_y, end_tex_y;
+    float diffuse_l, diffuse_r;
+
     int x,length;
 
     tmp_xpos = calc(vMid10->x - vMin->x ,div_y, i, vMin->x);
     tmp_end  = calc(vMid->x  - vMin->x ,div_y, i, vMin->x);
     tmp_z    = calc(vMid10->z - vMin->z ,div_y, i, vMin->z);
     tmp_zpos = calc(vMid->z  - vMin->z ,div_y, i, vMin->z);
+    // test
+    tmp_diffuse_l = calc(vMid10->diffuse  - vMin->diffuse, div_y, i, vMin->diffuse);
+    tmp_diffuse_r = calc(vMid->diffuse  - vMin->diffuse, div_y, i, vMin->diffuse);
     
     length = (tmp_xpos > tmp_end)
         ? (int)tmp_xpos - (int)tmp_end : (int)tmp_end - (int)tmp_xpos;
@@ -231,6 +258,9 @@
         end_tex_x = tmp_tex1;
         start_tex_y = tmp_tey2;
         end_tex_y = tmp_tey1;
+        // test
+        diffuse_l = tmp_diffuse_l;
+        diffuse_r = tmp_diffuse_r;
     } else {
         x = (int)tmp_xpos;
         length = (int)(tmp_end)-(int)(tmp_xpos)+1;
@@ -240,6 +270,9 @@
         end_tex_x = tmp_tex2;
         start_tex_y = tmp_tey1;
         end_tex_y = tmp_tey2;
+        // test
+        diffuse_l = tmp_diffuse_r;
+        diffuse_r = tmp_diffuse_l;
     }
     
     // ここいる? load してその後必ず、wait してるように見える。
@@ -257,6 +290,9 @@
     span->tex_x2     = end_tex_x;
     span->tex_y1     = start_tex_y;
     span->tex_y2     = end_tex_y;
+    // test
+    span->diffuse_l  = diffuse_l;
+    span->diffuse_r  = diffuse_r;
     
     /*
      * ここで頂点分法線ベクトルがあったんだけど、
--- a/Renderer/Engine/task/DrawSpan.cc	Tue Dec 20 18:46:50 2011 +0900
+++ b/Renderer/Engine/task/DrawSpan.cc	Tue Dec 20 18:54:19 2011 +0900
@@ -16,7 +16,7 @@
 #define LITTLEENDIAN 0
 #endif
 
-SchedDefineTask(DrawSpan);
+SchedDefineTask1(DrawSpan, drawSpan);
 
 #define TEX_LOAD1      0
 #define TEX_LOAD2      1
@@ -67,9 +67,11 @@
 
 //static int getDrawParam(SchedTask *smanager, Gptr g, SpanPtr span, int localy, int startx, int endx, int index, int j, DrawParamPtr param);
 
-static    int infinity_light_calc(int color,float normal_x, float normal_y, float normal_z,
-                                  SchedTask *smanager,int x, int y, float z, int world_x, int world_y, float world_z);
 
+static int infinity_light_calc(int color,float normal_x, float normal_y,
+                               float normal_z, SchedTask *smanager, int x, int y, float z,
+                               int world_x, int world_y, float world_z, 
+                               float diffuse_l, float diffuse_r, int rangex, int j);
 
 
 
@@ -203,7 +205,8 @@
 static void
 updateBuffer(Gptr g, float zpos, int rangex, int localx, int localy, int tex_x, int tex_y,
 	     float normal_x, float normal_y, float normal_z, TilePtr tile,
-	     int world_x, int world_y, float world_z, SchedTask *smanager)
+	     int world_x, int world_y, float world_z, SchedTask *smanager,
+             float diffuse_l, float diffuse_r, int j)
 {
 
     int color = get_rgb(tex_x, tex_y, tile);
@@ -223,7 +226,8 @@
     if ( *light_sysswitch == 1 && flag) {
         color = infinity_light_calc(color,normal_x,normal_y,normal_z,
 				    smanager,localx,localy,zpos,
-				    world_x,world_y,world_z);
+				    world_x,world_y,world_z,
+                                    diffuse_l, diffuse_r, rangex, j);
     }
 
     g->zRow[localx + (rangex*localy)] = zpos*flag + g->zRow[localx + (rangex*localy)]*(1-flag);
@@ -292,7 +296,8 @@
         updateBuffer(g, zpos, rangex, localx, localy,
                      tex_localx, tex_localy,
                      normal_x,normal_y,normal_z,tile,
-		     span->x, span->y, zpos, smanager);
+		     span->x, span->y, zpos, smanager,
+                     span->diffuse_l, span->diffuse_r, 0);
     }
 
     return -1;
@@ -389,6 +394,9 @@
     float normal_y = span->normal_y;
     float normal_z = span->normal_z;
 
+    // startx は DrawSpanTask の描画担当範囲の左端のx座標
+    // xは span の左端のx座標
+
     int js = (x < startx) ? startx - x : 0;
     int je = (x + x_len > endx) ? endx - x : x_len;
 
@@ -458,7 +466,8 @@
             updateBuffer(g, tex_z, rangex, localx, localy,
                          tex_localx, tex_localy,
                          normal_x, normal_y, normal_z, tile,
-			 span->x+j, span->y, world_z, smanager);
+			 span->x+j, span->y, world_z, smanager,
+                         span->diffuse_l, span->diffuse_r, j);
         }
     }
 
@@ -540,7 +549,8 @@
 static int
 infinity_light_calc(int color,float normal_x, float normal_y,
 		    float normal_z, SchedTask *smanager, int x, int y, float z,
-		    int world_x, int world_y, float world_z)
+		    int world_x, int world_y, float world_z, 
+                    float diffuse_l, float diffuse_r, int rangex, int j)
 
 {
 
@@ -575,10 +585,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;
@@ -606,6 +612,11 @@
       }
     }
 
+    //float diffuse = ( diffuse_r - diffuse_l ) / rangex * j + diffuse_l;
+    //tmp_rgb[0] = (unsigned char)(rgb[0]*diffuse);
+    //tmp_rgb[1] = (unsigned char)(rgb[1]*diffuse);
+    //tmp_rgb[2] = (unsigned char)(rgb[2]*diffuse);
+
     int rgb_flag[3];
     for (int i = 0; i < 3; i++) {
       rgb_flag[i] = (tmp_rgb[i] > 255);
@@ -629,7 +640,7 @@
 
 
 static int
-run(SchedTask *smanager, void *rbuf, void *wbuf)
+drawSpan(SchedTask *smanager, void *rbuf, void *wbuf)
 {
 
   //get_param(5) is spack->info.size