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
|
|
257 arg->start_time = get_ticks();
|
|
258 arg->pf = screen->format;
|
|
259 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
260 arg->p = new Polygon;
|
|
261 arg->p->set_data("cube.xml");
|
|
262 arg->p->viewer = this;
|
|
263 arg->sgp = new SceneGraphPack;
|
48
|
264 create_sgp(arg->p, arg->sgp);
|
42
|
265 arg->pp = new PolygonPack;
|
|
266
|
|
267 pixels = new Uint32[width*height];
|
|
268
|
|
269 graph_line();
|
|
270
|
|
271 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
|
|
272 width*4, redMask, greenMask,
|
|
273 blueMask, alphaMask);
|
|
274
|
|
275 fd = manager->open("ViewerRunLoop");
|
48
|
276 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
277 manager->spawn_task(task);
|
42
|
278 }
|
|
279
|
|
280 void
|
48
|
281 Viewer::run_loop(void)
|
42
|
282 {
|
|
283 HTaskPtr task_update_sgp = NULL;
|
|
284 HTaskPtr task_create_pp = NULL;
|
|
285 HTaskPtr task_finish = NULL;
|
|
286 int fd_update_sgp;
|
|
287 int fd_create_pp;
|
|
288 int fd_finish;
|
|
289
|
48
|
290 HTaskPtr task;
|
|
291 int fd;
|
|
292
|
42
|
293 if (quit_check()) {
|
|
294 arg->this_time = get_ticks();
|
|
295 fd_finish = manager->open("ViewerRunFinish");
|
48
|
296 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
|
42
|
297 manager->spawn_task(task_finish);
|
|
298 return;
|
|
299 }
|
|
300
|
|
301 clean_pixels();
|
|
302
|
48
|
303 zRow_init();
|
42
|
304 graph_line();
|
|
305
|
|
306 fd_update_sgp = manager->open("UpdateSGP");
|
|
307 fd_create_pp = manager->open("CreatePP");
|
48
|
308 fd = manager->open("ViewerRunDraw");
|
|
309 task_update_sgp = manager->create_task(fd_update_sgp,
|
|
310 sizeof(SceneGraphPack),
|
|
311 (unsigned int)arg->sgp,
|
|
312 (unsigned int)arg->sgp,
|
|
313 NULL);
|
|
314 task_create_pp = manager->create_task(fd_create_pp,
|
|
315 sizeof(SceneGraphPack),
|
|
316 (unsigned int)arg->sgp,
|
|
317 (unsigned int)arg->pp,
|
|
318 NULL);
|
|
319 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
320
|
|
321 manager->set_task_depend(task_update_sgp, task);
|
|
322 manager->set_task_depend(task_create_pp, task);
|
42
|
323
|
|
324 manager->spawn_task(task_update_sgp);
|
|
325 manager->spawn_task(task_create_pp);
|
48
|
326 manager->spawn_task(task);
|
|
327 }
|
42
|
328
|
48
|
329 void
|
|
330 Viewer::run_draw(void)
|
|
331 {
|
|
332 HTaskPtr task;
|
|
333 int fd;
|
|
334
|
|
335 arg->p->draw(arg->pp); // test draw of PolygonPack
|
42
|
336
|
|
337 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL);
|
48
|
338 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
42
|
339
|
|
340 //swap_buffers();
|
|
341 arg->frames++;
|
48
|
342
|
|
343 fd = manager->open("ViewerRunLoop");
|
|
344 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
345 manager->spawn_task(task);
|
42
|
346 }
|
|
347
|
|
348 void
|
48
|
349 Viewer::run_finish(void)
|
42
|
350 {
|
|
351 if (arg->this_time != arg->start_time) {
|
48
|
352 cout<< ((float)arg->frames/(arg->this_time-arg->start_time))*1000.0 << " FPS\n";
|
42
|
353 }
|
|
354
|
|
355 SDL_FreeSurface(arg->bitmap);
|
|
356 delete [] pixels;
|
|
357 arg->p->delete_data();
|
|
358 delete arg->p;
|
|
359 delete arg->sgp;
|
|
360 delete arg->pp;
|
|
361 quit();
|
|
362
|
|
363 delete arg;
|
|
364 }
|
|
365
|
28
|
366 void
|
|
367 Viewer::zRow_init()
|
|
368 {
|
|
369 for (int i = 0; i < width; i++) {
|
|
370 for (int j = 0; j < height; j++) {
|
|
371 zRow[i][j] = 65535;
|
26
|
372 }
|
28
|
373 }
|
26
|
374 }
|