507
|
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 float xyz1[4], xyz2[4], xyz3[4];
|
|
77 /*
|
|
78 *頂点毎に法線ベクトルがある
|
|
79 *面毎じゃない
|
|
80 *なにかに使うのかな?わからないから、一応とっておく。
|
|
81 *by yutaka
|
|
82 */
|
|
83 float normal1[4],normal2[4],normal3[4];
|
|
84
|
|
85 SceneGraphPtr sg_top = (SceneGraphPtr)smanager->get_param(0);
|
|
86 SceneGraphPtr sg = sg_top;
|
|
87
|
|
88 PolygonPackPtr pp
|
|
89 = (PolygonPackPtr)smanager->allocate(sizeof(PolygonPack));
|
|
90 PolygonPackPtr send_pp
|
|
91 = (PolygonPackPtr)smanager->allocate(sizeof(PolygonPack));
|
|
92 PolygonPackPtr pp_addr = (PolygonPackPtr)smanager->get_param(1);
|
|
93 PolygonPackPtr tmp_pp;
|
|
94
|
|
95 pp->init();
|
|
96 send_pp->init();
|
|
97
|
|
98 while (sg) {
|
|
99 if (sg->flag_drawable) { // sg->isDrawable() とかの方がよくね?
|
|
100 for (int i = 0; i < sg->size; i += 3) {
|
|
101 if (pp->info.size >= MAX_SIZE_TRIANGLE) {
|
|
102 PolygonPackPtr next;
|
|
103
|
|
104 smanager->mainMem_alloc(0, sizeof(PolygonPack));
|
|
105 smanager->mainMem_wait();
|
|
106 next = (PolygonPackPtr)smanager->mainMem_get(0);
|
|
107
|
|
108 pp->next = next;
|
|
109
|
|
110 tmp_pp = pp;
|
|
111 pp = send_pp;
|
|
112 send_pp = tmp_pp;
|
|
113
|
|
114 smanager->dma_wait(PP_STORE);
|
|
115 smanager->dma_store(send_pp, (uint32)pp_addr,
|
|
116 sizeof(PolygonPack), PP_STORE);
|
|
117
|
|
118 pp_addr = next;
|
|
119
|
|
120 smanager->dma_wait(PP_LOAD);
|
|
121 smanager->dma_load(pp, (uint32)pp_addr,
|
|
122 sizeof(PolygonPack), PP_LOAD);
|
|
123 smanager->dma_wait(PP_LOAD);
|
|
124 pp->init();
|
|
125 }
|
|
126
|
|
127 TrianglePack *triangle = &pp->tri[pp->info.size++];
|
|
128
|
|
129 xyz1[0] = sg->coord_xyz[(i+0)*3];
|
|
130 xyz1[1] = sg->coord_xyz[(i+0)*3+1];
|
|
131 xyz1[2] = sg->coord_xyz[(i+0)*3+2]*-1.0f;
|
|
132 xyz1[3] = 1.0f;
|
|
133
|
|
134 xyz2[0] = sg->coord_xyz[(i+1)*3];
|
|
135 xyz2[1] = sg->coord_xyz[(i+1)*3+1];
|
|
136 xyz2[2] = sg->coord_xyz[(i+1)*3+2]*-1.0f;
|
|
137 xyz2[3] = 1.0f;
|
|
138
|
|
139 xyz3[0] = sg->coord_xyz[(i+2)*3];
|
|
140 xyz3[1] = sg->coord_xyz[(i+2)*3+1];
|
|
141 xyz3[2] = sg->coord_xyz[(i+2)*3+2]*-1.0f;
|
|
142 xyz3[3] = 1.0f;
|
|
143
|
|
144 // sg->matrix = 回転行列*透視変換行列
|
|
145 ApplyMatrix(xyz1, sg->matrix);
|
|
146 ApplyMatrix(xyz2, sg->matrix);
|
|
147 ApplyMatrix(xyz3, sg->matrix);
|
|
148
|
|
149 xyz1[0] /= xyz1[2];
|
|
150 xyz1[1] /= xyz1[2];
|
|
151 xyz2[0] /= xyz2[2];
|
|
152 xyz2[1] /= xyz2[2];
|
|
153 xyz3[0] /= xyz3[2];
|
|
154 xyz3[1] /= xyz3[2];
|
|
155
|
|
156 triangle->ver1.x = xyz1[0];
|
|
157 triangle->ver1.y = xyz1[1];
|
|
158 triangle->ver1.z = xyz1[2];
|
|
159 triangle->ver1.tex_x = sg->coord_tex[(i+0)*3];
|
|
160 triangle->ver1.tex_y = sg->coord_tex[(i+0)*3+1];
|
|
161
|
|
162 triangle->ver2.x = xyz2[0];
|
|
163 triangle->ver2.y = xyz2[1];
|
|
164 triangle->ver2.z = xyz2[2];
|
|
165 triangle->ver2.tex_x = sg->coord_tex[(i+1)*3];
|
|
166 triangle->ver2.tex_y = sg->coord_tex[(i+1)*3+1];
|
|
167
|
|
168 triangle->ver3.x = xyz3[0];
|
|
169 triangle->ver3.y = xyz3[1];
|
|
170 triangle->ver3.z = xyz3[2];
|
|
171 triangle->ver3.tex_x = sg->coord_tex[(i+2)*3];
|
|
172 triangle->ver3.tex_y = sg->coord_tex[(i+2)*3+1];
|
|
173
|
|
174 //法線もApplyMatrixとかするのかな?一応値をとっておくby yutaka
|
|
175
|
|
176 normal1[0] = sg->normal[(i+0)*3];
|
|
177 normal1[1] = sg->normal[(i+0)*3+1];
|
|
178 normal1[2] = sg->normal[(i+0)*3+2]*-1.0f;
|
|
179 normal1[3] = 1.0f;
|
|
180
|
|
181 normal2[0] = sg->normal[(i+1)*3];
|
|
182 normal2[1] = sg->normal[(i+1)*3+1];
|
|
183 normal2[2] = sg->normal[(i+1)*3+2]*-1.0f;
|
|
184 normal2[3] = 1.0f;
|
|
185
|
|
186 normal3[0] = sg->normal[(i+2)*3];
|
|
187 normal3[1] = sg->normal[(i+2)*3+1];
|
|
188 normal3[2] = sg->normal[(i+2)*3+2]*-1.0f;
|
|
189 normal3[3] = 1.0f;
|
|
190
|
|
191 ApplyNormalMatrix(normal1,sg->matrix);
|
|
192
|
|
193 //ここでpolygonに法線ベクトルの情報がわたったby yutaka
|
|
194
|
|
195 triangle->normal1.x = normal1[0];
|
|
196 triangle->normal1.y = normal1[1];
|
|
197 triangle->normal1.z = normal1[2];
|
|
198
|
|
199 triangle->normal2.x = normal2[0];
|
|
200 triangle->normal2.y = normal2[1];
|
|
201 triangle->normal2.z = normal2[2];
|
|
202
|
|
203 triangle->normal3.x = normal3[0];
|
|
204 triangle->normal3.y = normal3[1];
|
|
205 triangle->normal3.z = normal3[2];
|
|
206
|
|
207
|
|
208
|
|
209 triangle->tex_info.addr = sg->texture_info.pixels;
|
|
210 triangle->tex_info.width = sg->texture_info.t_w;
|
|
211 triangle->tex_info.height = sg->texture_info.t_h;
|
|
212 triangle->tex_info.scale_max = sg->texture_info.scale_max;
|
|
213 }
|
|
214 }
|
|
215
|
|
216 if (sg->children != NULL) {
|
|
217 sg = sg->children;
|
|
218 } else if (sg->brother != NULL) {
|
|
219 sg = sg->brother;
|
|
220 } else {
|
|
221 while (sg) {
|
|
222 if (sg->brother != NULL) {
|
|
223 sg = sg->brother;
|
|
224 break;
|
|
225 } else {
|
|
226 if (sg->parent == NULL) {
|
|
227 sg = NULL;
|
|
228 break;
|
|
229 } else {
|
|
230 sg = sg->parent;
|
|
231 }
|
|
232 }
|
|
233 }
|
|
234 }
|
|
235 }
|
|
236
|
|
237 smanager->dma_wait(PP_STORE);
|
|
238 smanager->dma_store(pp, (uint32)pp_addr,
|
|
239 sizeof(PolygonPack), PP_STORE);
|
|
240 smanager->dma_wait(PP_STORE);
|
|
241
|
|
242 free(pp);
|
|
243 free(send_pp);
|
|
244
|
|
245 return 0;
|
|
246 }
|