PDA

View Full Version : normalize vertices

sa0sin
11-27-2011, 11:09 AM
hi, i'm a noob to opengl but i have to do operations without openGL help. so far, i've made shapes, rotation/translation/scaling of my custom 3d shapes and now i need to do lighting and i am stuck at normalizing. i can tell that my lighting works because i added a glutSolidTeapot to my scene and it has some smooth lighting on it while my custom shapes are just all white.

i have my vertices stored like this:

verts[i][j][0] = some val for x equation
verts[i][j][1] = some val for y equation
verts[i][j][2] = some val for z equation
verts[i][j][3] = 1 for homogenous

and my shapes work. now, i don't know how to normalize them. i think i understand that you use the cross product and then normalize but i don't understand how to make the cross product for my vertices. i have them as "quads" not triangles if that means anything. any help on how to normalize with the way my vertices are stored would be appreciated

BionicBytes
11-27-2011, 12:26 PM
You need to construct normals from your vertices. You then normalise the normals not the verticies.
You can construct the normals from any triangle by using the 3 points to create a plane. By definition the normal is perpendicular to the plane. For a quad you simply have two triangles so you can pick which verticies you want when creating the plane.

sa0sin
11-27-2011, 01:41 PM
i still cant get it to work. heres the code i have to calculate the normal of a shape (such as a cone, sphere, etc)

while(i < 50){
while(j < 50){
calcCross(po->vertices[i][j], po->vertices[i][j+1], po->vertices[i+1][j+1], po->normals[i][j]);
j++;
}
i++;
}

void calcCross(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *n){
n[0] = (b[1] - a[1])*(c[2] - a[2]) - (b[2] - a[2])*(c[1] - a[1]);
n[1] = (b[2] - a[2])*(c[0] - a[0]) - (b[0] - a[0])*(c[2] - a[2]);
n[2] = (b[0] - a[0])*(c[1] - a[1]) - (b[1] - a[1])*(c[0] - a[0]);
normalize(n);
}

void normalize(GLfloat *p){
GLfloat d = 0.0;
int i;
for(i=0; i<3; i++){
d+=p[i]*p[i];
}
d=sqrt(d);
if(d > 0.0){
for(i=0; i<3; i++){
p[i]/=d;
//printf("normal: %f\n", p[i]);
}
}
}

with my shape on when i open the app i see

http://i.imgur.com/VJPLv.jpg

if i turn mine off and turn on glutSolidTeapot i see

http://i.imgur.com/z2Wau.jpg

anyone can tell me what i need to do to get mine to have the lighting?