View Full Version : Lighting flickering

11-01-2011, 01:44 AM
Dear Friends
Previously i was not able to calculate normals for lighting purpose. But now I am able to calculate and assign normals to each QUADS of my surface model. But now nmear the edges light is flickering. Any idea of why this is happening. Any help would be appreciated. while i am zooming out reducing the size its flickering more. Thanks for any help.

11-01-2011, 01:45 AM
This is my lighting algorithm

void CRevolutionProjView::OnViewShade()
// TODO: Add your command handler code here
CDC* pDC = GetDC();
wglMakeCurrent(pDC->m_hDC, m_hrc);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
GLfloat ambientLight0[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight0[] = { 1.0f, 0.5f, 0.5f, 1.0f };
GLfloat specularLight0[] = { 0.0f, 0.0f, 1.0f, 1.0f };
GLfloat emissionLight0[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat position0[] = {1.0f,1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
glLightfv(GL_LIGHT0, GL_EMISSION, emissionLight0);
glLightfv(GL_LIGHT0, GL_POSITION, position0);

11-01-2011, 01:59 AM
glLightfv(GL_LIGHT0, GL_POSITION, position0);

Before you can issue that command, you need to know what the GL_MODELVIEWMATRIX is set to.
Lights are positioned in camera space and the current modelview matrix will be used to multiply the light position0. So, becareful of the ordering of your rendering code as the ModelView matrix may not be setup correctly for the lighting.

Also, with regard to the normals. Have you tried visualising each normal by drawing little arrows along the normal direction. You'll be surprised when you do see them, because this methods allows you to see that some won't be defined properly (typically at the edges).

11-01-2011, 02:20 AM
I have not thoughrouhgly understood your comment. You mean do I need to call glMatrixMode(GL_MODELVIEW) before positioning the light. i9.e before

is that what you mean ??

11-01-2011, 03:13 AM
Hi Bionic I have the normal like this below. How can I draw a normal arrow i know to draw GL_LINES so one point is the normal point what about the other point. How would i get the direction. I want to draw all the normals please help me. Thanks Sujan


11-01-2011, 03:37 AM
My normal calculation cannot be wrong I am calculating it in the correct method. How could it be wrong. Check my calculations..

I have V0,v1,v2,v3 4 points.

Now 2 vectors vec1 and vec2 having x,y,z points float.

GLfloat *normal1 = new GLfloat[3];

Now check my code and tell me where its wrong. ???

vec1.x = v1[0] - v0[0];
vec1.y = v1[1] - v0[1];
vec1.z = v1[2] - v0[2];

vec2.x = v3[0] - v0[0];
vec2.y = v3[1] - v0[1];
vec2.z = v3[2] - v0[2];

normal1[0] = ((vec1.y * vec2.z)-(vec1.z * vec2.y));
normal1[1] = ((vec1.z * vec2.x)-(vec1.x * vec2.z));
normal1[2] = ((vec1.x * vec2.y)-(vec1.y * vec2.x));

GLfloat len = sqrt((normal1[0]*normal1[0]) + (normal1[1]*normal1[1]) + (normal1[2]*normal1[2]));

normal1[0] /= len;
normal1[1] /= len;
normal1[2] /= len;

11-01-2011, 04:09 AM
Are you using textures when the "flickering" is happening or are you just lighting? Does the illumination change overall when you zoom out or does it stay constant?

11-01-2011, 06:01 AM
I am not using textures I am just drawing a surface made up of some QUAD elements.Moreover my shading is coming so nicely it looks realistic model. But near the edges behind the model some elements are illumintating and glowing off. When I zoom to the place where its flickering when i go near that portion the flickering gets off. When I zoom out the model looks smaller then its all oevr flickering heavily. Whats the cause of this flickers. Please help me. Thanks Sujan

11-01-2011, 06:04 AM
This should be the light position problem , my normal calculation cannot be wrong i believe. Can u check my lighting algorithm above ?? Thannks Sujan

Dan Bartlett
11-01-2011, 06:32 AM
Is it a problem with too few vertices to provide decent per-vertex lighting? See point 2) in http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
If it is then use more vertices, or per-fragment shading instead.

11-01-2011, 07:10 AM
can u tell me how can i calculate the normal for each vertex. I am currently calculating normal for every four vertex, i.e for a quad. Please help me how can I calculate for each of the 4 vertices. Thanks Sujan

11-01-2011, 09:07 AM
Dear Friends thanks for your helps. I am able to resolve the problem. actually I had to assign normals for each of the four vertices not only one. This solves my problem. Now its not flickering. I am going make a new post soon please if possible reply to that. Thanks a lot. Sujan