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
|
74
|
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 // run_arg_t �˽Τ����ɤ�����ä�
|
|
215 // global �˽Τ�ɤ���
|
|
216 int start_time;
|
|
217 int this_time;
|
|
218 int frames;
|
|
219 SDL_Surface *bitmap;
|
|
220 SDL_PixelFormat *pixelFormat;
|
|
221 Uint32 background;
|
|
222 Polygon *polygon;
|
81
|
223 //SceneGraphPack *sgp;
|
|
224 //PolygonPack *pp;
|
|
225 DmaBuffer *sgp_buff;
|
|
226 DmaBuffer *pp_buff;
|
|
227 DmaBuffer *spl_buff;
|
|
228 DmaBuffer *sp_buff;
|
|
229 DmaBuffer *ssl_buff;
|
|
230 DmaBuffer *splssl_buff;
|
|
231
|
54
|
232
|
42
|
233 void
|
|
234 Viewer::run_init()
|
|
235 {
|
|
236 HTaskPtr task;
|
|
237 int fd;
|
|
238
|
54
|
239 SceneGraphPack *sgp;
|
81
|
240 SPANPACKLIST *spl;
|
|
241 SPLSSL *splssl;
|
54
|
242
|
|
243 start_time = get_ticks();
|
|
244 this_time = 0;
|
|
245 frames = 0;
|
|
246
|
|
247 pixelFormat = screen->format;
|
|
248 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
249 polygon = new Polygon;
|
|
250 polygon->set_data("cube.xml");
|
|
251 polygon->viewer = this;
|
|
252
|
81
|
253 #if 1
|
54
|
254 // �̿��� buffer �� allocate
|
|
255 // ���줸��ʤ����̿��Ѥ˻Ȥ��ʤ�(�褦�˽�����ʤ��ȡ�)
|
|
256 sgp_buff = manager->allocate(sizeof(SceneGraphPack));
|
|
257
|
81
|
258 pp_buff = manager->allocate(sizeof(PolygonPack));
|
|
259
|
|
260 sp_buff = manager->allocate(sizeof(SPANPACK));
|
|
261 spl_buff = manager->allocate(sizeof(SPANPACKLIST));
|
|
262
|
|
263 spl_buff->get_buffer(&spl);
|
|
264 spl->size = 60;
|
|
265 spl_buff->swap_buffer();
|
|
266 spl_buff->get_buffer(&spl);
|
|
267 spl->size = 60;
|
|
268 spl_buff->swap_buffer();
|
|
269
|
|
270 ssl_buff = manager->allocate(sizeof(SPUSPANLIST));
|
|
271 splssl_buff = manager->allocate(sizeof(SPLSSL));
|
|
272
|
54
|
273 sgp_buff->get_buffer(&sgp);
|
81
|
274 //spl_buff->get_buffer(&spl);
|
|
275
|
54
|
276 create_sgp(polygon, sgp);
|
|
277 sgp_buff->swap_buffer();
|
|
278
|
|
279 sgp_buff->get_buffer(&sgp);
|
|
280 create_sgp(polygon, sgp);
|
|
281 sgp_buff->swap_buffer();
|
|
282
|
81
|
283
|
|
284
|
|
285
|
79
|
286 #else
|
81
|
287 //sgp = new SceneGraphPack;
|
|
288 posix_memalign((void**)&sgp, DEFAULT_ALIGNMENT, sizeof(SceneGraphPack));
|
79
|
289 create_sgp(polygon, sgp);
|
|
290
|
81
|
291 //pp = new PolygonPack;
|
|
292 posix_memalign((void**)&pp, DEFAULT_ALIGNMENT, sizeof(PolygonPack));
|
79
|
293 #endif
|
42
|
294
|
|
295 pixels = new Uint32[width*height];
|
|
296
|
|
297 graph_line();
|
|
298
|
54
|
299 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
|
42
|
300 width*4, redMask, greenMask,
|
|
301 blueMask, alphaMask);
|
|
302
|
|
303 fd = manager->open("ViewerRunLoop");
|
48
|
304 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
305 task->spawn();
|
42
|
306 }
|
|
307
|
|
308 void
|
48
|
309 Viewer::run_loop(void)
|
42
|
310 {
|
|
311 HTaskPtr task_update_sgp = NULL;
|
81
|
312 HTaskPtr task_create_pp = NULL;
|
|
313 HTaskPtr task_create_sp = NULL;
|
|
314 HTaskPtr task_spu_sp = NULL;
|
|
315 HTaskPtr task_finish = NULL;
|
42
|
316 int fd_update_sgp;
|
|
317 int fd_create_pp;
|
81
|
318 int fd_create_sp;
|
|
319 int fd_spu_sp;
|
42
|
320 int fd_finish;
|
|
321
|
48
|
322 HTaskPtr task;
|
|
323 int fd;
|
|
324
|
76
|
325 bool quit_flg;
|
|
326
|
|
327 quit_flg = quit_check();
|
|
328
|
|
329 if (quit_flg == true) {
|
54
|
330 this_time = get_ticks();
|
42
|
331 fd_finish = manager->open("ViewerRunFinish");
|
48
|
332 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
|
63
|
333 task_finish->spawn();
|
42
|
334 return;
|
|
335 }
|
|
336
|
|
337 clean_pixels();
|
|
338
|
48
|
339 zRow_init();
|
42
|
340 graph_line();
|
|
341
|
|
342 fd_update_sgp = manager->open("UpdateSGP");
|
|
343 fd_create_pp = manager->open("CreatePP");
|
81
|
344 //fd_create_sp = manager->open("CreateSP");
|
|
345 //fd_spu_sp = manager->open("SpuSP");
|
48
|
346 fd = manager->open("ViewerRunDraw");
|
81
|
347
|
|
348 task_update_sgp
|
|
349 = manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
|
|
350 sgp_buff, sgp_buff, NULL);
|
|
351 task_create_pp
|
|
352 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
353 sgp_buff, pp_buff, NULL);
|
|
354 #if 0
|
|
355 task_create_sp
|
|
356 = manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
|
|
357 sgp_buff, sgp_buff, NULL);
|
|
358 task_spu_sp
|
|
359 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
360 sgp_buff, pp_buff, NULL);
|
|
361 #endif
|
48
|
362 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
363
|
63
|
364 task->set_depend(task_update_sgp);
|
|
365 task->set_depend(task_create_pp);
|
71
|
366
|
74
|
367 //task_update_sgp->set_cpu(CPU_SPE);
|
81
|
368 task_create_pp->set_cpu(CPU_SPE);
|
70
|
369
|
63
|
370 task_update_sgp->spawn();
|
|
371 task_create_pp->spawn();
|
|
372 task->spawn();
|
54
|
373
|
73
|
374 //sgp_buff->swap_buffer();
|
48
|
375 }
|
42
|
376
|
48
|
377 void
|
|
378 Viewer::run_draw(void)
|
|
379 {
|
|
380 HTaskPtr task;
|
|
381 int fd;
|
|
382
|
81
|
383 PolygonPack *pp;
|
54
|
384
|
81
|
385 pp_buff->get_buffer(&pp);
|
54
|
386
|
74
|
387 polygon->draw(pp); // test draw of PolygonPack
|
54
|
388 SDL_BlitSurface(bitmap, NULL, screen, NULL);
|
48
|
389 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
42
|
390
|
54
|
391 frames++;
|
48
|
392
|
|
393 fd = manager->open("ViewerRunLoop");
|
|
394 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
395 task->spawn();
|
42
|
396 }
|
|
397
|
|
398 void
|
48
|
399 Viewer::run_finish(void)
|
42
|
400 {
|
54
|
401 if (this_time != start_time) {
|
|
402 cout<< (((float)frames)/(this_time-start_time))*1000.0 << " FPS\n";
|
42
|
403 }
|
|
404
|
54
|
405 SDL_FreeSurface(bitmap);
|
42
|
406 delete [] pixels;
|
54
|
407 polygon->delete_data();
|
|
408 delete polygon;
|
76
|
409 //delete sgp_buff;
|
|
410 //delete pp_buff;
|
42
|
411 quit();
|
|
412 }
|
|
413
|
28
|
414 void
|
|
415 Viewer::zRow_init()
|
|
416 {
|
|
417 for (int i = 0; i < width; i++) {
|
|
418 for (int j = 0; j < height; j++) {
|
|
419 zRow[i][j] = 65535;
|
26
|
420 }
|
28
|
421 }
|
26
|
422 }
|