# Thread: Help finding the right lighting normal to use.

1. ## Help finding the right lighting normal to use.

So I have these, basically floor tiles, that are like this:

glNormal3f(0,1.0f,0);
glVertex3f(x,heightmap[x][z],z);
glVertex3f(x+1,heightmap[x+1][z],z); glVertex3f(x+1,heightmap[x+1][z+1],z+1);
glVertex3f(x,heightmap[x][z+1],z+1);

and they work fine.

Then I have a wall tile like this:
glNormal3f(0,0,1.0f);
glVertex3f(35,5,52);
glVertex3f(35,0,52);
glVertex3f(65,0,52);
glVertex3f(65,5,52);

and no light will appear on it, what should the normal be?

Can someone tell me how to calculate some lighting normals for some simple, straight, quads?

2. ## Re: Help finding the right lighting normal to use.

Regarding the order of your vertices, the normals are in good orientation.

Maybe it's just your light that is not directed to your wall ?

3. ## Re: Help finding the right lighting normal to use.

What do you mean my light isn't directed?
This is the code to my light:
GLfloat position[] = { 50.0f,1.0f,50.0f,1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, position);
GLfloat diffuseLight[] = { 2.0f, 2.0f, 2.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
GLfloat ambient[] = { 0.05f, 0.05f, 0.05f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
glEnable(GL_LIGHT0);

5. ## Re: Help finding the right lighting normal to use.

Here's my scene draw code:
void DrawGLScene()
{

GLfloat position[] = { 50.0f,1.0f,50.0f,1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, position);
GLfloat diffuseLight[] = { 2.0f, 2.0f, 2.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
GLfloat ambient[] = { 0.05f, 0.05f, 0.05f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
glEnable(GL_LIGHT0);

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer

GLfloat x_m, y_m, z_m, u_m, v_m;
GLfloat xtrans, ztrans, ytrans;
GLfloat sceneroty;
int numtriangles;

// calculate translations and rotations.
xtrans = -xpos;
ztrans = -zpos;
ytrans = -walkbias-0.5f;
sceneroty = 360.0f - yrot;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

glRotatef(lookupdown, 1.0f, 0, 0);
glRotatef(sceneroty, 0, 1.0f, 0);

glTranslatef(xtrans, ytrans+0.025f, ztrans);

glMateriali(GL_FRONT, GL_SHININESS, 50);
float mcolor[] = {0.5,0.5,0.5,1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
glNormal3f(0,0,1.0f);
glVertex3f(35,5,52);
glVertex3f(35,0,52);
glVertex3f(65,0,52);
glVertex3f(65,5,52);
glEnd();

for(int z = 1; z<99; z++)
{
for(int x = 1; x<99; x++)
{
glBegin(GL_QUADS); // start drawing the cube.

glMateriali(GL_FRONT, GL_SHININESS, 50);

if(terrainmap[x][z] == 0)
{
float mcolor[] = {0.419,0.556,0.137,1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
}
else
{
float mcolor[] = {0.96,0.815,0.686,1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
}

glNormal3f(0,1.0f,0);
glVertex3f(x,heightmap[x][z],z); // Top Right Of The Quad (Top)
glVertex3f(x+1,heightmap[x+1][z],z); // Top Left Of The Quad (Top)
glVertex3f(x+1,heightmap[x+1][z+1],z+1); // Bottom Left Of The Quad (Top)
glVertex3f(x,heightmap[x][z+1],z+1); // Bottom Right Of The Quad (Top)

glEnd();
}
}

// swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers();
}

And that's the draw scene code.
All heightmap values are zero currently.

6. ## Re: Help finding the right lighting normal to use.

Okay, so for a standing tile like this:
glVertex3f(35,5,52);
glVertex3f(35,0,52);
glVertex3f(65,0,52);
glVertex3f(65,5,52);
what should the normal be?

7. ## Re: Help finding the right lighting normal to use.

The normal should be (0,0,1).
Your poly lies in a plane parallel to the XY plane.
Thus the normal has to either +Z or -Z.
(No calculations have to be done.)
In your case it's +Z because of the order in which
the vertices are defined. This assumes you are
using counterclockwise winding to define the frontface
of your polygons (which is the OpenGL default).

8. ## Re: Help finding the right lighting normal to use.

Originally Posted by DrenDran
and no light will appear on it, what should the normal be?
What does it mean `no light appear on it`?

Remember that the lighting is computed only in the vertexes, so if your light is equidistant from the vertexes and the dot product between the normal and the distance vector is the same (the light is over the center of the face), you will see an uniform color.
Also having ambient light and diffuse light with the same parameter don't help a lot.

9. ## Re: Help finding the right lighting normal to use.

Using a normal/settings like this:
glMateriali(GL_FRONT, GL_SHININESS, 50);
float mcolor[] = {0.5,0.5,0.5,1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
glNormal3f(0,0,1.0f);
glVertex3f(35,5,52);
glVertex3f(35,0,52);
glVertex3f(65,0,52);
glVertex3f(65,5,52);
glEnd();

I get this:

As you can see, the ground is fine, but the polygon/quad wall is just black.

10. ## Re: Help finding the right lighting normal to use.

You will need to calculate the normals for the wall too, which will probably be (1,0,0), (-1,0,0), (0,1,0) and (0,-1,0). If you need to calculate the normal of a triangle, you can use the vector cross product ( http://en.wikipedia.org/wiki/Cross_product ), to calculate the surface normal http://en.wikipedia.org/wiki/Surface_normal .

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•