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