PDA

View Full Version : Normals to use for a viewer facing rectangle



Mctittles
04-24-2011, 08:26 AM
Hello. I'm attempting to render a very simple normal map scene. I'm making a 2d program for now so every object drawn will always be on a quad facing in the same direction, as I didn't want to get into too complex of 3d to start.
I would however like to use normal mapping and lighting for some shading effect and am having difficulty understanding how to set up the normals for this.

Here is the relevant code which works for lighting, but the moving the light around just increases intensity and doesn't change the direction so I'm guessing I have my normals wrong?



GLfloat vertices[] = {1,1,1, -1,1,1, -1,-1,1, 1,-1,1};
GLubyte indices[] = {0,1,2,3};
GLfloat textureCoords [] = {1.0f,0.0f, 0.0f,0.0f, 0.0f,1.0f, 1.0f,1.0f};
GLfloat normals[] = {0,0,1, 0,0,1, 0,0,1, 0,0,1};

//....

glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glClientActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2, GL_FLOAT, 0,textureCoords);
glClientActiveTexture(GL_TEXTURE1);
glTexCoordPointer(2, GL_FLOAT, 0,textureCoords);


glNormalPointer(GL_FLOAT, 0, normals);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);


The result of the normal map and my texture shows up, so I think I have that correct, just the light direction is always the same. Could someone let me know the correct normals to use on a simple quad that will always be facing the user? Thanks.

Mctittles
04-24-2011, 09:13 AM
I think I've made some progress but still not working. I think I've figured out in the normal array the first 2 coordinates are for each vertex and the last is the normal direction, so setting the first ones to each vertex and the last to 1 should be facing the user correct? I've changed it to this:

GLfloat normals[] = {1,1,1, -1,1,1, -1,-1,1, 1,-1,1};

Now some part of the image gets shaded light or dark, however the normal map I have still gets rendered as if the light were coming from the same direction no matter where I move the light.

BionicBytes
04-25-2011, 08:34 AM
Firstly, using the fixed function pipeline to render with lighting a simple cube is never going to look very good - it's not tesselated anywhere near enough for proper lighting.

Secondly, each and every vertex should have a normal - usually pointing perpendicular to the plane on which the vertex is part of. All 3 elements of the normal (x,y,z components) define the normal's direction - not just the last one.
If you imagine a peice of paper on the floor, then at each corner (your vertex) you would have a normal defined as (0,1,0) so that the normal is pointing upward (Y-axis). Tesselate this grid sufficiently and then the lighting will approach something half decent. Repeat this for all 6 faces of your cube, ensuring your normals are poining towards the opposite face of the cube.