changeset 762:10a8a80c2ea7

add lights
author yutaka@henri.cr.ie.u-ryukyu.ac.jp
date Sat, 06 Feb 2010 03:02:48 +0900
parents be6e43d977d1
children 987d4cced279
files Renderer/Engine/SceneGraphRoot.cc Renderer/Engine/SceneGraphRoot.h Renderer/Engine/spe/DataLoad.cc Renderer/Engine/spe/DataUpdate.cc Renderer/Engine/spe/DrawSpan.cc Renderer/Engine/task/CreatePolygonFromSceneGraph.cc Renderer/Engine/viewer.cc Renderer/Test/Makefile.macosx Renderer/Test/universe.cc
diffstat 9 files changed, 155 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/SceneGraphRoot.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -25,7 +25,6 @@
     sg_src = (SceneGraphPtr*) malloc(sizeof(SceneGraphPtr)*SGLIST_LENGTH);
 
     camera = new Camera(w, h, this);
-    light = new SceneGraph;
     iterator = new SceneGraphIterator;
     controller = create_controller();
 
@@ -41,9 +40,14 @@
     //int size = 4;
     //light_vector = (float*)malloc(sizeof(float)*size);
 
-    light->xyz[0] = 0;
-    light->xyz[1] = 0;
-    light->xyz[2] = 0;
+    int light_num = 4;
+
+    for (int i = 0; i < light_num; i++) {
+      light[i] = new SceneGraph;
+      light[i]->xyz[0] = 0;
+      light[i]->xyz[1] = 0;
+      light[i]->xyz[2] = 0;
+    }
 
     move_finish_flag = 0;
     // TODO
@@ -74,7 +78,10 @@
 
     free(sg_src);
     delete camera;
-    delete light;
+    int light_num = 4;
+    for (int i = 0; i < light_num; i++) {
+      delete light[i];
+    }
     delete iterator;
     delete controller;
 }
@@ -415,17 +422,23 @@
         list = list->next;
     }    
 
-    get_matrix(light->matrix, light->angle, light->xyz, camera->matrix);
+    int light_num = 4;
+    for (int i = 0; i < light_num; i++) {
 
-    light_vector[0] = 0.0f;
-    light_vector[1] = 0.0f;
-    light_vector[2] = 0.0f;
-    light_vector[3] = 1.0f;
+      get_matrix(light[i]->matrix, light[i]->angle, light[i]->xyz, camera->matrix);
+      
+      light_vector[i*4] = 0.0f;
+      light_vector[i*4+1] = 0.0f;
+      light_vector[i*4+2] = 0.0f;
+      light_vector[i*4+3] = 1.0f;
 
-    ApplyMatrix(light_vector, light->matrix);
+      ApplyMatrix(&light_vector[i*4], light[i]->matrix);
+      
+      light_vector[i*4] /= light_vector[i*4+2];
+      light_vector[i*4+1] /= light_vector[i*4+2];
 
-    light_vector[0] /= light_vector[2];
-    light_vector[1] /= light_vector[2];
+    }
+
    
     if(sg_exec_tree != NULL) {
 	return;
@@ -522,17 +535,23 @@
     list->frame++; 
     list = list->next;
 
-    get_matrix(light->matrix, light->angle, light->xyz, camera->matrix);
+
+    int light_num = 4;
+    for (int i = 0; i < light_num; i++) {
 
-    light_vector[0] = 0.0f;
-    light_vector[1] = 0.0f;
-    light_vector[2] = 0.0f;
-    light_vector[3] = 1.0f;
+      get_matrix(light[i]->matrix, light[i]->angle, light[i]->xyz, camera->matrix);
+      
+      light_vector[i*4] = 0.0f;
+      light_vector[i*4+1] = 0.0f;
+      light_vector[i*4+2] = 0.0f;
+      light_vector[i*4+3] = 1.0f;
 
-    ApplyMatrix(light_vector, light->matrix);
+      ApplyMatrix(&light_vector[i*4], light[i]->matrix);
+      
+      light_vector[i*4] /= light_vector[i*4+2];
+      light_vector[i*4+1] /= light_vector[i*4+2];
 
-    light_vector[0] /= light_vector[2];
-    light_vector[1] /= light_vector[2];
+    }
    
     if(sg_exec_tree != NULL) {
 	return;
@@ -549,18 +568,23 @@
 
     list->frame++; 
     //list = list->next;
-    
-    get_matrix(light->matrix, light->angle, light->xyz, camera->matrix);
+
+    int light_num = 4;
+    for (int i = 0; i < light_num; i++) {
 
-    light_vector[0] = 0.0f;
-    light_vector[1] = 0.0f;
-    light_vector[2] = 0.0f;
-    light_vector[3] = 1.0f;
+      get_matrix(light[i]->matrix, light[i]->angle, light[i]->xyz, camera->matrix);
+      
+      light_vector[i*4] = 0.0f;
+      light_vector[i*4+1] = 0.0f;
+      light_vector[i*4+2] = 0.0f;
+      light_vector[i*4+3] = 1.0f;
 
-    ApplyMatrix(light_vector, light->matrix);
+      ApplyMatrix(&light_vector[i*4], light[i]->matrix);
+      
+      light_vector[i*4] /= light_vector[i*4+2];
+      light_vector[i*4+1] /= light_vector[i*4+2];
 
-    light_vector[0] /= light_vector[2];
-    light_vector[1] /= light_vector[2];
+    }    
     
     //sgchange->viewer->light_xyz_stock = getLightVector();
 }
@@ -660,10 +684,10 @@
 
 
 SceneGraphPtr
-SceneGraphRoot::getLight()
+SceneGraphRoot::getLight(int id)
 {
 
-  return light;
+  return light[id];
 
 }
 
--- a/Renderer/Engine/SceneGraphRoot.h	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/SceneGraphRoot.h	Sat Feb 06 03:02:48 2010 +0900
@@ -56,9 +56,11 @@
     Camera *camera;
 
     // 光源のオブジェクト
-    SceneGraphPtr light;
+    SceneGraphPtr light[4];
     // 光源の疑似 xml file
-    float light_vector[4];
+    // 光源は4つで決め打ち。                                                         
+    // 4 * lightnum (4) = 16;
+    float light_vector[16];
 
     // SceneGraphIterator
     SceneGraphIteratorPtr iterator;
@@ -96,7 +98,7 @@
     SceneGraphIteratorPtr getIterator();
     SceneGraphIteratorPtr getIterator(SceneGraphPtr list);
     CameraPtr getCamera();
-    SceneGraphPtr getLight();
+    SceneGraphPtr getLight(int id);
     float* getLightVector();
 
     /* Other System API */
--- a/Renderer/Engine/spe/DataLoad.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/spe/DataLoad.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -13,7 +13,7 @@
   int size = (int)s->get_param(0);
   int load_id = (int)s->get_param(1);
 
-  //printf("size %d",sizeof(float)*length);
+  //printf("size %d\n",size);
 
   s->global_alloc(load_id, size);
 
--- a/Renderer/Engine/spe/DataUpdate.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/spe/DataUpdate.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -15,6 +15,10 @@
   int load_id = (int)s->get_param(1);
   void *global_data = (void*)s->global_get(load_id);
 
+  if (size != 64) {
+    printf("hogehoge\n");
+  }
+
   memcpy(global_data,idata,size);
 
   return 0;
--- a/Renderer/Engine/spe/DrawSpan.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/spe/DrawSpan.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -10,6 +10,7 @@
 #include "SchedTask.h"
 #include "Tapestry.h"
 #include "SpanPack.h"
+#include <math.h>
 
 #if (__LITTLE_ENDIAN__)
 #define LITTLEENDIAN 1
@@ -55,6 +56,21 @@
                                   SchedTask *smanager,int x, int y, float z, int world_x, int world_y, float world_z);
 
 
+void
+normalize(float *v0, float *v1)
+{
+    float norm, dnorm;
+
+    norm = sqrt(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]);
+    if (norm > 0) {
+	dnorm = 1.0/norm;
+	v0[0] = v1[0]*dnorm;
+	v0[1] = v1[1]*dnorm;
+	v0[2] = v1[2]*dnorm;
+	v0[3] = v1[3]*dnorm;
+    }
+}
+
 static float
 innerProduct1(float *v0, float *v1)
 {
@@ -330,7 +346,7 @@
     float zpos2 = span->end_z;
 
     //spanを右から左に見ていくうちに、zが下がるのか、上がっていくのか。
-    float z_inclination = (zpos1 - zpos2) / x_len;
+    float z_inclination = (zpos2 - zpos1) / x_len;
     float world_z = zpos2;
 
     // Tile 内での座標
@@ -342,7 +358,7 @@
     for (int j = je; j >= js; j--) {
         float tex_x, tex_y, tex_z;
 
-	world_z += z_inclination;
+	world_z -= z_inclination;
 
         localx = getLocalX(x-1+j);
 
@@ -392,8 +408,12 @@
     int flag;
     float normal_vector[4] = {normal_x,normal_y,normal_z,0};
     // 光のベクトル,きめうちしちゃった。どうにかする
-    float light_vector[4] = {0,0,-1,0};
+    //float light_vector[4] = {0,0,-1,0};
+    float light_vector[4];
     float inner_product;
+    float *light_xyz = (float*)smanager->global_get(Light);
+
+    normalize(normal_vector, normal_vector);
 
     // 引数で受け取った color の rgb 情報の抜き出し
 #if LITTLEENDIAN
@@ -408,15 +428,46 @@
     rgb[0] = (color & 0x000000ff);
 #endif
 
-    // 法線ベクトルと光源ベクトルとの内積をとる
-    inner_product = innerProduct1(normal_vector,light_vector);
-    // 内積がマイナスの場合は色がない。
-    flag = (inner_product > 0);
+    int tmp_rgb[3] = {0,0,0};
+    int light_num = 4;
+    for (int i = 0; i < light_num; i++) {
+
+      //printf("light_xyz[%d] %f\n",i*4,light_xyz[i*4]);
+      //printf("light_xyz[%d] %f\n",i*4+1,light_xyz[i*4+1]);
+      //printf("light_xyz[%d] %f\n",i*4+2,light_xyz[i*4+2]);
+      //printf("light_xyz[%d] %f\n",i*4+3fg,light_xyz[i*4+3]);
+
+      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[3] = light_xyz[i*4+3];
+
+      normalize(light_vector, light_vector);
+
+      // 法線ベクトルと光源ベクトルとの内積をとる
+      inner_product = innerProduct1(normal_vector,light_vector);
 
-    // 内積を rgb にかけていく
-    rgb[0] = (unsigned char)(rgb[0]*inner_product*flag);
-    rgb[1] = (unsigned char)(rgb[1]*inner_product*flag);
-    rgb[2] = (unsigned char)(rgb[2]*inner_product*flag);
+      //printf("inner_product %f\n",inner_product);
+
+      // 内積がマイナスの場合は色がない。
+      flag = (inner_product > 0);
+
+      // 内積を rgb にかけていく
+      tmp_rgb[0] += (unsigned char)(rgb[0]*inner_product*flag);
+      tmp_rgb[1] += (unsigned char)(rgb[1]*inner_product*flag);
+      tmp_rgb[2] += (unsigned char)(rgb[2]*inner_product*flag);
+
+    }
+
+    int rgb_flag[3];
+    for (int i = 0; i < 3; i++) {
+      rgb_flag[i] = (tmp_rgb[i] > 255);
+    }
+
+    rgb[0] = tmp_rgb[0]*(1 - rgb_flag[0]) + 255*(rgb_flag[0]);
+    rgb[1] = tmp_rgb[1]*(1 - rgb_flag[1]) + 255*(rgb_flag[1]);
+    rgb[2] = tmp_rgb[2]*(1 - rgb_flag[2]) + 255*(rgb_flag[2]);
+
 
     //計算した rgb を light_rgb にまとめる。
 #if LITTLEENDIAN
--- a/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -168,17 +168,20 @@
                 normal1[0] = sg->normal[(i+0)*3];
                 normal1[1] = sg->normal[(i+0)*3+1];
                 normal1[2] = sg->normal[(i+0)*3+2]*-1.0f;
-                normal1[3] = 1.0f;
+                //normal1[3] = 1.0f;
+		normal1[3] = 0.0f;
 
                 normal2[0] = sg->normal[(i+1)*3];
                 normal2[1] = sg->normal[(i+1)*3+1];
                 normal2[2] = sg->normal[(i+1)*3+2]*-1.0f;
-                normal2[3] = 1.0f;
+                //normal2[3] = 1.0f;
+                normal2[3] = 0.0f;
 
                 normal3[0] = sg->normal[(i+2)*3];
                 normal3[1] = sg->normal[(i+2)*3+1];
                 normal3[2] = sg->normal[(i+2)*3+2]*-1.0f;
-                normal3[3] = 1.0f;
+		//normal3[3] = 1.0f;
+                normal3[3] = 0.0f;
 
                 ApplyNormalMatrix(normal1,sg->real_matrix);
                 ApplyNormalMatrix(normal2,sg->real_matrix);
--- a/Renderer/Engine/viewer.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Engine/viewer.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -105,14 +105,17 @@
     sgroot = new SceneGraphRoot(this->width, this->height);
     sgroot->tmanager = manager;
     
-    int size = sizeof(float)*4;
+    int size = sizeof(float)*4*4; //xyz+alfa(4) * light_num(4)
+    int light_size = size / sizeof(float);
 
     light_xyz_stock = (float *)manager->allocate(size);
     light_xyz = (float *)manager->allocate(size);
-    light_xyz[0] = 0.0f;
-    light_xyz[1] = 0.0f;
-    light_xyz[2] = 0.0f;
-    light_xyz[3] = 0.0f;
+
+    for (int i = 0; i < light_size ; i++) {
+      
+      light_xyz[i] = 0.0f;
+
+    }
 
     HTaskPtr data_load;
     data_load = manager->create_task(DataLoad);
@@ -130,7 +133,6 @@
     }
 
     MainLoop *mainloop = app->init(this, this->width, this->height);
-
     mainloop->mainLoop();
 }
 
@@ -470,16 +472,18 @@
     //task_next = manager->create_task(Dummy);
     //task_next->set_post(post2runLoop, (void*)this);
 
-    //Light info update
+   //Light info update
                                                                    
     HTaskPtr data_update;
     HTaskPtr data_update_wait;
-    int size = sizeof(float)*4;
+    int size = sizeof(float)*4*4; //xyz+alfa(4) * light_num(4) 
+    int light_size = size / sizeof(float);
 
-    light_xyz[0] = light_xyz_stock[0]; 
-    light_xyz[1] = light_xyz_stock[1]; 
-    light_xyz[2] = light_xyz_stock[2]; 
-    light_xyz[3] = light_xyz_stock[3]; 
+    for (int i = 0; i < light_size; i++) {
+
+      light_xyz[i] = light_xyz_stock[i]; 
+
+    }
     
     data_update_wait = manager->create_task(DataUpdate);
     data_update_wait->add_inData(light_xyz,size);
--- a/Renderer/Test/Makefile.macosx	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Test/Makefile.macosx	Sat Feb 06 03:02:48 2010 +0900
@@ -10,7 +10,7 @@
 .cc.o:
 	$(CC) $(CFLAGS)  -c $< -o $@
 
-ALL =  ball_bound boss1_action direction gaplant ieshoot node panel universe untitled vacuum property_test send_linda dynamic writer chain_old SgRootChange 
+ALL =  ball_bound boss1_action direction gaplant ieshoot node panel universe untitled vacuum property_test send_linda dynamic writer chain_old SgRootChange
 oFLAGS=-g -O2
 CFLAGt=-g -O2
 all: $(ALL)
--- a/Renderer/Test/universe.cc	Thu Feb 04 14:50:01 2010 +0900
+++ b/Renderer/Test/universe.cc	Sat Feb 06 03:02:48 2010 +0900
@@ -17,7 +17,7 @@
 static void
 moon_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
 {
-    node->angle[0] += 3.0f;
+  //node->angle[0] += 3.0f;
 }
 
 
@@ -52,9 +52,9 @@
     earth = sgroot->createSceneGraph("Earth");
 
     // SceneGraph の move と collision を設定
-    earth->set_move_collision(earth_move, earth_collision);
-    earth->stack_xyz[0] = 3.0f;
-    earth->stack_xyz[1] = 3.0f;
+    earth->set_move_collision(moon_move, earth_collision);
+    earth->xyz[0] = screen_w / 2;
+    earth->xyz[1] = screen_h / 2;
     
     moon = sgroot->createSceneGraph("Moon");
     moon->set_move_collision(moon_move, moon_collision);