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