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 "viewer.h"
|
|
8 #include "sys.h"
|
|
9 using namespace std;
|
|
10
|
28
|
11 #define redMask 0x00ff0000
|
|
12 #define greenMask 0x0000ff00
|
|
13 #define blueMask 0x000000ff
|
|
14 #define alphaMask 0
|
|
15
|
48
|
16 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
|
26
|
17 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
|
48
|
18 extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp);
|
26
|
19
|
|
20 Viewer::Viewer(int b, int w, int h)
|
|
21 {
|
28
|
22 bpp = b;
|
|
23 width = w;
|
|
24 height = h;
|
26
|
25 }
|
|
26
|
|
27
|
|
28 void Viewer::sdl_init()
|
|
29 {
|
28
|
30 if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
26
|
31 {
|
28
|
32 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
33 exit( 1 );
|
26
|
34 }
|
28
|
35 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE );
|
26
|
36 }
|
|
37
|
|
38
|
|
39 int Viewer::get_ticks()
|
|
40 {
|
28
|
41 int time;
|
|
42 time = SDL_GetTicks();
|
|
43 return time;
|
26
|
44 }
|
|
45
|
|
46 bool Viewer::quit_check()
|
|
47 {
|
28
|
48 bool quit = false;
|
|
49 SDL_Event event;
|
|
50 while(SDL_PollEvent(&event))
|
26
|
51 {
|
28
|
52 if(event.type==SDL_QUIT)
|
26
|
53 {
|
28
|
54 quit = true;
|
|
55 return quit;
|
26
|
56 }
|
|
57 }
|
28
|
58 return quit;
|
26
|
59 }
|
|
60
|
|
61 void Viewer::quit()
|
|
62 {
|
28
|
63 SDL_Quit();
|
26
|
64 }
|
|
65
|
|
66
|
|
67 void Viewer::swap_buffers()
|
|
68 {
|
28
|
69 SDL_GL_SwapBuffers();
|
26
|
70 }
|
|
71
|
|
72
|
51
|
73 void
|
|
74 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
|
|
75 {
|
28
|
76 SDL_PixelFormat *pf;
|
|
77 pf = screen->format;
|
|
78 x += width/2;
|
|
79 y += height/2;
|
26
|
80
|
28
|
81 static int diffz,diffz1;
|
51
|
82
|
28
|
83 diffz1 = diffz;
|
|
84 diffz = (zRow[x][y]>z);
|
51
|
85 if (z < zRow[x][y]) {
|
|
86 if (x < width && x > 0 && y > 0 && y < height) {
|
28
|
87 zRow[x][y] = z;
|
|
88 y = height - y;
|
|
89 pixels[width*y + x] = rgb;
|
|
90 }
|
26
|
91 }
|
|
92 }
|
|
93
|
51
|
94 void
|
|
95 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
|
26
|
96 {
|
51
|
97 if (x1 > x2) {
|
28
|
98 float x=0;
|
|
99 float y=0;
|
|
100 x=x1;
|
|
101 y=y1;
|
|
102 x1 = x2;
|
|
103 y1 = y2;
|
|
104 x2 = x;
|
|
105 y2 = y;
|
26
|
106 }
|
51
|
107
|
28
|
108 float s = y1;
|
26
|
109
|
51
|
110 if ((int)x1 == (int)x2) {
|
|
111 if (y1 > y2) {
|
28
|
112 float y=0;
|
|
113 y = y1;
|
|
114 y1 = y2;
|
|
115 y2 = y;
|
26
|
116 }
|
51
|
117
|
|
118 for (float i=y1; i<y2; i++) {
|
28
|
119 //write_pixel((int)x1,(int)i);
|
|
120 write_pixel((int)x1,(int)i,0,rgb);
|
26
|
121 }
|
51
|
122 } else {
|
28
|
123 float t = (y2 - y1)/(x2 - x1);
|
51
|
124 if (t < -1) {
|
28
|
125 float f = 0;
|
51
|
126 for (float i=x1; i<x2; i++) {
|
|
127 for (float a=(int)t; a<0; a++) {
|
28
|
128 write_pixel((int)i,(int)s,0,rgb);
|
|
129 s--;
|
26
|
130 }
|
51
|
131
|
28
|
132 f += t-(int)t;
|
51
|
133
|
|
134 if (f <= -1) {
|
28
|
135 write_pixel((int)i,(int)s,0,rgb);
|
|
136 f = 0;
|
|
137 s--;
|
26
|
138 }
|
|
139 }
|
51
|
140 } else if (t <= 1) {
|
|
141 for(float i=x1; i<x2; i++) {
|
28
|
142 //write_pixel((int)i,(int)s);
|
|
143 write_pixel((int)i,(int)s,0,rgb);
|
|
144 s += t;
|
26
|
145 }
|
51
|
146 } else {
|
28
|
147 float f = 0;
|
51
|
148 for (float i=x1; i<x2; i++) {
|
|
149 for (float a=0; a<(int)t; a++) {
|
28
|
150 write_pixel((int)i,(int)s,0,rgb);
|
|
151 s++;
|
26
|
152 }
|
51
|
153
|
28
|
154 f += t-(int)t;
|
51
|
155
|
|
156 if (f >= 1) {
|
28
|
157 write_pixel((int)i,(int)s,0,rgb);
|
|
158 f = 0;
|
|
159 s++;
|
26
|
160 }
|
|
161 }
|
|
162 }
|
|
163 }
|
|
164 }
|
|
165
|
|
166 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
|
|
167 {
|
28
|
168 write_line(x1,y1,x2,y2,rgb);
|
|
169 write_line(x2,y2,x3,y3,rgb);
|
|
170 write_line(x3,y3,x1,y1,rgb);
|
26
|
171 }
|
|
172
|
|
173 void Viewer::clean_pixels()
|
|
174 {
|
28
|
175 for(int i=0; i<width*height; i++)
|
26
|
176 {
|
28
|
177 pixels[i] = 0x00;
|
26
|
178 }
|
|
179 }
|
|
180
|
|
181 void Viewer::graph_line()
|
|
182 {
|
28
|
183 int xl = width*height/2;
|
|
184 int yl = width/2;
|
|
185 for(int i=0; i<width; i++)
|
26
|
186 {
|
28
|
187 for(int t=0; t<height; t+=20)
|
26
|
188 {
|
28
|
189 pixels[width*t+i] = 0x5a;
|
26
|
190 }
|
28
|
191 pixels[xl +i] = 0xff;
|
26
|
192 }
|
28
|
193 for(int i=0; i<height; i++)
|
26
|
194 {
|
28
|
195 for(int t=0; t<width; t+=20)
|
26
|
196 {
|
28
|
197 pixels[i*width+t] = 0x5a;
|
26
|
198 }
|
28
|
199 pixels[i*width+yl] = 0xff;
|
26
|
200 }
|
|
201 }
|
|
202
|
|
203
|
42
|
204 struct run_arg_t {
|
|
205 int start_time;
|
|
206 int this_time;
|
|
207 int frames;
|
|
208 SDL_Surface *bitmap;
|
|
209 SDL_PixelFormat *pf;
|
|
210 Uint32 background;
|
|
211 Polygon *p;
|
|
212 SceneGraphPack *sgp;
|
|
213 PolygonPack *pp;
|
|
214 };
|
|
215
|
48
|
216 struct run_arg_t *arg;
|
|
217
|
42
|
218 void
|
|
219 Viewer::run_init()
|
|
220 {
|
48
|
221 arg = new run_arg_t;
|
42
|
222 HTaskPtr task;
|
|
223 int fd;
|
|
224
|
|
225 arg->start_time = get_ticks();
|
51
|
226 arg->this_time = 0;
|
|
227 arg->frames = 0;
|
|
228
|
42
|
229 arg->pf = screen->format;
|
|
230 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
231 arg->p = new Polygon;
|
|
232 arg->p->set_data("cube.xml");
|
|
233 arg->p->viewer = this;
|
|
234 arg->sgp = new SceneGraphPack;
|
48
|
235 create_sgp(arg->p, arg->sgp);
|
42
|
236 arg->pp = new PolygonPack;
|
|
237
|
|
238 pixels = new Uint32[width*height];
|
|
239
|
|
240 graph_line();
|
|
241
|
|
242 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
|
|
243 width*4, redMask, greenMask,
|
|
244 blueMask, alphaMask);
|
|
245
|
|
246 fd = manager->open("ViewerRunLoop");
|
48
|
247 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
248 manager->spawn_task(task);
|
42
|
249 }
|
|
250
|
|
251 void
|
48
|
252 Viewer::run_loop(void)
|
42
|
253 {
|
|
254 HTaskPtr task_update_sgp = NULL;
|
|
255 HTaskPtr task_create_pp = NULL;
|
|
256 HTaskPtr task_finish = NULL;
|
|
257 int fd_update_sgp;
|
|
258 int fd_create_pp;
|
|
259 int fd_finish;
|
|
260
|
48
|
261 HTaskPtr task;
|
|
262 int fd;
|
|
263
|
42
|
264 if (quit_check()) {
|
|
265 arg->this_time = get_ticks();
|
|
266 fd_finish = manager->open("ViewerRunFinish");
|
48
|
267 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
|
42
|
268 manager->spawn_task(task_finish);
|
|
269 return;
|
|
270 }
|
|
271
|
|
272 clean_pixels();
|
|
273
|
48
|
274 zRow_init();
|
42
|
275 graph_line();
|
|
276
|
|
277 fd_update_sgp = manager->open("UpdateSGP");
|
|
278 fd_create_pp = manager->open("CreatePP");
|
48
|
279 fd = manager->open("ViewerRunDraw");
|
|
280 task_update_sgp = manager->create_task(fd_update_sgp,
|
|
281 sizeof(SceneGraphPack),
|
|
282 (unsigned int)arg->sgp,
|
|
283 (unsigned int)arg->sgp,
|
|
284 NULL);
|
|
285 task_create_pp = manager->create_task(fd_create_pp,
|
|
286 sizeof(SceneGraphPack),
|
|
287 (unsigned int)arg->sgp,
|
|
288 (unsigned int)arg->pp,
|
|
289 NULL);
|
|
290 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
291
|
|
292 manager->set_task_depend(task_update_sgp, task);
|
|
293 manager->set_task_depend(task_create_pp, task);
|
42
|
294
|
|
295 manager->spawn_task(task_update_sgp);
|
|
296 manager->spawn_task(task_create_pp);
|
48
|
297 manager->spawn_task(task);
|
|
298 }
|
42
|
299
|
48
|
300 void
|
|
301 Viewer::run_draw(void)
|
|
302 {
|
|
303 HTaskPtr task;
|
|
304 int fd;
|
|
305
|
|
306 arg->p->draw(arg->pp); // test draw of PolygonPack
|
42
|
307
|
|
308 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL);
|
48
|
309 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
42
|
310
|
|
311 arg->frames++;
|
48
|
312
|
|
313 fd = manager->open("ViewerRunLoop");
|
|
314 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
315 manager->spawn_task(task);
|
42
|
316 }
|
|
317
|
|
318 void
|
48
|
319 Viewer::run_finish(void)
|
42
|
320 {
|
|
321 if (arg->this_time != arg->start_time) {
|
50
|
322 cout<< (((float)arg->frames)/(arg->this_time-arg->start_time))*1000.0 << " FPS\n";
|
42
|
323 }
|
|
324
|
|
325 SDL_FreeSurface(arg->bitmap);
|
|
326 delete [] pixels;
|
|
327 arg->p->delete_data();
|
|
328 delete arg->p;
|
|
329 delete arg->sgp;
|
|
330 delete arg->pp;
|
|
331 quit();
|
|
332
|
|
333 delete arg;
|
|
334 }
|
|
335
|
28
|
336 void
|
|
337 Viewer::zRow_init()
|
|
338 {
|
|
339 for (int i = 0; i < width; i++) {
|
|
340 for (int j = 0; j < height; j++) {
|
|
341 zRow[i][j] = 65535;
|
26
|
342 }
|
28
|
343 }
|
26
|
344 }
|