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