Textur Game Engine Programming

Embed Size (px)

Citation preview

  • 8/13/2019 Textur Game Engine Programming

    1/30

    3D Game Engine ProgrammingHelping you build your dream game engine.

    http://3dgep.com/
  • 8/13/2019 Textur Game Engine Programming

    2/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 2

    Texture Properties

    GL_TEXTURE_MIN_FILTER

    GL_TEXTURE_MAG_FILTER

    GL_TEXTURE_WRAP

    Texture Environment

    GL_TEXTURE_ENV

    GL_TEXTURE_ENV_COLOR

    GL_TEXTURE_ENV_MODE

    GL_TEXTURE_FILTER_CONTROL

    The Basic Lighting Model

    The Emissive Term

    The Ambient Term

    The Light's Contribution

    Attenuation Factor

    Ambient

    Diffuse

    Specular

    Materials

    Lighting

    Putting it All Together

    Initialize OpenGL

    Loading Textures

    Creating a Sphere

    Global Ambient

    Enable Lighting

    Define a Light SourceDefining Materials

    Render the Scene

    View the Demo

    References

    Introduction

    Texturing an object is the process of mapping a 2D (or sometimes 3D) image onto a 3D

    model so that you can achieve much higher detail in the model without using a lot of vertices.

    Without textures the only way to acquire the level of detail that can be achieved using texture

    mapping would be to specify a different colored vertex for each pixel of the screen. As you can

    imagine, adding detail to a model in this way can be very inefficient.

    Lighting is required to set the mood for a scene. Lighting also provides a cue to the eye what

    the direction and shape of an object has. With the correct use of lighting we can make a

    scene dark and gloomy or bright and cheery.

    http://3dgep.com/?p=2417#Referenceshttp://3dgep.com/?p=2417#View_the_Demohttp://3dgep.com/?p=2417#Render_the_Scenehttp://3dgep.com/?p=2417#Defining_Materialshttp://3dgep.com/?p=2417#Define_a_Light_Sourcehttp://3dgep.com/?p=2417#Enable_Lightinghttp://3dgep.com/?p=2417#Global_Ambienthttp://3dgep.com/?p=2417#Creating_a_Spherehttp://3dgep.com/?p=2417#Loading_Textures_1http://3dgep.com/?p=2417#Initialize_OpenGLhttp://3dgep.com/?p=2417#Putting_it_All_Togetherhttp://3dgep.com/?p=2417#Lightinghttp://3dgep.com/?p=2417#Materialshttp://3dgep.com/?p=2417#Specularhttp://3dgep.com/?p=2417#Diffusehttp://3dgep.com/?p=2417#Ambienthttp://3dgep.com/?p=2417#Attenuation_Factorhttp://3dgep.com/?p=2417#The_Lights_Contributionhttp://3dgep.com/?p=2417#The_Ambient_Termhttp://3dgep.com/?p=2417#The_Emissive_Termhttp://3dgep.com/?p=2417#The_Basic_Lighting_Modelhttp://3dgep.com/?p=2417#GL_TEXTURE_FILTER_CONTROLhttp://3dgep.com/?p=2417#GL_TEXTURE_ENV_MODEhttp://3dgep.com/?p=2417#GL_TEXTURE_ENV_COLORhttp://3dgep.com/?p=2417#GL_TEXTURE_ENVhttp://3dgep.com/?p=2417#Texture_Environmenthttp://3dgep.com/?p=2417#GL_TEXTURE_WRAPhttp://3dgep.com/?p=2417#GL_TEXTURE_MAG_FILTERhttp://3dgep.com/?p=2417#GL_TEXTURE_MIN_FILTERhttp://3dgep.com/?p=2417#Texture_Properties
  • 8/13/2019 Textur Game Engine Programming

    3/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 3

    Materials are used to determine how the light interacts with the surface of the model. A model

    can appear very shiny or very dull depending on the material properties of the model.

    Dependencies

    For this article, I will rely on several 3rd party libraries to simplify programming.

    GLUT: GLUT is used to quickly setup a render window and to handle keyboard andmouse events. You can obtain pre-compiled GLUT libraries for Win32 here:

    http://user.xmission.com/~nate/glut.html .

    GLM: GLM is a math library that is designed with the OpenGL specification in mind.

    You can get the latest version of GLM from here: http://glm.g-truc.net/.

    SOIL: The Simple OpenGL Image Library is the easiest way I am currently aware of that

    to load textures into OpenGL. With a single method, you can load textures and get an

    OpenGL texture object back. You can get the latest version of SOIL here:

    http://www.lonesock.net/soil.html

    Texturing

    Texture mapping is the process of applying a 2D (or 3D) image to a 3D model. Textures can

    provide much more detail to the model that would otherwise only be possible by creating

    models with a huge number of vertices. In order to correctly map the texture to a model we

    must specify a texture coordinate to each vertex of the model. A texture coordinate can be

    either be a single scalar in the case of 1D textures (a single row of texels which may

    represent a gradient, opacity or weight), a 2-component value in the case of 2D textures, or a

    3-component value in the case of 3D textures or even a 4-component value in the case of

    projected textures. In this article I will only show applying 2D textures using 2-component

    texture coordinates.

    Computing the correct texture coordinate for a vertex can be a complex process and it usually

    the job of the 3D modeling artists to generate the correct texture coordinates in a 3D content

    creation program like 3D Studio Max, Maya, or Blender.

    The texture coordinate determines what texel is sampled from the texture. When dealing with

    2D textures, the axis along the width of the texture is usually referred to as the U(or s

    coordinate) axis and the axis along the height of the texture is usually referred to as the V(or

    tcoordinate) axis. For this reason, texture mapping can also be called UV mapping.

    Texture coordinates are usually normalized in the range 0 to 1 in each axis. Normalized

    texture coordinates are useful because we may not always know the size of the texture in

    advance (and when using MIP maps, the size of the texture will actually change depending on

    the view of the object being textured).

    The image below shows the texture coordinates and the corresponding axes that are used to

    http://www.blender.org/http://www.lonesock.net/soil.htmlhttp://glm.g-truc.net/http://user.xmission.com/~nate/glut.html
  • 8/13/2019 Textur Game Engine Programming

    4/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 4

    refer to the texels.

    Loading Textures

    Since there are so many different image formats, I will not go into much detail about how to

    load textures into memory. Instead, I will stick to the simple OpenGL image library (SOIL) for

    loading textures into graphics memory. SOIL is a free, open source image library that can

    load images from multiple image formats.

    To load a texture using SOIL requires very little code:

    Thats it. The textureObjectnow refers to a texture that is loaded in graphics memory and

    Texture Coordinates

    12

    GLuint textureObject = 0;textureObject = SOIL_load_OGL_texture( "path/to/file.JPG", SOIL_LOAD_AU

    http://www.lonesock.net/soil.htmlhttp://www.lonesock.net/soil.htmlhttp://3dgep.com/wp-content/uploads/2012/02/Texture-Coordinates.png
  • 8/13/2019 Textur Game Engine Programming

    5/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 5

    can be used to apply textures to models.

    This example shows loading a JPEG file from a mythical path, but SOIL has support for BMP,

    PNG, TGA, DDS, PSD, and HDR file types.

    The function shown here will load an image directly from disk and create an OpenGL texture

    object and return the ID of that texture object, ready to be used by your application. If an error

    occured while trying to load the texture (for example, the specified file was not found) this

    function will return 0.

    This function has the following signature:

    Where:

    const char *filename: Is the relative or absolute file path to the texture to be loaded.

    int force_channels: Specifies the format of image. This can be any one of the following

    values:

    SOIL_LOAD_AUTO: Leaves the image format in whatever format it was found.

    SOIL_LOAD_L: Forces the image to be loaded as a Luminous (grayscale) image.

    SOIL_LOAD_LA: Forces the image to be loaded as a Luminous image with an

    alpha channel.

    SOIL_LOAD_RGB: Forces the image to be loaded as a 3-component (Red,

    Green, Blue) image.

    SOIL_LOAD_RGBA: Forces the image to be loaded as a 4-component (Red,

    Green, Blue, Alpha) image.

    unsigned int reuse_texture_ID: Specify 0 or SOIL_CREATE_NEW_IDif you want

    SOIL to generate a new texture object ID, otherwise the texture ID specified will be

    reused replacing the existing texture at that ID.

    unsigned int flags: Can be a combination of the following flags:

    SOIL_FLAG_POWER_OF_TWO: Forces the size of the final image to be a power

    of 2.

    SOIL_FLAG_MIPMAPS: Tells SOIL to generate mipmaps for the texture.

    SOIL_FLAG_TEXTURE_REPEATS: Specifies that SOIL should set the texture to

    GL_REPEATin each dimension of the texture.

    SOIL_FLAG_MULTIPLY_ALPHA: Tells SOIL to pre-multiply the alpha value into

    the color channel of the resulting texture.

    SOIL_FLAG_INVERT_Y: Flips the image on the vertical axis.

    SOIL_FLAG_COMPRESS_TO_DXT: If the graphics card supports it, SOIL will

    123456

    7

    unsigned intSOIL_load_OGL_texture( constchar*filename, intforce_channels, unsigned intreuse_texture_ID, unsigned intflags

    );

  • 8/13/2019 Textur Game Engine Programming

    6/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 6

    convert RGB images to DXT1 and RGBA images to DXT5.

    SOIL_FLAG_DDS_LOAD_DIRECT: Specify this flag to load DDS textures directly

    without any additional processing. Using this flag will cause all other flags to be

    ignored (with the exception of SOIL_FLAG_TEXTURE_REPEATS).

    Mipmaps

    A mipmap(also called MIP maps) is a method to define smaller versions of a base

    image in a single file. Using mipmaps increases the amount of memory required to storethe base image alone by 33% but the benefit is increased rendering speeds and

    reducing aliasing effects.

    The base image has a mipmap index of 0 and each successive image in the mipmap

    array is generated by halving the width and height of the previous mipmap level. The final

    mipmap level is reached when the resulting texture is a single pixel in one or both

    dimensions of the texture.

    The image below shows a texture that has had the mipmap levels generated.

    The SOIL library can be used to generate the mipmap levels automatically if you supply

    the SOIL_FLAG_MIPMAPSflag to the load function.

    Texture Properties

    Using the SOIL function described above to load a texture can also be used to

    Mipmaps

    http://3dgep.com/wp-content/uploads/2012/02/mipmaps.pnghttp://en.wikipedia.org/wiki/Mipmap
  • 8/13/2019 Textur Game Engine Programming

    7/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 7

    automatically specify a few properties for the texture (for example, the texture wrap

    mode) but you can also modify the properties of the texture yourself after the texture is

    loaded.

    Texture properties are specified using the glTexParameterfamily of functions. Before

    you can modify the properties of a texture, you must first bind the texture to an

    appropriate texture target. To bind a texture to a texture target, you use the

    glBindTexturemethod.

    This method has the following signature:

    Where:

    GLenum target: Used to specify the texture target to bind the texture to. Valid

    values are:

    GL_TEXTURE_1D: A texture target that is used to bind to a 1D texture

    object.

    GL_TEXTURE_2D: A texture target that is used to bind to a 2D texture

    object.

    GL_TEXTURE_3D: A texture target that is used to bind to a 3D texture

    object.

    GL_TEXTURE_CUBE_MAP: A texture target that is used to bind a cube

    map texture.

    GLuint texture

    : Specifies the texture object ID to bind to the current target.

    After a texture object is bound to a texture target several functions can be used to read

    from the texture, or write to the texture, or modify the properties of the texture.

    The texture can be unbound by binding the default texture object ID of 0 to the

    appropriate texture target.

    How a texel is fetched from a texture resource is often dependent on the properties that

    are associated with the texture object. Textures have several properties that can be

    modified using the glTexParameterfamily of functions.

    The glTexParameterfunction has the following signature:

    Where:

    1 voidglBindTexture (GLenum target, GLuint texture);

    1 voidglTexParameter[f|i]{v}(GLenum target, GLenum pname, [GLfloat|

  • 8/13/2019 Textur Game Engine Programming

    8/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 8

    GLenum target: Specify the texture target. This can be any of the targets

    specified when a valid texture was previously bound using the glBindTexture

    method.

    GLenum pname: Specify the symbolic name for the texture parameter. This can

    be one of the following values:

    GL_TEXTURE_MIN_FILTER: This parameter allows you to specify the

    function that is used to sample a texture when several texels from the

    texture fit within a single screen pixel (or pixel fragment).

    GL_TEXTURE_MAG_FILTER: This parameter allows you to specify the

    function that is used to sample a texture when a single texel fits within

    multiple screen pixels (or pixel fragments).

    GL_TEXTURE_MIN_LOD: This parameter allows you to specify a floating

    point value that determines the selection of the highest resolution mipmap

    (the lowest mipmap level). The default value is -1000.0f.

    GL_TEXTURE_MAX_LOD: This parameter allows you to specify a floating

    point value that determines the selection of the lowest resolution mipmap

    (the highest mipmap level). The default value is 1000.0f.

    GL_TEXTURE_BASE_LEVEL: This parameter allows you to specify an

    integer index of the lowest defined mipmap level. The default value is 0.

    GL_TEXTURE_MAX_LEVEL : This parameter allows you to specify an

    integer index that defines the highest defined mipmap level. The default value

    is 1000.

    GL_TEXTURE_WRAP_S: This parameter allows you to determine how a

    texture is sampled when the S texture coordinate is out-of the [0..1] range.

    By default this value is set to GL_REPEAT.

    GL_TEXTURE_WRAP_T: This parameter allows you to determine how atexture is sampled when the T texture coordinate is out of the [0..1] range.

    By default this value is set to GL_REPEAT.

    GL_TEXTURE_WRAP_R: This parameter allows you to determine how a

    texture is sampled when the R texture coordinate is out of the [0..1] range.

    By default this value is set to GL_REPEAT.

    GL_TEXTURE_BORDER_COLOR: This 4-component color parameter

    allows you to specify the color that is used when the texture is sampled on

    its border.

    GL_TEXTURE_COMPARE_MODE: This parameter allows you to specify thetexture comparison mode for the currently bound depth texture. This

    parameter is useful when you want to perform projected shadow mapping or

    other effects that require depth comparison.

    GL_TEXTURE_COMPARE_FUNC: This parameter allows you to specify the

    depth comparison function that is used when the

    GL_TEXTURE_COMPARE_MODEparameter is set to

    GL_COMPARE_R_TO_TEXTURE.

    GL_GENERATE_MIPMAP : This boolean parameter specifies if all levels of a

  • 8/13/2019 Textur Game Engine Programming

    9/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 9

    mipmap array should be automatically updated when any modification to the

    base level mipmap is made. The default value is GL_FALSE.

    [GLfloat|GLint] param: Specifies the value for the parameter.

    GL_TEXTURE_M I N_FI LTER

    The GL_TEXTURE_MIN_FILTERtexture parameter allows you to specify the

    minification filter function that is applied when several texels from the texture fit within

    the same pixel (or fragment) that is rendered to the current color buffer. The image below

    shows an example of when this happens:

    The GL_TEXTURE_MIN_FILTERparameter can have one of the following values:

    GL_NEAREST: Returns the texel that is nearest to the center of the pixel being

    rendered.

    GL_LINEAR: Returns the weighted average of the four texture elements that are

    closest to the center of the pixel being rendered.

    GL_NEAREST_MIPMAP_NEAREST : Choose the mipmap that most closely

    matches the size of the pixel being textured and then apply the GL_NEAREST

    method to produce the sampled texture value.

    GL_LINEAR_MIPMAP_NEAREST: Choose the mipmap that most closely

    matches the size of the pixel being textured and then apply the GL_LINEAR

    method to produce the sampled texture value.

    GL_NEAREST_MIPMAP_LINEAR: Choose the two mipmaps that most closely

    matches the size of the pixel being textured. Each of the two textures are

    sampled using the GL_NEARESTmethod and the weighted average of the two

    samples are used to produce the final value.

    GL_LINEAR_MIPMAP_LINEAR: Choose the two mipmaps that most closely

    matches the size of the pixel being textured. Each of the two textures are

    GL_TEXTURE_MIN_FILTER

    http://3dgep.com/wp-content/uploads/2012/02/GL_TEXTURE_MIN_FILTER.png
  • 8/13/2019 Textur Game Engine Programming

    10/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 10

    sampled using the GL_LINEARmethod and the weighted average of the two

    samples are used to produce the final value.

    GL_TEXTURE_M AG_FI LTER

    The GL_TEXTURE_MAG_FILTERtexture parameter allows you to specify the

    magnification filter function that is applied when one texel from the sampled texture is

    mapped to multiple screen pixels (or pixel fragments). The image below shows this

    phenomenon.

    Since this particular parameter only effects textures using the lowest mipmap level (thehighest resolution) so the only valid values for the GL_TEXTURE_MAG_FILTER

    parameter are GL_NEARESTand GL_LINEAR. These values have the same meaning

    as for GL_TEXTURE_MIN_FILTERexplained above.

    GL_TEXTURE_WRAP

    The GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T, and GL_TEXTURE_WRAP_R

    texture parameters allow you to control how out-of-bound texture coordinates are

    treated. Texture coordinates in the range of [0..1] will be interpreted as-is, but in some

    cases you may actually want to define texture coordinates outside of this allowed range.This may actually occur without intention when a transformation is applied to the texture

    matrix (texture scaling could scale texture coordinates out-of-range).

    The GL_TEXTURE_WRAPparameters can have the following values:

    GL_REPEAT: This the default texture wrap mode for all 3 coordinates. Using this mode

    will tell OpenGL to ignore the integer part of the texture coordinate and only use the

    fractional part to determine the sampled texel. For example, a texture coordinate of 2.05

    GL_TEXTURE_MAG_FILTER

    http://3dgep.com/wp-content/uploads/2012/02/GL_TEXTURE_MAG_FILTER.png
  • 8/13/2019 Textur Game Engine Programming

    11/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 1

    will simply be interpreted as 0.05 and a texture coordinate of -3.5 will be interpreted as

    0.5.

    The image below shows an example of using GL_REPEATfor both

    GL_TEXTURE_WRAP_Sand GL_TEXTURE_WRAP_Ttexture parameters:

    GL_CLAMP: Will cause the texture coordinate to be clamped in the range [0..1]. The

    image below shows the result of using GL_CLAMP for both GL_TEXTURE_WRAP_S

    and GL_TEXTURE_WRAP_Ttexture parameters:

    GL_REPEAT

    http://3dgep.com/wp-content/uploads/2012/02/GL_REPEAT.jpg
  • 8/13/2019 Textur Game Engine Programming

    12/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 12

    Texture Environment

    The glTexEnvfamily of functions allows you to specify the texture environment

    parameters that will effect all texture operations when a fragment is textured. The

    currently bound texture does not have any influence on the texture environment settings.

    The glTexEnvhas the following signature:

    Where:

    GLenum target: Specifies the texture environment this is effected by the

    parameter setting. The texture environment can be either GL_TEXTURE_ENV, or

    GL_TEXTURE_FILTER_CONTROL.

    GL_CLAMP

    1 voidglTexEnv[f|i]{v}(GLenum target, GLenum pname, [GLfloat|GLint]

    http://3dgep.com/wp-content/uploads/2012/02/GL_CLAMP.jpg
  • 8/13/2019 Textur Game Engine Programming

    13/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 13

    GLenum pname: Specifies the texture environment parameter to set.

    [GLfloat|GLint]{*} param: Used to specify the value of the parameter being set.

    GL_TEXTURE_ENV

    When the targetparameter is GL_TEXTURE_ENVthen the valid values of the pname

    paramater are GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COLOR.

    GL_TEXTURE_ENV_COLOR

    If pnameis GL_TEXTURE_EVN_COLORthe the paramsargument should point to a

    4-component floating-point color value that will henceforth be referred to as the

    environment color. The default value for the GL_TEXTURE_EVN_COLORparameter is

    transparent black (0, 0, 0, 0).

    GL_TEXTURE_ENV_MODE

    If pnameis GL_TEXTURE_ENV_MODE, then paramscan be set to one of the five

    texture functions:

    GL_ADD: Adds the color of the incoming fragment with the color of the sampled

    texture. The alpha value of the incoming fragment is multiplied with the alpha value

    of the sampled texture.

    GL_MODULATE: Both the color and the alpha channel of the incoming fragment

    are multiplied by the sampled texture.

    GL_DECAL: The alpha value of the sampled texture is used to blend the incoming

    fragment color with the color of the sampled texture. The alpha value of the

    incoming fragment is used for the alpha value of the resulting fragment. This

    results in the texture replacing the original color of the fragment only if the alpha of

    the decal texture is not 0.

    GL_BLEND: The sampled texture color is used to determine the per-component

    blending weight between the incoming fragment color and the environment color

    (as determined by the value of the GL_TEXTURE_EVN_COLORparameter. The

    final alpha value is determined by multiplying the incoming fragments alpha value

    and the sampled textures alpha value.

    GL_REPLACE: The sampled textures color and alpha value will replace the

    incoming fragments color and alpha values.

    The tables below show the texture functions that are applied specific to the value of the

    GL_TEXTURE_ENV_MODEparameter.

    For the following tables, we will make the following definitions:

    refers to the Red, Green, and Blue color components of the previous texture

    stage (or the incoming fragment if the current texture stage is 0).

    refers to the Red, Green, and Blue color components of the sampled texture

    (the texture source color).

    refers to the Red, Green, and Blue color components of the texture

  • 8/13/2019 Textur Game Engine Programming

    14/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 14

    environment color. This is determined by the value of the

    GL_TEXTURE_EVN_COLORparameter.

    refers to the Alpha component of the previous texture stage (or the incoming

    fragment if the current texture stage is 0).

    refers to the Alpha component of the sampled texture in the case of textures

    with GL_RGBAinternal texture formats.

    The functions for textures with GL_RGBAinternal texture format:

    FUNCT ION COL OR VALUE ALP HA VALUE

    GL_REPLACE

    GL_MODULATE

    GL_DECAL

    GL_BLEND

    GL_ADD

    For textures with GL_RGBinternal texture format:

    FUNCT ION COL OR VALUE ALP HA VALUE

    GL_REPLACE

    GL_MODULATE

    GL_DECAL

    GL_BLEND

    GL_ADD

    The default value for the GL_TEXTURE_ENV_MODEparameter is GL_MODULATE.

    It should be noted that t he glTexEnv f amily of f unctions hav e been deprecatedin OpenGL 3.0 and removed in core GL 3.1. An alternative f or using the t exture environment parameters is to implement

    the texture blending in f ragment shaders.

    GL_TEXTURE_FILTER_CONTROL

    When the targetparameter is set to GL_TEXTURE_FILTER_CONTROLthen pname

    must be GL_TEXTURE_LOD_BIAS . This is an integer parameter whose value is added

    to the chosen mipmap level depending on the selected value of the

    GL_TEXTURE_MIN_FILTERparameter.

    For example, if GL_TEXTURE_LOD_BIAS is set to 1 and the

  • 8/13/2019 Textur Game Engine Programming

    15/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 15

    GL_TEXTURE_MIN_FILTERparameter is set to one of the filtering methods that uses

    mipmaps (GL_NEAREST_MIPMAP_NEAREST , GL_LINEAR_MIPMAP_NEAREST,

    GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR) then the chosen

    mipmap level will be added by 1, resulting in a lower mipmap level when the texture is

    sampled.

    The Basic Lighting Model

    When lighting is enabled, OpenGL applies a specific lighting model to compute the color

    of a vertex being lit. Keep in mind that when using the fixed-function pipeline, OpenGL

    does not perform per-fragment lighting. For this reason, shapes with visibly low polygon

    counts will not be as nicely shaded as an object that are highly tesselated (contains

    many vertices). The only way to resolve these lighting artifacts created by low

    tesselated objects is by generating enough vertices so that there is one vertex per

    screen pixel when the geometry is rendered. The alternative is to implement your

    lighting equations in a fragment shader program however this is beyond the scope of this

    article.

    The final color for the vertex being lit is computed as follows:

    The final color is then clamped in the range before it is passed to the texture

    stage.

    The Emissive Term

    The emissive term in the above formula refers to the current materials emissive

    component. Material properties will be described later.

    This component has the effect of adding color to a vertex even if there is no ambient

    contribution or any lights illuminating the vertex.

    Keep in mind, this will not cause the vertex to emit light and no emissive light transfer

    will occur between objects.

    This component can be written as:

    Where refers to the materials emissive color component.

    The Ambient Term

  • 8/13/2019 Textur Game Engine Programming

    16/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 16

    The ambient term is computed by multiplying the global ambient color (as determined

    by the GL_LIGHT_MODEL_AMBIENTparameter) by the materials ambient property.

    This component can be written as:

    Where refers to the global ambient term and refers to the

    materials ambient color component.

    The Lights Contribution

    Every light that is currently active will contribute to the final vertex color. The total light

    contribution is the sum of all active light contributions.

    This formula can be written as such:

    AT T ENU AT ION FA CT OR

    The lights attenuation factor defines how the intensity of the light gradually falls-off as

    the lights position moves farther away from the vertex position.

    The lights final attenuation is a function of three attenuation constants. These three

    constants define the constant, linear and quadratic attenuation factors.

    If we define the following variables:

    is the scalar distance between the point being shaded and the light source.

    is the constant attenuation factor of the light source.

    is the linear attenuation factor of the light source.

    is the quadratic attenuation factor of the light source.

    The the formula for the attenuation factor is:

    The attenuation factor is only taken into account when we are dealing with positional

    lights (point lights and spot lights). If the light is defined as a directional light, then the

    attenuation factor is always 1.0 and no attenuation will occur.

    We can also force positional light sources to 1.0 by setting the constant attenuation to

    1.0 and the linear and quadratic attenuation factors to 0.0. The method to set these

    factors in OpenGL will be discussed in the following sect ions.

  • 8/13/2019 Textur Game Engine Programming

    17/30

  • 8/13/2019 Textur Game Engine Programming

    18/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 18

    This is also called the view vector.

    is the normalized direction vector pointing from the point we want to shade to

    the light source. This is also called the light vector.

    is the half-angle vector between the view vector and the light vector.

    is the specular contribution of the light source.

    is the specular reflection constant for the material that is used to

    shade the object.

    is the shininess constant for the material. The higher the shininess of the

    material, the smaller the highlight on the material.

    Then, using the Blinn-Phong lighting model, the specular term is calculated as follows:

    Similar to the diffuse term, the specular term must also consider the direction of the

    normal vector. If the vector is pointing away from the light (when ) then the

    specular term is 0.

    Materials

    When lighting is enabled, the color vertex attribute that was specified with glColor

    family of functions is ignored. This color by itself doesnt provide enough information to

    determine the final color of a fragment.

    Instead of defining the color of each vertex, we must define a material definition that will

    be used to render an object.

    Material parameter are specified using the glMaterialfamily of functions which has the

    following signature:

    Where:

    GLenum face: Specifies the faces that are being updated. This parameter must

    be:

    GL_FRONT: The material parameter is being specified for front-facing

    polygons.

    GL_BACK: The material parameter is being specified for back-facing

    polygons.

    1 voidglMaterial[f|i]{v}(GLenum face, GLenum pname, [GLfloat|GLint]

  • 8/13/2019 Textur Game Engine Programming

    19/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 19

    GL_FRONT_AND_BACK: The material parameter is being specified for both

    front- and back-facing polygons.

    GLenum pname: Specifies the material parameter being updated. This

    parameter can be one of the following values:

    GL_AMBIENT: A 4-component RGBA vector that defines the ambient color

    component of the material. The default value is (0.2, 0.2, 0.2, 1.0).

    GL_DIFFUSE: A 4-component RGBA vector that defines the diffuse color

    component of the material. The default value is (0.8, 0.8, 0.8, 1.0).

    GL_SPECULAR:A 4-component RGBA vector that defines the specular

    color component of the material. The default value is (0, 0, 0, 1).

    GL_EMISSION: A 4-component RGBA vector that defines the emissive

    color component of the material. The default value is (0, 0, 0, 1).

    GL_SHININESS: A scalar integer or floating point value that defines the

    specular exponent of the material. Only values in the range [0, 128] are

    accepted. The default value is 0.

    GL_AMBIENT_AND_DIFFUSE: A single parameter that allows you to

    specify both the GL_AMBIENTand GL_DIFFUSEwith the same color value.

    [GLfloat|GLint]{*} param: Specifies the value (or values) to set the pname

    parameter to.

    Lighting

    By default, OpenGL does not perform lighting calculations in the scene. To enable

    lighting, you must perform at least three steps:

    1. All geometry that will be lit with dynamic lights must define vertex normals.

    Lighting will not look correct on vertices that dont define correct vertex normals.

    2. The lighting calculations must be enabled by calling glEnable(GL_LIGHTING). If

    you want to render geometry that should not be lit (for example a skybox or user

    interface elements), you must disable lighting again using

    glDisable(GL_LIGHTING).

    3. Enable at least one light source using glEnable(GL_LIGHT0+i)where iis within

    the range [0,GL_MAX_LIGHTS-1].

    You can specify a number of dynamic lights in an OpenGL scene. The number of lights

    that you can specify is dependent on the capabilities of the graphics hardware. You can

    query the maximum number of dynamic lights by using the following function:

    Your hardware implementation is required to have support for at least 8 lights.

    In the section titled The Basic Lighting Model I discussed the different properties of

    light and how they effect the final color of the vertex being lit. The light properties for

    each active light can be specified using the glLightfamily of functions. This function

    has the following signature:

    12

    GLint maxLights = 0;glGetIntegerv( GL_MAX_LIGHTS, &maxLights );

  • 8/13/2019 Textur Game Engine Programming

    20/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 20

    Where:

    GLenum light: Specifies the light number. Light numbers are specified as offsets

    from the symbolic name GL_LIGHT0. For example, to set the properties for the

    third dynamic light, you would specify GL_LIGHT0+2.

    GLenum pname: Specifies the light parameter being set. This value must be one

    of the following parameters:

    GL_AMBIENT: This 4-component RGBA vector specifies the lights ambient

    intensity. The default value is (0, 0, 0, 1).

    GL_DIFFUSE: This 4-component RGBA vector specifies the lights diffuse

    intensity. The default value for GL_LIGHT0is (1, 1, 1, 1) and (0, 0, 0, 1) for

    all other lights.

    GL_SPECULAR: This 4-component RGBA vector specifies the lights

    specular component. The default value for GL_LIGHT0is (1, 1, 1, 1) and (0,

    0, 0, 1) for all other lights.

    GL_POSITION: This 4-component XYZW vector specifies the position of the

    light in homogeneous object coordinates. The lights position is transformed

    by the current GL_MODELVIEWmatrix when the glLightmethod is called.

    This implies that lights are transformed by the same matrix transformation

    that standard objects are transformed by. If the w-component of the

    GL_POSITIONparameter is 0, then the light is treated as a directional light

    and its placed at an infinite distance away from the scene. When treating

    the light as a directional light, the attenuation factor is ignored. The default

    value of this parameter is (0, 0, 1, 0). Initially light sources are directional

    light sources whose direction points parallel to the z-axis.

    GL_SPOT_DIRECTION: This 3-component XYZ direction vector specifies

    the direction of a spotlight. This parameter is transformed by the current

    GL_MODELVIEWmatrix when glLightis called. This parameter is ignored

    unless GL_SPOT_CUTOFFis not 180. By default, this parameter is set to

    (0, 0, -1).

    GL_SPOT_EXPONENT: The spotlight intensity is attenuated by the cosine

    of the angle between the direction of the light ( ) and the direction

    from the position of the light ( ) to the position of the vertex ( ) being

    lit, raised to the power of the spot exponent.

    GL_SPOT_CUTOFF: This scalar value specifies the maximum spread angle

    of the spotlight measured in degrees. Valid values for this parameter are in

    the range [0,90] and 180. Lights with a cutoff angle of 180 degrees are point-

    lights and lights with a cutoff angle in the range [0,90] are computed as

    spotlights.

    GL_CONSTANT_ATTENUATION: This scalar parameter represents the

    constant attenuation factor of the light.

    1 voidglLight[f|i]{v}(GLenum light, GLenum pname, [GLfloat|GLint]{*

  • 8/13/2019 Textur Game Engine Programming

    21/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 2

    GL_LINEAR_ATTENUATION: This scalar parameter represents the linear

    attenuation factor of the light.

    GL_QUADRATIC_ATTENUATION : This scalar parameter represents the

    quadratic attenuation factor of the light.

    [GLfloat|GLint]{*} param: The value to set the parameter to.

    Putting it All Together

    Now that weve seen how we can use textures, materials and lighting to render theobjects in our scene, lets examine an example that renders the moon rotating around

    the earth. The objects are realistically light by a point-light that represents the sun.

    Initialize OpenGL

    Using GLUTwe can easily initialize the OpenGL context and the rendering window.

    We must also register the window callbacks so we can respond to keyboard events and

    window resizing events.

    main.cpp

    289290291292293294295296297298

    299300301302303304305306307308309310311312

    313314315316317318319320321322323324325

    voidInitGL( intargc, char* argv[] ){ std::cout

  • 8/13/2019 Textur Game Engine Programming

    22/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 22

    From lines 293-312 are standard GLUT initialization code. If this code is unfamiliar to

    you then refer to my previous article titled Introduction to OpenGL.

    On lines 315-322 a few OpenGL states are initialized. On line 322, we enable the

    GL_NORMALIZEstate. This is done because the objects in the scene will be scaled

    which means that the objects vertex normals will also be scaled. Scaled normals will

    effect the lighting calculations and the lighting will not look right. Enabling the

    GL_NORMALIZEstate will instruct OpenGL to renormalize normal vectors before the

    lighting contribution is computed.

    Loading Textures

    Well use the SOILlibrary to load the textures used by this demo. Well also specify a

    few texture properties when after the texture has been loaded.

    The SOIL_load_OGL_texture method can be used to load a texture and generate an

    OpenGL texture object that can then be used to texture the objects in our scene.

    We also want to specify the texture filtering mode to GL_LINEARand the texture wrap

    mode to GL_REPEAT.

    On line 274, the default texture object ID of 0 is bound to the GL_TEXTURE_2Dtexture

    target and thus un-binding any previously bound texture object.

    Creating a Sphere

    Since all of the objects in our scene (the earth, the moon, and the sun) can all be

    represented by a sphere, we will generate a single sphere display list and use it to

    render each object in the scene.

    I will use a GLU Quadric to generate a sphere that contains the texture coordinates and

    the vertex normals that are necessary to render the objects. I havent discussed

    main.cpp265266267268269270271272273274275276277

    GLuint LoadTexture( constchar* texture ){ GLuint textureID = SOIL_load_OGL_texture( texture, SOIL_LOAD

    glBindTexture( GL_TEXTURE_2D, textureID ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LI

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LIglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEATglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEATglBindTexture( GL_TEXTURE_2D, 0 );

    returntextureID;

    }

    http://www.lonesock.net/soil.htmlhttp://3dgep.com/?p=636
  • 8/13/2019 Textur Game Engine Programming

    23/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 23

    quadrics in this article but Ill leave it up to the reader to investigate the usage of

    quadrics.

    On lines 223-228 we define the quadric object. We must specify that we want the

    quadric to generate texture coordinates and smooth normals at each vertex.

    On line 229, we create a new display list (display lists were discussed in a previous

    article titled Rendering Primitives with OpenGL). A sphere quadric is rendered into the

    display list.

    Once we have captured the sphere definition into a display list, we dont need the

    quadric shape any longer. On line 234 we delete the quadric object but retain the

    display list so that we can quickly render the sphere again.

    Global Ambient

    As discussed previously, the global ambient value is multiplied by the materials

    ambient term to produce the ambient contribution. The global ambient value is specified

    with the glLightModelfvmethod passing GL_LIGHT_MODEL_AMBIENTas the name

    of the parameter.

    Specifying a global ambient value will allow materials with an ambient term (greater than

    0) to show some detail even in the absence of any light source.

    Enable Lighting

    Before any lighting calculations will be performed, we must also enable lighting

    somewhere. To do that, we must call the glEnable method with GL_LIGHTINGas the

    only argument.

    main.cpp

    223224225226

    227228229230231232233234

    g_SphereDisplayList = glGenLists(1);GLUquadric* pSphereQuadric = gluNewQuadric();gluQuadricDrawStyle( pSphereQuadric, GLU_FILL );gluQuadricOrientation( pSphereQuadric, GLU_OUTSIDE );

    gluQuadricTexture( pSphereQuadric, GL_TRUE );gluQuadricNormals( pSphereQuadric, GLU_SMOOTH );glNewList( g_SphereDisplayList, GL_COMPILE );{ gluSphere( pSphereQuadric, 1.0, 360, 180 );}glEndList();gluDeleteQuadric( pSphereQuadric );

    main.cpp

    236237238

    // Specify a global ambientGLfloat globalAmbient[] = { 0.2, 0.2, 0.2, 1.0 };glLightModelfv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );

    http://3dgep.com/?p=2365
  • 8/13/2019 Textur Game Engine Programming

    24/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 24

    Define a Light Source

    For convenience, I will define a light source object that can be used to encapsulate all of

    the properties of lights discussed in this article.

    ma n.cpp

    240 glEnable( GL_LIGHTING );

    main.cpp868788899091929394959697

    9899100101102103104105106107108109110111

    112113114115116117118119120121122123124125126127128129130131132133134135136137138

    structLight{ Light( GLenum lightID = GL_LIGHT0 , color4 ambient = color4( 0.0, 0.0, 0.0, 1.0 ) , color4 diffuse = color4( 1.0, 1.0, 1.0, 1.0 ) , color4 specular = color4( 1.0, 1.0, 1.0, 1.0 ) , float4 position = float4( 0.0, 0.0, 1.0, 0.0 ) , float3 spotDirection = float3( 0.0, 0.0, 1.0 ) , float spotExponent = 0.0 , float spotCutoff = 180.0f , float constantAttenuation = 1.0 , float linearAttenuation = 0.0

    ,float

    quadraticAttenuation = 0.0 ) : m_LightID ( lightID ) , m_Ambient( ambient ) , m_Diffuse( diffuse ) , m_Specular( specular ) , m_Position( position ) , m_SpotDirection( spotDirection ) , m_SpotExponent( spotExponent ) , m_SpotCutoff( spotCutoff ) , m_ConstantAttenuation( constantAttenuation ) , m_LinearAttenuation( linearAttenuation ) , m_QuadraticAttenuation( quadraticAttenuation ) {}

    voidActivate() { glEnable( m_LightID ); glLightfv( m_LightID, GL_AMBIENT, &(m_Ambient.r) ); glLightfv( m_LightID, GL_DIFFUSE, &(m_Diffuse.r) ); glLightfv( m_LightID, GL_SPECULAR, &(m_Specular.r) ); glLightfv( m_LightID, GL_POSITION, &(m_Position.x) ); glLightfv( m_LightID, GL_SPOT_DIRECTION, &(m_SpotDirecti

    glLightf( m_LightID, GL_SPOT_EXPONENT, m_SpotExponent ); glLightf( m_LightID, GL_SPOT_CUTOFF, m_SpotCutoff ); glLightf( m_LightID, GL_CONSTANT_ATTENUATION, m_Constant

    glLightf( m_LightID, GL_LINEAR_ATTENUATION, m_LinearAtteglLightf( m_LightID, GL_QUADRATIC_ATTENUATION, m_Quadrat

    }

    voidDeactivate() { glDisable( m_LightID ); }

    GLenum m_LightID; color4 m_Ambient; color4 m_Diffuse; color4 m_Specular;

    float4 m_Position; float3 m_SpotDirection;

  • 8/13/2019 Textur Game Engine Programming

    25/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 25

    The light is activated and positioned in the scene using the Light::Activate method and

    the light can be deactivated using the Light::Deactivatemethod.

    We will define a single point-light object that will represent the sun.

    Defining Materials

    Similar to the light source, I wanted to define a material object that can be used to

    define the different materials used in the scene.

    The material properties have been discussed already. The material is applied to all

    subsequent objects by calling the Material::Applymethod.

    139140141142143144

    float m_SpotExponent; float m_SpotCutoff; float m_ConstantAttenuation; float m_LinearAttenuation; float m_QuadraticAttenuation;};

    main.cpp

    156 Light g_SunLight( GL_LIGHT0, color4(0,0,0,1), color4(1,1,1,1), c

    main.cpp

    5556575859606162

    63646566676869707172737475767778798081828384

    structMaterial{ Material( color4 ambient = color4(0.2, 0.2, 0.2, 1.0) , color4 diffuse = color4(0.8, 0.8, 0.8, 1.0) , color4 specular = color4(0.0, 0.0, 0.0, 1.0) , color4 emission = color4(0.0, 0.0, 0.0, 1.0) , floatshininess = 0 ) : m_Ambient( ambient )

    , m_Diffuse( diffuse ) , m_Specular( specular ) , m_Emission( emission ) , m_Shininess( shininess ) {}

    voidApply() { glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &(m_Ambient.

    glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &(m_Diffuse.glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &(m_SpeculaglMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &(m_EmissioglMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, m_Shininess

    }

    color4 m_Ambient; color4 m_Diffuse; color4 m_Specular; color4 m_Emission; float m_Shininess;};

  • 8/13/2019 Textur Game Engine Programming

    26/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 26

    We want to render three objects in the scene, each object having its own material. The

    Earths material should be brighter and produce a higher specular shine while the moon

    material should be dull with a subtle dull specular shine. The object that represents the

    sun should just be an un-shaded white ball.

    Render the Scene

    To render this scene, well draw 3 spheres. The first sphere will represent the sun. This

    will be an unlit white sphere that will rotate about 90,000 Km around the center of the

    scene. The position of the only light in the scene will be the same as the object that

    represents the sun. The sun will also only define a constant attenuation of 1.0 thus

    making sure that no attenuation takes place and every object in the scene is rendered

    with the lights full intensity regardless of the distance away from the sun.

    Placed at the center of the scene will be the earth. The earth rotates around its poles,

    but its positions stays fixed at the center of the scene.

    The final object will be the moon. The moon appears to rotate around the earth but at a

    distance of 60,000 Km away from the earth.

    In this scene, we make the agreement that 1 unit is approximately 1,000 Km. Although

    the units used are completely arbitrary, it makes sense to choose the units so that it

    makes sense in you position and scale objects in your scene.

    main.cpp

    157

    158159160

    // Material properties

    Material g_SunMaterial( color4(0,0,0,1), color4(1,1,1,1), color4Material g_EarthMaterial( color4( 0.2, 0.2, 0.2, 1.0), color4( 1Material g_MoonMaterial( color4( 0.1, 0.1, 0.1, 1.0), color4( 1,

    main.cpp

    539540541542543544

    545546547548549550551552553554555556557

    voidRenderScene1(){ glMatrixMode( GL_MODELVIEW );

    glLoadIdentity();

    // Move the scene back so we can see everything

    glTranslatef( 0.0f, 0.0f, -100.0f );

    // First draw the sun glPushMatrix(); // In this simulation, the sun rotates around the earth! glRotatef( g_fRotate3, 0.0f, -1.0f, 0.0f ); glTranslatef( 90.0f, 0.0f, 0.0f );

    g_SunLight.Activate();

    glDisable( GL_TEXTURE_2D ); glDisable( GL_LIGHTING ); glColor3f( 1.0f, 1.0f, 1.0f );

  • 8/13/2019 Textur Game Engine Programming

    27/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 27

    This first thing we do before rendering the scene is zero-out the model view matrix. On

    line 539, we set the model-view matrix to identity, resetting any transformations that

    may still be in the matrix stack.

    We want to place the camera view sufficiently far enough away so that we can capture

    all of the scene elements in view. Translating the camera 100 units in the positive z-axis

    is equivalent to moving the world origin 100 units in the negative z-axis.

    We first want to posit ion our light correctly in the scene. If we dont do this, then any

    object in the scene that is rendered before the light is positioned and activated will not

    be lit correctly. On lines 548-553, the world-view matrix is manipulated so that the origin

    is placed at the position we want the light to appear.

    On line 555-560 we draw a white sphere with the lighting disabled at the exact position

    of the sun light point light that was activated on line 553. This gives us the impression

    that the point light is emitt ing from some object (in this case, the white sphere). This

    also has the result of demonstrating the fact that lights are positioned in the scene in

    558559560561562563564565566567

    568569570571572573574575576577578579580581

    582583584585586587588589590591592593594

    glCallList(g_SphereDisplayList ); glPopMatrix(); glEnable( GL_LIGHTING );

    // Draw the Earth glPushMatrix(); glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); // Rotate the earth so

    glRotatef( g_fRotate1, 0.0f, 0.0f, -1.0f ); // Rotate the eaglScalef( 12.756f, 12.756f, 12.756f ); // The earth's diame

    glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, g_EarthTexture ); g_EarthMaterial.Apply(); glCallList( g_SphereDisplayList ); // Render a sphere with

    glPopMatrix();

    // Draw the moon glPushMatrix(); // Rotate the view around the Earth's axis glRotatef( g_fRotate2, 0.0f, 1.0f, 0.0f ); // Translate the moon away from the earth glTranslatef( 60.0f, 0.0f, 0.0f ); glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); // Rotate the moon so

    // Rotate the moon around it's axis glRotatef( g_fRotate2, 0.0f, 0.0f, -1.0f ); glScalef( 3.476f, 3.476f, 3.476f );

    glBindTexture( GL_TEXTURE_2D, g_MoonTexture ); // Render the sphere with the moon texture. g_MoonMaterial.Apply(); glCallList( g_SphereDisplayList );

    glPopMatrix();

    glBindTexture( GL_TEXTURE_2D, 0 );}

  • 8/13/2019 Textur Game Engine Programming

    28/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    3dgep.com/?p=2417 28

    the same way as regular geometry. Using this technique, we must ensure that lighting

    is disabled when drawing this sphere otherwise it will be rendered completely black

    since the light is inside this sphere, the outside faces of the white sphere will always be

    facing away from the light.

    On line 563-573 the earth is rendered. Again, the origin of the world is positioned and

    rotated accordingly. This time, before we render the sphere display list again, we enable

    the earth texture that we loaded earlier. We also apply the material that we want to use

    to render the earth. After the lighting calculations are performed, the texture will be

    modulated (multiplied) with the lit vertices.

    The moon object is drawn on lines 576-591. This is almost identical to the earth model,

    except the moon rotates around the earth which requires an additional translation and a

    rotation.

    On line 593, we should not forget to unbind the texture that we previously bound to the

    GL_TEXTURE_2Dtexture target.

    What is not shown in this method is the final call that tells GLUT to swap the front and

    back buffers so that we can see the results of our render method. GLUT provides the

    glutSwapBuffersmethod for this purpose. You should not forget to call this method

    after you are finished rendering your scene.

    View the Demo

    The demo below demonstrates the example shown in this article. The demo uses

    WebGL to render the scene. The WebGL demo will render in the latest FireFox browser

    as well as the latest Chrome browser. If you are using Internet Explorer however, then

    you are probably not going to see the beautiful demo but instead you will see the

    YouTube video.

  • 8/13/2019 Textur Game Engine Programming

    29/30

    Texture and Lighting Demo

    Beginning OpenGL

    Game Programming

    - Second Edition

    (2009)

  • 8/13/2019 Textur Game Engine Programming

    30/30

    5/5/13 Texturing and Lighting in OpenGL | 3D Game Engine Programming

    1 2

    Mason Woo, Jackie Neider, Tom Davies, Dave Shreiner (1999). OpenGL Programming

    Guide. 3rd. ed.Massachusetts, USA: Addison Wesley.

    OpenGL 2.1 Reference Pages [online]. (1991-2006) [Accessed 27 January 2012].

    Available from: http://www.opengl.org/sdk/docs/man/.

    Post to Facebook

    3

    This entry was posted in Graphics Programming, OpenGLand tagged ambient, diffuse,

    directional, emission, glLight, glMaterial, glTexture, materials, OpenGL, point,

    Programming, rendering, specular, spotlight, texturesby Jeremiah van Oosten.

    Bookmark the permalink [http://3dgep.com/?p=2417] .

    OpenGL

    Program ming Guide

    - 3rd Edition

    http://3dgep.com/?p=2417http://3dgep.com/?author=2http://3dgep.com/?tag=textureshttp://3dgep.com/?tag=spotlighthttp://3dgep.com/?tag=specularhttp://3dgep.com/?tag=renderinghttp://3dgep.com/?tag=programminghttp://3dgep.com/?tag=pointhttp://3dgep.com/?tag=openglhttp://3dgep.com/?tag=materialshttp://3dgep.com/?tag=gltexturehttp://3dgep.com/?tag=glmaterialhttp://3dgep.com/?tag=gllighthttp://3dgep.com/?tag=emissionhttp://3dgep.com/?tag=directionalhttp://3dgep.com/?tag=diffusehttp://3dgep.com/?tag=ambienthttp://3dgep.com/?cat=11http://3dgep.com/?cat=20http://www.shareaholic.com/api/share/?title=Texturing+and+Lighting+in+OpenGL&link=http%3A%2F%2F3dgep.com%2F%3Fp%3D2417&notes=&short_link=&shortener=google&shortener_key=&v=1&apitype=1&apikey=8afa39428933be41f8afdb8ea21a495c&source=Shareaholic-Publishers&template=&service=5&ctype=&tags=opengl%20glsl%20of-shader%20of-lighting%7c1%7cdelicioushttp://www.opengl.org/sdk/docs/man/http://glprogramming.com/red/index.htmlhttp://3dgep.com/wp-content/uploads/2012/02/OpenGL-Programming-Guide.jpg