109
|
1 #include <stdlib.h>
|
|
2 #include <string.h>
|
|
3 #include "DrawSpan.h"
|
|
4 #include "polygon_pack.h"
|
|
5 #include "SpanPack.h"
|
|
6 #include "texture.h"
|
|
7 #include "viewer_types.h"
|
|
8
|
|
9 #define SPAN_PACK_LOAD 0
|
|
10
|
|
11 SchedDefineTask(DrawSpan);
|
|
12
|
|
13 void
|
|
14 DrawSpan::linebuf_init(int *buf, int x, int rgb)
|
|
15 {
|
|
16 for (int i = 0; i < x; i++) {
|
|
17 buf[i] = rgb;
|
|
18 }
|
|
19 }
|
|
20
|
|
21 float*
|
|
22 DrawSpan::zRow_init(int w, int h)
|
|
23 {
|
|
24 float *zRow = NULL;
|
|
25 float z = 65535.0f;
|
|
26 int length = w*h;
|
|
27
|
|
28 zRow = (float*)smanager->allocate(sizeof(float)*length);
|
|
29
|
|
30 for (int i = 0; i < length; i++) {
|
|
31 zRow[i] = z;
|
|
32 }
|
|
33
|
|
34 return zRow;
|
|
35 }
|
|
36
|
|
37
|
|
38 char*
|
|
39 DrawSpan::get_pixel(int tx, int ty, void *texture_image)
|
|
40 {
|
|
41 return (char*)texture_image+(3*((128)*ty+tx));
|
|
42 }
|
|
43
|
|
44 Uint32
|
|
45 DrawSpan::get_rgb(int tx, int ty, void *texture)
|
|
46 {
|
|
47 Uint8 red, green, blue, alpha;
|
|
48
|
|
49 if (tx<0) tx = 0;
|
|
50 if (128-1< tx) tx = 128-1 ;
|
|
51 if (ty<0) ty = 0;
|
|
52 if (128-1< ty) ty = 128-1 ;
|
|
53
|
|
54 char *p = get_pixel(tx,ty,texture);
|
|
55
|
|
56 blue = (Uint8) p[0];
|
|
57 green = (Uint8) p[1];
|
|
58 red = (Uint8) p[2];
|
|
59 alpha = 255;
|
|
60
|
|
61 return (red & 0xff) * 0x10000 + (green & 0xff) * 0x100
|
|
62 + (blue & 0xff) + (alpha << 24);
|
|
63 }
|
|
64
|
|
65 int
|
|
66 DrawSpan::run(void *rbuf, void *wbuf)
|
|
67 {
|
|
68 SpanPack *sp = (SpanPack*)smanager->get_input(0);
|
|
69 SpanPack *next_sp =
|
|
70 (SpanPack*)smanager->allocate(sizeof(SpanPack));
|
|
71 SpanPack *free_sp = next_sp; // next_sp の free() 用
|
|
72 SpanPack *tmp_sp = NULL;
|
|
73 Span *span;
|
|
74
|
|
75 int render_y = sp->info.y_top;
|
|
76 void *texture_image = global_get(TEXTURE_ID);
|
|
77
|
|
78 int rangex_start = get_param(0); // このタスクが担当する x の範囲の始点
|
|
79 int rangex_end = get_param(1); // 終点 (start <= x <= end)
|
|
80 int rangey = get_param(2); // y の範囲 (render_y + rangey - 1)
|
|
81 int rangex = rangex_end - rangex_start + 1;
|
|
82
|
|
83 float *zRow = zRow_init(rangex, rangey);
|
|
84
|
|
85 int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);
|
|
86
|
|
87 for (int i = 0; i < rangey; i++) {
|
|
88 linebuf[i] = (int*)smanager->get_output(i);
|
|
89 linebuf_init(linebuf[i], rangex, 0x00ff00ff);
|
|
90 }
|
|
91
|
|
92 do {
|
|
93 /**
|
|
94 * SpanPack->next が存在する場合、
|
|
95 * 現在の SpanPack を処理してる間に
|
|
96 * 次の SpanPack の DMA 転送を行う
|
|
97 */
|
|
98 if (sp->next != NULL) {
|
|
99 smanager->dma_load(next_sp, (uint32)sp->next,
|
|
100 sizeof(SpanPack), SPAN_PACK_LOAD);
|
|
101 } else {
|
|
102 next_sp = NULL;
|
|
103 }
|
|
104
|
|
105 for (int t = 0; t < sp->info.size; t++) {
|
|
106 span = &sp->span[t];
|
|
107
|
|
108 int end = span->length_x;
|
|
109 Uint32 rgb = 0x00ff00;
|
|
110 float tex1 = span->tex_x1;
|
|
111 float tex2 = span->tex_x2;
|
|
112 float tey1 = span->tex_y1;
|
|
113 float tey2 = span->tex_y2;
|
|
114 int tex_xpos;
|
|
115 int tex_ypos;
|
|
116 int tex_zpos;
|
|
117 int x = span->x;
|
|
118 int y = span->y;
|
|
119 float z = span->start_z;
|
|
120 float zpos = span->end_z;
|
|
121
|
|
122 // 座標が [0 .. split_screen_w-1] に入るように x,y を -1
|
|
123 int localx = getLocalX(x-1);
|
|
124 int localy = getLocalY(y-1);
|
|
125
|
|
126 if (end == 1) {
|
|
127 if (x < rangex_start || rangex_end < x) {
|
|
128 continue;
|
|
129 }
|
|
130
|
|
131 tex_xpos = (int)((span->tex_height-1) * tex1);
|
|
132 tex_ypos = (int)((span->tex_width-1) * tey1);
|
|
133 tex_zpos = (int)z;
|
|
134
|
|
135 if (zpos < zRow[localx + (rangex * localy)]) {
|
|
136 rgb = get_rgb(tex_xpos, tex_ypos, texture_image);
|
|
137 zRow[localx + (rangex * localy)] = zpos;
|
|
138 linebuf[localy][localx] = rgb;
|
|
139 }
|
|
140 } else {
|
|
141 float tex_x, tex_y, tex_z;
|
|
142 int js = (x < rangex_start) ? rangex_start - x : 0;
|
|
143 int je = (x + end > rangex_end) ? rangex_end - x : end;
|
|
144
|
|
145 for (int j = js; j <= je; j++) {
|
|
146 localx = getLocalX(x-1+j);
|
|
147
|
|
148 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
149 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
150 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
151 if (tex_x > 1) tex_x = 1;
|
|
152 if (tex_y > 1) tex_y = 1;
|
|
153 tex_xpos = (int)((span->tex_height-1) * tex_x);
|
|
154 tex_ypos = (int)((span->tex_width-1) * tex_y);
|
|
155
|
|
156 if (tex_z < zRow[localx + (rangex*localy)]) {
|
|
157 rgb = get_rgb(tex_xpos, tex_ypos, texture_image);
|
|
158 zRow[localx + (rangex*localy)] = tex_z;
|
|
159 linebuf[localy][localx] = rgb;
|
|
160 }
|
|
161 }
|
|
162 }
|
|
163 }
|
|
164
|
|
165 smanager->dma_wait(SPAN_PACK_LOAD);
|
|
166
|
|
167 tmp_sp = sp;
|
|
168 sp = next_sp;
|
|
169 next_sp = tmp_sp;
|
|
170 } while (sp);
|
|
171
|
|
172
|
|
173 FINISH:
|
|
174 free(free_sp);
|
|
175 free(linebuf);
|
|
176 free(zRow);
|
|
177
|
|
178 return 0;
|
|
179 }
|