Advertisement

Normals & Lighting...

Started by August 11, 2000 02:09 PM
3 comments, last by Mak 24 years, 3 months ago
A Newbie to OpenGL writes... Having spent a few days and 6-8 hours going through the tutorials, I am now loading my own text-file defined models and textures into my little engine and I am well chuffed. However - I am now calculating my own Normals for lighting and everything is going a bit awry... Using the VERTEX typedef from NeHe''s tutorials, I am passing three vertices into the following function, which creates a normal vector ("wsvnormal" again a VERTEX structure) which can be accessed by all my other functions. All the values and such like calculate correctly, values go in and values come out, all as it should be - only the normals aren''t "normal", they go off on one. It must be the math in the function, but I''m using a book to get the math and it all checks out there. My "normal" is based on VERTEX v1. Any one help? Is this math b*ll*x? Am I being lead astray? void wsCalculateNormal(VERTEX *v0, VERTEX *v1, VERTEX *v2) { float vNx,vNy,vNz; // work values float vAx,vAy,vAz; // work values float vBx,vBy,vBz; // work values float nmagnitude; // float nsum; vAx = v1->vx - v0->vx; vAy = v1->vy - v0->vy; vAz = v1->vz - v0->vz; vBx = v2->vx - v1->vx; vBy = v2->vy - v1->vy; vBz = v2->vz - v1->vz; vNx = (vAz * vBy) - (vAy * vBz); vNy = (vAx * vBz) - (vAz * vBx); vNz = (vAy * vBz) - (vAx * vBy); nsum = (vNx * vNx) + (vNy * vNy) + (vNz * vNz); nmagnitude = sqrtf(nsum); vNx = vNx / nmagnitude; vNy = vNy / nmagnitude; vNz = vNz / nmagnitude; wsvnormal.vx = vNx; wsvnormal.vy = vNy; wsvnormal.vz = vNz; wsvnormal.vr = 0.0f; wsvnormal.vg = 0.0f; wsvnormal.vb = 0.0f; wsvnormal.va = 0.0f; } I''m using GL_LINES to draw the normals on screen from vertex one of every face to the Normal vertex, and they look like an explosion in a porcupine farm! NOTE : I always get the same vectors, so there are no wierd initialisation problems.
Hi!
i had the same problem, but now it works fine ... :-) ...
first of all, here''s the vertex definition, i use:

struct vertex {
GLfloat x,y,z;
}




now you can call my function
vertex normalenvector(vertex a, vertex b)
with two vectors as parameter.

if you want to calc a normal to a triangle
you get the two vectors doing this.
a = first_point_of_triangle - second_point_of_triangle
b = first_point_of_triangle - third_point_of_triangle

for doing this, i wrote this function:

vertex bilde_differenzen_vector(vertex v1, vertex v2)
{
vertex help;
help.x = v2.x - v1.x;
help.y = v2.y - v1.y;
help.z = v2.z - v1.z;
return help;
}
//it substracts two vectors






so, now, here''s my code:

float laenge_vom_vector(vertex v)
{
float help;
help = (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
return help;
}
vertex normalisierter_vector(vertex v)
{
vertex help;
float laenge;
laenge = laenge_vom_vector(v);
help.x = v.x / laenge;
help.y = v.y / laenge;
help.z = v.z / laenge;
return help;
}

vertex cross_product(vertex a, vertex b)
{
vertex c;
c.x = (a.y * b.z) - (a.z * b.y);
c.y = (a.z * b.x) - (a.x * b.z);
c.z = (a.x * b.y) - (a.y * b.x);
return c;
}

vertex normalenvector(vertex a, vertex b)
{
vertex help;
help = cross_product(a,b);
help = normalisierter_vector(help);
return help;
}




if this does not solve your problem, please send me an email:
harry@daiw.de

cu
Harry
Advertisement
You wrote
quote:
vNx = (vAz * vBy) - (vAy * vBz);
vNy = (vAx * vBz) - (vAz * vBx);
vNz = (vAy * vBz) - (vAx * vBy);


but the correct version is

vNx = (vAy * vBz) - (vAz * vBy);
vNy = (vAz * vBx) - (vAx * vBz);
vNz = (vAx * vBy) - (vAy * vBx);


GA

Edited by - ga on August 12, 2000 9:58:10 AM
Visit our homepage: www.rarebyte.de.stGA
yeah, right, it would have been much easier, if i had checked this shit, instead of sending my code ... :-) ...
Guys - thanks!

GA - pasted the new bit of code in and it didn''t work!
Think there is something fundamentally wrong with my function somewhere.

Harry - Don''t despair - your effort won''t be wasted. I''ll restructure my code with your routines and see if it solves my problem.

Thanks for the help.

This topic is closed to new replies.

Advertisement