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