Problems with Shading/lightning

int0 wrote on Thursday, April 16, 2009:


The following simple c++ code puts some light to the scene or rather sphere, but my sphere is shaded incorrect (see links below) and I dont know why. I tried nearly all but it still doesn’t work. I’m using VC++ 2008 express on Windows XP.

Here are my problems visualized :slight_smile: :

For example if I zoom (increasing z value —> gltranslate(0,0,z) ) I get the following effect (see link --> the screenshot shows the sphere with an other material but it has the same effect):
–> when I zoom further it finally looks like that:

When I rotate the object it looks like this:

The strange thing is that when I reach the point where the farplane ends (z values lower than -20 --> that means -21,-22, … until -infinity) the sphere is shaded correctly
The farplane is defined here: --> gluPerspective(45.0, (double)window_width / (double)window_height, 1.0, 20);

I hope you can help me - would be very glad!


#include <cstdlib>
#include <windows.h>
#include <gl\GL.h> // Header Datei für die OpenGL32 Library
#include <gl\glu.h> // Header Datei für die GLu32 Library
#include <gl\glfw.h>

void Init();
void Shut_Down(int return_code);
void Main_Loop();
void Draw();
void DemoLight(void);
void draw_Planet();

float rotate_y = 0,
rotate_z = 0,
z = -8;
const float rotations_per_tick = 0.2;

int main()

void Init()
const int window_width = 800,
window_height = 600;

if (glfwInit() != GL_TRUE)

float aspect_ratio = ((float)window_height) / window_width;

// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
if (glfwOpenWindow(window_width, window_height, 5, 6, 5,
0, 0, 0, GLFW_WINDOW) != GL_TRUE)
glfwSetWindowTitle("The GLFW Window");
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1

// set the projection matrix to a normal frustum with a max depth of 50
gluPerspective(45.0, (double)window_width / (double)window_height, 1.0, 20);
//glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);


void Shut_Down(int return_code)

void Main_Loop()
// the time of the previous frame
double old_time = glfwGetTime();
// this just loops as long as the program runs
// calculate time elapsed, and the amount by which stuff rotates
double current_time = glfwGetTime(),
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
old_time = current_time;
// escape to quit, arrow keys to rotate view
if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
rotate_y += delta_rotate;
rotate_y -= delta_rotate;
z -=0.1;
if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS)
z +=0.1;

// clear the buffer

GLfloat ambientColor\[\] = \{0.2f, 0.2f, 0.2f, 1.0f\}; //Color \(0.2, 0.2, 0.2\)
	glLightModelfv\(GL\_LIGHT\_MODEL\_AMBIENT, ambientColor\);

	//Add positioned light
	GLfloat lightColor0\[\] = \{0.5f, 0.5f, 0.5f, 1.0f\}; //Color \(0.5, 0.5, 0.5\)
	GLfloat lightPos0\[\] = \{4.0f, 0.0f, 8.0f, 1.0f\}; //Positioned at \(4, 0, 8\)
	glLightfv\(GL\_LIGHT0, GL\_DIFFUSE, lightColor0\);
	glLightfv\(GL\_LIGHT0, GL\_POSITION, lightPos0\);

	//Add directed light
	GLfloat lightColor1\[\] = \{0.5f, 0.2f, 0.2f, 1.0f\}; //Color \(0.5, 0.2, 0.2\)
	//Coming from the direction \(-1, 0.5, 0.5\)
	GLfloat lightPos1\[\] = \{-1.0f, 0.5f, 0.5f, 0.0f\};
	glLightfv\(GL\_LIGHT1, GL\_DIFFUSE, lightColor1\);
	glLightfv\(GL\_LIGHT1, GL\_POSITION, lightPos1\);
	glColor3f\(1.0f, 1.0f, 0.0f\);
	// draw the figure
// swap back and front buffers


void Draw()

glTranslatef(0, 0, z);
// apply the current rotation
glRotatef(rotate_y, 0, 1, 0);
glRotatef(rotate_z, 0, 0, 1);



void draw_Planet(){

glTranslatef\(0, 0, 0\);
glColor3f\(.5, .8, .2\);
gluSphere\(gluNewQuadric\(\),1, 20, 20\);


elmindreda wrote on Thursday, April 16, 2009:

GLFW is not involved in actual rendering and does not itself provide the OpenGL API. GLFW is one way to create the required OpenGL context, in order to use the (already existing) OpenGL API provided by your operating system.

If you have trouble with OpenGL, you should seek help in a forum dedicated to the OpenGL API. Here are a few:
##opengl on Freenode

int0 wrote on Friday, April 17, 2009:

thanks for the answer, but it seems this problem is caused by glfw, because with glut this example is working fine … :frowning:

artblanc wrote on Friday, April 17, 2009:


I think the problem is not caused by glfw.

I haven’t compiled your code and don’t know why is working in a different way in GLUT but, just giving a quick look to your code, you are using hardware lights and you are trying to color your object with glColor. You should use a material instead, see glMaterialfv.

I recommend to check the links that elmindreda suggested.

int0 wrote on Friday, April 17, 2009:


thanks for the fast answer - I’ve already posted a thread to the opengl discussion board but got no good answer. I changed glColor to glMaterialfv but getting still the same.

Instead of

glColor3f(1.0f, 1.0f, 0.0f);

I took this code:

glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0f);

I would be very, very, very glad if you could compile the code - it’s not that much code - just to know that the problem is not my graphics card or something else *paranoid*

artblanc wrote on Friday, April 17, 2009:

Yes, you are getting the same because you are using glEnable(GL_COLOR_MATERIAL) (I just noticed that). So the glColor affects the material.

I can’t compile your code right now. I’m on vacations! far away from my computer.

Trust elmindreda, glfw is not the problem.

Good luck.

int0 wrote on Friday, April 17, 2009:

hi thanks again for the answer, I’ve removed “glEnable(GL_COLOR_MATERIAL)” - the effect is still the same :frowning: … I guess it would be very strange if this would work and “glColor(…)” not.
Maybe elmindreda wants to compile the code :slight_smile: … hehe

Have a nice holiday!