PDA

View Full Version : Help finding the right lighting normal to use.



DrenDran
02-19-2011, 01:39 PM
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?

_arts_
02-19-2011, 02:39 PM
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 ?

DrenDran
02-19-2011, 06:56 PM
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);

_arts_
02-20-2011, 03:27 AM
Can you show more of your code, mainly where the light code is, and your drawing function ?

DrenDran
02-23-2011, 06:15 PM
Oh wow, I forgot about this thread, well gonna continue anyway...
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
glLoadIdentity(); // Reset The View

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
glLoadIdentity();

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

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

glBegin(GL_QUADS);
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.

DrenDran
02-25-2011, 10:23 AM
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?

MaxH
02-25-2011, 11:20 AM
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).

Rosario Leonardi
02-25-2011, 01:34 PM
and no light will appear on it, what should the normal be?
What does it mean `no light appear on it`?
You see your quad? It's black? it's a flat color?

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.

DrenDran
02-25-2011, 11:44 PM
Using a normal/settings like this:
glBegin(GL_QUADS);
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:
http://img192.imageshack.us/img192/8766/badnormals.png

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

Dan Bartlett
02-26-2011, 01:59 AM
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 .

DrenDran
02-26-2011, 12:01 PM
You will need to calculate the normals for the wall too,
But that is what I was doing when I had this line of code:


glNormal3f(0,0,1.0f);

Alfonse Reinheart
02-26-2011, 12:25 PM
But that is what I was doing when I had this line of code:

That doesn't calculate a normal; that specifies what the normal is. That's where you put the results of the calculation.

_arts_
02-26-2011, 01:12 PM
I think your light is modified by your transformations since you define its position before anything.