comparison TaskManager/Test/simple_pack/viewer.cpp @ 68:35a6cf176c38

*** empty log message ***
author chiaki
date Sun, 17 Feb 2008 21:45:07 +0900
parents 0c8ae614d421
children c9b973f0673e
comparison
equal deleted inserted replaced
67:7d2874fb0671 68:35a6cf176c38
2 #include <SDL.h> 2 #include <SDL.h>
3 #include <SDL_opengl.h> 3 #include <SDL_opengl.h>
4 #include <math.h> 4 #include <math.h>
5 #include <unistd.h> 5 #include <unistd.h>
6 #include "polygon.h" 6 #include "polygon.h"
7 //#include "demonstration.h"
8 //#include "scene.h"
9 #include "viewer.h" 7 #include "viewer.h"
10 #include "sys.h" 8 #include "sys.h"
11 using namespace std; 9 using namespace std;
12 10
13 #define redMask 0x00ff0000 11 #define redMask 0x00ff0000
14 #define greenMask 0x0000ff00 12 #define greenMask 0x0000ff00
15 #define blueMask 0x000000ff 13 #define blueMask 0x000000ff
16 #define alphaMask 0 14 #define alphaMask 0
17 15
18 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp); 16 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
19 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp); 17 //extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
20 extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp); 18 //extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp);
21 19
22 Viewer::Viewer(int b, int w, int h) 20 Viewer::Viewer(int b, int w, int h)
23 { 21 {
24 bpp = b; 22 bpp = b;
25 width = w; 23 width = w;
70 { 68 {
71 SDL_GL_SwapBuffers(); 69 SDL_GL_SwapBuffers();
72 } 70 }
73 71
74 72
75 void Viewer::write_pixel(int x, int y,float z, Uint32 rgb) { 73 void
74 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
75 {
76 SDL_PixelFormat *pf; 76 SDL_PixelFormat *pf;
77 pf = screen->format; 77 pf = screen->format;
78 //printf("x:%d y:%d z:%d\n",x,y,z);
79
80 //cout << "write_pixel" << endl;
81 //cout << x << " " << y << endl;
82
83 //cout << SDL_MapRGB(pf,0,0,150) << endl;
84
85 x += width/2; 78 x += width/2;
86 y += height/2; 79 y += height/2;
87 80
88 static int diffz,diffz1; 81 static int diffz,diffz1;
82
89 diffz1 = diffz; 83 diffz1 = diffz;
90 diffz = (zRow[x][y]>z); 84 diffz = (zRow[x][y]>z);
91 if(diffz != diffz1) { 85 if (z < zRow[x][y]) {
92 //printf("diffz :%d zRow[%d][%d] = %f z = %f\n",diffz,x,y,zRow[x][y],z); 86 if (x < width && x > 0 && y > 0 && y < height) {
93 }
94 //printf("x:%d,y:%d,z:%f,zRow:%f\n",x,y,z,zRow[x][y]);
95 if(z < zRow[x][y]) {
96 // printf("x:%d,y:%d,z:%d\n",x,y,z,zRow);
97 if(x < width && x > 0 && y > 0 && y < height)
98 {
99 //pixels[width*y + x] = SDL_MapRGB(pf,70,70,71);
100 zRow[x][y] = z; 87 zRow[x][y] = z;
101 y = height - y; 88 y = height - y;
102 pixels[width*y + x] = rgb; 89 pixels[width*y + x] = rgb;
103 } 90 }
104 } 91 }
105 } 92 }
106 93
107 void Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb) 94 void
108 { 95 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
109 //cout << "write_line("<< x1 << "," << y1 << "," << x2 << "," << y2 << ")"<< endl; 96 {
110 97 if (x1 > x2) {
111 //Uint32 rgb = 9830400;
112
113 if(x1 > x2)
114 {
115 float x=0; 98 float x=0;
116 float y=0; 99 float y=0;
117 x=x1; 100 x=x1;
118 y=y1; 101 y=y1;
119 x1 = x2; 102 x1 = x2;
120 y1 = y2; 103 y1 = y2;
121 x2 = x; 104 x2 = x;
122 y2 = y; 105 y2 = y;
123 } 106 }
107
124 float s = y1; 108 float s = y1;
125 109
126 if((int)x1 == (int)x2) 110 if ((int)x1 == (int)x2) {
127 { 111 if (y1 > y2) {
128 if(y1 > y2)
129 {
130 float y=0; 112 float y=0;
131 y = y1; 113 y = y1;
132 y1 = y2; 114 y1 = y2;
133 y2 = y; 115 y2 = y;
134 } 116 }
135 for(float i=y1; i<y2; i++) 117
136 { 118 for (float i=y1; i<y2; i++) {
137 //write_pixel((int)x1,(int)i); 119 //write_pixel((int)x1,(int)i);
138 write_pixel((int)x1,(int)i,0,rgb); 120 write_pixel((int)x1,(int)i,0,rgb);
139 } 121 }
140 } 122 } else {
141 else
142 {
143 float t = (y2 - y1)/(x2 - x1); 123 float t = (y2 - y1)/(x2 - x1);
144 if(t < -1) 124 if (t < -1) {
145 {
146 float f = 0; 125 float f = 0;
147 for(float i=x1; i<x2; i++) 126 for (float i=x1; i<x2; i++) {
148 { 127 for (float a=(int)t; a<0; a++) {
149 for(float a=(int)t; a<0; a++)
150 {
151 //write_pixel((int)i,(int)s);
152 write_pixel((int)i,(int)s,0,rgb); 128 write_pixel((int)i,(int)s,0,rgb);
153 s--; 129 s--;
154 } 130 }
131
155 f += t-(int)t; 132 f += t-(int)t;
156 if(f <= -1) 133
157 { 134 if (f <= -1) {
158 //write_pixel((int)i,(int)s);
159 write_pixel((int)i,(int)s,0,rgb); 135 write_pixel((int)i,(int)s,0,rgb);
160 f = 0; 136 f = 0;
161 s--; 137 s--;
162 } 138 }
163 } 139 }
164 } 140 } else if (t <= 1) {
165 else if(t <= 1) 141 for(float i=x1; i<x2; i++) {
166 {
167 for(float i=x1; i<x2; i++)
168 {
169 //write_pixel((int)i,(int)s); 142 //write_pixel((int)i,(int)s);
170 write_pixel((int)i,(int)s,0,rgb); 143 write_pixel((int)i,(int)s,0,rgb);
171 s += t; 144 s += t;
172 } 145 }
173 } 146 } else {
174 else
175 {
176 float f = 0; 147 float f = 0;
177 for(float i=x1; i<x2; i++) 148 for (float i=x1; i<x2; i++) {
178 { 149 for (float a=0; a<(int)t; a++) {
179 for(float a=0; a<(int)t; a++)
180 {
181 //write_pixel((int)i,(int)s);
182 write_pixel((int)i,(int)s,0,rgb); 150 write_pixel((int)i,(int)s,0,rgb);
183 s++; 151 s++;
184 } 152 }
153
185 f += t-(int)t; 154 f += t-(int)t;
186 if(f >= 1) 155
187 { 156 if (f >= 1) {
188 //write_pixel((int)i,(int)s);
189 write_pixel((int)i,(int)s,0,rgb); 157 write_pixel((int)i,(int)s,0,rgb);
190 f = 0; 158 f = 0;
191 s++; 159 s++;
192 } 160 }
193 } 161 }
232 } 200 }
233 } 201 }
234 202
235 203
236 struct run_arg_t { 204 struct run_arg_t {
237 int start_time; 205 int start_time;
238 int this_time; 206 int this_time;
239 int frames; 207 int frames;
240 SDL_Surface *bitmap; 208 SDL_Surface *bitmap;
241 SDL_PixelFormat *pf; 209 SDL_PixelFormat *pf;
242 Uint32 background; 210 Uint32 background;
243 Polygon *p; 211 Polygon *p;
244 SceneGraphPack *sgp; 212 SceneGraphPack *sgp;
245 PolygonPack *pp; 213 PolygonPack *pp;
214 SPANPACKLIST *spl;
215 SPANPACK *sp;
216 SPANPACK sps[135];
217 SPAN s1[250];
218 SPAN s2[250];
219 SPAN s3[250];
220 SPAN s4[250];
221 SPAN s5[250];
222 SPAN s6[250];
246 }; 223 };
247 224
248 struct run_arg_t *arg; 225 struct run_arg_t *arg;
249 226
250 void 227 void
252 { 229 {
253 arg = new run_arg_t; 230 arg = new run_arg_t;
254 HTaskPtr task; 231 HTaskPtr task;
255 int fd; 232 int fd;
256 233
257 fprintf(stderr, " ** %s **\n", __FUNCTION__);
258
259 arg->start_time = get_ticks(); 234 arg->start_time = get_ticks();
235 arg->this_time = 0;
236 arg->frames = 0;
237
260 arg->pf = screen->format; 238 arg->pf = screen->format;
261 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); 239 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
262 arg->p = new Polygon; 240 arg->p = new Polygon;
263 arg->p->set_data("cube.xml"); 241 arg->p->set_data("cube.xml");
264 arg->p->viewer = this; 242 arg->p->viewer = this;
265 arg->sgp = new SceneGraphPack; 243 arg->sgp = new SceneGraphPack;
266 create_sgp(arg->p, arg->sgp); 244 create_sgp(arg->p, arg->sgp);
267 arg->pp = new PolygonPack; 245 arg->pp = new PolygonPack;
246 arg->sp = new SPANPACK;
247 arg->spl = new SPANPACKLIST;
248 arg->spl->size = 6;
249 //cout << "run_init :spl->size = " << arg->spl->size << endl;
250 //arg->spl->dline = arg->sps;
251 /*
252 arg->spl->dline[0].span = arg->s1;
253 arg->spl->dline[1].span = arg->s2;
254 arg->spl->dline[2].span = arg->s3;
255 arg->spl->dline[3].span = arg->s4;
256 arg->spl->dline[4].span = arg->s5;
257 arg->spl->dline[5].span = arg->s6;
258 */
259 //cout << "run_init :spl->dline[0].span = " << arg->spl->dline[0].span << endl;
260
261 /*
262 for(int i=0; i<arg->spl->size; i++)
263 {
264 arg->spl->dline[i] = new SPANPACK;
265 }
266 */
268 267
269 pixels = new Uint32[width*height]; 268 pixels = new Uint32[width*height];
270 269
271 graph_line(); 270 graph_line();
272 271
273 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32, 272 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
274 width*4, redMask, greenMask, 273 width*4, redMask, greenMask,
275 blueMask, alphaMask); 274 blueMask, alphaMask);
277 fd = manager->open("ViewerRunLoop"); 276 fd = manager->open("ViewerRunLoop");
278 task = manager->create_task(fd, 0, 0, 0, NULL); 277 task = manager->create_task(fd, 0, 0, 0, NULL);
279 manager->spawn_task(task); 278 manager->spawn_task(task);
280 } 279 }
281 280
281 /*
282 void distribute_polygon_pack(int spu_num, PolygonPackList *ppl, PolygonPack *pp)
283 {
284 ppl->list[0].info.size = 0;
285 ppl->list[1].info.size = 0;
286 ppl->list[2].info.size = 0;
287 ppl->list[3].info.size = 0;
288 ppl->list[4].info.size = 0;
289 ppl->list[5].info.size = 0;
290
291 for(int i=0; i<pp->info.size; i++)
292 {
293 int num = i%spu_num;
294 int mysize = ppl->list[num].info.size;
295 ppl->list[num].tri[mysize] = pp->tri[i];
296 ppl->list[num].info.size++;
297 }
298 }
299 */
300
282 void 301 void
283 Viewer::run_loop(void) 302 Viewer::run_loop(void)
284 { 303 {
285 HTaskPtr task_update_sgp = NULL; 304 HTaskPtr task_update_sgp = NULL;
286 HTaskPtr task_create_pp = NULL; 305 HTaskPtr task_create_pp = NULL;
306 HTaskPtr task_create_sp = NULL;
287 HTaskPtr task_finish = NULL; 307 HTaskPtr task_finish = NULL;
288 int fd_update_sgp; 308 int fd_update_sgp;
289 int fd_create_pp; 309 int fd_create_pp;
310 int fd_create_sp;
290 int fd_finish; 311 int fd_finish;
291 312
292 HTaskPtr task; 313 HTaskPtr task;
293 int fd; 314 int fd;
294
295 fprintf(stderr, " ** %s **\n", __FUNCTION__);
296
297 315
298 if (quit_check()) { 316 if (quit_check()) {
299 arg->this_time = get_ticks(); 317 arg->this_time = get_ticks();
300 fd_finish = manager->open("ViewerRunFinish"); 318 fd_finish = manager->open("ViewerRunFinish");
301 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL); 319 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
308 zRow_init(); 326 zRow_init();
309 graph_line(); 327 graph_line();
310 328
311 fd_update_sgp = manager->open("UpdateSGP"); 329 fd_update_sgp = manager->open("UpdateSGP");
312 fd_create_pp = manager->open("CreatePP"); 330 fd_create_pp = manager->open("CreatePP");
331 fd_create_sp = manager->open("CreateSP");
313 fd = manager->open("ViewerRunDraw"); 332 fd = manager->open("ViewerRunDraw");
314 task_update_sgp = manager->create_task(fd_update_sgp, 333 task_update_sgp = manager->create_task(fd_update_sgp,
315 sizeof(SceneGraphPack), 334 sizeof(SceneGraphPack),
316 (unsigned int)arg->sgp, 335 (unsigned int)arg->sgp,
317 (unsigned int)arg->sgp, 336 (unsigned int)arg->sgp,
319 task_create_pp = manager->create_task(fd_create_pp, 338 task_create_pp = manager->create_task(fd_create_pp,
320 sizeof(SceneGraphPack), 339 sizeof(SceneGraphPack),
321 (unsigned int)arg->sgp, 340 (unsigned int)arg->sgp,
322 (unsigned int)arg->pp, 341 (unsigned int)arg->pp,
323 NULL); 342 NULL);
343 //cout << "run_loop :spl->size = " << arg->spl->size << endl;
344 //cout << "run_loop :spl->dline[0].span = " << arg->spl->dline[0].span << endl;
345 task_create_sp = manager->create_task(fd_create_sp,
346 sizeof(PolygonPack),
347 (unsigned int)arg->pp,
348 (unsigned int)arg->spl,
349 NULL);
324 task = manager->create_task(fd, 0, 0, 0, NULL); 350 task = manager->create_task(fd, 0, 0, 0, NULL);
325 351
326 manager->set_task_depend(task_update_sgp, task); 352 manager->set_task_depend(task_update_sgp, task);
327 manager->set_task_depend(task_create_pp, task); 353 manager->set_task_depend(task_create_pp, task);
354 manager->set_task_depend(task_create_pp, task_create_sp);
355 manager->set_task_depend(task_create_sp, task);
328 356
329 manager->spawn_task(task_update_sgp); 357 manager->spawn_task(task_update_sgp);
330 manager->spawn_task(task_create_pp); 358 manager->spawn_task(task_create_pp);
359 manager->spawn_task(task_create_sp);
331 manager->spawn_task(task); 360 manager->spawn_task(task);
332 } 361 }
333 362
334 void 363 void
335 Viewer::run_draw(void) 364 Viewer::run_draw(void)
336 { 365 {
337 HTaskPtr task; 366 HTaskPtr task;
338 int fd; 367 int fd;
339 368
340 369 //cout << "size = " << arg->sp->info.size << endl;
341 fprintf(stderr, " ** %s **\n", __FUNCTION__); 370 //cout << "span x = " << arg->sp->span[0].x << endl;
342 371 //printf("run_draw x:%d y:%d end:%d z:%f zpos:%f\n", arg->sp->span[0].x, arg->sp->span[0].y, arg->sp->span[0].length_x, arg->sp->span[0].start_z , arg->sp->span[0].end_z);
343 arg->p->draw(arg->pp); // test draw of PolygonPack 372 //cout << endl;
344 373
374 //arg->p->draw(arg->pp); // test draw of PolygonPack
375 //arg->p->draw(arg->sp); // test draw of SpanPack
376 arg->p->draw(&arg->spl->dline[0]); // test draw of SpanPackList
377
345 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL); 378 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL);
346 SDL_UpdateRect(screen, 0, 0, 0, 0); 379 SDL_UpdateRect(screen, 0, 0, 0, 0);
347 380
348 //swap_buffers();
349 arg->frames++; 381 arg->frames++;
350 382
351 fd = manager->open("ViewerRunLoop"); 383 fd = manager->open("ViewerRunLoop");
352 task = manager->create_task(fd, 0, 0, 0, NULL); 384 task = manager->create_task(fd, 0, 0, 0, NULL);
353 manager->spawn_task(task); 385 manager->spawn_task(task);
354 } 386 }
355 387
356 void 388 void
357 Viewer::run_finish(void) 389 Viewer::run_finish(void)
358 { 390 {
359 fprintf(stderr, " ** %s **\n", __FUNCTION__);
360
361 if (arg->this_time != arg->start_time) { 391 if (arg->this_time != arg->start_time) {
362 cout<< ((float)arg->frames/(arg->this_time-arg->start_time))*1000.0 << " FPS\n"; 392 cout<< (((float)arg->frames)/(arg->this_time-arg->start_time))*1000.0 << " FPS\n";
363 } 393 }
364 394
365 SDL_FreeSurface(arg->bitmap); 395 SDL_FreeSurface(arg->bitmap);
366 delete [] pixels; 396 delete [] pixels;
367 arg->p->delete_data(); 397 arg->p->delete_data();
368 delete arg->p; 398 delete arg->p;
369 delete arg->sgp; 399 delete arg->sgp;
370 delete arg->pp; 400 delete arg->pp;
401 delete arg->spl;
402 delete arg->sp;
371 quit(); 403 quit();
372 404
373 delete arg; 405 delete arg;
374 } 406 }
375
376 #if 0
377 void Viewer::run()
378 {
379 int frames = 0;
380 int start_time, this_time;
381
382 HTaskPtr task_create_sgp = NULL;
383 HTaskPtr task_update_sgp = NULL;
384 HTaskPtr task_create_pp = NULL;
385 int fd_create_sgp;
386 int fd_update_sgp;
387 int fd_create_pp;
388
389 start_time = get_ticks();
390
391 SDL_Surface *bitmap = NULL;
392 SDL_PixelFormat *pf;
393 pf = screen->format;
394
395 Uint32 background;
396 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
397
398 pixels = new Uint32[width*height];
399
400 Polygon *p = new Polygon;
401 p->set_data("cube.xml");
402 //p->set_data("cube-p.xml");
403
404 p->viewer = this;
405 //p->screen = screen;
406
407 SceneGraphPack *sgp = new SceneGraphPack;
408 create_sgp(p, sgp);
409
410 PolygonPack *pp = new PolygonPack;
411
412 graph_line();
413
414 float r = 0;
415 float x = 0;
416 float y = 0;
417 float z = 0;
418
419 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
420 width*4, redMask, greenMask,
421 blueMask, alphaMask);
422
423 // Loop until done.
424 while(1)
425 {
426 // Destroy our GL context, etc.
427 //if(quit_check() || scene->action_scene==NULL)
428 if(quit_check())
429 {
430 this_time = get_ticks();
431 if (this_time != start_time)
432 {
433 cout<<((float)frames/(this_time-start_time))*1000.0<<" FPS\n";
434 }
435 SDL_FreeSurface(bitmap);
436 delete pixels;
437 p->delete_data();
438 delete p;
439 delete sgp;
440 delete pp;
441 quit();
442 break;
443 }
444 /////////////////////
445 clean_pixels();
446
447 this->zRow_init();
448 graph_line();
449
450 #if 0 // ここは update_sgp
451 if(r > 360) r = 0;
452 r+= 1.0;
453 // r= 0;
454 p->angle[0] = 0;
455 p->angle[1] = r;
456 p->angle[2] = 0;
457 //p->child->angle[1] = r*2;
458 //p->child->brother->angle[1] = r*3;
459 //p->child->brother->child->angle[1] = r*4;
460 x += 0.5;
461 y += 0.5;
462 z += 0.5;
463 p->xyz[0] = x;
464 p->xyz[1] = y;
465 p->xyz[2] = z;
466 //p->tree_draw();
467 #endif
468
469 #if 0
470 update_sgp(sgp, sgp);
471 create_pp(pp, sgp);
472 #else
473 fd_update_sgp = manager->open("UpdateSGP");
474 fd_create_pp = manager->open("CreatePP");
475 task_update_sgp =
476 manager->create_task(fd_update_sgp,
477 sizeof(SceneGraphPack),
478 (unsigned int)sgp,
479 (unsigned int)sgp,
480 NULL);
481 task_create_pp =
482 manager->create_task(fd_create_pp,
483 sizeof(SceneGraphPack),
484 (unsigned int)sgp,
485 (unsigned int)pp,
486 NULL);
487
488 manager->spawn_task(task_update_sgp);
489 manager->spawn_task(task_create_pp);
490 manager->run();
491 #endif
492
493 //p->draw(sgp); // test draw of SceneGraphPack
494 p->draw(pp); // test draw of PolygonPack
495
496 // 一回のみ生成で、その後は再利用
497 //bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
498 //width*4, redMask, greenMask,
499 //blueMask, alphaMask);
500
501 //SDL_FillRect(screen, NULL, background);
502 SDL_BlitSurface(bitmap, NULL, screen, NULL);
503 SDL_UpdateRect(screen, 0, 0, 0, 0);
504
505
506 /////////////////////
507
508 //swap_buffers();
509 ++frames;
510 }
511 }
512 #else
513 void Viewer::run() {}
514 #endif
515 407
516 void 408 void
517 Viewer::zRow_init() 409 Viewer::zRow_init()
518 { 410 {
519 for (int i = 0; i < width; i++) { 411 for (int i = 0; i < width; i++) {