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