26
|
1 #include <iostream>
|
|
2 #include <SDL.h>
|
|
3 #include "polygon.h"
|
|
4 #include "viewer.h"
|
|
5 #include "sys.h"
|
81
|
6 #include "spu_span.h"
|
26
|
7 using namespace std;
|
|
8
|
28
|
9 #define redMask 0x00ff0000
|
|
10 #define greenMask 0x0000ff00
|
|
11 #define blueMask 0x000000ff
|
|
12 #define alphaMask 0
|
|
13
|
48
|
14 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
|
26
|
15 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
|
48
|
16 extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp);
|
26
|
17
|
|
18 Viewer::Viewer(int b, int w, int h)
|
|
19 {
|
28
|
20 bpp = b;
|
|
21 width = w;
|
|
22 height = h;
|
26
|
23 }
|
|
24
|
|
25
|
|
26 void Viewer::sdl_init()
|
|
27 {
|
67
|
28 if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
|
28
|
29 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
30 exit( 1 );
|
26
|
31 }
|
67
|
32
|
86
|
33 #ifndef _DEBUG
|
67
|
34 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE);
|
|
35 if (screen == NULL) {
|
|
36 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
|
|
37 SDL_Quit();
|
|
38 exit(1);
|
|
39 }
|
70
|
40 #else
|
71
|
41 void *_pixels = new Uint32[width*height*32/8];
|
70
|
42 //posix_memalign(&pixels, width*heigh/8, DEFAULT_ALIGNMENT);
|
|
43 screen = SDL_CreateRGBSurfaceFrom(_pixels, width, height, 32,
|
|
44 width*4, redMask, greenMask,
|
|
45 blueMask, alphaMask);
|
|
46 #endif
|
26
|
47 }
|
|
48
|
|
49
|
|
50 int Viewer::get_ticks()
|
|
51 {
|
28
|
52 int time;
|
|
53 time = SDL_GetTicks();
|
|
54 return time;
|
26
|
55 }
|
|
56
|
|
57 bool Viewer::quit_check()
|
|
58 {
|
28
|
59 bool quit = false;
|
|
60 SDL_Event event;
|
|
61 while(SDL_PollEvent(&event))
|
26
|
62 {
|
28
|
63 if(event.type==SDL_QUIT)
|
26
|
64 {
|
28
|
65 quit = true;
|
|
66 return quit;
|
26
|
67 }
|
|
68 }
|
28
|
69 return quit;
|
26
|
70 }
|
|
71
|
|
72 void Viewer::quit()
|
|
73 {
|
28
|
74 SDL_Quit();
|
26
|
75 }
|
|
76
|
|
77
|
|
78 void Viewer::swap_buffers()
|
|
79 {
|
28
|
80 SDL_GL_SwapBuffers();
|
26
|
81 }
|
|
82
|
|
83
|
51
|
84 void
|
|
85 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
|
|
86 {
|
28
|
87 SDL_PixelFormat *pf;
|
|
88 pf = screen->format;
|
|
89 x += width/2;
|
|
90 y += height/2;
|
26
|
91
|
28
|
92 static int diffz,diffz1;
|
51
|
93
|
28
|
94 diffz1 = diffz;
|
|
95 diffz = (zRow[x][y]>z);
|
51
|
96 if (z < zRow[x][y]) {
|
|
97 if (x < width && x > 0 && y > 0 && y < height) {
|
28
|
98 zRow[x][y] = z;
|
|
99 y = height - y;
|
|
100 pixels[width*y + x] = rgb;
|
|
101 }
|
26
|
102 }
|
|
103 }
|
|
104
|
51
|
105 void
|
|
106 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
|
26
|
107 {
|
51
|
108 if (x1 > x2) {
|
28
|
109 float x=0;
|
|
110 float y=0;
|
|
111 x=x1;
|
|
112 y=y1;
|
|
113 x1 = x2;
|
|
114 y1 = y2;
|
|
115 x2 = x;
|
|
116 y2 = y;
|
26
|
117 }
|
51
|
118
|
28
|
119 float s = y1;
|
26
|
120
|
51
|
121 if ((int)x1 == (int)x2) {
|
|
122 if (y1 > y2) {
|
28
|
123 float y=0;
|
|
124 y = y1;
|
|
125 y1 = y2;
|
|
126 y2 = y;
|
26
|
127 }
|
51
|
128
|
|
129 for (float i=y1; i<y2; i++) {
|
28
|
130 //write_pixel((int)x1,(int)i);
|
|
131 write_pixel((int)x1,(int)i,0,rgb);
|
26
|
132 }
|
51
|
133 } else {
|
28
|
134 float t = (y2 - y1)/(x2 - x1);
|
51
|
135 if (t < -1) {
|
28
|
136 float f = 0;
|
51
|
137 for (float i=x1; i<x2; i++) {
|
|
138 for (float a=(int)t; a<0; a++) {
|
28
|
139 write_pixel((int)i,(int)s,0,rgb);
|
|
140 s--;
|
26
|
141 }
|
51
|
142
|
28
|
143 f += t-(int)t;
|
51
|
144
|
|
145 if (f <= -1) {
|
28
|
146 write_pixel((int)i,(int)s,0,rgb);
|
|
147 f = 0;
|
|
148 s--;
|
26
|
149 }
|
|
150 }
|
51
|
151 } else if (t <= 1) {
|
|
152 for(float i=x1; i<x2; i++) {
|
28
|
153 //write_pixel((int)i,(int)s);
|
|
154 write_pixel((int)i,(int)s,0,rgb);
|
|
155 s += t;
|
26
|
156 }
|
51
|
157 } else {
|
28
|
158 float f = 0;
|
51
|
159 for (float i=x1; i<x2; i++) {
|
|
160 for (float a=0; a<(int)t; a++) {
|
28
|
161 write_pixel((int)i,(int)s,0,rgb);
|
|
162 s++;
|
26
|
163 }
|
51
|
164
|
28
|
165 f += t-(int)t;
|
51
|
166
|
|
167 if (f >= 1) {
|
28
|
168 write_pixel((int)i,(int)s,0,rgb);
|
|
169 f = 0;
|
|
170 s++;
|
26
|
171 }
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175 }
|
|
176
|
|
177 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
|
|
178 {
|
28
|
179 write_line(x1,y1,x2,y2,rgb);
|
|
180 write_line(x2,y2,x3,y3,rgb);
|
|
181 write_line(x3,y3,x1,y1,rgb);
|
26
|
182 }
|
|
183
|
|
184 void Viewer::clean_pixels()
|
|
185 {
|
28
|
186 for(int i=0; i<width*height; i++)
|
26
|
187 {
|
28
|
188 pixels[i] = 0x00;
|
26
|
189 }
|
|
190 }
|
|
191
|
|
192 void Viewer::graph_line()
|
|
193 {
|
28
|
194 int xl = width*height/2;
|
|
195 int yl = width/2;
|
|
196 for(int i=0; i<width; i++)
|
26
|
197 {
|
28
|
198 for(int t=0; t<height; t+=20)
|
26
|
199 {
|
28
|
200 pixels[width*t+i] = 0x5a;
|
26
|
201 }
|
28
|
202 pixels[xl +i] = 0xff;
|
26
|
203 }
|
28
|
204 for(int i=0; i<height; i++)
|
26
|
205 {
|
28
|
206 for(int t=0; t<width; t+=20)
|
26
|
207 {
|
28
|
208 pixels[i*width+t] = 0x5a;
|
26
|
209 }
|
28
|
210 pixels[i*width+yl] = 0xff;
|
26
|
211 }
|
|
212 }
|
|
213
|
54
|
214 int start_time;
|
|
215 int this_time;
|
|
216 int frames;
|
|
217 SDL_Surface *bitmap;
|
|
218 SDL_PixelFormat *pixelFormat;
|
|
219 Uint32 background;
|
|
220 Polygon *polygon;
|
81
|
221 //SceneGraphPack *sgp;
|
|
222 //PolygonPack *pp;
|
|
223 DmaBuffer *sgp_buff;
|
|
224 DmaBuffer *pp_buff;
|
|
225 DmaBuffer *ssl_buff;
|
|
226
|
54
|
227
|
42
|
228 void
|
|
229 Viewer::run_init()
|
|
230 {
|
|
231 HTaskPtr task;
|
|
232 int fd;
|
|
233
|
54
|
234 SceneGraphPack *sgp;
|
86
|
235 PolygonPack *pp;
|
|
236 SPUSPANLIST *ssl;
|
|
237
|
54
|
238
|
|
239 start_time = get_ticks();
|
|
240 this_time = 0;
|
|
241 frames = 0;
|
|
242
|
|
243 pixelFormat = screen->format;
|
|
244 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
245 polygon = new Polygon;
|
|
246 polygon->set_data("cube.xml");
|
|
247 polygon->viewer = this;
|
|
248
|
|
249 sgp_buff = manager->allocate(sizeof(SceneGraphPack));
|
85
|
250 pp_buff = manager->allocate(sizeof(PolygonPack));
|
81
|
251 ssl_buff = manager->allocate(sizeof(SPUSPANLIST));
|
|
252
|
86
|
253 // DmaBuffer ¤Î½é´ü²½¡¢¤«¤Ã¤³¤¤¤¤ÊýË¡¤Ê¤¤¤«
|
|
254 for (int i = 0; i < 2; i ++) {
|
|
255 sgp_buff->get_buffer(&sgp);
|
|
256 create_sgp(polygon, sgp);
|
|
257 sgp_buff->swap_buffer();
|
54
|
258
|
86
|
259 pp_buff->get_buffer(&pp);
|
|
260 ssl_buff->get_buffer(&ssl);
|
79
|
261
|
86
|
262 pp->ssl = ssl;
|
|
263 pp_buff->swap_buffer();
|
|
264 ssl_buff->swap_buffer();
|
|
265 }
|
42
|
266
|
|
267 pixels = new Uint32[width*height];
|
|
268
|
|
269 graph_line();
|
|
270
|
54
|
271 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
|
42
|
272 width*4, redMask, greenMask,
|
|
273 blueMask, alphaMask);
|
|
274
|
|
275 fd = manager->open("ViewerRunLoop");
|
48
|
276 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
277 task->spawn();
|
42
|
278 }
|
|
279
|
|
280 void
|
48
|
281 Viewer::run_loop(void)
|
42
|
282 {
|
|
283 HTaskPtr task_update_sgp = NULL;
|
81
|
284 HTaskPtr task_create_pp = NULL;
|
|
285 HTaskPtr task_create_sp = NULL;
|
|
286 HTaskPtr task_finish = NULL;
|
42
|
287 int fd_update_sgp;
|
|
288 int fd_create_pp;
|
81
|
289 int fd_create_sp;
|
42
|
290 int fd_finish;
|
|
291
|
48
|
292 HTaskPtr task;
|
|
293 int fd;
|
|
294
|
76
|
295 bool quit_flg;
|
|
296
|
|
297 quit_flg = quit_check();
|
|
298
|
|
299 if (quit_flg == true) {
|
54
|
300 this_time = get_ticks();
|
42
|
301 fd_finish = manager->open("ViewerRunFinish");
|
48
|
302 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
|
63
|
303 task_finish->spawn();
|
42
|
304 return;
|
|
305 }
|
|
306
|
|
307 clean_pixels();
|
|
308
|
48
|
309 zRow_init();
|
42
|
310 graph_line();
|
|
311
|
|
312 fd_update_sgp = manager->open("UpdateSGP");
|
|
313 fd_create_pp = manager->open("CreatePP");
|
86
|
314 fd_create_sp = manager->open("CreateSP");
|
48
|
315 fd = manager->open("ViewerRunDraw");
|
81
|
316
|
|
317 task_update_sgp
|
|
318 = manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
|
|
319 sgp_buff, sgp_buff, NULL);
|
|
320 task_create_pp
|
|
321 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
322 sgp_buff, pp_buff, NULL);
|
|
323 task_create_sp
|
85
|
324 = manager->create_task(fd_create_sp, sizeof(SceneGraphPack),
|
86
|
325 pp_buff, ssl_buff, NULL);
|
|
326 #if 0
|
85
|
327 task_draw_finish = manager->create();
|
|
328 for (int i = 0; i < draw_spe_num; i++) {
|
|
329 task_spu_draw[i]
|
|
330 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
331 spuspan_buff, pp_buff, NULL);
|
|
332 task_spu_draw[i]->set_depend(task_spu_span);
|
|
333 task_draw_finish->set_depend(tas_spu_draw[i]);
|
|
334 }
|
81
|
335 #endif
|
48
|
336 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
337
|
63
|
338 task->set_depend(task_update_sgp);
|
|
339 task->set_depend(task_create_pp);
|
86
|
340 //task->set_depend(task_create_sp);
|
|
341 //task_create_sp->set_depend(task_create_pp);
|
85
|
342
|
74
|
343 //task_update_sgp->set_cpu(CPU_SPE);
|
81
|
344 task_create_pp->set_cpu(CPU_SPE);
|
70
|
345
|
63
|
346 task_update_sgp->spawn();
|
|
347 task_create_pp->spawn();
|
86
|
348 //task_create_sp->spawn();
|
63
|
349 task->spawn();
|
54
|
350
|
73
|
351 //sgp_buff->swap_buffer();
|
48
|
352 }
|
42
|
353
|
48
|
354 void
|
|
355 Viewer::run_draw(void)
|
|
356 {
|
|
357 HTaskPtr task;
|
|
358 int fd;
|
|
359
|
81
|
360 PolygonPack *pp;
|
54
|
361
|
81
|
362 pp_buff->get_buffer(&pp);
|
54
|
363
|
74
|
364 polygon->draw(pp); // test draw of PolygonPack
|
54
|
365 SDL_BlitSurface(bitmap, NULL, screen, NULL);
|
48
|
366 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
42
|
367
|
54
|
368 frames++;
|
48
|
369
|
|
370 fd = manager->open("ViewerRunLoop");
|
|
371 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
372 task->spawn();
|
42
|
373 }
|
|
374
|
85
|
375 #if 0
|
|
376 void
|
|
377 Viewer::draw_finish(void)
|
|
378 {
|
|
379 //
|
|
380
|
|
381 HTaskPtr task = manager->create(task_r);
|
|
382
|
|
383 }
|
|
384 #endif
|
|
385
|
42
|
386 void
|
48
|
387 Viewer::run_finish(void)
|
42
|
388 {
|
54
|
389 if (this_time != start_time) {
|
|
390 cout<< (((float)frames)/(this_time-start_time))*1000.0 << " FPS\n";
|
42
|
391 }
|
|
392
|
54
|
393 SDL_FreeSurface(bitmap);
|
42
|
394 delete [] pixels;
|
54
|
395 polygon->delete_data();
|
|
396 delete polygon;
|
76
|
397 //delete sgp_buff;
|
|
398 //delete pp_buff;
|
42
|
399 quit();
|
|
400 }
|
|
401
|
28
|
402 void
|
|
403 Viewer::zRow_init()
|
|
404 {
|
|
405 for (int i = 0; i < width; i++) {
|
|
406 for (int j = 0; j < height; j++) {
|
|
407 zRow[i][j] = 65535;
|
26
|
408 }
|
28
|
409 }
|
26
|
410 }
|