Multimedia Page 4 - Basic Lighting in OpenGL and SDL Game Programming |
The modifications to the framework will provide lighting to the framework. The changes need to be done in the following function: CreateWindowGL – Here the code for initializing the lighting and defining the light source will be added. Let us get started. The following is the existing code: 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) } In the above code, we will add the statements to initialize and define the lighting and light source. 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 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 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) } That’s it. That completes implementing code for adding basic lighting. However, this discussion has left some questions open. These questions include the effect of lighting on material, how the rotation of an object will affect the lighting, and so forth. These will be topic of discussion in the next part. Till then…
blog comments powered by Disqus |
|
|
|
|
|
|
|