# HG changeset patch # User Shinji KONO # Date 1291899309 -32400 # Node ID 294bc9364bee1763a138f1589e0ec7cc2ad4c596 # Parent 91500a6c4defee0f53f5e465c1b30b87af554b3e matrix diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/Camera.cc --- a/Renderer/Engine/Camera.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/Camera.cc Thu Dec 09 21:55:09 2010 +0900 @@ -1,7 +1,7 @@ #include #include "SceneGraphRoot.h" #include "Camera.h" -#include "sys.h" +#include "matrix_calc.h" #include "Scheduler.h" #include "TaskManager.h" diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/Light.cc --- a/Renderer/Engine/Light.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/Light.cc Thu Dec 09 21:55:09 2010 +0900 @@ -1,7 +1,7 @@ #include #include "SceneGraphRoot.h" #include "Light.h" -#include "sys.h" +#include "matrix_calc.h" #include "Scheduler.h" #include "TaskManager.h" diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/SceneGraph.cc --- a/Renderer/Engine/SceneGraph.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/SceneGraph.cc Thu Dec 09 21:55:09 2010 +0900 @@ -6,7 +6,7 @@ #include #include "SceneGraph.h" #include "xml.h" -#include "sys.h" +#include "matrix_calc.h" #include "TextureHash.h" #include "texture.h" #include "TaskManager.h" @@ -222,7 +222,7 @@ #endif - texture_id = -1; + texture_info.texture_id = -1; move = no_move; collision = no_collision; @@ -563,7 +563,8 @@ exit(0); } - texture_id = makeTapestries(manager, texture_image, tex_id); + texture_info.texture_id = makeTapestries(manager, texture_image, tex_id); + tex_id = texture_info.texture_id; if (unlink(image_name)) { cout << "unlink error\n"; @@ -572,17 +573,17 @@ /** * 以前に Load されている Texture を共用 */ - texture_id = tex_id; + texture_info.texture_id = tex_id; } // こんなことすると list[] のいみあるのかなーと // 微妙に思う、自分で書き換えた感想 by gongo - texture_info.t_w = list[texture_id].t_w; - texture_info.t_h = list[texture_id].t_h;; - texture_info.pixels_orig = list[texture_id].pixels_orig; - texture_info.pixels = list[texture_id].pixels; - texture_info.scale_max = list[texture_id].scale_max; - texture_info.texture_image = list[texture_id].texture_image; + texture_info.t_w = list[tex_id].t_w; + texture_info.t_h = list[tex_id].t_h;; + texture_info.pixels_orig = list[tex_id].pixels_orig; + texture_info.pixels = list[tex_id].pixels; + texture_info.scale_max = list[tex_id].scale_max; + texture_info.texture_image = list[tex_id].texture_image; } diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/SceneGraphRoot.cc --- a/Renderer/Engine/SceneGraphRoot.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/SceneGraphRoot.cc Thu Dec 09 21:55:09 2010 +0900 @@ -3,7 +3,7 @@ #include #include "SceneGraphRoot.h" #include "xml.h" -#include "sys.h" +#include "matrix_calc.h" #include "TextureHash.h" #include "texture.h" #include "Application.h" @@ -290,7 +290,7 @@ int light_num = 4; 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, light[i]->scale, camera->matrix); light_vector[i*4] = 0.0f; light_vector[i*4+1] = 0.0f; @@ -381,9 +381,9 @@ cur_parent->addChild(c); c->frame = t->frame; /*親の回転、座標から、子の回転、座標を算出*/ - get_matrix(c->matrix, c->angle, c->xyz, cur_parent->matrix); + get_matrix(c->matrix, c->angle, c->xyz, c->scale, 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, c->scale, cur_parent->real_matrix); //get_matrix(c->real_matrix, c->angle, c->xyz, camera->real_matrix); } @@ -428,7 +428,7 @@ int light_num = 4; 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, light[i]->scale, camera->matrix); light_vector[i*4] = 0.0f; light_vector[i*4+1] = 0.0f; diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/SgChange.cc --- a/Renderer/Engine/SgChange.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/SgChange.cc Thu Dec 09 21:55:09 2010 +0900 @@ -4,7 +4,7 @@ #include "SceneGraph.h" #include "SceneGraphRoot.h" #include "scene_graph_pack.h" -#include "sys.h" +#include "matrix_calc.h" #include "Func.h" #include "error.h" #include "TaskManager.h" diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/matrix.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Engine/matrix.cc Thu Dec 09 21:55:09 2010 +0900 @@ -0,0 +1,160 @@ +#include "matrix.h" + +// 完全にMac仕様。。sg_matrix を allocate してやらないといけないよ。 +float* +copy_matrix(SceneGraphPtr sg, TaskManager *manager) { + + float *matrix = sg->matrix; + float *real_matrix = sg->real_matrix; + + //変換行列は4x4 なんで、16。が二つで32.と言い訳を書いてみる。 + float *sg_matrix = (float*)manager->allocate(sizeof(float)*32); + + for (int i = 0; i < 16; i++) { + sg_matrix[i] = matrix[i]; + sg_matrix[i+16] = real_matrix[i]; + } + + return sg_matrix; + +} + +void +print_matrix(float *matrix) { + + for (int i = 0; i < 32; i++) { + printf("%f\n",matrix[i]); + } + +} + +void +add_matrix_list(SceneGraphPtr sg, TaskManager *manager, MatrixListInfo* info) { + + MatrixList *matrix_list = (MatrixList*)manager->allocate(sizeof(MatrixList)); + +#if SPE_CREATE_POLYGON_CHECK + print_matrix(sg->sg_matrix); +#endif + + matrix_list->matrix = copy_matrix(sg, manager);; + matrix_list->next = NULL; + + + if (info->last != NULL) { + info->last->next = matrix_list; + } + + info->last = matrix_list; + info->list_length += 1; + +} + +void +new_matrix_info(SceneGraphPtr sg, TaskManager *manager, MatrixListInfo* info) { + + MatrixListInfo *next = NULL; + + if (info->id == -1) { + + info->id = sg->sgid; + info->list_length = 1; + info->coord_pack = sg->coord_pack; + info->coord_pack_size = sg->coord_pack_size; + next = info; + + } else { + + MatrixListInfo* t; + + for (t = info; t->next != NULL; t = t->next) { + } + + next = (MatrixListInfo*)manager->allocate(sizeof(MatrixListInfo)); + next->id = sg->sgid; + next->list_length = 1; + next->coord_pack = sg->coord_pack; + next->coord_pack_size = sg->coord_pack_size; + next->next = NULL; + t->next = next; + + } + + MatrixList *new_list = (MatrixList*)manager->allocate(sizeof(MatrixList)); + new_list->matrix = copy_matrix(sg, manager); + +#if SPE_CREATE_POLYGON_CHECK + print_matrix(sg->sg_matrix); +#endif + + new_list->next = NULL; + + next->first = new_list; + next->last = new_list; + +} + +void +collect_matrix(SceneGraphPtr sg, MatrixListInfo *matrix_info, TaskManager *manager) { + + matrix_info->id = -1; + matrix_info->list_length = 0; + matrix_info->next = NULL; + matrix_info->first = NULL; + matrix_info->last = NULL; + + while (sg) { + + if (sg->flag_drawable) { + + int flag = 0; + + for (MatrixListInfo* t = matrix_info; t != NULL; t = t->next) { + if (sg->sgid == t->id) { + add_matrix_list(sg, manager, t); + flag = 1; + } + } + + if (flag != 1) { + new_matrix_info(sg, manager, matrix_info); + } + + // search SceneGraph. でも、ただのリストがあったハズだから、あとでそれに直す。はず・・ + if (sg->children != NULL) { + sg = sg->children; + } else if (sg->brother != NULL) { + sg = sg->brother; + } else { + while (sg) { + if (sg->brother != NULL) { + sg = sg->brother; + break; + } else { + if (sg->parent == NULL) { + sg = NULL; + break; + } else { + sg = sg->parent; + } + } + } + } + } + } +} + +void +check_matrix(MatrixListInfo *matrix_info,SceneGraphPtr sg) { + + for (MatrixListInfo* t = matrix_info; t != NULL; t = t->next) { + for (MatrixList* u = t->first; u != NULL; u = u->next) { + print_matrix(u->matrix); + } + } + +} + + + +/* end */ diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/matrix.h --- a/Renderer/Engine/matrix.h Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/matrix.h Thu Dec 09 21:55:09 2010 +0900 @@ -1,6 +1,9 @@ #ifndef INCLUDED_MATRIX #define INCLUDED_MATRIX +#include "TaskManager.h" +#include "SceneGraphRoot.h" + struct MatrixList { float *matrix; MatrixList *next; @@ -17,4 +20,22 @@ MatrixListInfo *next; }; + +extern float* copy_matrix(SceneGraphPtr sg, TaskManager *manager) ; + +extern void print_matrix(float *matrix) ; + + +extern void add_matrix_list(SceneGraphPtr sg, TaskManager *manager, MatrixListInfo* info) ; + + +extern void new_matrix_info(SceneGraphPtr sg, TaskManager *manager, MatrixListInfo* info) ; + + +extern void collect_matrix(SceneGraphPtr sg, MatrixListInfo *matrix_info, TaskManager *manager) ; + + +extern void check_matrix(MatrixListInfo *matrix_info,SceneGraphPtr sg) ; + #endif + diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/matrix_calc.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Engine/matrix_calc.cc Thu Dec 09 21:55:09 2010 +0900 @@ -0,0 +1,275 @@ +#include +#include +#include +#include +#include "matrix_calc.h" +using namespace std; + +void noMoreMemory() +{ + cout << "can't allocate memory\n"; + exit(1); +} + +void +transMatrix(float *m0, float *m1, float *v) +{ + memcpy(m0, m1, sizeof(float)*16); + + m0[12] = m1[12] + v[0]; + m0[13] = m1[13] + v[1]; + m0[14] = m1[14] + v[2]; + m0[15] = m1[15] + v[3]; +} + +void +unitMatrix(float *m) +{ + bzero(m, sizeof(float)*16); + + m[0] = 1.0f; + m[5] = 1.0f; + m[10] = 1.0f; + m[15] = 1.0f; +} + +void +inversMatrix(float *m0, float *m1) +{ + float m[16]; + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + m[i*4+j] = m1[j*4+i]; + } + } + + m[12] = -(m1[12]*m[0] + m1[13]*m[1] + m1[14]*m[2]); + m[13] = -(m1[12]*m[4] + m1[13]*m[5] + m1[14]*m[6]); + m[14] = -(m1[12]*m[8] + m1[13]*m[9] + m1[14]*m[10]); + m[3] = m[7] = m[11] = 0.0f; + m[15] = 1.0f; + + memcpy(m0, m, sizeof(float)*16); +} + +/** + * マトリックス m にベクトル v1 を右から乗算して、v0に与える + * @param[out] v0 output vector (float[4]) + * @param[in] v1 input vector (float[4]) + * @param[in] m matrix (float[16]) + */ +void +applyMatrix(float *v0, float *m, float *v1) +{ + for (int i = 0; i < 4; i++) { + v0[i] = v1[0]*m[i] + v1[1]*m[i+4] + v1[2]*m[i+8] + v1[3]*m[i+12]; + } +} + +/** + * ベクトルの正規化 + * + * @param[out] v0 output vector + * @param[in] v1 input vector + */ +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; + } +} + +/** + * ベクトルの減算 v0 = v1 - v2 + */ +void +subVector(float *v0, float *v1, float *v2) +{ + v0[0] = v1[0] - v2[0]; + v0[1] = v1[1] - v2[1]; + v0[2] = v1[2] - v2[2]; + v0[3] = v1[3] - v2[3]; +} + +/** + * ベクトルの外積 v0 = v1 x v2 + */ +void +outerProduct(float *v0, float *v1, float *v2) +{ + v0[0] = v1[1] * v2[2] - v1[2] * v2[1]; + v0[1] = v1[2] * v2[0] - v1[0] * v2[2]; + v0[2] = v1[0] * v2[1] - v1[1] * v2[0]; + v0[3] = 0; +} + +void +transposeMatrix(float *m0, float *m1) +{ + float t[16]; + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + t[i*4+j] = m1[j*4+i]; + } + } + + memcpy(m0, t, sizeof(float)*16); +} + +/** + * ベクトルの内積 f = v0 * v1 + */ +float +innerProduct(float *v0, float *v1) +{ + return (v0[0]*v1[0] + v0[1]*v1[1] + v0[2]*v1[2]); +} + +void matrix4x4(float *xyz, float *xyz1, float *xyz2) //xyz[16] +{ + for(int t=0; t<16; t+=4) + { + for(int i=0; i<4; i++) + { + xyz[t+i] = xyz1[t]*xyz2[i] + xyz1[t+1]*xyz2[4+i] + xyz1[t+2]*xyz2[8+i] + xyz1[t+3]*xyz2[12+i]; + } + } +} + +/** + stack 上の変換行列に、相対的に、rxyz の回転、txyz の平行移動、scale の拡大を + 行ったものを matrix に代入する + */ +void +get_matrix( float *matrix, float *rxyz, float *txyz, float *scale, float *stack) +{ + float radx,rady,radz; + radx = rxyz[0]*3.14/180; + rady = rxyz[1]*3.14/180; + radz = rxyz[2]*3.14/180; + + float sinx = sin(radx); + float cosx = cos(radx); + float siny = sin(rady); + float cosy = cos(rady); + float sinz = sin(radz); + float cosz = cos(radz); + + float m1[16]; + float *m = stack? m1 : matrix; + + /* View Transform */ + m[0] = cosz*cosy+sinz*sinx*siny*scale[0]; + m[1] = sinz*cosx*scale[1]; + m[2] = -cosz*siny+sinz*sinx*cosy*scale[2]; + m[3] = 0; + m[4] = -sinz*cosy+cosz*sinx*siny*scale[0]; + m[5] = cosz*cosx*scale[1]; + m[6] = sinz*siny+cosz*sinx*cosy*scale[2]; + m[7] = 0; + m[8] = cosx*siny*scale[0]; + m[9] = -sinx*scale[1]; + m[10] = cosx*cosy*scale[2]; + m[11] = 0; + m[12] = txyz[0]; + m[13] = txyz[1]; + m[14] = txyz[2]; + m[15] = 1; + + if(stack) + { + matrix4x4(matrix, m, stack); + } + +} + +void rotate_x(float *xyz, float r) +{ + float rad = r*3.14/180; + + xyz[0] = xyz[0]; + xyz[1] = xyz[1]*cos(rad) - xyz[2]*sin(rad); + xyz[2] = xyz[1]*sin(rad) + xyz[2]*cos(rad); +} + +void rotate_y(float *xyz, float r) +{ + float rad = r*3.14/180; + + xyz[0] = xyz[0]*cos(rad) + xyz[2]*sin(rad); + xyz[1] = xyz[1]; + xyz[2] = -xyz[0]*sin(rad) + xyz[2]*cos(rad); +} + +void rotate_z(float *xyz, float r) +{ + float rad = r*3.14/180; + + xyz[0] = xyz[0]*cos(rad) - xyz[1]*sin(rad); + xyz[1] = xyz[0]*sin(rad) + xyz[1]*cos(rad); + xyz[2] = xyz[2]; +} + +void rotate(float *xyz, float *matrix) +{ + float abc[4]; + abc[0] = xyz[0]; + abc[1] = xyz[1]; + abc[2] = xyz[2]; + abc[3] = xyz[3]; + + for(int i=0; i<4; i++) + { + //xyz[i] = abc[0]*rot[i] + abc[1]*rot[i+4] + abc[2]*rot[i+8] + abc[3]*rot[i+12]; + xyz[i] = abc[0]*matrix[i] + abc[1]*matrix[i+4] + abc[2]*matrix[i+8] + abc[3]*matrix[i+12]; + } +} + + +void translate(float *xyz, float x, float y, float z) +{ + xyz[0] += x; + xyz[1] += y; + xyz[2] += z; +} + +/** + * ベクトルに行列を乗算する + * @param[out] v vector (float[4]) + * @param[in] m matrix (float[16]) + */ +void +ApplyMatrix(float *v, float *m) +{ + float t[4]; + + t[0] = v[0]; + t[1] = v[1]; + t[2] = v[2]; + t[3] = v[3]; + + for (int i = 0; i < 4; i++) { + v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; + } +} + +void +ScaleMatrixXYZ(float *m, float x, float y, float z) +{ + for(int i=0;i<3;i++) { + m[i] *= x; + m[i+4] *= y; + m[i+8] *= z; + } +} + diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/matrix_calc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Engine/matrix_calc.h Thu Dec 09 21:55:09 2010 +0900 @@ -0,0 +1,29 @@ +#ifndef CR_SYS_H +#define CR_SYS_H + +void noMoreMemory(); +void get_matrix(float *matrix, float *rxyz, float *txyz, float *scale, float *stack); +void rotate_x(float *xyz, float r); +void rotate_y(float *xyz, float r); +void rotate_z(float *xyz, float r); +//void rotate(float *xyz, float *matrix, float *rxyz, float *txyz, float *stack[]); +void rotate(float *xyz, float *matrix); +void translate(float *xyz, float x, float y, float z); +void matrix4x4(float *, float *, float *); + +void normalize(float *v0, float *v1); +void subVector(float *v0, float *v1, float *v2); +void outerProduct(float *v0, float *v1, float *v2); +float innerProduct(float *v0, float *v1); +void applyMatrix(float *v0, float *m, float *v1); +void inversMatrix(float *m0, float *m1); +void transposeMatrix(float *m0, float *m1); +void unitMatrix(float *m); +void transMatrix(float *m0, float *m1, float *v); +void ApplyMatrix(float *v1, float *v2); +void ScaleMatrix(float *m, float v); +void ScaleMatrixXYZ(float *m, float sx,float sy, float sz); +static inline unsigned long align(unsigned long x,unsigned long alig) { return ((x+(alig-1))&~(alig-1)); } + + +#endif diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/polygon.cc --- a/Renderer/Engine/polygon.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/polygon.cc Thu Dec 09 21:55:09 2010 +0900 @@ -4,7 +4,7 @@ #include #include "polygon.h" #include "xml.h" -#include "sys.h" +#include "matrix_calc.h" #include "triangle.h" #include "vertex.h" #include "Span.h" @@ -17,28 +17,7 @@ Polygon::Polygon(void) { - xyz[0] = 0; - xyz[1] = 0; - xyz[2] = 0; - xyz[3] = 1; - c_xyz[0] = 0; - c_xyz[1] = 0; - c_xyz[2] = 0; - c_xyz[3] = 1; - angle[0] = 0; - angle[1] = 0; - angle[2] = 0; - angle[3] = 1; - - -#if !SPE_CREATE_POLYGON - - for (int i = 0; i < 16; i++) { - matrix[i] = 0; - } - -#endif - + position_init(); } void @@ -56,6 +35,9 @@ angle[1] = 0; angle[2] = 0; angle[3] = 1; + scale[0] = 1; + scale[1] = 1; + scale[2] = 1; #if !SPE_CREATE_POLYGON @@ -68,122 +50,6 @@ } -#if 0 -void Polygon::draw(SceneGraphPack *sgp) -{ - float xyz1[4],xyz2[4],xyz3[4]; - - /***SceneGraphUpdate***/ - //sgp_update(); - for (int i = 0; i < sgp->info.size; i++) { - SceneGraphNode node = sgp->node[i]; - - /***draw***/ - int n,nt; - for(n=0,nt=0; nviewer = viewer; - span->p = this; - span->create_span(tri,texture_image); - delete ver1; - delete ver2; - delete ver3; - delete tri; - delete span; - } - } -} - - -void Polygon::draw(PolygonPack *pp) -{ - for(int n=0; ninfo.size; n++) - { - Vertex *ver1 = new Vertex(pp->tri[n].ver1.x,pp->tri[n].ver1.y,pp->tri[n].ver1.z,pp->tri[n].ver1.tex_x,pp->tri[n].ver1.tex_y); - Vertex *ver2 = new Vertex(pp->tri[n].ver2.x,pp->tri[n].ver2.y,pp->tri[n].ver2.z,pp->tri[n].ver2.tex_x,pp->tri[n].ver2.tex_y); - Vertex *ver3 = new Vertex(pp->tri[n].ver3.x,pp->tri[n].ver3.y,pp->tri[n].ver3.z,pp->tri[n].ver3.tex_x,pp->tri[n].ver3.tex_y); - - Triangle *tri = new Triangle(ver1,ver2,ver3); - Span_c *span = new Span_c(); - span->viewer = viewer; - span->p = this; - span->create_span(tri,texture_image); - delete ver1; - delete ver2; - delete ver3; - delete tri; - delete span; - } -} - -void Polygon::draw(SpanPack *sp) -{ - Span *span; - - for (int n = 0; n < sp->info.size; n++) { - span = &sp->span[n]; - - //int x = span->x; - //int y = span->y; - float z = span->start_z; - int end = span->length_x; - float zpos = span->end_z; - float tex1 = span->tex_x1; - float tex2 = span->tex_x2; - float tey1 = span->tex_y1; - float tey2 = span->tex_y2; - Uint32 rgb; - int tex_xpos; - int tex_ypos; - int tex_zpos; - float tex_x, tex_y, tex_z; - - if (end == 1) { - tex_xpos = (int)((span->tex_height-1) * tex1); - tex_ypos = (int)((span->tex_width-1) * tey1); - tex_zpos = (int)z; - rgb = get_rgb(tex_xpos, tex_ypos); - //viewer->write_pixel(x, y, zpos, rgb); - } else { - for (int j = 0; j < end; j++) { - tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); - tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); - tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); - if (tex_x > 1) tex_x = 1; - if (tex_y > 1) tex_y = 1; - tex_xpos = (int)((span->tex_height-1) * tex_x); - tex_ypos = (int)((span->tex_width-1) * tex_y); - rgb = get_rgb(tex_xpos,tex_ypos); - //viewer->write_pixel(j + x, y, tex_z, rgb); - } - } - } -} - -#endif - void Polygon::pickup_coordinate(char *cont) { @@ -396,27 +262,6 @@ //SDL_LockSurface(texture_image); char *p = get_pixel(tx,ty,texture_image); -#if 0 - pixel = my_ntohl(*(Uint32*)p); - //printf("pixel = %d\n", pixel); - //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); - //SDL_UnlockSurface(texture_image); - - temp = pixel&fmt->Rmask; - temp = temp>>fmt->Rshift; - temp = temp<Rloss; - red = (Uint8)temp; - - temp = pixel&fmt->Gmask; - temp = temp>>fmt->Gshift; - temp = temp<Gloss; - green = (Uint8)temp; - - temp = pixel&fmt->Bmask; - temp = temp>>fmt->Bshift; - temp = temp<Bloss; - blue = (Uint8)temp; -#endif blue = (Uint8) p[0]; green = (Uint8) p[1]; red = (Uint8) p[2]; diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/polygon.h --- a/Renderer/Engine/polygon.h Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/polygon.h Thu Dec 09 21:55:09 2010 +0900 @@ -28,7 +28,7 @@ float angle[4]; // angle float c_xyz[4]; // center of rotation float *anim; - int texture_id; //texture id number + float scale[3]; SDL_Surface* texture_image; diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/spe/DrawSpan.cc --- a/Renderer/Engine/spe/DrawSpan.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/spe/DrawSpan.cc Thu Dec 09 21:55:09 2010 +0900 @@ -5,7 +5,7 @@ #include "task_texture.h" #include "viewer_types.h" #include "Func.h" -#include "sys.h" +#include "matrix_calc.h" #include "SchedTask.h" #include "Tapestry.h" #include "SpanPack.h" diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/sys.cc --- a/Renderer/Engine/sys.cc Wed Dec 08 13:06:18 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,285 +0,0 @@ -#include -#include -#include -#include -#include "sys.h" -using namespace std; - -void noMoreMemory() -{ - cout << "can't allocate memory\n"; - exit(1); -} - -void -transMatrix(float *m0, float *m1, float *v) -{ - memcpy(m0, m1, sizeof(float)*16); - - m0[12] = m1[12] + v[0]; - m0[13] = m1[13] + v[1]; - m0[14] = m1[14] + v[2]; - m0[15] = m1[15] + v[3]; -} - -void -unitMatrix(float *m) -{ - bzero(m, sizeof(float)*16); - - m[0] = 1.0f; - m[5] = 1.0f; - m[10] = 1.0f; - m[15] = 1.0f; -} - -void -inversMatrix(float *m0, float *m1) -{ - float m[16]; - - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - m[i*4+j] = m1[j*4+i]; - } - } - - m[12] = -(m1[12]*m[0] + m1[13]*m[1] + m1[14]*m[2]); - m[13] = -(m1[12]*m[4] + m1[13]*m[5] + m1[14]*m[6]); - m[14] = -(m1[12]*m[8] + m1[13]*m[9] + m1[14]*m[10]); - m[3] = m[7] = m[11] = 0.0f; - m[15] = 1.0f; - - memcpy(m0, m, sizeof(float)*16); -} - -/** - * マトリックス m にベクトル v1 を右から乗算して、v0に与える - * @param[out] v0 output vector (float[4]) - * @param[in] v1 input vector (float[4]) - * @param[in] m matrix (float[16]) - */ -void -applyMatrix(float *v0, float *m, float *v1) -{ - for (int i = 0; i < 4; i++) { - v0[i] = v1[0]*m[i] + v1[1]*m[i+4] + v1[2]*m[i+8] + v1[3]*m[i+12]; - } -} - -/** - * ベクトルの正規化 - * - * @param[out] v0 output vector - * @param[in] v1 input vector - */ -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; - } -} - -/** - * ベクトルの減算 v0 = v1 - v2 - */ -void -subVector(float *v0, float *v1, float *v2) -{ - v0[0] = v1[0] - v2[0]; - v0[1] = v1[1] - v2[1]; - v0[2] = v1[2] - v2[2]; - v0[3] = v1[3] - v2[3]; -} - -/** - * ベクトルの外積 v0 = v1 x v2 - */ -void -outerProduct(float *v0, float *v1, float *v2) -{ - v0[0] = v1[1] * v2[2] - v1[2] * v2[1]; - v0[1] = v1[2] * v2[0] - v1[0] * v2[2]; - v0[2] = v1[0] * v2[1] - v1[1] * v2[0]; - v0[3] = 0; -} - -void -transposeMatrix(float *m0, float *m1) -{ - float t[16]; - - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - t[i*4+j] = m1[j*4+i]; - } - } - - memcpy(m0, t, sizeof(float)*16); -} - -/** - * ベクトルの内積 f = v0 * v1 - */ -float -innerProduct(float *v0, float *v1) -{ - return (v0[0]*v1[0] + v0[1]*v1[1] + v0[2]*v1[2]); -} - -void matrix4x4(float *xyz, float *xyz1, float *xyz2) //xyz[16] -{ - for(int t=0; t<16; t+=4) - { - for(int i=0; i<4; i++) - { - xyz[t+i] = xyz1[t]*xyz2[i] + xyz1[t+1]*xyz2[4+i] + xyz1[t+2]*xyz2[8+i] + xyz1[t+3]*xyz2[12+i]; - } - } -} - -/** - stack 上の変換行列に、相対的に、rxyz の回転、txyz の平行移動を - 行ったものを matrix に代入する - */ -void -get_matrix( float *matrix, float *rxyz, float *txyz, float *stack) -{ - float radx,rady,radz; - radx = rxyz[0]*3.14/180; - rady = rxyz[1]*3.14/180; - radz = rxyz[2]*3.14/180; - - float sinx = sin(radx); - float cosx = cos(radx); - float siny = sin(rady); - float cosy = cos(rady); - float sinz = sin(radz); - float cosz = cos(radz); - - /* View Transform */ - matrix[0] = cosz*cosy+sinz*sinx*siny; - matrix[1] = sinz*cosx; - matrix[2] = -cosz*siny+sinz*sinx*cosy; - matrix[3] = 0; - matrix[4] = -sinz*cosy+cosz*sinx*siny; - matrix[5] = cosz*cosx; - matrix[6] = sinz*siny+cosz*sinx*cosy; - matrix[7] = 0; - matrix[8] = cosx*siny; - matrix[9] = -sinx; - matrix[10] = cosx*cosy; - matrix[11] = 0; - matrix[12] = txyz[0]; - matrix[13] = txyz[1]; - matrix[14] = txyz[2]; - matrix[15] = 1; - - float m[16]; - - for(int i=0; i<16; i++) - { - m[i] = matrix[i]; - } - - if(stack) - { - matrix4x4(matrix, m, stack); - } - -} - -void rotate_x(float *xyz, float r) -{ - float rad = r*3.14/180; - - xyz[0] = xyz[0]; - xyz[1] = xyz[1]*cos(rad) - xyz[2]*sin(rad); - xyz[2] = xyz[1]*sin(rad) + xyz[2]*cos(rad); -} - -void rotate_y(float *xyz, float r) -{ - float rad = r*3.14/180; - - xyz[0] = xyz[0]*cos(rad) + xyz[2]*sin(rad); - xyz[1] = xyz[1]; - xyz[2] = -xyz[0]*sin(rad) + xyz[2]*cos(rad); -} - -void rotate_z(float *xyz, float r) -{ - float rad = r*3.14/180; - - xyz[0] = xyz[0]*cos(rad) - xyz[1]*sin(rad); - xyz[1] = xyz[0]*sin(rad) + xyz[1]*cos(rad); - xyz[2] = xyz[2]; -} - -void rotate(float *xyz, float *matrix) -{ - float abc[4]; - abc[0] = xyz[0]; - abc[1] = xyz[1]; - abc[2] = xyz[2]; - abc[3] = xyz[3]; - - for(int i=0; i<4; i++) - { - //xyz[i] = abc[0]*rot[i] + abc[1]*rot[i+4] + abc[2]*rot[i+8] + abc[3]*rot[i+12]; - xyz[i] = abc[0]*matrix[i] + abc[1]*matrix[i+4] + abc[2]*matrix[i+8] + abc[3]*matrix[i+12]; - } -} - - -void translate(float *xyz, float x, float y, float z) -{ - xyz[0] += x; - xyz[1] += y; - xyz[2] += z; -} - -/** - * ベクトルに行列を乗算する - * @param[out] v vector (float[4]) - * @param[in] m matrix (float[16]) - */ -void -ApplyMatrix(float *v, float *m) -{ - float t[4]; - - t[0] = v[0]; - t[1] = v[1]; - t[2] = v[2]; - t[3] = v[3]; - - for (int i = 0; i < 4; i++) { - v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; - } -} - -void -ScaleMatrix(float *m, float v) -{ - for(int i=0;i<16;i++) - m[i] *= v; -} - -void -ScaleMatrixXY(float *m, float x, float y) -{ - for(int i=0;i<3;i++) { - m[i] *= x; - m[i+4] *= y; - } -} - diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/sys.h --- a/Renderer/Engine/sys.h Wed Dec 08 13:06:18 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#ifndef CR_SYS_H -#define CR_SYS_H - -void noMoreMemory(); -void get_matrix(float *matrix, float *rxyz, float *txyz, float *stack); -void rotate_x(float *xyz, float r); -void rotate_y(float *xyz, float r); -void rotate_z(float *xyz, float r); -//void rotate(float *xyz, float *matrix, float *rxyz, float *txyz, float *stack[]); -void rotate(float *xyz, float *matrix); -void translate(float *xyz, float x, float y, float z); -void matrix4x4(float *, float *, float *); - -void normalize(float *v0, float *v1); -void subVector(float *v0, float *v1, float *v2); -void outerProduct(float *v0, float *v1, float *v2); -float innerProduct(float *v0, float *v1); -void applyMatrix(float *v0, float *m, float *v1); -void inversMatrix(float *m0, float *m1); -void transposeMatrix(float *m0, float *m1); -void unitMatrix(float *m); -void transMatrix(float *m0, float *m1, float *v); -void ApplyMatrix(float *v1, float *v2); -void ScaleMatrix(float *m, float v); -void ScaleMatrixXY(float *m, float sx,float sy); -static inline unsigned long align(unsigned long x,unsigned long alig) { return ((x+(alig-1))&~(alig-1)); } - - -#endif diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/task/CreatePolygonFromSceneGraph.cc --- a/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/task/CreatePolygonFromSceneGraph.cc Thu Dec 09 21:55:09 2010 +0900 @@ -34,7 +34,7 @@ v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; } } - +#if 0 static void ApplyNormalMatrix(float *v, float *m) { @@ -48,7 +48,7 @@ v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8]; } } - +#endif /** * 行列の積 diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/task/DrawSpan.cc --- a/Renderer/Engine/task/DrawSpan.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/task/DrawSpan.cc Thu Dec 09 21:55:09 2010 +0900 @@ -5,10 +5,10 @@ #include "task_texture.h" #include "viewer_types.h" #include "Func.h" -#include "sys.h" #include "SchedTask.h" #include "Tapestry.h" #include "SpanPack.h" +#include "matrix_calc.h" #if (__LITTLE_ENDIAN__) #define LITTLEENDIAN 1 diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/task/update_sgp.cc --- a/Renderer/Engine/task/update_sgp.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/task/update_sgp.cc Thu Dec 09 21:55:09 2010 +0900 @@ -3,7 +3,7 @@ #include #include #include "scene_graph_pack.h" -#include "sys.h" +#include "matrix_calc.h" #include "update_sgp.h" SchedDefineTask(Update_SGP); @@ -16,6 +16,7 @@ SceneGraphPack *_sgp = (SceneGraphPack*)s->get_output(wbuf, 0); //int screen_width = get_param(0); //int screen_height = get_param(1); + float scale[] = {1,1,1}; // 本当はここでやるもんじゃないんだが。。。 for (int i = 0; i < sgp->info.size && i < 3; i++) { @@ -24,11 +25,11 @@ do { if (node->pn != -1) { get_matrix(node->translation, - node->angle, node->obj_pos, + node->angle, node->obj_pos, scale, sgp->node[node->pn].translation); } else { get_matrix(node->translation, - node->angle, node->obj_pos, + node->angle, node->obj_pos, scale, NULL); } diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/texture.h --- a/Renderer/Engine/texture.h Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/texture.h Thu Dec 09 21:55:09 2010 +0900 @@ -17,7 +17,7 @@ int scale_max; SDL_Surface *texture_image; GLuint gl_tex; - int pad[1]; // 12 + int texture_id; } texture_list, *texture_list_ptr ; // 20 + pad(12) = 32 @@ -31,7 +31,7 @@ int scale_max; void *texture_image; void *gl_tex; - int pad[1]; // 12 + int texture_id; } texture_list, *texture_list_ptr ; // 20 + pad(12) = 32 diff -r 91500a6c4def -r 294bc9364bee Renderer/Engine/viewer.cc --- a/Renderer/Engine/viewer.cc Wed Dec 08 13:06:18 2010 +0900 +++ b/Renderer/Engine/viewer.cc Thu Dec 09 21:55:09 2010 +0900 @@ -4,7 +4,7 @@ #include "SceneGraph.h" #include "SceneGraphRoot.h" #include "scene_graph_pack.h" -#include "sys.h" +#include "matrix_calc.h" #include "Func.h" #include "error.h" #include "TaskManager.h" @@ -436,161 +436,6 @@ } -// 完全にMac仕様。。sg_matrix を allocate してやらないといけないよ。 -float* -copy_matrix(SceneGraphPtr sg, TaskManager *manager) { - - float *matrix = sg->matrix; - float *real_matrix = sg->real_matrix; - - //変換行列は4x4 なんで、16。が二つで32.と言い訳を書いてみる。 - float *sg_matrix = (float*)manager->allocate(sizeof(float)*32); - - for (int i = 0; i < 16; i++) { - sg_matrix[i] = matrix[i]; - sg_matrix[i+16] = real_matrix[i]; - } - - return sg_matrix; - -} - -void -print_matrix(float *matrix) { - - for (int i = 0; i < 32; i++) { - printf("%f\n",matrix[i]); - } - -} - -void -add_matrix_list(SceneGraphPtr sg, TaskManager *manager, MatrixListInfo* info) { - - MatrixList *matrix_list = (MatrixList*)manager->allocate(sizeof(MatrixList)); - -#if SPE_CREATE_POLYGON_CHECK - print_matrix(sg->sg_matrix); -#endif - - matrix_list->matrix = copy_matrix(sg, manager);; - matrix_list->next = NULL; - - - if (info->last != NULL) { - info->last->next = matrix_list; - } - - info->last = matrix_list; - info->list_length += 1; - -} - -void -new_matrix_info(SceneGraphPtr sg, TaskManager *manager, MatrixListInfo* info) { - - MatrixListInfo *next = NULL; - - if (info->id == -1) { - - info->id = sg->sgid; - info->list_length = 1; - info->coord_pack = sg->coord_pack; - info->coord_pack_size = sg->coord_pack_size; - next = info; - - } else { - - MatrixListInfo* t; - - for (t = info; t->next != NULL; t = t->next) { - } - - next = (MatrixListInfo*)manager->allocate(sizeof(MatrixListInfo)); - next->id = sg->sgid; - next->list_length = 1; - next->coord_pack = sg->coord_pack; - next->coord_pack_size = sg->coord_pack_size; - next->next = NULL; - t->next = next; - - } - - MatrixList *new_list = (MatrixList*)manager->allocate(sizeof(MatrixList)); - new_list->matrix = copy_matrix(sg, manager); - -#if SPE_CREATE_POLYGON_CHECK - print_matrix(sg->sg_matrix); -#endif - - new_list->next = NULL; - - next->first = new_list; - next->last = new_list; - -} - -void -collect_matrix(SceneGraphPtr sg, MatrixListInfo *matrix_info, TaskManager *manager) { - - matrix_info->id = -1; - matrix_info->list_length = 0; - matrix_info->next = NULL; - matrix_info->first = NULL; - matrix_info->last = NULL; - - while (sg) { - - if (sg->flag_drawable) { - - int flag = 0; - - for (MatrixListInfo* t = matrix_info; t != NULL; t = t->next) { - if (sg->sgid == t->id) { - add_matrix_list(sg, manager, t); - flag = 1; - } - } - - if (flag != 1) { - new_matrix_info(sg, manager, matrix_info); - } - - // search SceneGraph. でも、ただのリストがあったハズだから、あとでそれに直す。はず・・ - if (sg->children != NULL) { - sg = sg->children; - } else if (sg->brother != NULL) { - sg = sg->brother; - } else { - while (sg) { - if (sg->brother != NULL) { - sg = sg->brother; - break; - } else { - if (sg->parent == NULL) { - sg = NULL; - break; - } else { - sg = sg->parent; - } - } - } - } - } - } -} - -void -check_matrix(MatrixListInfo *matrix_info,SceneGraphPtr sg) { - - for (MatrixListInfo* t = matrix_info; t != NULL; t = t->next) { - for (MatrixList* u = t->first; u != NULL; u = u->next) { - print_matrix(u->matrix); - } - } - -} - void coord_allocate(int &cur_point, float *coord_pack, int spe_num,