1013
|
1 /**
|
|
2 * SceneGraph が増えてくると動かなくなるかもしれない。
|
|
3 * 一応 mainMem とかで動くようになるとは思うけど。
|
|
4 * だめだったら、そこら辺が怪しいと思うべき
|
|
5 */
|
|
6
|
|
7 #include "CreatePolygonFromSceneGraph.h"
|
|
8 #include "polygon_pack.h"
|
|
9 #include "scene_graph_pack.h"
|
|
10
|
|
11 SchedDefineTask(CreatePolygonFromSceneGraph);
|
|
12
|
|
13 #define SG_PACK_LOAD 10
|
|
14 #define SG_NODE_LOAD 11
|
|
15 #define PP_LOAD 12
|
|
16 #define PP_STORE 13
|
|
17
|
|
18 /**
|
|
19 * ベクトルに行列を乗算する
|
|
20 * @param[out] v vector (float[4])
|
|
21 * @param[in] m matrix (float[16])
|
|
22 */
|
|
23 static void
|
|
24 ApplyMatrix(float *v, float *m)
|
|
25 {
|
|
26 float t[4];
|
|
27
|
|
28 t[0] = v[0];
|
|
29 t[1] = v[1];
|
|
30 t[2] = v[2];
|
|
31 t[3] = v[3];
|
|
32
|
|
33 for (int i = 0; i < 4; i++) {
|
|
34 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12];
|
|
35 }
|
|
36 }
|
|
37
|
|
38 static void
|
|
39 ApplyNormalMatrix(float *v, float *m)
|
|
40 {
|
|
41 float t[4];
|
|
42
|
|
43 t[0] = v[0];
|
|
44 t[1] = v[1];
|
|
45 t[2] = v[2];
|
|
46
|
|
47 for (int i = 0; i < 3; i++) {
|
|
48 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8];
|
|
49 }
|
|
50 }
|
|
51
|
|
52 static int
|
|
53 run(SchedTask *smanager, void *rbuf, void *wbuf)
|
|
54 {
|
|
55 float xyz1[4], xyz2[4], xyz3[4];
|
|
56 float normal1[4],normal2[4],normal3[4];
|
|
57
|
|
58 //coord_xyz, coord_tex, normal, matrix, real_matrix を受け取る
|
|
59 float *coord_xyz = (float)smanager->get_inData(0);
|
|
60 float *coord_tex = (float)smanager->get_inData(1);
|
|
61 float *normal = (float)smanager->get_inData(2);
|
|
62 float *matrix = (float)smanager->get_inData(3);
|
|
63 float *real_matrix = (float)smanager->get_inData(4);
|
|
64 TrianglePackPtr triangle = (TrianglePackPtr)smanager->get_inData(5);
|
|
65
|
|
66 for (int i = 0; i < sg->size; i += 3) {
|
|
67
|
|
68 xyz1[0] = coord_xyz[(i+0)*3];
|
|
69 xyz1[1] = coord_xyz[(i+0)*3+1];
|
|
70 xyz1[2] = coord_xyz[(i+0)*3+2]*-1.0f;
|
|
71 xyz1[3] = 1.0f;
|
|
72
|
|
73 xyz2[0] = coord_xyz[(i+1)*3];
|
|
74 xyz2[1] = coord_xyz[(i+1)*3+1];
|
|
75 xyz2[2] = coord_xyz[(i+1)*3+2]*-1.0f;
|
|
76 xyz2[3] = 1.0f;
|
|
77
|
|
78 xyz3[0] = coord_xyz[(i+2)*3];
|
|
79 xyz3[1] = coord_xyz[(i+2)*3+1];
|
|
80 xyz3[2] = coord_xyz[(i+2)*3+2]*-1.0f;
|
|
81 xyz3[3] = 1.0f;
|
|
82
|
|
83 // matrix = 回転行列*透視変換行列
|
|
84 ApplyMatrix(xyz1, matrix);
|
|
85 ApplyMatrix(xyz2, matrix);
|
|
86 ApplyMatrix(xyz3, matrix);
|
|
87
|
|
88 xyz1[0] /= xyz1[2];
|
|
89 xyz1[1] /= xyz1[2];
|
|
90 xyz2[0] /= xyz2[2];
|
|
91 xyz2[1] /= xyz2[2];
|
|
92 xyz3[0] /= xyz3[2];
|
|
93 xyz3[1] /= xyz3[2];
|
|
94
|
|
95 triangle->ver1.x = xyz1[0];
|
|
96 triangle->ver1.y = xyz1[1];
|
|
97 triangle->ver1.z = xyz1[2];
|
|
98 triangle->ver1.tex_x = coord_tex[(i+0)*3];
|
|
99 triangle->ver1.tex_y = coord_tex[(i+0)*3+1];
|
|
100
|
|
101 triangle->ver2.x = xyz2[0];
|
|
102 triangle->ver2.y = xyz2[1];
|
|
103 triangle->ver2.z = xyz2[2];
|
|
104 triangle->ver2.tex_x = coord_tex[(i+1)*3];
|
|
105 triangle->ver2.tex_y = coord_tex[(i+1)*3+1];
|
|
106
|
|
107 triangle->ver3.x = xyz3[0];
|
|
108 triangle->ver3.y = xyz3[1];
|
|
109 triangle->ver3.z = xyz3[2];
|
|
110 triangle->ver3.tex_x = coord_tex[(i+2)*3];
|
|
111 triangle->ver3.tex_y = coord_tex[(i+2)*3+1];
|
|
112
|
|
113 normal1[0] = sg->normal[(i+0)*3];
|
|
114 normal1[1] = sg->normal[(i+0)*3+1];
|
|
115 normal1[2] = sg->normal[(i+0)*3+2]*-1.0f;
|
|
116 //normal1[3] = 1.0f;
|
|
117 normal1[3] = 0.0f;
|
|
118
|
|
119 normal2[0] = sg->normal[(i+1)*3];
|
|
120 normal2[1] = sg->normal[(i+1)*3+1];
|
|
121 normal2[2] = sg->normal[(i+1)*3+2]*-1.0f;
|
|
122 //normal2[3] = 1.0f;
|
|
123 normal2[3] = 0.0f;
|
|
124
|
|
125 normal3[0] = sg->normal[(i+2)*3];
|
|
126 normal3[1] = sg->normal[(i+2)*3+1];
|
|
127 normal3[2] = normal[(i+2)*3+2]*-1.0f;
|
|
128 //normal3[3] = 1.0f;
|
|
129 normal3[3] = 0.0f;
|
|
130
|
|
131 ApplyNormalMatrix(normal1,real_matrix);
|
|
132 ApplyNormalMatrix(normal2,real_matrix);
|
|
133 ApplyNormalMatrix(normal3,real_matrix);
|
|
134
|
|
135 normal1[0] /= normal1[2];
|
|
136 normal1[1] /= normal1[2];
|
|
137
|
|
138 normal2[0] /= normal2[2];
|
|
139 normal2[1] /= normal2[2];
|
|
140
|
|
141 normal3[0] /= normal3[2];
|
|
142 normal3[1] /= normal3[2];
|
|
143
|
|
144 triangle->normal1.x = normal1[0];
|
|
145 triangle->normal1.y = normal1[1];
|
|
146 triangle->normal1.z = normal1[2];
|
|
147
|
|
148 triangle->normal2.x = normal2[0];
|
|
149 triangle->normal2.y = normal2[1];
|
|
150 triangle->normal2.z = normal2[2];
|
|
151
|
|
152 triangle->normal3.x = normal3[0];
|
|
153 triangle->normal3.y = normal3[1];
|
|
154 triangle->normal3.z = normal3[2];
|
|
155
|
|
156 triangle->tex_info.addr = sg->texture_info.pixels;
|
|
157 triangle->tex_info.width = sg->texture_info.t_w;
|
|
158 triangle->tex_info.height = sg->texture_info.t_h;
|
|
159 triangle->tex_info.scale_max = sg->texture_info.scale_max;
|
|
160 }
|
|
161 return 0;
|
|
162 }
|