0
|
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
|
|
53 /**
|
|
54 * 行列の積
|
|
55 *
|
|
56 * @param[out] m0 output matrix
|
|
57 * @param[in] m1 input left matrix
|
|
58 * @param[in] m2 input right matrix
|
|
59 */
|
|
60 /*
|
|
61 static void
|
|
62 MulMatrix(float *m0, float *m1, float *m2) //xyz[16]
|
|
63 {
|
|
64 for(int t = 0; t < 16; t += 4) {
|
|
65 for (int i = 0; i < 4; i++) {
|
|
66 m0[t+i] =
|
|
67 m1[t+0]*m2[ i ] + m1[t+1]*m2[i+ 4] +
|
|
68 m1[t+2]*m2[i+8] + m1[t+3]*m2[i+12];
|
|
69 }
|
|
70 }
|
|
71 }
|
|
72 */
|
|
73 static int
|
|
74 run(SchedTask *smanager, void *rbuf, void *wbuf)
|
|
75 {
|
|
76
|
|
77
|
|
78 float xyz1[4], xyz2[4], xyz3[4];
|
|
79 float normal1[4],normal2[4],normal3[4];
|
|
80
|
|
81 SceneGraphPtr sg_top = (SceneGraphPtr)smanager->get_param(0);
|
|
82 SceneGraphPtr sg = sg_top;
|
|
83
|
|
84 PolygonPackPtr pp
|
|
85 = (PolygonPackPtr)smanager->allocate(sizeof(PolygonPack));
|
|
86 PolygonPackPtr send_pp
|
|
87 = (PolygonPackPtr)smanager->allocate(sizeof(PolygonPack));
|
|
88 PolygonPackPtr pp_addr = (PolygonPackPtr)smanager->get_param(1);
|
|
89 PolygonPackPtr tmp_pp;
|
|
90
|
|
91 pp->init();
|
|
92 send_pp->init();
|
|
93
|
|
94 while (sg) {
|
|
95 if (sg->flag_drawable) { // sg->isDrawable() とかの方がよくね?
|
|
96 for (int i = 0; i < sg->size; i += 3) {
|
|
97 if (pp->info.size >= MAX_SIZE_TRIANGLE) {
|
|
98 PolygonPackPtr next;
|
|
99
|
|
100 smanager->mainMem_alloc(0, sizeof(PolygonPack));
|
|
101 smanager->mainMem_wait();
|
|
102 next = (PolygonPackPtr)smanager->mainMem_get(0);
|
|
103
|
|
104 pp->next = next;
|
|
105
|
|
106 tmp_pp = pp;
|
|
107 pp = send_pp;
|
|
108 send_pp = tmp_pp;
|
|
109
|
|
110 smanager->dma_wait(PP_STORE);
|
|
111 smanager->dma_store(send_pp, (memaddr)pp_addr,
|
|
112 sizeof(PolygonPack), PP_STORE);
|
|
113
|
|
114 pp_addr = next;
|
|
115
|
|
116 smanager->dma_wait(PP_LOAD);
|
|
117 smanager->dma_load(pp, (memaddr)pp_addr,
|
|
118 sizeof(PolygonPack), PP_LOAD);
|
|
119 smanager->dma_wait(PP_LOAD);
|
|
120 pp->init();
|
|
121 }
|
|
122
|
|
123 TrianglePack *triangle = &pp->tri[pp->info.size++];
|
|
124
|
|
125 xyz1[0] = sg->coord_xyz[(i+0)*3];
|
|
126 xyz1[1] = sg->coord_xyz[(i+0)*3+1];
|
|
127 xyz1[2] = sg->coord_xyz[(i+0)*3+2]*-1.0f;
|
|
128 xyz1[3] = 1.0f;
|
|
129
|
|
130 xyz2[0] = sg->coord_xyz[(i+1)*3];
|
|
131 xyz2[1] = sg->coord_xyz[(i+1)*3+1];
|
|
132 xyz2[2] = sg->coord_xyz[(i+1)*3+2]*-1.0f;
|
|
133 xyz2[3] = 1.0f;
|
|
134
|
|
135 xyz3[0] = sg->coord_xyz[(i+2)*3];
|
|
136 xyz3[1] = sg->coord_xyz[(i+2)*3+1];
|
|
137 xyz3[2] = sg->coord_xyz[(i+2)*3+2]*-1.0f;
|
|
138 xyz3[3] = 1.0f;
|
|
139
|
|
140 // sg->matrix = 回転行列*透視変換行列
|
|
141 ApplyMatrix(xyz1, sg->matrix);
|
|
142 ApplyMatrix(xyz2, sg->matrix);
|
|
143 ApplyMatrix(xyz3, sg->matrix);
|
|
144
|
|
145 xyz1[0] /= xyz1[2];
|
|
146 xyz1[1] /= xyz1[2];
|
|
147 xyz2[0] /= xyz2[2];
|
|
148 xyz2[1] /= xyz2[2];
|
|
149 xyz3[0] /= xyz3[2];
|
|
150 xyz3[1] /= xyz3[2];
|
|
151
|
|
152 triangle->ver1.x = xyz1[0];
|
|
153 triangle->ver1.y = xyz1[1];
|
|
154 triangle->ver1.z = xyz1[2];
|
|
155 triangle->ver1.tex_x = sg->coord_tex[(i+0)*3];
|
|
156 triangle->ver1.tex_y = sg->coord_tex[(i+0)*3+1];
|
|
157
|
|
158 triangle->ver2.x = xyz2[0];
|
|
159 triangle->ver2.y = xyz2[1];
|
|
160 triangle->ver2.z = xyz2[2];
|
|
161 triangle->ver2.tex_x = sg->coord_tex[(i+1)*3];
|
|
162 triangle->ver2.tex_y = sg->coord_tex[(i+1)*3+1];
|
|
163
|
|
164 triangle->ver3.x = xyz3[0];
|
|
165 triangle->ver3.y = xyz3[1];
|
|
166 triangle->ver3.z = xyz3[2];
|
|
167 triangle->ver3.tex_x = sg->coord_tex[(i+2)*3];
|
|
168 triangle->ver3.tex_y = sg->coord_tex[(i+2)*3+1];
|
|
169
|
|
170 normal1[0] = sg->normal[(i+0)*3];
|
|
171 normal1[1] = sg->normal[(i+0)*3+1];
|
|
172 normal1[2] = sg->normal[(i+0)*3+2]*-1.0f;
|
|
173 //normal1[3] = 1.0f;
|
|
174 normal1[3] = 0.0f;
|
|
175
|
|
176 normal2[0] = sg->normal[(i+1)*3];
|
|
177 normal2[1] = sg->normal[(i+1)*3+1];
|
|
178 normal2[2] = sg->normal[(i+1)*3+2]*-1.0f;
|
|
179 //normal2[3] = 1.0f;
|
|
180 normal2[3] = 0.0f;
|
|
181
|
|
182 normal3[0] = sg->normal[(i+2)*3];
|
|
183 normal3[1] = sg->normal[(i+2)*3+1];
|
|
184 normal3[2] = sg->normal[(i+2)*3+2]*-1.0f;
|
|
185 //normal3[3] = 1.0f;
|
|
186 normal3[3] = 0.0f;
|
|
187
|
|
188 ApplyNormalMatrix(normal1,sg->real_matrix);
|
|
189 ApplyNormalMatrix(normal2,sg->real_matrix);
|
|
190 ApplyNormalMatrix(normal3,sg->real_matrix);
|
|
191
|
|
192 normal1[0] /= normal1[2];
|
|
193 normal1[1] /= normal1[2];
|
|
194
|
|
195 normal2[0] /= normal2[2];
|
|
196 normal2[1] /= normal2[2];
|
|
197
|
|
198 normal3[0] /= normal3[2];
|
|
199 normal3[1] /= normal3[2];
|
|
200
|
|
201 triangle->normal1.x = normal1[0];
|
|
202 triangle->normal1.y = normal1[1];
|
|
203 triangle->normal1.z = normal1[2];
|
|
204
|
|
205 triangle->normal2.x = normal2[0];
|
|
206 triangle->normal2.y = normal2[1];
|
|
207 triangle->normal2.z = normal2[2];
|
|
208
|
|
209 triangle->normal3.x = normal3[0];
|
|
210 triangle->normal3.y = normal3[1];
|
|
211 triangle->normal3.z = normal3[2];
|
|
212
|
|
213 triangle->tex_info.addr = sg->texture_info.pixels;
|
|
214 triangle->tex_info.width = sg->texture_info.t_w;
|
|
215 triangle->tex_info.height = sg->texture_info.t_h;
|
|
216 triangle->tex_info.scale_max = sg->texture_info.scale_max;
|
|
217 }
|
|
218 }
|
|
219
|
|
220 if (sg->children != NULL) {
|
|
221 sg = sg->children;
|
|
222 } else if (sg->brother != NULL) {
|
|
223 sg = sg->brother;
|
|
224 } else {
|
|
225 while (sg) {
|
|
226 if (sg->brother != NULL) {
|
|
227 sg = sg->brother;
|
|
228 break;
|
|
229 } else {
|
|
230 if (sg->parent == NULL) {
|
|
231 sg = NULL;
|
|
232 break;
|
|
233 } else {
|
|
234 sg = sg->parent;
|
|
235 }
|
|
236 }
|
|
237 }
|
|
238 }
|
|
239 }
|
|
240
|
|
241 smanager->dma_wait(PP_STORE);
|
|
242 smanager->dma_store(pp, (memaddr)pp_addr,
|
|
243 sizeof(PolygonPack), PP_STORE);
|
|
244 smanager->dma_wait(PP_STORE);
|
|
245
|
|
246 free(pp);
|
|
247 free(send_pp);
|
|
248
|
|
249 return 0;
|
|
250
|
|
251 }
|