Mercurial > hg > Game > Cerium
changeset 1295:5a67df5570e8 draft
add matrix_test file
author | Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 02 Dec 2011 15:05:05 +0900 |
parents | 3fa46ff7824e |
children | 4a33b2e340a9 |
files | Renderer/Test/matrix_test.cc Renderer/Test/matrix_test.h |
diffstat | 2 files changed, 350 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Test/matrix_test.cc Fri Dec 02 15:05:05 2011 +0900 @@ -0,0 +1,340 @@ +#include <math.h> +#include <stdlib.h> +#include "SceneGraphRoot.h" +#include "matrix_calc.h" +#include "matrix_test.h" + +/* + * ある頂点と、その法線を対象に、それぞれの座標系で光源計算をするテストコード + * +*/ + + +/* + * 行列 m で obj_v, ligth_v を変換. normal vector を平行移動抜きの行列で変換 + * 正規化して, light vector , nor_re_v を正規化して内積を求める. (拡散成分) + * + * @param[in] obj input scenegraph object + * @param[in] light input light object + * @param[in] m 4x4 matrix + * @param[in] obj_v scenegraph object vertex + * @param[in] light_v light object vertex + * @param[in] normal normal vector + */ + +static void +print_vector4(const float *obj_re_v, const float *light_re_v, + const float *light_vector, const float *nor_re_v) + +{ + + printf("obj_v"); + for (int i = 0; i < 4; i++) printf(" %f",obj_re_v[i]); + printf("\n"); + + printf("light_v"); + for (int i = 0; i < 4; i++) printf(" %f",light_re_v[i]); + printf("\n"); + + printf("light_vector"); + for (int i = 0; i < 4; i++) printf(" %f",light_vector[i]); + printf("\n"); + + printf("normal_vector"); + for (int i = 0; i < 4; i++) printf(" %f",nor_re_v[i]); + printf("\n"); + printf("\n"); + + +} + +static void +reject_translation(float *m_out, const float *m_in) +{ + + for (int i = 0; i < 16; i++) m_out[i] = m_in[i]; + m_out[4*0+3]=m_out[4*1+3]=m_out[4*2+3]=0; + +} + + + +MainLoopPtr +matrix_test::init(Viewer *sgroot, int screen_w, int screen_h) +{ + + CameraPtr ca = sgroot->getCamera(); + + SceneGraphPtr light = sgroot->getLight(0); + + + light->xyz[0] = screen_w/2; + light->xyz[1] = screen_h/2; + light->xyz[2] = 10; + + float light_v[4] = {0,0,0,1}; //ローカル座標 + + sgroot->createFromXMLfile("xml_file/panel_512.xml"); + SceneGraphPtr obj = sgroot->createSceneGraph("PANEL_512"); + obj->xyz[0] = screen_w/2; + obj->xyz[1] = screen_h/2; + obj->xyz[2] = 100; + + //obj->angle[0] = 0; + + PolygonPack *pp = obj->pp; + + int polygon_size = pp->info.size; + printf("polygon size %d \n",polygon_size); + + VertexPack v = obj->pp->tri[0].ver1; + float obj_v[4]; + + obj_v[0] = v.x; + obj_v[1] = v.y; + obj_v[2] = v.z; + obj_v[3] = 1.0f; + + printf("ver x %f\n",v.x); + printf("ver y %f\n",v.y); + printf("ver z %f\n",v.z); + + NormalPack n = obj->pp->tri[0].normal1; + float normal[4]; + normal[0] = n.x; + normal[1] = n.y; + normal[2] = n.z; + normal[3] = 0.0f; + + printf("nor x %f\n",n.x); + printf("nor y %f\n",n.y); + printf("nor z %f\n",n.z); + printf("\n\n"); + + float obj_re_v[4]; + float light_re_v[4]; + float nor_re_v[4]; + float light_vector[4]; + float normal_matrix[16]; + + /* + * + * --world-- + * + */ + + printf("--world--\n"); + + float unit[16]; + unitMatrix(unit); + + // wrold matrix * unit matrix + get_matrix(obj->matrix, obj->angle, obj->xyz, unit); + get_matrix(light->matrix, light->angle, light->xyz, unit); + + // vertex * matrix + applyMatrix(obj_re_v, obj->matrix, obj_v); + applyMatrix(light_re_v, light->matrix, light_v); + + // make normal matrix + reject_translation(normal_matrix, obj->matrix); + + light_vector[0] = obj_re_v[0] - light_re_v[0]; + light_vector[1] = obj_re_v[1] - light_re_v[1]; + light_vector[2] = obj_re_v[2] - light_re_v[2]; + + applyMatrix(nor_re_v, obj->matrix, normal); + + normalize(light_vector, light_vector); + normalize(nor_re_v, nor_re_v); + + float p = innerProduct(light_vector, nor_re_v); + + printf("inner Product %f\n", p); + printf("\n"); + printf("\n"); + + print_vector4(obj_re_v, light_re_v, light_vector, nor_re_v); + + /* + * + * --world * view-- + * + */ + + printf("--world * view--\n"); + + // world matrix * view matrix + get_matrix(obj->matrix, obj->angle, obj->xyz, ca->m_view); + get_matrix(light->matrix, light->angle, light->xyz, ca->m_view); + + // vertex * matrix + applyMatrix(obj_re_v, obj->matrix, obj_v); + applyMatrix(light_re_v, light->matrix, light_v); + + // make normal matrix + reject_translation(normal_matrix, obj->matrix); + + applyMatrix(nor_re_v, obj->matrix, normal); + + light_vector[0] = obj_re_v[0] - light_re_v[0]; + light_vector[1] = obj_re_v[1] - light_re_v[1]; + light_vector[2] = obj_re_v[2] - light_re_v[2]; + + normalize(light_vector, light_vector); + normalize(nor_re_v, nor_re_v); + + p = innerProduct(light_vector, nor_re_v); + printf("inner Product %f\n", p); + + print_vector4(obj_re_v, light_re_v, light_vector, nor_re_v); + + /* + * + * --world * view * perspective-- + * + */ + + printf("--world * view * perspective--\n"); + mulMatrix4x4(obj->matrix, ca->m_pers); + mulMatrix4x4(light->matrix, ca->m_pers); + + applyMatrix(obj_re_v, obj->matrix, obj_v); + applyMatrix(light_re_v, light->matrix, light_v); + + // make normal matrix + reject_translation(normal_matrix, obj->matrix); + + applyMatrix(nor_re_v, obj->matrix, normal); + + obj_re_v[0] /= obj_re_v[3]; + obj_re_v[1] /= obj_re_v[3]; + obj_re_v[2] /= obj_re_v[3]; + + light_re_v[0] /= light_re_v[3]; + light_re_v[1] /= light_re_v[3]; + light_re_v[2] /= light_re_v[3]; + + light_vector[0] = obj_re_v[0] - light_re_v[0]; + light_vector[1] = obj_re_v[1] - light_re_v[1]; + light_vector[2] = obj_re_v[2] - light_re_v[2]; + + normalize(light_vector, light_vector); + normalize(nor_re_v, nor_re_v); + + p = innerProduct(light_vector, nor_re_v); + printf("inner Product %f\n", p); + + print_vector4(obj_re_v, light_re_v, light_vector, nor_re_v); + + /* + * + * --world * view * perspective * screen-- + * + */ + + printf("--world * view * perspective * screen--\n"); + mulMatrix4x4(obj->matrix, ca->m_screen); + mulMatrix4x4(light->matrix, ca->m_screen); + + applyMatrix(obj_re_v, obj->matrix, obj_v); + applyMatrix(light_re_v, light->matrix, light_v); + + // make normal matrix + reject_translation(normal_matrix, obj->matrix); + + applyMatrix(nor_re_v, obj->matrix, normal); + + obj_re_v[0] /= obj_re_v[3]; + obj_re_v[1] /= obj_re_v[3]; + obj_re_v[2] /= obj_re_v[3]; + + light_re_v[0] /= light_re_v[3]; + light_re_v[1] /= light_re_v[3]; + light_re_v[2] /= light_re_v[3]; + + light_vector[0] = obj_re_v[0] - light_re_v[0]; + light_vector[1] = obj_re_v[1] - light_re_v[1]; + light_vector[2] = obj_re_v[2] - light_re_v[2]; + + normalize(light_vector, light_vector); + normalize(nor_re_v, nor_re_v); + + p = innerProduct(light_vector, nor_re_v); + printf("inner Product %f\n", p); + + print_vector4(obj_re_v, light_re_v, light_vector, nor_re_v); + + + /* + * + * --Cerium-- + * + */ + + printf("--Cerium--\n"); + + get_matrix(obj->matrix, obj->angle, obj->xyz, ca->matrix); + get_matrix(light->matrix, light->angle, light->xyz, ca->matrix); + + applyMatrix(obj_re_v, obj->matrix, obj_v); + applyMatrix(light_re_v, light->matrix, light_v); + + // make normal matrix + reject_translation(normal_matrix, obj->matrix); + + applyMatrix(nor_re_v, obj->matrix, normal); + + obj_re_v[0] /= obj_re_v[3]; + obj_re_v[1] /= obj_re_v[3]; + obj_re_v[2] /= obj_re_v[3]; + + light_re_v[0] /= light_re_v[3]; + light_re_v[1] /= light_re_v[3]; + light_re_v[2] /= light_re_v[3]; + + light_vector[0] = obj_re_v[0] - light_re_v[0]; + light_vector[1] = obj_re_v[1] - light_re_v[1]; + light_vector[2] = obj_re_v[2] - light_re_v[2]; + + normalize(light_vector, light_vector); + normalize(nor_re_v, nor_re_v); + + p = innerProduct(light_vector, nor_re_v); + printf("inner Product %f\n", p); + + print_vector4(obj_re_v, light_re_v, light_vector, nor_re_v); + + sgroot->setSceneData(obj); + + + return sgroot; +} + +extern Application * +application() { + return new matrix_test(); +} + +const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; + +extern int init(TaskManager *manager, int argc, char *argv[]); +extern void task_initialize(); +static void TMend(TaskManager *manager); + +int +TMmain(TaskManager *manager, int argc, char *argv[]) +{ + task_initialize(); + manager->set_TMend(TMend); + return init(manager, argc, argv); + +} + +void +TMend(TaskManager *manager) +{ + printf("test_nogl end\n"); +} + +/* end */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Test/matrix_test.h Fri Dec 02 15:05:05 2011 +0900 @@ -0,0 +1,10 @@ +#include <math.h> +#include <stdlib.h> +#include "SceneGraphRoot.h" +#include "Application.h" +#include "MainLoop.h" + +class matrix_test : public Application { + + MainLoopPtr init(Viewer *viewer, int screen_w, int screen_h); +};