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