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