comparison Renderer/Engine/viewerGL.cc @ 931:660e9190c40c

Add Lighting to GL mode
author koba <koba@cr.ie.u-ryukyu.ac.jp>
date Sat, 31 Jul 2010 00:22:26 +0900
parents 35efda39c2d9
children b2f7d2ad3804
comparison
equal deleted inserted replaced
930:35efda39c2d9 931:660e9190c40c
44 } 44 }
45 45
46 void 46 void
47 cViewerGL::video_init(int bpp, int width, int height) 47 cViewerGL::video_init(int bpp, int width, int height)
48 { 48 {
49 SDL_Surface *screen;
49 int rgb_size[3]; 50 int rgb_size[3];
51 int value = 1;
50 Uint32 sdl_flag = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK; 52 Uint32 sdl_flag = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK;
51 Uint32 video_flag = SDL_OPENGL; 53 Uint32 video_flag = SDL_OPENGL;
52 int value = 1;
53 54
54 if (SDL_Init(sdl_flag) < 0) { 55 if (SDL_Init(sdl_flag) < 0) {
55 fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError()); 56 fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
56 exit(1); 57 exit(1);
57 } 58 }
123 //OpenGLの設定 124 //OpenGLの設定
124 glViewport( 0, 0, width, height ); 125 glViewport( 0, 0, width, height );
125 glMatrixMode( GL_PROJECTION ); 126 glMatrixMode( GL_PROJECTION );
126 glLoadIdentity( ); 127 glLoadIdentity( );
127 128
129 //正射影
128 glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::near, OPENGL_PARAM::far ); 130 glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::near, OPENGL_PARAM::far );
129 131
130 glMatrixMode( GL_MODELVIEW ); 132 glMatrixMode( GL_MODELVIEW );
131 glLoadIdentity( ); 133 glLoadIdentity( );
134
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);
132 143
133 glEnable(GL_DEPTH_TEST); 144 glEnable(GL_DEPTH_TEST);
134 glDepthFunc(GL_LESS); 145 glDepthFunc(GL_LESS);
135 glShadeModel(GL_SMOOTH); 146 glShadeModel(GL_SMOOTH);
136 } 147 }
137 148
138 void 149 void
139 cViewerGL::clean_pixels() 150 cViewerGL::mainLoop()
140 { 151 {
141 glClearColor( 0.0, 0.0, 0.0, 1.0 ); 152 glEnable(GL_LIGHTING);
142 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 153 glEnable(GL_LIGHT0);
143 } 154 glEnable(GL_BLEND);
144 155
145 void 156 while(!quit_flag) {
146 cViewerGL::clear_screen() 157 run_loop();
147 { 158 }
148 GLenum gl_error; 159
149 char* sdl_error; 160
150 161 }
151 SDL_GL_SwapBuffers( ); 162
152 163 void
153 /* Check for error conditions. */ 164 cViewerGL::run_loop()
154 gl_error = glGetError( ); 165 {
155 166 clear_screen();
156 if( gl_error != GL_NO_ERROR ) 167
157 { 168 quit_flag = quit_check();
158 fprintf( stderr, "OpenGL error: %d\n", gl_error ); 169 if (quit_flag == true) {
159 } 170 this_time = get_ticks();
160 171 run_finish();
161 sdl_error = SDL_GetError( ); 172 return;
162 173 }
163 if( sdl_error[0] != '\0' ) 174
164 { 175 clean_pixels();
165 fprintf(stderr, "SDL error '%s'\n", sdl_error); 176
166 SDL_ClearError(); 177 sgroot->updateControllerState();
167 } 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++;
168 } 186 }
169 187
170 void 188 void
171 cViewerGL::pickup_vertex() 189 cViewerGL::pickup_vertex()
172 { 190 {
180 198
181 while (sg) { 199 while (sg) {
182 if (sg->flag_drawable) { 200 if (sg->flag_drawable) {
183 texture = sg->gl_tex; 201 texture = sg->gl_tex;
184 glBindTexture(GL_TEXTURE_2D, texture); 202 glBindTexture(GL_TEXTURE_2D, texture);
185 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
186 203
187 glEnable(GL_TEXTURE_2D); 204 glEnable(GL_TEXTURE_2D);
188 glEnable(GL_BLEND);
189 glBegin( GL_TRIANGLES); 205 glBegin( GL_TRIANGLES);
190 for (int i = 0; i < sg->size; i += 3) { 206 for (int i = 0; i < sg->size; i += 3) {
191 xyz1[0] = sg->coord_xyz[(i+0)*3]; 207 xyz1[0] = sg->coord_xyz[(i+0)*3];
192 xyz1[1] = sg->coord_xyz[(i+0)*3+1]; 208 xyz1[1] = sg->coord_xyz[(i+0)*3+1];
193 xyz1[2] = sg->coord_xyz[(i+0)*3+2]*-1.0f; 209 xyz1[2] = sg->coord_xyz[(i+0)*3+2]*-1.0f;
253 obj_draw(xyz1, tex_xy1, normal1); 269 obj_draw(xyz1, tex_xy1, normal1);
254 obj_draw(xyz2, tex_xy2, normal2); 270 obj_draw(xyz2, tex_xy2, normal2);
255 obj_draw(xyz3, tex_xy3, normal3); 271 obj_draw(xyz3, tex_xy3, normal3);
256 } 272 }
257 glEnd( ); 273 glEnd( );
258 glDisable(GL_BLEND);
259 glDisable(GL_TEXTURE_2D); 274 glDisable(GL_TEXTURE_2D);
260 } 275 }
261 276
262 if (sg->children != NULL) { 277 if (sg->children != NULL) {
263 sg = sg->children; 278 sg = sg->children;
276 sg = sg->parent; 291 sg = sg->parent;
277 } 292 }
278 } 293 }
279 } 294 }
280 } 295 }
281
282 } 296 }
283 } 297 }
284 298
285 void 299 void
286 cViewerGL::obj_draw(float *xyz, float *tex_xyz, float *normal_xyz) 300 cViewerGL::obj_draw(float *xyz, float *tex_xyz, float *normal_xyz)
289 glVertex3f(xyz[0], xyz[1], xyz[2]); 303 glVertex3f(xyz[0], xyz[1], xyz[2]);
290 glNormal3f(normal_xyz[0], normal_xyz[1], normal_xyz[2]); 304 glNormal3f(normal_xyz[0], normal_xyz[1], normal_xyz[2]);
291 } 305 }
292 306
293 void 307 void
294 cViewerGL::mainLoop() 308 cViewerGL::clean_pixels()
295 { 309 {
296 while(!quit_flag) { 310 glClearColor( 0.0, 0.0, 0.0, 1.0 );
297 run_loop(); 311 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
298 } 312 }
299 } 313
300 314 void
301 void 315 cViewerGL::clear_screen()
302 cViewerGL::run_loop() 316 {
303 { 317 GLenum gl_error;
304 clear_screen(); 318 char* sdl_error;
305 319
306 quit_flag = quit_check(); 320 SDL_GL_SwapBuffers( );
307 if (quit_flag == true) { 321
308 this_time = get_ticks(); 322 /* Check for error conditions. */
309 run_finish(); 323 gl_error = glGetError( );
310 return; 324
311 } 325 if( gl_error != GL_NO_ERROR )
312 326 {
313 clean_pixels(); 327 fprintf( stderr, "OpenGL error: %d\n", gl_error );
314 328 }
315 sgroot->updateControllerState(); 329
316 sgroot->allExecute(width, height); 330 sdl_error = SDL_GetError( );
317 light_xyz_stock = sgroot->getLightVector(); 331
318 light_switch_stock = sgroot->getLightSwitch(); 332 if( sdl_error[0] != '\0' )
319 light_sysswitch_stock = sgroot->getLightSysSwitch(); 333 {
320 pickup_vertex(); 334 fprintf(stderr, "SDL error '%s'\n", sdl_error);
321 335 SDL_ClearError();
322 psx_sync_n(); 336 }
323 frames++;
324 } 337 }
325 338
326 void 339 void
327 cViewerGL::run_finish() 340 cViewerGL::run_finish()
328 { 341 {
342 glDisable(GL_BLEND);
343 glDisable(GL_LIGHT0);
344 glDisable(GL_LIGHTING);
345
329 if (this_time != start_time) { 346 if (this_time != start_time) {
330 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); 347 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0);
331 } 348 }
332 349
333 delete sgroot; 350 delete sgroot;