109
|
1 #include "SDL.h"
|
|
2 #include "polygon.h"
|
|
3 #include "viewer.h"
|
|
4 #include "SpanPack.h"
|
|
5
|
|
6 #define YTOP(y) (y & ~(TEXTURE_SPLIT_PIXEL-1))
|
|
7 #define YOFF(y) (y & (TEXTURE_SPLIT_PIXEL-1))
|
|
8
|
|
9 #define FRAMEBUFFER_X Viewer::width
|
|
10 #define FRAMEBUFFER_Y Viewer::height
|
|
11 #define IMG_MAX_X Viewer::width
|
|
12
|
|
13 int *linebuf;
|
|
14 float *zRow;
|
|
15
|
|
16 static char *
|
|
17 get_pixel(int tx, int ty, void *texture_image)
|
|
18 {
|
|
19 return (char*)texture_image+(3*((128)*ty+tx));
|
|
20 }
|
|
21
|
|
22 static void
|
|
23 linebuf_init(int rgb)
|
|
24 {
|
|
25 int y = TEXTURE_SPLIT_PIXEL;
|
|
26 int x = Viewer::width;
|
|
27
|
|
28 linebuf = (int*)malloc(sizeof(int)*x*y);
|
|
29 memset(linebuf, rgb, sizeof(int)*x*y);
|
|
30 }
|
|
31
|
|
32 static void
|
|
33 zRow_init(void)
|
|
34 {
|
|
35 int y = TEXTURE_SPLIT_PIXEL;
|
|
36 int x = Viewer::width;
|
|
37 float z = 65535.0f;
|
|
38 int length = x*y;
|
|
39
|
|
40 zRow = (float*)malloc(sizeof(float)*length);
|
|
41
|
|
42 for (int i = 0; i < length; i++) {
|
|
43 zRow[i] = z;
|
|
44 }
|
|
45 }
|
|
46
|
|
47 static Uint32
|
|
48 get_rgb(int tx, int ty, SDL_Surface *texture_image)
|
|
49 {
|
|
50 Uint8 red, green, blue, alpha;
|
|
51
|
|
52 if (tx<0) tx = 0;
|
|
53 if (texture_image->w-1< tx) tx = texture_image->w-1 ;
|
|
54 if (ty<0) ty = 0;
|
|
55 if (texture_image->h-1< ty) ty = texture_image->h-1 ;
|
|
56
|
|
57 char *p = get_pixel(tx,ty,texture_image->pixels);
|
|
58 blue = (Uint8) p[0];
|
|
59 green = (Uint8) p[1];
|
|
60 red = (Uint8) p[2];
|
|
61 alpha = 255;
|
|
62
|
|
63
|
|
64 return (red & 0xff) * 0x10000 + (green & 0xff) * 0x100
|
|
65 + (blue & 0xff) + (alpha << 24);
|
|
66 }
|
|
67
|
|
68 static void
|
|
69 write_buffer(unsigned int fbdev_addr, int y)
|
|
70 {
|
|
71 int end_y;
|
|
72 int start_y;
|
|
73
|
321
|
74 // 画面上の領域に描き込まないように
|
|
75 // ・・・なんか変だな。
|
|
76 // とりあえず if 文どうにかしようぜ俺
|
109
|
77 if (y < 0) {
|
|
78 if (y + TEXTURE_SPLIT_PIXEL < 0) {
|
|
79 return;
|
|
80 } else {
|
|
81 start_y = -y;
|
|
82 }
|
|
83 } else {
|
|
84 start_y = 0;
|
|
85 }
|
|
86
|
321
|
87 // 画面下の領域に書き込まないように
|
|
88 // ってことは start_y も必要か。
|
109
|
89 end_y = FRAMEBUFFER_Y - y + TEXTURE_SPLIT_PIXEL;
|
|
90 end_y = (end_y < 0) ? TEXTURE_SPLIT_PIXEL + end_y : TEXTURE_SPLIT_PIXEL;
|
|
91
|
321
|
92 // 本家 write_pixel では、 y を y = height -y にしている。
|
|
93 // こうすると、画面的に、 y++ すると 上に進むようになる。
|
|
94 // でも、framebuffer は上から下で y++ になるから、混乱しそう
|
109
|
95 for (int i = start_y; i < end_y; i++) {
|
|
96 memcpy((void*)(fbdev_addr + (4*FRAMEBUFFER_X*(y+i))),
|
|
97 &linebuf[i*IMG_MAX_X], sizeof(int)*IMG_MAX_X);
|
|
98 }
|
|
99 }
|
|
100
|
|
101
|
|
102 int
|
|
103 span_pack_draw(SpanPack *sp, unsigned int fbdev_addr)
|
|
104 {
|
|
105 SpanPack *spack = sp;
|
|
106 SpanPack *next_spack;
|
|
107 Span *span;
|
|
108
|
|
109 zRow = NULL;
|
|
110
|
|
111 int render_y = 0;
|
|
112 int linebuf_rgb;
|
|
113
|
|
114 render_y = sp->span[0].y;
|
|
115 render_y = YTOP(render_y);
|
|
116 //render_y += Viewer::height/2;
|
|
117
|
|
118 if (sp->info.size < 0) {
|
|
119 linebuf_rgb = 0xffffff;
|
|
120 linebuf_init(linebuf_rgb);
|
|
121 goto WRITE;
|
|
122 } else {
|
|
123 linebuf_rgb = 0x000000;
|
|
124 linebuf_init(linebuf_rgb);
|
|
125 }
|
|
126 zRow_init();
|
|
127
|
|
128 do {
|
321
|
129 // 次の pack があれば
|
|
130 // 現在の pack の処理をしている間に
|
|
131 // DMA でロードしておく。入るかな・・・
|
109
|
132 if (spack->next != NULL) {
|
321
|
133 // Cell ではこんな感じ?
|
109
|
134 // dma_load(next_spack, spack->next, sizeof(SpanPack));
|
|
135 next_spack = spack->next;
|
|
136 } else {
|
|
137 next_spack = NULL;
|
|
138 }
|
|
139
|
|
140 for (int n = 0; n < spack->info.size; n++) {
|
|
141 span = &spack->span[n];
|
|
142
|
|
143 //int x = span->x + Viewer::width/2;
|
|
144 //int y = span->y + Viewer::height/2;
|
|
145 int x = span->x;
|
|
146 int y = span->y;
|
|
147 float z = span->start_z;
|
|
148 int end = span->length_x;
|
|
149 float zpos = span->end_z;
|
|
150 float tex1 = span->tex_x1;
|
|
151 float tex2 = span->tex_x2;
|
|
152 float tey1 = span->tex_y1;
|
|
153 float tey2 = span->tex_y2;
|
|
154 Uint32 rgb;
|
|
155 int tex_xpos;
|
|
156 int tex_ypos;
|
|
157 int tex_zpos;
|
|
158 float tex_x, tex_y, tex_z;
|
|
159
|
|
160 if (end == 1) {
|
|
161 tex_xpos = (int)((span->tex_height-1) * tex1);
|
|
162 tex_ypos = (int)((span->tex_width-1) * tey1);
|
|
163 tex_zpos = (int)z;
|
|
164
|
|
165 if (zpos < zRow[x + (Viewer::width * YOFF(y))]) {
|
|
166 rgb = get_rgb(tex_xpos, tex_ypos, Polygon::texture_image);
|
|
167 zRow[x + (Viewer::width * YOFF(y))] = zpos;
|
|
168 linebuf[x + (Viewer::width * YOFF(y))] = rgb;
|
|
169 }
|
|
170 } else {
|
|
171 for (int j = 0; j < end; j++) {
|
|
172 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
173 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
174 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
175 if (tex_x > 1) tex_x = 1;
|
|
176 if (tex_y > 1) tex_y = 1;
|
|
177 tex_xpos = (int)((span->tex_height-1) * tex_x);
|
|
178 tex_ypos = (int)((span->tex_width-1) * tex_y);
|
|
179
|
|
180 if (tex_z < zRow[x + j + (Viewer::width * YOFF(y))]) {
|
|
181 rgb = get_rgb(tex_xpos, tex_ypos, Polygon::texture_image);
|
|
182 zRow[x + j + (Viewer::width * YOFF(y))] = tex_z;
|
|
183 linebuf[x + j + (Viewer::width * YOFF(y))] = rgb;
|
|
184 }
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188
|
321
|
189 // Cell ではこんな感じ?
|
109
|
190 // dma_wait(SPAN_PACK_LOAD);
|
|
191 spack = next_spack;
|
|
192 } while (spack);
|
|
193
|
|
194 WRITE:
|
|
195 write_buffer(fbdev_addr, render_y);
|
|
196
|
|
197 if (zRow != NULL) free(zRow);
|
|
198 free(linebuf);
|
|
199
|
|
200 return 0;
|
|
201 }
|