Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/viewerGL.cc @ 1039:b9cd0c88b69a
AllExecute speparation.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 07 Dec 2010 23:59:46 +0900 |
parents | 7fafe412c28f |
children |
rev | line source |
---|---|
922 | 1 #include "viewerGL.h" |
2 | |
3 | |
4 static void | |
5 ApplyMatrix(float *v, float *m) | |
6 { | |
944 | 7 float t[4]; |
922 | 8 |
944 | 9 t[0] = v[0]; |
10 t[1] = v[1]; | |
11 t[2] = v[2]; | |
12 t[3] = v[3]; | |
922 | 13 |
944 | 14 for (int i = 0; i < 4; i++) { |
15 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; | |
16 } | |
922 | 17 } |
1039
b9cd0c88b69a
AllExecute speparation.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1031
diff
changeset
|
18 #if 0 |
922 | 19 static void |
20 ApplyNormalMatrix(float *v, float *m) | |
21 { | |
944 | 22 float t[4]; |
922 | 23 |
944 | 24 t[0] = v[0]; |
25 t[1] = v[1]; | |
26 t[2] = v[2]; | |
922 | 27 |
944 | 28 for (int i = 0; i < 3; i++) { |
29 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8]; | |
30 } | |
922 | 31 } |
1039
b9cd0c88b69a
AllExecute speparation.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1031
diff
changeset
|
32 #endif |
922 | 33 |
944 | 34 ViewerGL::ViewerGL(TaskManager *m, int b, int w, int h, int _num) |
922 | 35 { |
944 | 36 spe_num = _num; |
37 manager = m; | |
922 | 38 |
944 | 39 quit_flag = false; |
40 start_time = 0; | |
41 this_time = 0; | |
42 frames = 0; | |
927
651251d56f36
remove garbage codes from viewerGL
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
922
diff
changeset
|
43 |
922 | 44 video_init(b, w, h); |
45 } | |
46 | |
47 void | |
944 | 48 ViewerGL::video_init(int bpp, int width, int height) |
922 | 49 { |
944 | 50 SDL_Surface *screen; |
51 int rgb_size[3]; | |
52 int value = 1; | |
53 Uint32 sdl_flag = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK; | |
54 Uint32 video_flag = SDL_OPENGL; | |
55 | |
56 if (SDL_Init(sdl_flag) < 0) { | |
57 fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError()); | |
58 exit(1); | |
922 | 59 } |
944 | 60 |
61 /* See if we should detect the display depth */ | |
62 if ( bpp == 0 ) | |
63 { | |
64 if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) | |
65 { | |
66 bpp = 8; | |
67 } | |
68 else | |
69 { | |
70 bpp = 32; | |
71 } | |
72 } | |
73 | |
74 /* Initialize the bpp */ | |
75 switch (bpp) | |
76 { | |
77 case 8: | |
78 rgb_size[0] = 3; | |
79 rgb_size[1] = 3; | |
80 rgb_size[2] = 2; | |
81 break; | |
82 case 15: | |
83 case 16: | |
84 rgb_size[0] = 5; | |
85 rgb_size[1] = 5; | |
86 rgb_size[2] = 5; | |
87 break; | |
88 default: | |
89 rgb_size[0] = 8; | |
90 rgb_size[1] = 8; | |
91 rgb_size[2] = 8; | |
92 break; | |
93 } | |
94 | |
95 screen = SDL_SetVideoMode(width, height, bpp, video_flag); | |
96 if (screen == NULL) { | |
97 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError()); | |
98 SDL_Quit(); | |
99 exit(1); | |
100 } | |
101 this->width = screen->w; | |
102 this->height = screen->h; | |
103 this->bpp = screen->format->BitsPerPixel; | |
104 | |
105 //各パラメータがちゃんと取れているか確認 | |
106 printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel); | |
107 printf("\n"); | |
108 printf( "Vendor : %s\n", glGetString( GL_VENDOR ) ); | |
109 printf( "Renderer : %s\n", glGetString( GL_RENDERER ) ); | |
110 printf( "Version : %s\n", glGetString( GL_VERSION ) ); | |
111 printf( "Extensions : %s\n", glGetString( GL_EXTENSIONS ) ); | |
112 printf("\n"); | |
113 | |
114 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value ); | |
115 printf( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value); | |
116 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value ); | |
117 printf( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value); | |
118 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value ); | |
119 printf( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value); | |
120 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value ); | |
121 printf( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value ); | |
122 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value ); | |
123 printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value ); | |
124 | |
125 //OpenGLの設定 | |
126 glViewport( 0, 0, width, height ); | |
127 glMatrixMode( GL_PROJECTION ); | |
128 glLoadIdentity( ); | |
129 | |
130 //正射影 | |
1031 | 131 //glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::near, OPENGL_PARAM::far ); |
132 glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::far, OPENGL_PARAM::near ); | |
944 | 133 |
134 glMatrixMode( GL_MODELVIEW ); | |
135 glLoadIdentity( ); | |
136 | |
137 //アルファブレンディング | |
138 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
139 | |
140 //光源 | |
141 glLightfv(GL_LIGHT0, GL_AMBIENT, OPENGL_PARAM::lightAmbient); | |
142 glLightfv(GL_LIGHT0, GL_DIFFUSE, OPENGL_PARAM::lightDiffuse); | |
143 glLightfv(GL_LIGHT0, GL_SPECULAR, OPENGL_PARAM::lightSpecular); | |
144 glLightfv(GL_LIGHT0, GL_POSITION, OPENGL_PARAM::lightPosition); | |
145 | |
960
6945fb810ce3
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
146 glEnable(GL_DEPTH_TEST); |
6945fb810ce3
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
147 glEnable(GL_ALPHA_TEST); |
6945fb810ce3
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
148 glAlphaFunc(GL_GREATER, 0); |
944 | 149 glDepthFunc(GL_LESS); |
1031 | 150 //glShadeModel(GL_SMOOTH); |
922 | 151 } |
152 | |
153 void | |
944 | 154 ViewerGL::mainLoop() |
922 | 155 { |
944 | 156 glEnable(GL_LIGHTING); |
157 glEnable(GL_LIGHT0); | |
158 glEnable(GL_BLEND); | |
159 | |
160 while(!quit_flag) { | |
161 run_loop(); | |
162 } | |
922 | 163 } |
164 | |
165 void | |
944 | 166 ViewerGL::run_loop() |
922 | 167 { |
944 | 168 clear_screen(); |
922 | 169 |
944 | 170 quit_flag = quit_check(); |
171 if (quit_flag == true) { | |
172 this_time = get_ticks(); | |
173 run_finish(); | |
174 return; | |
175 } | |
176 | |
177 clean_pixels(); | |
178 | |
179 sgroot->updateControllerState(); | |
180 sgroot->allExecute(width, height); | |
181 light_xyz_stock = sgroot->getLightVector(); | |
182 light_switch_stock = sgroot->getLightSwitch(); | |
183 light_sysswitch_stock = sgroot->getLightSysSwitch(); | |
184 pickup_vertex(); | |
185 | |
186 psx_sync_n(); | |
187 frames++; | |
922 | 188 } |
189 | |
190 void | |
944 | 191 ViewerGL::pickup_vertex() |
922 | 192 { |
944 | 193 float xyz1[4], xyz2[4], xyz3[4]; |
194 float tex_xy1[2], tex_xy2[2], tex_xy3[2]; | |
195 float normal1[4],normal2[4],normal3[4]; | |
196 GLuint texture; | |
197 | |
198 SceneGraphPtr sg_top = sgroot->getDrawSceneGraph(); | |
199 SceneGraphPtr sg = sg_top; | |
200 | |
201 while (sg) { | |
202 if (sg->flag_drawable) { | |
203 if (!sg->texture_info.gl_tex) { | |
204 sg->texture_info.gl_tex = SDL_GL_LoadTexture(sg->texture_info.texture_image); | |
205 } | |
206 texture = sg->texture_info.gl_tex; | |
207 glBindTexture(GL_TEXTURE_2D, texture); | |
922 | 208 |
944 | 209 glEnable(GL_TEXTURE_2D); |
210 glBegin( GL_TRIANGLES); | |
211 for (int i = 0; i < sg->size; i += 3) { | |
212 xyz1[0] = sg->coord_xyz[(i+0)*3]; | |
213 xyz1[1] = sg->coord_xyz[(i+0)*3+1]; | |
214 xyz1[2] = sg->coord_xyz[(i+0)*3+2]*-1.0f; | |
1031 | 215 //xyz1[2] = sg->coord_xyz[(i+0)*3+2]; |
944 | 216 xyz1[3] = 1.0f; |
217 | |
218 xyz2[0] = sg->coord_xyz[(i+1)*3]; | |
219 xyz2[1] = sg->coord_xyz[(i+1)*3+1]; | |
220 xyz2[2] = sg->coord_xyz[(i+1)*3+2]*-1.0f; | |
1031 | 221 //xyz2[2] = sg->coord_xyz[(i+1)*3+2]; |
944 | 222 xyz2[3] = 1.0f; |
223 | |
224 xyz3[0] = sg->coord_xyz[(i+2)*3]; | |
225 xyz3[1] = sg->coord_xyz[(i+2)*3+1]; | |
226 xyz3[2] = sg->coord_xyz[(i+2)*3+2]*-1.0f; | |
1031 | 227 //xyz3[2] = sg->coord_xyz[(i+2)*3+2]; |
944 | 228 xyz3[3] = 1.0f; |
229 | |
230 // sg->matrix = 回転行列*透視変換行列 | |
231 ApplyMatrix(xyz1, sg->matrix); | |
232 ApplyMatrix(xyz2, sg->matrix); | |
233 ApplyMatrix(xyz3, sg->matrix); | |
234 | |
1031 | 235 |
944 | 236 xyz1[0] /= xyz1[2]; |
237 xyz1[1] /= xyz1[2]; | |
238 xyz2[0] /= xyz2[2]; | |
239 xyz2[1] /= xyz2[2]; | |
240 xyz3[0] /= xyz3[2]; | |
241 xyz3[1] /= xyz3[2]; | |
1031 | 242 |
944 | 243 tex_xy1[0] = sg->coord_tex[(i+0)*3]; |
244 tex_xy1[1] = sg->coord_tex[(i+0)*3+1]; | |
245 tex_xy2[0] = sg->coord_tex[(i+1)*3]; | |
246 tex_xy2[1] = sg->coord_tex[(i+1)*3+1]; | |
247 tex_xy3[0] = sg->coord_tex[(i+2)*3]; | |
248 tex_xy3[1] = sg->coord_tex[(i+2)*3+1]; | |
1031 | 249 |
944 | 250 normal1[0] = sg->normal[(i+0)*3]; |
251 normal1[1] = sg->normal[(i+0)*3+1]; | |
252 normal1[2] = sg->normal[(i+0)*3+2]*-1.0f; | |
1031 | 253 //normal1[2] = sg->normal[(i+0)*3+2]; |
944 | 254 normal1[3] = 0.0f; |
255 | |
256 normal2[0] = sg->normal[(i+1)*3]; | |
257 normal2[1] = sg->normal[(i+1)*3+1]; | |
258 normal2[2] = sg->normal[(i+1)*3+2]*-1.0f; | |
1031 | 259 //normal2[2] = sg->normal[(i+1)*3+2]; |
944 | 260 normal2[3] = 0.0f; |
261 | |
262 normal3[0] = sg->normal[(i+2)*3]; | |
263 normal3[1] = sg->normal[(i+2)*3+1]; | |
264 normal3[2] = sg->normal[(i+2)*3+2]*-1.0f; | |
1031 | 265 //normal3[2] = sg->normal[(i+2)*3+2]; |
944 | 266 normal3[3] = 0.0f; |
267 | |
1031 | 268 ApplyMatrix(normal1,sg->real_matrix); |
269 ApplyMatrix(normal2,sg->real_matrix); | |
270 ApplyMatrix(normal3,sg->real_matrix); | |
944 | 271 |
272 obj_draw(xyz1, tex_xy1, normal1); | |
273 obj_draw(xyz2, tex_xy2, normal2); | |
274 obj_draw(xyz3, tex_xy3, normal3); | |
275 } | |
276 glEnd( ); | |
277 glDisable(GL_TEXTURE_2D); | |
922 | 278 } |
944 | 279 |
280 if (sg->children != NULL) { | |
281 sg = sg->children; | |
282 } else if (sg->brother != NULL) { | |
283 sg = sg->brother; | |
284 } else { | |
285 while (sg) { | |
286 if (sg->brother != NULL) { | |
287 sg = sg->brother; | |
922 | 288 break; |
289 } else { | |
944 | 290 if (sg->parent == NULL) { |
291 sg = NULL; | |
292 break; | |
293 } else { | |
294 sg = sg->parent; | |
295 } | |
922 | 296 } |
297 } | |
298 } | |
299 } | |
300 } | |
301 | |
302 void | |
944 | 303 ViewerGL::obj_draw(float *xyz, float *tex_xyz, float *normal_xyz) |
922 | 304 { |
1031 | 305 |
306 | |
944 | 307 glTexCoord2f(tex_xyz[0], tex_xyz[1]); |
308 glVertex3f(xyz[0], xyz[1], xyz[2]); | |
309 glNormal3f(normal_xyz[0], normal_xyz[1], normal_xyz[2]); | |
922 | 310 } |
311 | |
312 void | |
944 | 313 ViewerGL::clean_pixels() |
922 | 314 { |
944 | 315 glClearColor( 0.0, 0.0, 0.0, 1.0 ); |
316 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
922 | 317 } |
318 | |
319 void | |
944 | 320 ViewerGL::clear_screen() |
922 | 321 { |
944 | 322 GLenum gl_error; |
323 char* sdl_error; | |
324 | |
325 SDL_GL_SwapBuffers( ); | |
326 | |
327 /* Check for error conditions. */ | |
328 gl_error = glGetError( ); | |
329 | |
330 if( gl_error != GL_NO_ERROR ) | |
331 { | |
332 fprintf( stderr, "OpenGL error: %d\n", gl_error ); | |
333 } | |
334 | |
335 sdl_error = SDL_GetError( ); | |
336 | |
337 if( sdl_error[0] != '\0' ) | |
338 { | |
339 fprintf(stderr, "SDL error '%s'\n", sdl_error); | |
340 SDL_ClearError(); | |
341 } | |
922 | 342 } |
343 | |
344 void | |
944 | 345 ViewerGL::run_finish() |
922 | 346 { |
960
6945fb810ce3
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
347 glDisable(GL_ALPHA_TEST); |
6945fb810ce3
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
348 glDisable(GL_DEPTH_TEST); |
944 | 349 glDisable(GL_BLEND); |
350 glDisable(GL_LIGHT0); | |
351 glDisable(GL_LIGHTING); | |
352 | |
353 if (this_time != start_time) { | |
354 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); | |
355 } | |
356 | |
357 delete sgroot; | |
358 quit(); | |
922 | 359 } |
360 | |
361 /* end */ |