Multimedia
  Home arrow Multimedia arrow Page 2 - Basic Lighting in OpenGL and SDL Game Programming
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MULTIMEDIA

Basic Lighting in OpenGL and SDL Game Programming
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2008-12-09


    Table of Contents:
  • Basic Lighting in OpenGL and SDL Game Programming
  • Implementing Lighting, Step by Step
  • Lighting Model
  • Lighting in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Basic Lighting in OpenGL and SDL Game Programming - Implementing Lighting, Step by Step
    ( Page 2 of 4 )

    In order to use the lighting API in OpenGL, certain steps need to be followed. These steps change according to the requirements. The requirements can include material, colors etc. However, for this discussion, the focus is on simple lighting, so the steps we will discuss are rudimentary. One point to keep in mind, however, is that the rudimentary steps remain unchanged even if the requirements change. Without further delay, here are the required steps:

    1. Enable lighting.

    2. Define normal vectors for each vertex.

    3. Create one or more light sources.

    4. Choosing a lighting model.

    Of these, the third and fourth steps have multiple sub-steps. Here are the details.

    Enable lighting

    OpenGL can be compared to a state machine. Using that analogy, lighting is also one of its states. Hence, lighting needs to be enabled. To enable lighting, the glEnable() function needs to be called with GL_LIGHTING as the parameter. Before performing any other operation regarding illumination, lighting should be enabled. In code the statement will be

    glEnable(GL_LIGHTING);


    Define normal vectors for each vertex

    It is necessary to define and determine the orientation of light with respect to an object, normal to the vertices of the object. OpenGL does not do normal vector calculations automatically. The calculation of normal vectors will be covered in the future.

    The calculated normal vectors need to be passed to OpenGL. To do so, any variation of glNormal function can be used. However, if no normal vectors are passed, then OpenGL takes (0, 0, 0, 0) as the normal vector.

    Create one or more light sources

    Creating one or more light sources can be sub-divided into the following two steps:

    1.  Defining a light source: In OpenGL one can define up to eight light sources. The names of these light sources are of the format GL_LIGHTn where n stands for numbers from 0 to 7. To define a light source, variants of glLight*() are used. Basically, it takes the light source number, the type of light and the values that will control the light (especially the color). The color is defined as RGBA i.e. Red, Green, Blue and the Alpha. For example, to define a light source with the source number as 0, the type as specular and the value as an array of floats, the statements will be

    GLfloat specular[] = {1.0f, 1.0f, 1.0f , 1.0f};

    glLightfv(GL_LIGHT0, GL_SPECULAR, specular); 

    2.  Enabling the light source: The next step is to enable the light source that has just been defined. To do so, one needs to use glEnable and pass the light source number as a parameter. By default, all of the light sources are disabled. Passing the light source number to glEnable(), enables the light source. For example, the following statement enables the light source that has 0 as its number:

    glEnable(GL_LIGHT0); 



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

       

    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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek