Game Development Reference
In-Depth Information
There are two SetPosition() functions that set the light's position in the 3D world. One function
takes x, y, and z float values for the light position, and the other function takes a Vector3 object
(see Listing 4-39).
Listing 4-39. SetPosition Functions
void SetPosition(float x, float y, float z)
{
m_Position.x = x;
m_Position.y = y;
m_Position.z = z;
}
void SetPosition(Vector3 Pos)
{
m_Position.x = Pos.x;
m_Position.y = Pos.y;
m_Position.z = Pos.z;
}
Finally, the PointLight class has functions that allow you to retrieve private data contained within the
class (see Listing 4-40).
Listing 4-40. Accessor Functions
Vector3 GetPosition(){return m_Position;}
float[] GetAmbientColor(){return m_light_ambient;}
float[] GetDiffuseColor(){return m_light_diffuse;}
float[] GetSpecularColor(){return m_light_specular;}
float GetSpecularShininess(){return m_specular_shininess;}
Building the Normal Matrix
In order to determine diffuse and specular lighting for an object, you need to transform the vertex
normals that are sent into the vertex shader into eye space coordinates. In order to do so, you must
use the Normal Matrix.
The Normal Matrix is created by
1.
Finding the matrix inverse of the ModelView Matrix.
2. Finding the matrix transpose of the matrix inverse calculated in step 1.
Listing 4-41 shows how building a normal matrix is done in code in the GenerateMatrices() function
in the Object3d class. Here are the steps:
1.
The Model Matrix is multiplied by the View Matrix to generate the ModelView
Matrix, which is placed in the m_NormalMatrix variable.
This ModelView Matrix is inverted using the Matrix.invertM() function.
2.
3.
The transpose of this inverted ModelView Matrix is taken and put in
m_NormalMatrix .
 
Search WWH ::




Custom Search