comparison TaskManager/Test/simple_render/viewer.cpp @ 26:3f68b2ef4fb0

*** empty log message ***
author gongo
date Sun, 10 Feb 2008 18:31:48 +0900 (2008-02-10)
parents
children 243ee0a1fe6f
comparison
equal deleted inserted replaced
25:6f01d41a8cfb 26:3f68b2ef4fb0
1 #include <iostream>
2 #include <SDL.h>
3 #include <SDL_opengl.h>
4 #include <math.h>
5 #include "polygon.h"
6 //#include "demonstration.h"
7 //#include "scene.h"
8 #include "viewer.h"
9 #include "sys.h"
10 using namespace std;
11
12 extern int create_sgp(SceneGraphPack *sgp, Polygon *sg);
13 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
14 extern int create_pp(PolygonPack *pp, SceneGraphPack *sgp);
15
16 Viewer::Viewer(int b, int w, int h)
17 {
18 bpp = b;
19 width = w;
20 height = h;
21 }
22
23
24 void Viewer::sdl_init()
25 {
26 if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
27 {
28 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
29 exit( 1 );
30 }
31 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE );
32
33 //manager = new TaskManager(1);
34 //manager->init();
35
36 manager->set_symbol("CreateSGP", (void*)create_sgp);
37 manager->set_symbol("UpdateSGP", (void*)update_sgp);
38 manager->set_symbol("CreatePP", (void*)create_pp);
39 }
40
41
42 void Viewer::init()
43 {
44 if(SDL_Init( SDL_INIT_VIDEO ) < 0)
45 {
46 cout << "Couldn't initialize SDL:" << SDL_GetError() << endl;
47 exit(1);
48 }
49
50 /* See if we should detect the display depth */
51 if(bpp == 0)
52 {
53 if (SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8)
54 {
55 bpp = 8;
56 }
57 else
58 {
59 bpp = 16; /* More doesn't seem to work */
60 }
61 }
62
63 //video_flags = SDL_OPENGL;
64 video_flags = SDL_HWSURFACE;
65
66 /* Initialize the display */
67 switch (bpp)
68 {
69 case 8:
70 rgb_size[0] = 3;
71 rgb_size[1] = 3;
72 rgb_size[2] = 2;
73 break;
74 case 15:
75 case 16:
76 rgb_size[0] = 5;
77 rgb_size[1] = 5;
78 rgb_size[2] = 5;
79 break;
80 default:
81 rgb_size[0] = 8;
82 rgb_size[1] = 8;
83 rgb_size[2] = 8;
84 break;
85 }
86
87 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rgb_size[0]);
88 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, rgb_size[1]);
89 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, rgb_size[2]);
90 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
91 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
92
93 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
94
95 screen = SDL_SetVideoMode(width, height, bpp, video_flags);
96
97 if(screen == NULL)
98 {
99 cout << "Couldn't set GL mode: " << SDL_GetError() << endl;
100 SDL_Quit();
101 exit(1);
102 }
103
104 SDL_WM_SetCaption("SDL GL test", "testgl");
105
106 glViewport(0, 0, width, height);
107
108 glMatrixMode(GL_PROJECTION);
109 glLoadIdentity();
110
111 glOrtho(-width/10, width/10, -height/10, height/10, -40.0, 400.0);
112
113 glMatrixMode(GL_MODELVIEW);
114 glLoadIdentity();
115
116 glEnable(GL_DEPTH_TEST);
117
118 glDepthFunc(GL_LESS);
119
120 glShadeModel(GL_SMOOTH);
121 }
122
123 int Viewer::get_ticks()
124 {
125 int time;
126 time = SDL_GetTicks();
127 return time;
128 }
129
130 bool Viewer::quit_check()
131 {
132 bool quit = false;
133 SDL_Event event;
134 while(SDL_PollEvent(&event))
135 {
136 if(event.type==SDL_QUIT)
137 {
138 quit = true;
139 return quit;
140 }
141 }
142 return quit;
143 }
144
145 void Viewer::quit()
146 {
147 SDL_Quit();
148 }
149
150
151 void Viewer::swap_buffers()
152 {
153 SDL_GL_SwapBuffers();
154 }
155
156
157 void Viewer::write_pixel(int x, int y,float z, Uint32 rgb) {
158 SDL_PixelFormat *pf;
159 pf = screen->format;
160 //printf("x:%d y:%d z:%d\n",x,y,z);
161
162 //cout << "write_pixel" << endl;
163 //cout << x << " " << y << endl;
164
165 //cout << SDL_MapRGB(pf,0,0,150) << endl;
166
167 x += width/2;
168 y += height/2;
169
170 static int diffz,diffz1;
171 diffz1 = diffz;
172 diffz = (zRow[x][y]>z);
173 if(diffz != diffz1) {
174 //printf("diffz :%d zRow[%d][%d] = %f z = %f\n",diffz,x,y,zRow[x][y],z);
175 }
176 //printf("x:%d,y:%d,z:%f,zRow:%f\n",x,y,z,zRow[x][y]);
177 if(z < zRow[x][y]) {
178 // printf("x:%d,y:%d,z:%d\n",x,y,z,zRow);
179 if(x < width && x > 0 && y > 0 && y < height)
180 {
181 //pixels[width*y + x] = SDL_MapRGB(pf,70,70,71);
182 zRow[x][y] = z;
183 y = height - y;
184 pixels[width*y + x] = rgb;
185 }
186 }
187 }
188
189 void Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
190 {
191 //cout << "write_line("<< x1 << "," << y1 << "," << x2 << "," << y2 << ")"<< endl;
192
193 //Uint32 rgb = 9830400;
194
195 if(x1 > x2)
196 {
197 float x=0;
198 float y=0;
199 x=x1;
200 y=y1;
201 x1 = x2;
202 y1 = y2;
203 x2 = x;
204 y2 = y;
205 }
206 float s = y1;
207
208 if((int)x1 == (int)x2)
209 {
210 if(y1 > y2)
211 {
212 float y=0;
213 y = y1;
214 y1 = y2;
215 y2 = y;
216 }
217 for(float i=y1; i<y2; i++)
218 {
219 //write_pixel((int)x1,(int)i);
220 write_pixel((int)x1,(int)i,0,rgb);
221 }
222 }
223 else
224 {
225 float t = (y2 - y1)/(x2 - x1);
226 if(t < -1)
227 {
228 float f = 0;
229 for(float i=x1; i<x2; i++)
230 {
231 for(float a=(int)t; a<0; a++)
232 {
233 //write_pixel((int)i,(int)s);
234 write_pixel((int)i,(int)s,0,rgb);
235 s--;
236 }
237 f += t-(int)t;
238 if(f <= -1)
239 {
240 //write_pixel((int)i,(int)s);
241 write_pixel((int)i,(int)s,0,rgb);
242 f = 0;
243 s--;
244 }
245 }
246 }
247 else if(t <= 1)
248 {
249 for(float i=x1; i<x2; i++)
250 {
251 //write_pixel((int)i,(int)s);
252 write_pixel((int)i,(int)s,0,rgb);
253 s += t;
254 }
255 }
256 else
257 {
258 float f = 0;
259 for(float i=x1; i<x2; i++)
260 {
261 for(float a=0; a<(int)t; a++)
262 {
263 //write_pixel((int)i,(int)s);
264 write_pixel((int)i,(int)s,0,rgb);
265 s++;
266 }
267 f += t-(int)t;
268 if(f >= 1)
269 {
270 //write_pixel((int)i,(int)s);
271 write_pixel((int)i,(int)s,0,rgb);
272 f = 0;
273 s++;
274 }
275 }
276 }
277 }
278 }
279
280 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
281 {
282 write_line(x1,y1,x2,y2,rgb);
283 write_line(x2,y2,x3,y3,rgb);
284 write_line(x3,y3,x1,y1,rgb);
285 }
286
287 void Viewer::clean_pixels()
288 {
289 for(int i=0; i<width*height; i++)
290 {
291 pixels[i] = 0x00;
292 }
293 }
294
295 void Viewer::graph_line()
296 {
297 int xl = width*height/2;
298 int yl = width/2;
299 for(int i=0; i<width; i++)
300 {
301 for(int t=0; t<height; t+=20)
302 {
303 pixels[width*t+i] = 0x5a;
304 }
305 pixels[xl +i] = 0xff;
306 }
307 for(int i=0; i<height; i++)
308 {
309 for(int t=0; t<width; t+=20)
310 {
311 pixels[i*width+t] = 0x5a;
312 }
313 pixels[i*width+yl] = 0xff;
314 }
315 }
316
317
318 void Viewer::run()
319 {
320 int frames = 0;
321 int start_time, this_time;
322
323 HTaskPtr task_update_sgp = NULL;
324 HTaskPtr task_create_pp = NULL;
325 int fd_update_sgp;
326 int fd_create_pp;
327
328 start_time = get_ticks();
329
330 SDL_Surface *bitmap = NULL;
331 SDL_PixelFormat *pf;
332 pf = screen->format;
333
334 Uint32 background;
335 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
336
337 pixels = new Uint32[width*height];
338
339 Polygon *p = new Polygon;
340 p->set_data("cube.xml");
341 //p->set_data("cube-p.xml");
342
343 p->viewer = this;
344 //p->screen = screen;
345 /*
346 p->next->viewer = this;
347 p->next->next->viewer = this;
348 p->next->next->next->viewer = this;
349 p->next->next->next->next->viewer = this;
350 p->next->next->next->next->next->viewer = this;
351 p->next->next->next->next->next->next->viewer = this;
352 p->next->next->next->next->next->next->next->viewer = this;
353 p->next->next->next->next->next->next->next->next->viewer = this;
354 p->next->next->next->next->next->next->next->next->next->viewer = this;
355 */
356
357 SceneGraphPack *sgp = new SceneGraphPack;
358 create_sgp(sgp,p);
359
360 PolygonPack *pp = new PolygonPack;
361
362 graph_line();
363
364 float r = 0;
365 float x = 0;
366 float y = 0;
367 float z = 0;
368
369 // Loop until done.
370 while(1)
371 {
372 // Destroy our GL context, etc.
373 //if(quit_check() || scene->action_scene==NULL)
374 if(quit_check())
375 {
376 this_time = get_ticks();
377 if (this_time != start_time)
378 {
379 cout<<((float)frames/(this_time-start_time))*1000.0<<" FPS\n";
380 }
381 SDL_FreeSurface(bitmap);
382 delete pixels;
383 p->delete_data();
384 delete p;
385 delete sgp;
386 delete pp;
387 quit();
388 break;
389 }
390 /////////////////////
391 clean_pixels();
392
393 this->zRow_init();
394 graph_line();
395
396 if(r > 360) r = 0;
397 r+= 1.0;
398 // r= 0;
399 p->angle[0] = 0;
400 p->angle[1] = r;
401 p->angle[2] = 0;
402 //p->child->angle[1] = r*2;
403 //p->child->brother->angle[1] = r*3;
404 //p->child->brother->child->angle[1] = r*4;
405 x += 0.5;
406 y += 0.5;
407 z += 0.5;
408 //p->xyz[0] = x;
409 //p->xyz[1] = y;
410 //p->xyz[2] = z;
411 //p->tree_draw();
412
413
414 update_sgp(sgp, sgp);
415 //create_pp(pp, sgp);
416
417 //fd_update_sgp = manager->open("UpdateSGP");
418 fd_create_pp = manager->open("CreatePP");
419 #if 1
420 //task_update_sgp = manager->create_task(fd_update_sgp,
421 //sizeof(SceneGraphPack),
422 //(unsigned int)sgp,
423 // (unsigned int)sgp,
424 //NULL);
425
426 task_create_pp = manager->create_task(fd_create_pp,
427 sizeof(SceneGraphPack),
428 (unsigned int)sgp,
429 (unsigned int)pp,
430 NULL);
431 //manager->set_task_depend(task_update_sgp, task_create_pp);
432 //manager->spawn_task(task_update_sgp);
433 manager->spawn_task(task_create_pp);
434
435 manager->run();
436 #endif
437 //p->draw(sgp);
438 p->draw(pp);
439
440 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels,width,height,32,width*4,0x00ff0000,0x0000ff00,0x000000ff,0);
441 SDL_FillRect(screen, NULL, background);
442 SDL_BlitSurface(bitmap,NULL,screen,NULL);
443 SDL_UpdateRect(screen,0,0,0,0);
444
445
446 /////////////////////
447
448 //swap_buffers();
449 ++frames;
450 }
451 }
452
453 void Viewer::zRow_init() {
454 int i,j;
455 for(i = 0;i < 640; i++) {
456 for(j = 0; j < 480; j++) {
457 zRow[i][j] = 65535;
458 }
459 }
460 }