changeset 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 736c03c301d4
files Renderer/Engine/viewerGL.cc Renderer/Engine/viewerGL.h
diffstat 2 files changed, 84 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/viewerGL.cc	Fri Jul 30 21:46:04 2010 +0900
+++ b/Renderer/Engine/viewerGL.cc	Sat Jul 31 00:22:26 2010 +0900
@@ -46,10 +46,11 @@
 void
 cViewerGL::video_init(int bpp, int width, int height)
 {
+  SDL_Surface *screen;
   int rgb_size[3];
+  int value = 1;
   Uint32 sdl_flag = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK;
   Uint32 video_flag = SDL_OPENGL;
-  int value = 1;
 
   if (SDL_Init(sdl_flag) < 0) {
     fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
@@ -125,46 +126,63 @@
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity( );
 
+  //正射影
   glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::near, OPENGL_PARAM::far );
 
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity( );
 
+  //アルファブレンディング
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+  //光源
+  glLightfv(GL_LIGHT0, GL_AMBIENT, OPENGL_PARAM::lightAmbient);
+  glLightfv(GL_LIGHT0, GL_DIFFUSE, OPENGL_PARAM::lightDiffuse);
+  glLightfv(GL_LIGHT0, GL_SPECULAR, OPENGL_PARAM::lightSpecular);
+  glLightfv(GL_LIGHT0, GL_POSITION, OPENGL_PARAM::lightPosition);
+
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LESS);
   glShadeModel(GL_SMOOTH);
 }
 
 void
-cViewerGL::clean_pixels()
+cViewerGL::mainLoop()
 {
-  glClearColor( 0.0, 0.0, 0.0, 1.0 );
-  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  glEnable(GL_LIGHTING);
+  glEnable(GL_LIGHT0);
+  glEnable(GL_BLEND);
+  
+  while(!quit_flag) {
+    run_loop();
+  }
+
+  
 }
 
 void
-cViewerGL::clear_screen()
+cViewerGL::run_loop()
 {
-  GLenum gl_error;
-  char* sdl_error;
+  clear_screen();
 
-  SDL_GL_SwapBuffers( );
-
-  /* Check for error conditions. */
-  gl_error = glGetError( );
+  quit_flag = quit_check();
+  if (quit_flag == true) {
+    this_time = get_ticks();
+    run_finish();
+    return;
+  }
 
-  if( gl_error != GL_NO_ERROR )
-    {
-      fprintf( stderr, "OpenGL error: %d\n", gl_error );
-    }
+  clean_pixels();
 
-  sdl_error = SDL_GetError( );
-
-  if( sdl_error[0] != '\0' )
-    {
-      fprintf(stderr, "SDL error '%s'\n", sdl_error);
-      SDL_ClearError();
-    }
+  sgroot->updateControllerState();
+  sgroot->allExecute(width, height);
+  light_xyz_stock = sgroot->getLightVector();
+  light_switch_stock = sgroot->getLightSwitch();
+  light_sysswitch_stock = sgroot->getLightSysSwitch();
+  pickup_vertex();
+  
+  psx_sync_n();
+  frames++;
 }
 
 void
@@ -182,10 +200,8 @@
     if (sg->flag_drawable) {
       texture = sg->gl_tex;
       glBindTexture(GL_TEXTURE_2D, texture);
-      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
       glEnable(GL_TEXTURE_2D);
-      glEnable(GL_BLEND);
       glBegin( GL_TRIANGLES);
       for (int i = 0; i < sg->size; i += 3) {
 	xyz1[0] = sg->coord_xyz[(i+0)*3];
@@ -255,7 +271,6 @@
 	obj_draw(xyz3, tex_xy3, normal3);
       }
       glEnd( );
-      glDisable(GL_BLEND);
       glDisable(GL_TEXTURE_2D);
     }
    
@@ -278,7 +293,6 @@
 	}
       }
     }
-
   }
 }
 
@@ -291,41 +305,44 @@
 }
 
 void
-cViewerGL::mainLoop()
+cViewerGL::clean_pixels()
 {
-  while(!quit_flag) {
-    run_loop();
-  }
+  glClearColor( 0.0, 0.0, 0.0, 1.0 );
+  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
 void
-cViewerGL::run_loop()
+cViewerGL::clear_screen()
 {
-  clear_screen();
+  GLenum gl_error;
+  char* sdl_error;
 
-  quit_flag = quit_check();
-  if (quit_flag == true) {
-    this_time = get_ticks();
-    run_finish();
-    return;
-  }
+  SDL_GL_SwapBuffers( );
+
+  /* Check for error conditions. */
+  gl_error = glGetError( );
 
-  clean_pixels();
+  if( gl_error != GL_NO_ERROR )
+    {
+      fprintf( stderr, "OpenGL error: %d\n", gl_error );
+    }
 
-  sgroot->updateControllerState();
-  sgroot->allExecute(width, height);
-  light_xyz_stock = sgroot->getLightVector();
-  light_switch_stock = sgroot->getLightSwitch();
-  light_sysswitch_stock = sgroot->getLightSysSwitch();
-  pickup_vertex();
-  
-  psx_sync_n();
-  frames++;
+  sdl_error = SDL_GetError( );
+
+  if( sdl_error[0] != '\0' )
+    {
+      fprintf(stderr, "SDL error '%s'\n", sdl_error);
+      SDL_ClearError();
+    }
 }
 
 void
 cViewerGL::run_finish()
 {
+  glDisable(GL_BLEND);
+  glDisable(GL_LIGHT0);
+  glDisable(GL_LIGHTING);
+
   if (this_time != start_time) {
     printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0);
   }
--- a/Renderer/Engine/viewerGL.h	Fri Jul 30 21:46:04 2010 +0900
+++ b/Renderer/Engine/viewerGL.h	Sat Jul 31 00:22:26 2010 +0900
@@ -23,8 +23,12 @@
 
 
 namespace OPENGL_PARAM {
-  static const double near = -500.0;
-  static const double far = 0.0;  
+  static const double near = -1000.0;
+  static const double far = 1000.0;
+  static const GLfloat lightAmbient[] = {0.25f, 0.25f, 0.25f};
+  static const GLfloat lightDiffuse[] = {1.0f, 1.0f, 1.0f};
+  static const GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f};
+  static const GLfloat lightPosition[] = {0.5f, 0.5f, 1.0f, 0.0f};
 }
 
 class cViewerGL : public Viewer {
@@ -32,27 +36,25 @@
   cViewerGL(TaskManager *m, int b, int w, int h, int _num);
   ~cViewerGL(){};
   
-  SDL_Surface *screen;
-
-  void video_init(int bpp, int width, int height);
-  void clean_pixels();
-  void clear_screen();
-
-  /* override function */
-  void mainLoop();
-  void run_finish();
-
-  void run_loop();
-  void pickup_vertex();
-  void obj_draw(float *xyz, float *tex_xyz, float *normal_xyz);
-
  private:
   bool quit_flag;
   
   /* measure for FPS (Frame Per Second) */
   int start_time;
   int this_time;
-  int frames;    
+  int frames;
+  
+ public:
+  void video_init(int bpp, int width, int height);
+  void run_loop();
+  void pickup_vertex();
+  void obj_draw(float *xyz, float *tex_xyz, float *normal_xyz);
+  void clean_pixels();
+  void clear_screen();
+  
+  /* override function */
+  void mainLoop();
+  void run_finish();
 };
 
 #endif