Skip to content

Archive

Tag: GL_SPECULAR

Before you can render objects with lighting, you need to setup the material. You do this before drawing each object or group of objects sharing the same material.

Use glMaterialfv to setup the current material

glMaterialfv(GL_FRONT_AND_BACK, channelcolor4f);

In OpenGL, you could specify different materials for the front and back of each triangle. In OpenGL-ES 1.1, you have to use the same for both.

channel can take one of the following values:

  • GL_AMBIENT
  • GL_DIFFUSE
  • GL_AMBIENT_AND_DIFFUSE
  • GL_SPECULAR
  • GL_EMISSION

color4f is a float array that specifies red, green blue and alpha values. This values get multiplied by illumination values generated by light sources. This works on a per channel basis, so for example you can have a given color for little shiny dots on your models (specular) and diffuse illumination (‘shading’).

GL_EMISSION lets you specify an absolute illumination value used regardless of light sources being available or not (but I guess still requires lighting to be enabled).

Speculars

You can use GL_SPECULAR and GL_SHININESS to add a kind of shiny spot to your material. Shininess defines the ‘tightness of the spot’:

  • glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0f )

1.0f gives a broad, dull shiny area, 128.0f gives a very tight shiny spot (here I should 64.0f, kind of in the middle)

Caveats

The biggest caveat is that your light source must emit a specular component(!) otherwise your objects won’t display any speculars:

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

glLightfv(glLightNum, GL_SPECULAR, specular );

Another caveat is that some simulators do not display speculars (at least on Android) so you need to test on device to be sure.

This tutorial/quick reference explains the steps to setup lighting in OpenGL-ES 1.1 (may be compatible with OpenGL-ES 2.0).

Lighting requires that you prepare your models for lighting first, i.e. set/update material and provide per face/per vertex normals

1. Enable lighting; optionally, enable per vertex/per face color and smooth
shading.

glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);

2. Enable up to 8 light sources:

glEnable(GL_LIGHTn) // (e.g: GL_LIGHT0)

3. Configure your light sources:

Common to all light types, set light color for ambient, diffuse and specular
light components. Ambient lets a light illuminate every point in a scene, diffuse lets a light illuminate objects around it and specular adds a ‘shiny’ spot to your models.

glLightfv(GL_LIGHTn, GL_AMBIENT, color4f );
glLightfv(GL_LIGHTn, GL_DIFFUSE, color4f );
glLightfv(GL_LIGHTn, GL_SPECULAR, color4f );

note: color4f is array of floats with rgba, eg: {1,0,0,1} is red.

3a. Directional or positional light

glLightfv(GL_LIGHTn, GL_POSITION, vector4f );

note: vector4f is x,y,z + w component
w=0 to create a directional light (x,y,z is the light direction) like the sun
w=1 to create a positional light like a fireball*.

3b. Spotlight

glLightfv(GL_LIGHTn, GL_POSITION, vector4f );
glLightfv(GL_LIGHTn, GL_SPOT_DIRECTION, vector3f );
glLightf(GL_LIGHTn, GL_SPOT_CUTOFF, angle); // angle is 0 to 180
glLightf(GL_LIGHTn, GL_SPOT_EXPONENT, exp); // exponent is 0 to 128

note: high exponent values make the light stronger at the middle of the
light cone.

4. Attenuation

Attenuation makes the power of a light source fade when the model is further from the light source. May slow down rendering.

glLightf(GL_LIGHTn, attenuation, value)

where attenuation is one of:

GL_CONSTANT_ATTENUATION
GL_LINEAR_ATTENUATION
GL_QUADRATIC_ATTENUATION

(*) that won’t actually draw a fireball :), just the lighting effect caused by something like a fireball.