Sign in to follow this  
juxie

Lighting problem

Recommended Posts

Hi I encountered a problem with lights. I am not sure why it behaves this way.
	gluLookAt(0, 50, 0,
			  0, 0, 0,
			  0, 0, -1);

	glEnable(GL_LIGHTING);

	float light_pos[4] = {0, 20, 0, 1};
	float light_am[4] = {0, 0, 0, 1};
	float light_di[4] = {1, 1, 1, 1};
	float light_spe[4] = {1, 1, 1, 1};
	float light_dir[] = {0, -1, 0};

	glLightfv(GL_LIGHT0, GL_AMBIENT, light_am);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_di);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_spe);

	glLightfv(GL_LIGHT0, GL_POSITION, light_pos);

	glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10);
	glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 1);
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_dir);

	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	float mat_am[4] = {0, 0, 0, 1};
	float mat_di[4] = {1, 1, 1, 1};
	float mat_spe[4] = {1, 1, 1, 1};
	float mat_em[4] = {0, 0, 0, 1};

        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_am);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_di);
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_spe);
        //glMaterialfv(GL_FRONT, GL_EMISSION, mat_em);
        glMaterialf(GL_FRONT, GL_SHININESS, 70);

	glEnable(GL_LIGHT0);

	glPushMatrix();	
	glColor3f(1, 0, 0);
	glTranslatef(0, -20, 0);;
	glutSolidCube(10);
	glPopMatrix();

	glPushMatrix();	
	glColor3f(1, 0, 0);
	glTranslatef(5, -5, 0);
	auxSolidSphere(2.0);
	glPopMatrix();

	glPushMatrix();	
	glColor3f(1, 0, 0);
	glTranslatef(-5, -5, 0);
	auxSolidSphere(2.0);
	glPopMatrix();

	glPushMatrix();	
	glColor3f(1, 0, 0);
	glTranslatef(0, -5, -5);
	auxSolidSphere(2.0);
	glPopMatrix();

	glPushMatrix();	
	glColor3f(1, 0, 0);
	glTranslatef(0, -5, 5);
	auxSolidSphere(2.0);
	glPopMatrix();

I get the sphere lit correctly. but I don't get the cube lit correctly. I thought the cube supposed to be lit by the spotlight created too. But it didn't. Any idea why?

Share this post


Link to post
Share on other sites
I have 4 spheres on top of a cube.

---O---
| |
O X O
| |
---O---

O = sphere
X = spotlight from (0, 20, 0) in direction (0, -1, 0)

With the spotlight cut off is 10, I can see each of the spheres has half of its face lit.
But the cube is just black.

Share this post


Link to post
Share on other sites
Yes, and bare in mind that what you're seeing is probably correct, but that you are mistaking per-vertex lighting with per-pixel lighting. On a regular, non-tesselated cube, per-vertex lighting will look "off" (especially specular highlights). One option is to tesselate each face of the cube into smaller elements. Also, depending on your setup, vertices shouldn't be shared by adjacent faces, because of conflicting normals.

EDIT: although it shouldn't be completely black I guess. I'm not sure about this, but do GLUT primitives generate normals?

Share this post


Link to post
Share on other sites
Check the FAQ. Basically you get some webspace to host your image then insert a link here.

Here it is said that glut primitives provide vertex positions and normals, so that shouldn't be the problem. However, enable GL_NORMALIZE to make sure the normals are normalized ([looksaround]) before rendering (not sure if glutXXX does this already, might be so but I didn't find a clue).

Share this post


Link to post
Share on other sites
I tried with GL_NORMALIZE, however, it doesn't work.
I wanted to model some kind of table.

I wonder if texturing will solve the problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by juxie
I have 4 spheres on top of a cube.
       ---O---
| |
O X O
| |
---O---

O = sphere
X = spotlight from (0, 20, 0) in direction (0, -1, 0)

With the spotlight cut off is 10, I can see each of the spheres has half of its face lit.
But the cube is just black.
Is the spotlight cutoff large enough to include the vertices of the cube? As Todo said, OpenGL lighting is per-vertex so if no vertices receive light, the cube doesn't receive light.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this