Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lighting problem

Started by juxie Mar 9, 2007 at 3:52 AM 8 replies 800+ views
Original Post
juxie
juxie
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?
Lord_Evil
Lord_Evil
Could you elaborate a bit on what is wrong with the rendering? What is wrong with tne lighting of the cube?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
juxie
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.
Todo
Todo
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?
rfterdarc
rfterdarc
Post a screenshot(s) to confirm Todo answer :)
juxie
juxie
how do I post screen shot?
Lord_Evil
Lord_Evil
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).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
juxie
juxie
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?
Lord_Evil
Lord_Evil
Texturing wouldn't solve lighting problems just make the cube look different. Post a screenshot, please.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Kalidor
Kalidor
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.