# Lighting problem

This topic is 4293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Could you elaborate a bit on what is wrong with the rendering? What is wrong with tne lighting of the cube?

##### 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 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 on other sites
Post a screenshot(s) to confirm Todo answer :)

##### Share on other sites
how do I post screen shot?

##### 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 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 on other sites
Texturing wouldn't solve lighting problems just make the cube look different. Post a screenshot, please.

##### Share on other sites
Quote:
 Original post by juxieI have 4 spheres on top of a cube. ---O--- | | O X O | | ---O---O = sphereX = 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.

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×