Open Source Web Development Tutorials - Dev Shed
OpenGLでの基本的なライティング
(2009/02/24公開)
実際のライティング
フレームワークに変更を加え、ライティングを追加します。次の関数に変更を加える必要があります。
CreateWindowGL
ライティングの初期化と光源の定義とを行うコードを付け加えます。既存のコードは次のとおりです。
bool CreateWindowGL(int W, int H, int B, Uint32 F)
// This Code Creates Our OpenGL Window
{
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
// In order to use SDL_OPENGLBLIT we have to
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
// set GL attributes first
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
// colors and double buffering
if(!(Screen = SDL_SetVideoMode(W, H, B, F)))
// SDL_SetVideoMode to create the window
{
return false; // if it fails, return false
}
SDL_FillRect(Screen, NULL, SDL_MapRGBA(Screen->format,0,0,0,0));
ReshapeGL(SCREEN_W, SCREEN_H);
// calling reshape as the window is created
return true; // return true (initialization successful)
}
上のコードに、ライティングの初期化と光源の定義を行うステートメントを追加します。
bool CreateWindowGL(int W, int H, int B, Uint32 F)
// This Code Creates Our OpenGL Window
{
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
// In order to use SDL_OPENGLBLIT we have to
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
// set GL attributes first
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );// colors and double
//buffering
glEnable(GL_LIGHTING);//enable lighting
GLfloat specular[] = {1.0f, 1.0f, 1.0f , 1.0f};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular); //create a light source
glEnable(GL_LIGHT0); //enable the light source
Glfloat global_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);//set the
//global lighting model
if(!(Screen = SDL_SetVideoMode(W, H, B, F)))
// SDL_SetVideoMode to create the window
{
return false; // if it fails, return false
}
SDL_FillRect(Screen, NULL, SDL_MapRGBA(Screen->format,0,0,0,0));
ReshapeGL(SCREEN_W, SCREEN_H);
// calling reshape as the window is created
return true; // return true (initialization successful)
}
これで完了です。基本的なライティングを追加するコードの実装は以上です。ただし、この解説ではいくつかの問題点について触れませんでした。例えば、材質のライティングへの影響や、オブジェクトの回転がライティングに及ぼす影響などです。これらの点については、いつかまた別の記事で解説します。ではまた・・・。
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








