private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get OpenGL.
var gl = openGLControl1.OpenGL;
// Clear and load identity.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
// Use gluLookAt to look at torus.
gl.LookAt(0.0f,10.0f,10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
// Rotate torus.
angle += .5f;
gl.Rotate(angle, 0.0f, 1.0f, 0.0f);
// Push all attribute bits.
gl.PushAttrib(OpenGL.GL_ALL_ATTRIB_BITS);
// Get the inverse model matrix
gl.PushMatrix();
gl.LoadIdentity();
gl.Rotate(-angle, 0.0f, 1.0f, 0.0f);
var inverseModelMatrix = gl.GetModelViewMatrix();
gl.PopMatrix();
// Get the object space light vector.
Vertex objectLightPosition = inverseModelMatrix * worldLightPosition;
// Loop through vertices
for(int i=0; i<torus.NumVertices; ++i)
{
Vertex lightVector = objectLightPosition - torus.Vertices[i].position;
//Calculate tangent space light vector
torus.Vertices[i].tangentSpaceLight.X =
torus.Vertices[i].sTangent.ScalarProduct(lightVector);
torus.Vertices[i].tangentSpaceLight.Y =
torus.Vertices[i].tTangent.ScalarProduct(lightVector);
torus.Vertices[i].tangentSpaceLight.Z =
torus.Vertices[i].normal.ScalarProduct(lightVector);
}
// Draw bump pass
if(drawBumps)
{
// Bind normal map to texture unit 0
normalMap.Bind(gl);
gl.Enable(OpenGL.GL_TEXTURE_2D);
// Bind normalisation cube map to texture unit 1
// Extensions: We can use the Extension format, such as below. However in this
// case we'll get a warning saying that this particular extension is deprecated
// in OpenGL 3.0. However...
gl.ActiveTextureARB(OpenGL.GL_TEXTURE1_ARB);
gl.BindTexture(OpenGL.GL_TEXTURE_CUBE_MAP_EXT, normalisationCubeMap);
gl.Enable(OpenGL.GL_TEXTURE_CUBE_MAP_EXT);
// Extensions: ...it's deprecated because it's actually core functionality
// in OpenGL 3.0.
gl.ActiveTexture(OpenGL.GL_TEXTURE0_ARB);