Home arrow Multimedia arrow Page 4 - Basic Lighting in OpenGL and SDL Game Programming

Lighting in the Real World - Multimedia

Lighting is one of the components of a game that can provide an immersive reality to the player -- or be so distracting that playing becomes difficult. To ease working with lighting, OpenGL provides simplified API. The focus of this discussion will be on the basics of using lighting API provided by OpenGL. This article is the latest part in a multi-part series on game programming with OpenGL and SDL.

TABLE OF CONTENTS:
  1. Basic Lighting in OpenGL and SDL Game Programming
  2. Implementing Lighting, Step by Step
  3. Lighting Model
  4. Lighting in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 3
December 09, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

//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)

}


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…



 
 
>>> More Multimedia Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MULTIMEDIA ARTICLES

- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: