Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/spe/DrawSpan.cc @ 762:10a8a80c2ea7
add lights
author | yutaka@henri.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Sat, 06 Feb 2010 03:02:48 +0900 |
parents | b7376415fa5f |
children | dc26593f8c40 |
rev | line source |
---|---|
507 | 1 #include <stdlib.h> |
2 #include <string.h> | |
3 #include "DrawSpan.h" | |
4 #include "polygon_pack.h" | |
5 #include "texture.h" | |
6 #include "viewer_types.h" | |
7 #include "Func.h" | |
594 | 8 #include "sys.h" |
507 | 9 #include "global_alloc.h" |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
10 #include "SchedTask.h" |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
11 #include "Tapestry.h" |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
12 #include "SpanPack.h" |
762 | 13 #include <math.h> |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
14 |
623 | 15 #if (__LITTLE_ENDIAN__) |
16 #define LITTLEENDIAN 1 | |
17 #else | |
18 #define LITTLEENDIAN 0 | |
19 #endif | |
507 | 20 |
21 SchedDefineTask(DrawSpan); | |
22 | |
23 #define TEX_LOAD1 0 | |
24 #define TEX_LOAD2 1 | |
25 #define SPAN_PACK_LOAD 2 | |
26 #define FB_STORE 3 | |
27 | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
28 typedef struct { |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
29 int **linebuf; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
30 float *zRow; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
31 TileListPtr tileList; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
32 int doneWrite; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
33 } G, *Gptr; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
34 |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
35 static int** linebuf_init(SchedTask *smanager, int width, int height, int rgb); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
36 static float* zRow_init(SchedTask *smanager, int width, int height); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
37 // static TilePtr set_rgb(memaddr addr, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
38 // static void set_rgbs(memaddr addr, uint32 *max_addr, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
39 static uint32 get_rgb(int tx, int ty, TilePtr tile); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
40 // static TilePtr isAvailableTile(memaddr addr); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
41 static memaddr getTile(int tx, int ty, int tw, memaddr tex_addr_top); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
42 static int getTexBlock(int tx, int ty, int twidth); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
43 |
748 | 44 static void updateBuffer(Gptr g, float zpos, int rangex, int loaclx, int localy, |
45 int tex_x, int tex_y, float normal_x, float nomral_x, float normal_z, | |
46 TilePtr tile, int world_x, int world_y, float world_z,SchedTask *smanager); | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
47 |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
48 // static void reboot(SpanPackPtr spack, int cur_span_x); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
49 |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
50 static int drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
51 //static void drawDot2(SchedTask *smanager, SpanPtr span, int startx, int endx, int js, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
52 static int drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
53 // static void drawLine2(SchedTask *smanager, SpanPtr span, int startx, int endx, int js, int wait_tag); |
748 | 54 |
55 static int infinity_light_calc(int color,float normal_x, float normal_y, float normal_z, | |
56 SchedTask *smanager,int x, int y, float z, int world_x, int world_y, float world_z); | |
57 | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
58 |
762 | 59 void |
60 normalize(float *v0, float *v1) | |
61 { | |
62 float norm, dnorm; | |
63 | |
64 norm = sqrt(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]); | |
65 if (norm > 0) { | |
66 dnorm = 1.0/norm; | |
67 v0[0] = v1[0]*dnorm; | |
68 v0[1] = v1[1]*dnorm; | |
69 v0[2] = v1[2]*dnorm; | |
70 v0[3] = v1[3]*dnorm; | |
71 } | |
72 } | |
73 | |
621 | 74 static float |
75 innerProduct1(float *v0, float *v1) | |
76 { | |
77 return (v0[0]*v1[0] + v0[1]*v1[1] + v0[2]*v1[2]); | |
78 } | |
79 | |
507 | 80 |
81 /** | |
82 * テクスチャは、TEXTURE_SPLIT_PIXEL^2 のブロックに分割する | |
83 * | |
84 * +---+---+---+---+---+---+ | |
85 * | 0 | 1 | 2 | 3 | 4 | 5 | | |
86 * +---+---+---+---+---+---+ | |
87 * | | | | | |11 | | |
88 * +---+---+---+---+---+---+ | |
89 * | | | | | |17 | | |
90 * +---+---+---+---+---+---+ | |
91 * | | | | | |23 | | |
92 * +---+---+---+---+---+---+ | |
93 * | | | | | |29 | | |
94 * +---+---+---+---+---+---+ | |
95 * | | | | | |35 | | |
96 * +---+---+---+---+---+---+ | |
97 * | |
98 * 一辺を TEXTURE_SPLIT とする | |
99 * 各ブロックの数字がブロックIDとなる。 | |
100 */ | |
101 | |
102 /** | |
103 * テクスチャの座標から、 | |
104 * テクスチャのどのブロックかを求める | |
105 * | |
106 * @param[in] tx X coordinates of texture | |
107 * @param[in] tx Y coordinates of texture | |
108 * @param[in] twidth Width of texture | |
109 * @return block ID | |
110 */ | |
111 static int | |
112 getTexBlock(int tx, int ty, int twidth) | |
113 { | |
114 int blockX, blockY; | |
115 | |
116 blockX = tx / TEXTURE_SPLIT_PIXEL; | |
117 blockY = ty / TEXTURE_SPLIT_PIXEL; | |
118 | |
119 return blockX + (twidth/TEXTURE_SPLIT_PIXEL)*blockY; | |
120 } | |
121 | |
122 /** | |
123 * block ID と、テクスチャの TOP address から | |
124 * (tx,ty) で使われるテクスチャの Tile addres を求める | |
125 * | |
126 * @param[in] tx X coordinates of texture | |
127 * @param[in] tx Y coordinates of texture | |
128 * @param[in] tw Width of texture | |
129 * @param[in] tex_addr_top (tx,ty) で使うテクスチャの先頭address | |
130 * @return block ID | |
131 */ | |
132 static memaddr | |
133 getTile(int tx, int ty, int tw, memaddr tex_addr_top) | |
134 { | |
135 int block = getTexBlock(tx, ty, tw); | |
594 | 136 return tex_addr_top + block * TEXTURE_BLOCK_SIZE * sizeof(uint32); |
507 | 137 } |
138 | |
139 /** | |
140 * FrameBuffer に書き込む rgb の領域初期化 | |
141 * | |
142 * @param width Width of Buffer | |
143 * @param height Height of Buffer | |
144 * @param rgb Initial value of RGB at Buffer | |
145 * @return Buffer | |
146 */ | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
147 static int ** |
507 | 148 linebuf_init(SchedTask *smanager, int width, int height, int rgb) |
149 { | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
150 int **linebuf = (int**)smanager->allocate(height*sizeof(int*)); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
151 for(int h = 0;h<height;h++) { |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
152 int *buf = linebuf[h] = (int*)smanager->get_output(h); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
153 for (int i = 0; i < width; i++) { |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
154 buf[i] = rgb; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
155 } |
507 | 156 } |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
157 return linebuf; |
507 | 158 } |
159 | |
160 /** | |
161 * Z-Buffer の初期化 | |
162 * | |
163 * @param width Width of Z-Buffer | |
164 * @param height Height of Z-Buffer | |
165 * @return Z-Buffer | |
166 */ | |
167 static float* | |
168 zRow_init(SchedTask *smanager, int width, int height) | |
169 { | |
170 float *buf = (float*)smanager->allocate(sizeof(float)*width*height); | |
171 float def = 65535.0f; | |
172 | |
594 | 173 for (int i = 0; i < width*height; i++) { |
174 buf[i] = def; | |
507 | 175 } |
176 | |
177 return buf; | |
178 } | |
179 | |
180 | |
181 static uint32 | |
182 get_rgb(int tx, int ty, TilePtr tile) | |
183 { | |
184 uint32 *data = (uint32 *)tile->data; | |
185 return data[(TEXTURE_SPLIT_PIXEL)*ty+tx]; | |
186 } | |
187 | |
188 | |
189 | |
190 /** | |
191 * zRow と Linebuf を更新する | |
192 * | |
193 * @param zpos 更新する pixel のZ座標 | |
194 * @param rangex このタスクが処理する描画領域の x の長さ | |
195 * @param x pixel の、描画領域内での x 座標 | |
196 * @param y 〃 の、y 座標 | |
197 * @param tex_x pixel が使用するテクスチャの、Tile (8x8) 内での x 座標 | |
198 * @param tex_y 〃 の y 座標 | |
199 * @param tex_addr テクスチャのアドレス(MainMemory) | |
200 */ | |
201 static void | |
748 | 202 updateBuffer(Gptr g, float zpos, int rangex, int localx, int localy, int tex_x, int tex_y, |
203 float normal_x, float normal_y, float normal_z, TilePtr tile, | |
204 int world_x, int world_y, float world_z, SchedTask *smanager) | |
507 | 205 { |
594 | 206 |
207 int color = get_rgb(tex_x, tex_y, tile); | |
208 /*下位4bitを抽出*/ | |
623 | 209 #if LITTLEENDIAN |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
210 int alpha = color & 0x000000ff; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
211 #else |
599 | 212 int alpha = color & 0xff000000; |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
213 #endif |
594 | 214 /*完全に透けているか判断*/ |
215 int flag = (alpha != 0); | |
216 | |
748 | 217 color = infinity_light_calc(color,normal_x,normal_y,normal_z, |
218 smanager,localx,localy,zpos, | |
219 world_x,world_y,world_z); | |
594 | 220 |
748 | 221 g->zRow[localx + (rangex*localy)] = zpos*flag + g->zRow[localx + (rangex*localy)]*(1-flag); |
222 int *point = &g->linebuf[localy][localx] ; | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
223 *point = color*flag + *point *(1-flag); |
594 | 224 |
748 | 225 |
226 | |
507 | 227 } |
228 | |
229 /** | |
230 * 長さが 1 の Span の描画 (要するに 1 pixel) | |
231 * | |
232 * @param span Span | |
233 * @param startx 描画開始範囲 | |
234 * @param endx 描画終了範囲 | |
235 */ | |
236 static int | |
237 drawDot1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
238 { | |
239 int rangex = endx - startx + 1; | |
240 | |
594 | 241 float normal_x = span->normal_x; |
242 float normal_y = span->normal_y; | |
243 float normal_z = span->normal_z; | |
244 | |
245 | |
507 | 246 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ |
247 int tex_xpos, tex_ypos; | |
248 | |
249 // span の始点に対応する Texture の座標 (tex1, tey1) | |
250 float tex = span->tex_x1; | |
251 float tey = span->tex_y1; | |
252 | |
253 // span の始点に対応する z 座標 | |
254 float zpos = span->start_z; | |
255 | |
256 /* Tile 内での座標 */ | |
257 int localx = getLocalX(span->x-1); | |
258 int localy = getLocalY(span->y-1); | |
259 | |
260 /** | |
261 * (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
262 * そのブロックのアドレス(MainMemory) | |
263 */ | |
264 int tex_localx; | |
265 int tex_localy; | |
266 memaddr tex_addr; | |
267 | |
268 if (span->x < startx || endx < span->x) { | |
269 return -1; | |
270 } | |
271 | |
272 tex_xpos = (int)((span->tex_width-1) * tex); | |
273 tex_ypos = (int)((span->tex_height-1) * tey); | |
274 | |
275 if (zpos < g->zRow[localx + (rangex*localy)]) { | |
276 tex_addr = getTile(tex_xpos, tex_ypos, | |
277 span->tex_width, (memaddr)span->tex_addr); | |
278 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
279 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
280 | |
594 | 281 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); |
282 smanager->wait_segment(tile); | |
507 | 283 |
284 updateBuffer(g, zpos, rangex, localx, localy, | |
594 | 285 tex_localx, tex_localy, |
748 | 286 normal_x,normal_y,normal_z,tile, |
287 span->x, span->y, zpos, smanager); | |
507 | 288 } |
289 | |
290 return -1; | |
291 } | |
292 | |
594 | 293 #if 0 |
294 static void | |
295 drawDot2(SchedTask *smanager, SpanPtr span, int startx, int end, int js, int wait_tag) | |
296 { | |
297 //printf("%d\n", js); | |
298 } | |
299 #endif | |
507 | 300 |
301 /** | |
302 * 長さが 1 より大きい Span の描画 | |
303 * | |
304 * 本来の目的として、この関数(drawLine1) では | |
305 * : 既に SPE 上に Tile のある pixel だけ描画 | |
306 * : それ以外は、ここで予め DMA load しておき、 | |
307 * : drawLine2 で一気に描画する | |
308 * ってものだったんだけど、どうも上手く行かなかったので | |
309 * 今は drawLine1 で load -> wait -> rendering を全部やってます | |
310 * (rendering といっても、rendering buffer に書き込むだけで | |
311 * まだ main memory (frame buffer) に dma store してるわけではない) | |
312 * | |
313 * @param span Span | |
314 * @param startx 描画開始範囲 | |
315 * @param endx 描画終了範囲 | |
316 * @return 「span のどの位置まで rendering が終わったか」の x 座標 | |
317 */ | |
318 static int | |
319 drawLine1(SchedTask *smanager, Gptr g, SpanPtr span, int startx, int endx, int wait_tag) | |
320 { | |
321 int x = span->x; | |
322 int rangex = endx - startx + 1; | |
323 int x_len = span->length_x; | |
324 | |
594 | 325 float normal_x = span->normal_x; |
326 float normal_y = span->normal_y; | |
327 float normal_z = span->normal_z; | |
328 | |
329 | |
507 | 330 int js = (x < startx) ? startx - x : 0; |
331 int je = (x + x_len > endx) ? endx - x : x_len; | |
332 | |
333 /* span->x に対応する Texture の座標 (tex_xpos, tex_ypos) */ | |
334 int tex_xpos, tex_ypos; | |
335 | |
336 // span の始点に対応する座標 (tex1, tey1) | |
337 float tex1 = span->tex_x1; | |
338 float tey1 = span->tex_y1; | |
339 | |
340 // span の終点に対応する座標 (tex2, tey2) | |
341 float tex2 = span->tex_x2; | |
342 float tey2 = span->tex_y2; | |
343 | |
344 // span の始点、終点に対応する z 座標 | |
345 float zpos1 = span->start_z; | |
346 float zpos2 = span->end_z; | |
347 | |
748 | 348 //spanを右から左に見ていくうちに、zが下がるのか、上がっていくのか。 |
762 | 349 float z_inclination = (zpos2 - zpos1) / x_len; |
748 | 350 float world_z = zpos2; |
351 | |
507 | 352 // Tile 内での座標 |
353 int localx, localy = getLocalY(span->y-1); | |
354 | |
355 int ret = je+1; | |
356 | |
357 //for (int j = js; j <= je; j++) { | |
358 for (int j = je; j >= js; j--) { | |
359 float tex_x, tex_y, tex_z; | |
360 | |
762 | 361 world_z -= z_inclination; |
748 | 362 |
507 | 363 localx = getLocalX(x-1+j); |
364 | |
365 tex_z = zpos1*(x_len-1-j)/(x_len-1) + zpos2*j/(x_len-1); | |
366 | |
367 tex_x = tex1*(x_len-1-j)/(x_len-1) + tex2*j/(x_len-1); | |
368 tex_y = tey1*(x_len-1-j)/(x_len-1) + tey2*j/(x_len-1); | |
369 if (tex_x > 1) tex_x = 1; | |
370 if (tex_x < 0) tex_x = 0; | |
371 if (tex_y > 1) tex_y = 1; | |
372 if (tex_y < 0) tex_y = 0; | |
373 tex_xpos = (int)((span->tex_width-1) * tex_x); | |
374 tex_ypos = (int)((span->tex_height-1) * tex_y); | |
375 | |
376 if (tex_z < g->zRow[localx + (rangex*localy)]) { | |
377 // (tex_xpos, tex_ypos) の、Tile 内(上の図参照)での座標と | |
378 // そのブロックのアドレス(MainMemory) | |
379 memaddr tex_addr; | |
380 int tex_localx; | |
381 int tex_localy; | |
382 | |
383 tex_addr = getTile(tex_xpos, tex_ypos, | |
384 span->tex_width, (memaddr)span->tex_addr); | |
385 tex_localx = tex_xpos % TEXTURE_SPLIT_PIXEL; | |
386 tex_localy = tex_ypos % TEXTURE_SPLIT_PIXEL; | |
387 TilePtr tile = smanager->get_segment(tex_addr,g->tileList); | |
388 smanager->wait_segment(tile); | |
389 | |
390 updateBuffer(g, tex_z, rangex, localx, localy, | |
594 | 391 tex_localx, tex_localy, |
748 | 392 normal_x, normal_y, normal_z, tile, |
393 span->x+j, span->y, world_z, smanager); | |
507 | 394 } |
395 } | |
396 | |
397 return ret; | |
398 } | |
399 | |
594 | 400 static int |
748 | 401 infinity_light_calc(int color,float normal_x, float normal_y, |
402 float normal_z, SchedTask *smanager, int x, int y, float z, | |
403 int world_x, int world_y, float world_z) | |
404 | |
507 | 405 { |
594 | 406 unsigned char rgb[4]; |
407 int light_rgb; | |
408 int flag; | |
409 float normal_vector[4] = {normal_x,normal_y,normal_z,0}; | |
410 // 光のベクトル,きめうちしちゃった。どうにかする | |
762 | 411 //float light_vector[4] = {0,0,-1,0}; |
412 float light_vector[4]; | |
594 | 413 float inner_product; |
762 | 414 float *light_xyz = (float*)smanager->global_get(Light); |
415 | |
416 normalize(normal_vector, normal_vector); | |
594 | 417 |
418 // 引数で受け取った color の rgb 情報の抜き出し | |
623 | 419 #if LITTLEENDIAN |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
420 rgb[0] = (color & 0xff000000) >> 24; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
421 rgb[1] = (color & 0x00ff0000) >> 16; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
422 rgb[2] = (color & 0x0000ff00) >> 8; |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
423 rgb[3] = (color & 0x000000ff); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
424 #else |
594 | 425 rgb[3] = (color & 0xff000000) >> 24; |
426 rgb[2] = (color & 0x00ff0000) >> 16; | |
427 rgb[1] = (color & 0x0000ff00) >> 8; | |
428 rgb[0] = (color & 0x000000ff); | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
429 #endif |
594 | 430 |
762 | 431 int tmp_rgb[3] = {0,0,0}; |
432 int light_num = 4; | |
433 for (int i = 0; i < light_num; i++) { | |
434 | |
435 //printf("light_xyz[%d] %f\n",i*4,light_xyz[i*4]); | |
436 //printf("light_xyz[%d] %f\n",i*4+1,light_xyz[i*4+1]); | |
437 //printf("light_xyz[%d] %f\n",i*4+2,light_xyz[i*4+2]); | |
438 //printf("light_xyz[%d] %f\n",i*4+3fg,light_xyz[i*4+3]); | |
439 | |
440 light_vector[0] = world_x - light_xyz[i*4]; | |
441 light_vector[1] = world_y - light_xyz[i*4+1]; | |
442 light_vector[2] = light_xyz[i*4+2] - world_z; | |
443 light_vector[3] = light_xyz[i*4+3]; | |
444 | |
445 normalize(light_vector, light_vector); | |
446 | |
447 // 法線ベクトルと光源ベクトルとの内積をとる | |
448 inner_product = innerProduct1(normal_vector,light_vector); | |
594 | 449 |
762 | 450 //printf("inner_product %f\n",inner_product); |
451 | |
452 // 内積がマイナスの場合は色がない。 | |
453 flag = (inner_product > 0); | |
454 | |
455 // 内積を rgb にかけていく | |
456 tmp_rgb[0] += (unsigned char)(rgb[0]*inner_product*flag); | |
457 tmp_rgb[1] += (unsigned char)(rgb[1]*inner_product*flag); | |
458 tmp_rgb[2] += (unsigned char)(rgb[2]*inner_product*flag); | |
459 | |
460 } | |
461 | |
462 int rgb_flag[3]; | |
463 for (int i = 0; i < 3; i++) { | |
464 rgb_flag[i] = (tmp_rgb[i] > 255); | |
465 } | |
466 | |
467 rgb[0] = tmp_rgb[0]*(1 - rgb_flag[0]) + 255*(rgb_flag[0]); | |
468 rgb[1] = tmp_rgb[1]*(1 - rgb_flag[1]) + 255*(rgb_flag[1]); | |
469 rgb[2] = tmp_rgb[2]*(1 - rgb_flag[2]) + 255*(rgb_flag[2]); | |
470 | |
594 | 471 |
472 //計算した rgb を light_rgb にまとめる。 | |
623 | 473 #if LITTLEENDIAN |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
474 light_rgb = (rgb[0] << 24) + (rgb[1] << 16) + (rgb[2] << 8) + (rgb[3]); |
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
475 #else |
594 | 476 light_rgb = (rgb[3] << 24) + (rgb[2] << 16) + (rgb[1] << 8) + (rgb[0]); |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
477 #endif |
594 | 478 |
479 return light_rgb; | |
507 | 480 } |
594 | 481 |
507 | 482 |
483 static int | |
484 run(SchedTask *smanager, void *rbuf, void *wbuf) | |
485 { | |
623 | 486 G g0; |
487 Gptr g = &g0; | |
507 | 488 |
489 SpanPackPtr spack = (SpanPackPtr)smanager->get_input(0); | |
490 SpanPackPtr next_spack = (SpanPackPtr)smanager->allocate(sizeof(SpanPack)); | |
491 SpanPackPtr free_spack = next_spack; // next_spack の free() 用 | |
492 Span *span; | |
493 | |
494 Span nop_span; | |
495 nop_span.length_x = 1; | |
496 | |
626
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
497 // uint32 display = (long)smanager->get_param(0); |
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
498 // int screen_width = (long)smanager->get_param(1); |
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
499 int rangex_start = (long)smanager->get_param(2); |
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
500 int rangex_end = (long)smanager->get_param(3); |
507 | 501 |
502 // このタスクが担当する x の範囲 | |
503 int rangex = rangex_end - rangex_start + 1; | |
504 | |
505 // y の範囲 | |
626
ab866bc8a624
64bit mode compatibility on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
623
diff
changeset
|
506 int rangey = (long)smanager->get_param(4); |
507 | 507 g->tileList = (TileListPtr)smanager->global_get(GLOBAL_TILE_LIST); |
508 | |
509 g->zRow = zRow_init(smanager, rangex, rangey); | |
510 g->linebuf = linebuf_init(smanager, rangex, rangey, 0); | |
511 | |
512 int tl_tag[2] = {TEX_LOAD1, TEX_LOAD2}; | |
513 int tl_tag_flg1 = 0; | |
514 int tl_tag_flg2 = 1; | |
515 | |
516 do { | |
517 /** | |
518 * SpanPack->next が存在する場合、 | |
519 * 現在の SpanPack を処理してる間に | |
520 * 次の SpanPack の DMA 転送を行う | |
521 */ | |
522 if (spack->next != NULL) { | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
523 smanager->dma_load(next_spack, (memaddr)spack->next, |
507 | 524 sizeof(SpanPack), SPAN_PACK_LOAD); |
525 } else { | |
526 next_spack = NULL; | |
527 } | |
528 | |
529 SpanPtr resume_span = &nop_span; | |
530 | |
531 for (int t = 0; t < spack->info.size; t++) { | |
532 SpanPtr next_span; | |
533 | |
534 span = &spack->span[t]; | |
535 | |
622
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
536 if (span->length_x != 1) { |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
537 drawLine1( |
594 | 538 smanager, g, |
539 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); | |
622
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
540 } else { |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
541 drawDot1( |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
542 smanager, g, |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
543 span, rangex_start, rangex_end, tl_tag[tl_tag_flg1]); |
6a5e88112ed9
remove non relocatable funcion call.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
544 } |
507 | 545 next_span = span; |
546 | |
547 resume_span = next_span; | |
548 | |
549 tl_tag_flg1 ^= 1; | |
550 tl_tag_flg2 ^= 1; | |
551 } | |
552 | |
553 smanager->dma_wait(SPAN_PACK_LOAD); | |
554 | |
555 SpanPackPtr tmp_spack = spack; | |
556 spack = next_spack; | |
557 next_spack = tmp_spack; | |
558 } while (spack); | |
559 | |
560 | |
561 free(g->zRow); | |
618
360032cc500e
worked. Cell fix. (not yet tested)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
614
diff
changeset
|
562 free(g->linebuf); |
507 | 563 |
564 //FINISH: | |
565 /** | |
566 * goto FINISH; の時は reboot なので | |
567 * linebuf, zRow は free() しない | |
568 */ | |
569 | |
570 free(free_spack); | |
571 | |
572 | |
573 return 0; | |
574 } | |
594 | 575 |
576 /* end */ |