Anyone ever experienced unpredictable results when using an OpenGL light that’s perfectly perpendicular to a normal?
I’m rendering several rectangular prisms, and I have a light that I’m trying to simulate the sun and moon with like this:
float afAmbientLight[4];
if (m_fFactor >= 0.0f && m_fFactor <= 0.5f) {
//sun’s out
afAmbientLight[0] = 0.1f + 0.9f * sin(3.14159 * m_fFactor * 2.0f);
afAmbientLight[1] = 0.1f + 0.9f * sin(3.14159 * m_fFactor * 2.0f);
afAmbientLight[2] = 0.1f + 0.9f * sin(3.14159 * m_fFactor * 2.0f);
afAmbientLight[3] = 1.0f; }
else {
//moon’s out
afAmbientLight[0] = 0.1f + 0.1f * sin(3.14159 * (m_fFactor - 0.5f) * 2.0f);
afAmbientLight[1] = 0.1f + 0.1f * sin(3.14159 * (m_fFactor - 0.5f) * 2.0f);
afAmbientLight[2] = 0.1f + 0.1f * sin(3.14159 * (m_fFactor - 0.5f) * 2.0f);
afAmbientLight[3] = 1.0f;
} glLightModelfv(GL_LIGHT_MODEL_AMBIENT, afAmbientLight);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 1.0f);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0f);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0f);
//I don’t know why this works, but I have better luck
//getting the sun or moon effect I want if I let the
//global ambient take care of ambient and let the
//directional light take care of diffuse and specular
float fAmbient[4] = {0.0f, 0.0f, 0.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, fAmbient);
float fDifSpec[4];
if (m_fFactor >= 0.0f && m_fFactor <= 0.5f) {
fDifSpec[0] = 1.0f;
fDifSpec[1] = 1.0f;
fDifSpec[2] = 0.0f;
fDifSpec[3] = 1.0f;
}
else
{
fDifSpec[0] = 1.0f;
fDifSpec[1] = 1.0f;
fDifSpec[2] = 1.0f;
fDifSpec[3] = 1.0f;
}
glLightfv(GL_LIGHT0, GL_DIFFUSE, fDifSpec);
glLightfv(GL_LIGHT0, GL_SPECULAR, fDifSpec);
//for all practical purposes, a directional light
//shines in the direction opposite its position
//and spot direction is ignored float
fPosition[4];
if (m_fFactor >= 0.0f && m_fFactor <= 0.5f) {
fPosition[0] = (float)cos(m_fFactor * 2 * 3.14159);
fPosition[1] = (float)sin(m_fFactor * 2 * 3.14159);
fPosition[2] = 0.0f;
fPosition[3] = 0.0f; }
else
{
fPosition[0] = (float)cos((m_fFactor - 0.5f) * 2 * 3.14159);
fPosition[1] = (float)sin((m_fFactor - 0.5f) * 2 * 3.14159);
fPosition[2] = 0.0f;
fPosition[3] = 0.0f; }
glLightfv(GL_LIGHT0, GL_POSITION, fPosition);
The normals on the prism sides facing front and back are (0, 0, 1) and (0, 0, -1).
What I’m seeing is these two sides “flicker” - and they flicker at exactly the same time each “cycle”. Are there certain angles that light up the surface?
[This message has been edited by DalTXColtsFan (edited 02-20-2004).]