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 {
|
67
|
30 if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
|
28
|
31 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
32 exit( 1 );
|
26
|
33 }
|
67
|
34
|
|
35 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE);
|
|
36 if (screen == NULL) {
|
|
37 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
|
|
38 SDL_Quit();
|
|
39 exit(1);
|
|
40 }
|
26
|
41 }
|
|
42
|
|
43
|
|
44 int Viewer::get_ticks()
|
|
45 {
|
28
|
46 int time;
|
|
47 time = SDL_GetTicks();
|
|
48 return time;
|
26
|
49 }
|
|
50
|
|
51 bool Viewer::quit_check()
|
|
52 {
|
28
|
53 bool quit = false;
|
|
54 SDL_Event event;
|
|
55 while(SDL_PollEvent(&event))
|
26
|
56 {
|
28
|
57 if(event.type==SDL_QUIT)
|
26
|
58 {
|
28
|
59 quit = true;
|
|
60 return quit;
|
26
|
61 }
|
|
62 }
|
28
|
63 return quit;
|
26
|
64 }
|
|
65
|
|
66 void Viewer::quit()
|
|
67 {
|
28
|
68 SDL_Quit();
|
26
|
69 }
|
|
70
|
|
71
|
|
72 void Viewer::swap_buffers()
|
|
73 {
|
28
|
74 SDL_GL_SwapBuffers();
|
26
|
75 }
|
|
76
|
|
77
|
51
|
78 void
|
|
79 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
|
|
80 {
|
28
|
81 SDL_PixelFormat *pf;
|
|
82 pf = screen->format;
|
|
83 x += width/2;
|
|
84 y += height/2;
|
26
|
85
|
28
|
86 static int diffz,diffz1;
|
51
|
87
|
28
|
88 diffz1 = diffz;
|
|
89 diffz = (zRow[x][y]>z);
|
51
|
90 if (z < zRow[x][y]) {
|
|
91 if (x < width && x > 0 && y > 0 && y < height) {
|
28
|
92 zRow[x][y] = z;
|
|
93 y = height - y;
|
|
94 pixels[width*y + x] = rgb;
|
|
95 }
|
26
|
96 }
|
|
97 }
|
|
98
|
51
|
99 void
|
|
100 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
|
26
|
101 {
|
51
|
102 if (x1 > x2) {
|
28
|
103 float x=0;
|
|
104 float y=0;
|
|
105 x=x1;
|
|
106 y=y1;
|
|
107 x1 = x2;
|
|
108 y1 = y2;
|
|
109 x2 = x;
|
|
110 y2 = y;
|
26
|
111 }
|
51
|
112
|
28
|
113 float s = y1;
|
26
|
114
|
51
|
115 if ((int)x1 == (int)x2) {
|
|
116 if (y1 > y2) {
|
28
|
117 float y=0;
|
|
118 y = y1;
|
|
119 y1 = y2;
|
|
120 y2 = y;
|
26
|
121 }
|
51
|
122
|
|
123 for (float i=y1; i<y2; i++) {
|
28
|
124 //write_pixel((int)x1,(int)i);
|
|
125 write_pixel((int)x1,(int)i,0,rgb);
|
26
|
126 }
|
51
|
127 } else {
|
28
|
128 float t = (y2 - y1)/(x2 - x1);
|
51
|
129 if (t < -1) {
|
28
|
130 float f = 0;
|
51
|
131 for (float i=x1; i<x2; i++) {
|
|
132 for (float a=(int)t; a<0; a++) {
|
28
|
133 write_pixel((int)i,(int)s,0,rgb);
|
|
134 s--;
|
26
|
135 }
|
51
|
136
|
28
|
137 f += t-(int)t;
|
51
|
138
|
|
139 if (f <= -1) {
|
28
|
140 write_pixel((int)i,(int)s,0,rgb);
|
|
141 f = 0;
|
|
142 s--;
|
26
|
143 }
|
|
144 }
|
51
|
145 } else if (t <= 1) {
|
|
146 for(float i=x1; i<x2; i++) {
|
28
|
147 //write_pixel((int)i,(int)s);
|
|
148 write_pixel((int)i,(int)s,0,rgb);
|
|
149 s += t;
|
26
|
150 }
|
51
|
151 } else {
|
28
|
152 float f = 0;
|
51
|
153 for (float i=x1; i<x2; i++) {
|
|
154 for (float a=0; a<(int)t; a++) {
|
28
|
155 write_pixel((int)i,(int)s,0,rgb);
|
|
156 s++;
|
26
|
157 }
|
51
|
158
|
28
|
159 f += t-(int)t;
|
51
|
160
|
|
161 if (f >= 1) {
|
28
|
162 write_pixel((int)i,(int)s,0,rgb);
|
|
163 f = 0;
|
|
164 s++;
|
26
|
165 }
|
|
166 }
|
|
167 }
|
|
168 }
|
|
169 }
|
|
170
|
|
171 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
|
|
172 {
|
28
|
173 write_line(x1,y1,x2,y2,rgb);
|
|
174 write_line(x2,y2,x3,y3,rgb);
|
|
175 write_line(x3,y3,x1,y1,rgb);
|
26
|
176 }
|
|
177
|
|
178 void Viewer::clean_pixels()
|
|
179 {
|
28
|
180 for(int i=0; i<width*height; i++)
|
26
|
181 {
|
28
|
182 pixels[i] = 0x00;
|
26
|
183 }
|
|
184 }
|
|
185
|
|
186 void Viewer::graph_line()
|
|
187 {
|
28
|
188 int xl = width*height/2;
|
|
189 int yl = width/2;
|
|
190 for(int i=0; i<width; i++)
|
26
|
191 {
|
28
|
192 for(int t=0; t<height; t+=20)
|
26
|
193 {
|
28
|
194 pixels[width*t+i] = 0x5a;
|
26
|
195 }
|
28
|
196 pixels[xl +i] = 0xff;
|
26
|
197 }
|
28
|
198 for(int i=0; i<height; i++)
|
26
|
199 {
|
28
|
200 for(int t=0; t<width; t+=20)
|
26
|
201 {
|
28
|
202 pixels[i*width+t] = 0x5a;
|
26
|
203 }
|
28
|
204 pixels[i*width+yl] = 0xff;
|
26
|
205 }
|
|
206 }
|
|
207
|
|
208
|
42
|
209 struct run_arg_t {
|
|
210 int start_time;
|
|
211 int this_time;
|
|
212 int frames;
|
|
213 SDL_Surface *bitmap;
|
|
214 SDL_PixelFormat *pf;
|
|
215 Uint32 background;
|
|
216 Polygon *p;
|
54
|
217 //SceneGraphPack *sgp;
|
|
218 //PolygonPack *pp;
|
|
219 DmaManager *sgp_buff;
|
|
220 DmaManager *pp_buff;
|
42
|
221 };
|
|
222
|
48
|
223 struct run_arg_t *arg;
|
|
224
|
54
|
225 // run_arg_t ¤Ë½ñ¤¯¤Î¤¬¤á¤ó¤É¤¤¤«¤é¤Ã¤Æ
|
|
226 // global ¤Ë½ñ¤¯¤Î¤â¤É¤¦¤«
|
|
227 int start_time;
|
|
228 int this_time;
|
|
229 int frames;
|
|
230 SDL_Surface *bitmap;
|
|
231 SDL_PixelFormat *pixelFormat;
|
|
232 Uint32 background;
|
|
233 Polygon *polygon;
|
|
234 //SceneGraphPack *sgp;
|
|
235 //PolygonPack *pp;
|
|
236 DmaBuffer *sgp_buff;
|
|
237 DmaBuffer *pp_buff;
|
|
238
|
42
|
239 void
|
|
240 Viewer::run_init()
|
|
241 {
|
|
242 HTaskPtr task;
|
|
243 int fd;
|
|
244
|
54
|
245 SceneGraphPack *sgp;
|
|
246
|
|
247 start_time = get_ticks();
|
|
248 this_time = 0;
|
|
249 frames = 0;
|
|
250
|
|
251 pixelFormat = screen->format;
|
|
252 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
253 polygon = new Polygon;
|
|
254 polygon->set_data("cube.xml");
|
|
255 polygon->viewer = this;
|
|
256
|
|
257 //sgp = new SceneGraphPack;
|
|
258 //create_sgp(arg->p, arg->sgp);
|
51
|
259
|
54
|
260 // ÄÌ¿®ÍÑ buffer ¤Î allocate
|
|
261 // ¤³¤ì¤¸¤ã¤Ê¤¤¤ÈÄÌ¿®ÍѤ˻Ȥ¨¤Ê¤¤(¤è¤¦¤Ë½èÍý¤ò½ñ¤«¤Ê¤¤¤È¡ª)
|
|
262 sgp_buff = manager->allocate(sizeof(SceneGraphPack));
|
|
263
|
|
264 sgp_buff->get_buffer(&sgp);
|
|
265 create_sgp(polygon, sgp);
|
|
266 sgp_buff->swap_buffer();
|
|
267
|
|
268 sgp_buff->get_buffer(&sgp);
|
|
269 create_sgp(polygon, sgp);
|
|
270 sgp_buff->swap_buffer();
|
|
271
|
|
272 //pp = new PolygonPack;
|
|
273 pp_buff = manager->allocate(sizeof(PolygonPack));
|
42
|
274
|
|
275 pixels = new Uint32[width*height];
|
|
276
|
|
277 graph_line();
|
|
278
|
54
|
279 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
|
42
|
280 width*4, redMask, greenMask,
|
|
281 blueMask, alphaMask);
|
|
282
|
|
283 fd = manager->open("ViewerRunLoop");
|
48
|
284 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
285 //manager->spawn_task(task);
|
|
286 task->spawn();
|
42
|
287 }
|
|
288
|
|
289 void
|
48
|
290 Viewer::run_loop(void)
|
42
|
291 {
|
|
292 HTaskPtr task_update_sgp = NULL;
|
|
293 HTaskPtr task_create_pp = NULL;
|
|
294 HTaskPtr task_finish = NULL;
|
|
295 int fd_update_sgp;
|
|
296 int fd_create_pp;
|
|
297 int fd_finish;
|
|
298
|
48
|
299 HTaskPtr task;
|
|
300 int fd;
|
|
301
|
42
|
302 if (quit_check()) {
|
54
|
303 this_time = get_ticks();
|
42
|
304 fd_finish = manager->open("ViewerRunFinish");
|
48
|
305 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
|
63
|
306 //manager->spawn_task(task_finish);
|
|
307 task_finish->spawn();
|
42
|
308 return;
|
|
309 }
|
|
310
|
|
311 clean_pixels();
|
|
312
|
48
|
313 zRow_init();
|
42
|
314 graph_line();
|
|
315
|
|
316 fd_update_sgp = manager->open("UpdateSGP");
|
|
317 fd_create_pp = manager->open("CreatePP");
|
48
|
318 fd = manager->open("ViewerRunDraw");
|
|
319 task_update_sgp = manager->create_task(fd_update_sgp,
|
|
320 sizeof(SceneGraphPack),
|
54
|
321 sgp_buff, sgp_buff, NULL);
|
48
|
322 task_create_pp = manager->create_task(fd_create_pp,
|
|
323 sizeof(SceneGraphPack),
|
54
|
324 sgp_buff, pp_buff, NULL);
|
48
|
325 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
326
|
63
|
327 task->set_depend(task_update_sgp);
|
|
328 task->set_depend(task_create_pp);
|
42
|
329
|
63
|
330 task_update_sgp->spawn();
|
|
331 task_create_pp->spawn();
|
|
332 task->spawn();
|
54
|
333
|
|
334 sgp_buff->swap_buffer();
|
48
|
335 }
|
42
|
336
|
48
|
337 void
|
|
338 Viewer::run_draw(void)
|
|
339 {
|
|
340 HTaskPtr task;
|
|
341 int fd;
|
|
342
|
54
|
343 PolygonPack *pp;
|
|
344
|
|
345 pp_buff->get_buffer(&pp);
|
|
346
|
|
347 polygon->draw(pp); // test draw of PolygonPack
|
|
348 SDL_BlitSurface(bitmap, NULL, screen, NULL);
|
48
|
349 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
42
|
350
|
54
|
351 frames++;
|
48
|
352
|
|
353 fd = manager->open("ViewerRunLoop");
|
|
354 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
355 //manager->spawn_task(task);
|
|
356 task->spawn();
|
42
|
357 }
|
|
358
|
|
359 void
|
48
|
360 Viewer::run_finish(void)
|
42
|
361 {
|
54
|
362 if (this_time != start_time) {
|
|
363 cout<< (((float)frames)/(this_time-start_time))*1000.0 << " FPS\n";
|
42
|
364 }
|
|
365
|
54
|
366 SDL_FreeSurface(bitmap);
|
42
|
367 delete [] pixels;
|
54
|
368 polygon->delete_data();
|
|
369 delete polygon;
|
|
370 delete sgp_buff;
|
|
371 delete pp_buff;
|
42
|
372 quit();
|
|
373
|
|
374 delete arg;
|
|
375 }
|
|
376
|
28
|
377 void
|
|
378 Viewer::zRow_init()
|
|
379 {
|
|
380 for (int i = 0; i < width; i++) {
|
|
381 for (int j = 0; j < height; j++) {
|
|
382 zRow[i][j] = 65535;
|
26
|
383 }
|
28
|
384 }
|
26
|
385 }
|