Lighting is Not Smooth as Objects Rotate

Hello,

When my objects rotate I am getting a strange lighting effect where it jumps from light to dark instead of a smooth transition. See video links below:

I have been trying to figure this out for over a year now. With all my research, I cannot find anyone that has quite the same issue. I am using the GLEW library. My brother uses the same library but does not have this issue. He codes in a different language then myself so can’t just use his code. I am using C++. I have compared our code the best I can and I do not see what I am missing. My brother says the way the lighting is handling the coloring also does not look right. My brother and I only have basic OpenGL experience.

It will be hard to post the code here but I can, just wanted to see first if any knows what the issue is off the top of their head?

Veerian

i dont like guessing games. you could calculate the normals wrong, you could mess up the rotation, or the light position, or whatever. just post your code.

Ok, I was not looking forward to the inevitable here, as to put the code here, but finally took the time to do it. The real code is not in a simple linear one sheet being it is in C++ but I tried to do the best I could to put the code below in the order it is called and the values that are used.

SetupDCPixelFormat

// set the pixel format for the DC
	PIXELFORMATDESCRIPTOR pfd =		// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),	// Size Of This Pixel Format Descriptor
		1,					// Version Number
		PFD_DRAW_TO_WINDOW |		// Format Must Support Window
		PFD_SUPPORT_OPENGL |		// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,			// Must Support Double Buffering
		PFD_TYPE_RGBA,			// Request An RGBA Format
		32,					// Select Our Color Depth
		0, 0, 0, 0, 0, 0,				// Color Bits Ignored
		0,					// No Alpha Buffer
		0,					// Shift Bit Ignored
		0,					// No Accumulation Buffer
		0, 0, 0, 0,				// Accumulation Bits Ignored
		24,					// 24Bit Z-Buffer (Depth Buffer)
		0,					// No Stencil Buffer
		0,					// No Auxiliary Buffer
		PFD_MAIN_PLANE,			// Main Drawing Layer
		0,					// Reserved
		0, 0, 0					// Layer Masks Ignored
	};


	// create and activate (in OGL) the render context
	m_hRC= wglCreateContext(hDC);

Initialize GL Context

	if( wglMakeCurrent(m_hDC, m_hRC) == FALSE )

	if( glewInit() != GLEW_OK )


	g_oGLErrorHandler.ClearError();

	// do we modify vsync?
	SetVSync(m_sVSynStatus);

	glShadeModel(GL_SMOOTH);					// enable smooth shading
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);	// background/clear color

	// set-up the depth buffer
	glDisable(GL_DEPTH_TEST);


	glEnable(GL_CULL_FACE); // Cull back-facing triangles
	glFrontFace(GL_CCW);
	glCullFace(GL_BACK);

	// perspective calculations
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	// use lighting
	GLfloat global_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; // Set Ambient Lighting To Fairly Dark Light (No Color)

 
	// Set the frame buffer clear color
	//   glClearColor(0.5, 0.5, 0.5, 1.0);
 
   	glShadeModel(GL_SMOOTH);
 
   	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); // Set The Global Ambient Light Model

 
   	glEnable(GL_LIGHTING);

   	glEnable (GL_COLOR_MATERIAL) ;


	glEnable(GL_LIGHT0);

	glEnable(GL_NORMALIZE);
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);


	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glClearColor(0.0, 0.0, 0.0, 0.0);

ResizeFrame

	// (re)size the viewport to consume the entire client area
	glViewport(m_uiVPOffsetX, m_uiVPOffsetY, m_uiVPWidth, m_uiVPHeight);

	// reset the projection matrix
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// (re)calculate the aspect ratio of the viewport (0,0 is bottom left)
	gluPerspective(45.0f, (float)m_uiVPWidth / (float)m_uiVPHeight, 1.0f,  24000.0f);

	// lastly, reset the modelview matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();\

SwapBuffers(m_hDC)

Start Render Loop

//Now prepare objects to be rendered below. I am only giving 2 examples here; Cuboid and Elipsoide. If you need more let me know but all objects I render have the same lighting issue.
CreateGLCuboid(double dXSize, double dYSize, double dZSize, BOOL bScalable)
{
	const static GLfloat MatYellowDiffuse[] = {0.86f, 0.74f, 0.14f, 1.0f};
	const static GLfloat MatOrangeDiffuse[] = {0.78f, 0.59f,  0.0f, 1.0f};

	if( bScalable == TRUE )
	{
		dXSize= 0.5;
		dYSize= 0.5;
		dZSize= 0.5;
	}
	else
	{
		dXSize*= 0.5;
		dYSize*= 0.5;
		dZSize*= 0.5;
	}

	glPushMatrix();

	glColor4ub(m_uiRedColorValue, m_uiGreenColorValue, m_uiBlueColorValue, m_uiAlphaColorValue);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, MatYellowDiffuse);

#define V(a,b,c) glVertex3d( a dXSize, b dYSize, c dZSize );
#define N(a,b,c) glNormal3d( a, b, c );

    glBegin( GL_QUADS );
        N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
        N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
        N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
        N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
        N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
        N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
    glEnd();

#undef V
#undef N

	 glPopMatrix();
}


voidCreateGLElipsoide(GLdouble width, GLdouble height, GLdouble depth, GLint slices, GLint stacks)
{
	int i,j;

	/* Adjust z and radius as stacks are drawn. */
	double y0, y1;
	double r0, r1;

	/* Pre-computed circle */
	double *sint1,*cost1;
	double *sint2,*cost2;

   width*= 0.5;
   height*= 0.5;
   depth*= 0.5;

	if( CircleTable(&cost1, &sint1, -slices) == FALSE )
	{
		return;
	}

	if( CircleTable(&sint2, &cost2, stacks*2) == FALSE )
	{
		free(sint1);
		free(cost1);

		return;
	}

	/* The top stack is covered with a triangle fan */
	y0 = 1.0;
	y1 = cost2[(stacks > 0) ? 1 : 0];
	r0 = 0.0;
	r1 = sint2[(stacks>0)?1:0];

	glBegin(GL_TRIANGLE_FAN);

	glColor4ub(m_uiRedColorValue, m_uiGreenColorValue, m_uiBlueColorValue, m_uiAlphaColorValue);

	glNormal3d(0, 1, 0);
	glVertex3d(0, height, 0);

	for (j=slices; j>=0; j--)
	{
		glNormal3d(cost1[j]*r1,        y1       ,        sint1[j]*r1);
		glVertex3d(cost1[j]*r1*width, y1*height, sint1[j]*r1*depth);
	}

	glEnd();

	/* Cover each stack with a quad strip, except the top and bottom stacks */

	for( i=1; i<stacks-1; i++ )
	{
		y0 = y1; y1 = cost2[i+1];
		r0 = r1; r1 = sint2[i+1];

		glBegin(GL_QUAD_STRIP);

		for(j=0; j<=slices; j++)
		{
			glNormal3d(cost1[j]*r1,        y1       ,        sint1[j]*r1);
			glVertex3d(cost1[j]*r1*width, y1*height, sint1[j]*r1*depth);

			glNormal3d(cost1[j]*r0,        y0       ,        sint1[j]*r0);
			glVertex3d(cost1[j]*r0*width, y0*height, sint1[j]*r0*depth);
		}

		glEnd();
	}

	/* The bottom stack is covered with a triangle fan */
	y0 = y1;
	r0 = r1;

	glBegin(GL_TRIANGLE_FAN);

	glNormal3d(0, -1, 0);
	glVertex3d(0, -height, 0);

	for (j=0; j<=slices; j++)
	{
		glNormal3d(cost1[j]*r0,        y0       ,        sint1[j]*r0);
		glVertex3d(cost1[j]*r0*width, y0*height, sint1[j]*r0*depth);
	}

	glEnd();

	/* Release sin and cos tables */

	free(sint1);
	free(cost1);
	free(sint2);
	free(cost2);
}

RenderFrame

wglMakeCurrent(m_hDC, m_hRC)


g_oGLErrorHandler.ClearError();

	glPushMatrix();

	//Camera Look is setup

Render Objects

   glDisable(GL_DEPTH_TEST);
   glEnable(GL_BLEND);
   glFrontFace(GL_CW);

   glPushMatrix();

	glTranslated
	   (
		m_vLocation.x,
	   	m_vLocation.y,
	   	m_vLocation.z
	   );

	glRotated
	 (
	 	  poData->m_qCurrentOrientation.getRotationAngle() * TO_DEGREE,
	 	  m_vAxis.x,
	 	  m_vAxis.y,
	 	  m_vAxis.z
   	 );

	if( m_bObserver == TRUE ) //Do not scale attachments
	{
		glScaled
		(
			1.0,
			1.0,
			1.0
		);
	}
	else
	{
		if( m_dObjectScale != 1.0 && poData->m_dWidth * 0.5 * m_dObjectScale >= vectorMag(m_vLocation) )
		{
			double dMag= vectorMag(m_vLocation);

			glScaled
			(
				m_dObjectScale / ((poData->m_dWidth * 0.5 * m_dObjectScale) / dMag) * 0.5,
				m_dObjectScale / ((poData->m_dWidth * 0.5 * m_dObjectScale) / dMag) * 0.5,
				m_dObjectScale / ((poData->m_dWidth * 0.5 * m_dObjectScale) / dMag) * 0.5
			);
		}
		else
		{
			glScaled
			(
				m_dScale,
				m_dScale,
				m_dScale
			);
		}
	}

      GLfloat mat_specular[] = { GetRValue(poData->m_uiSpecular) / 255.0 * 0.5,
         GetGValue(poData->m_uiSpecular) / 255.0 * 0.5,
         GetBValue(poData->m_uiSpecular) / 255.0 * 0.5, 1.0 };

      glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);

      if( poData->m_uiEmmisionColor != 0 )
      {
         GLfloat mat_emission[] = {GetRValue(poData->m_uiEmmisionColor) / 255.0,
         GetGValue(poData->m_uiEmmisionColor) / 255.0,
         GetBValue(poData->m_uiEmmisionColor) / 255.0, GetAValue(poData->m_uiEmmisionColor) / 255.0};
         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
      }
      else
      {
         GLfloat mat_emission[] = {0.0, 0.0, 0.0, 1.0};
         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
      }

      GLfloat mat_shininess[] = {0.0, 0.0, 0.0, 0.0};
      glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

      glPopMatrix();
*********************************************************

	glPopMatrix();

	glFlush();
	glFinish();

SwapBuffers(m_hDC)

End Render Loop

i see glDisable(GL_DEPTH_TEST) twice , one with a comment

which is not what glDisable(GL_DEPTH_TEST) does.

no glEnable(GL_DEPTH_TEST), so you are rendering without depth test all the time.

I am not sure if you are referring that this could be causing the lighting issue, but I do my own depth testing. Whether I do my own or enable depth test does not change anything with my lighting issue.

Has anyone ever even seen this kind of lighting issue?