Advertisement

Normals and CVAs

Started by December 06, 2000 07:49 AM
1 comment, last by Zadkiel 23 years, 11 months ago
Hi, I loading a model from a dxf file made only of Quads. I know how to load and convetr to it an simple array of vertices and render the but I don''t know how to calculate the normals. I don''t program in c so it would be most helpfull if someone could tell me the maths behind it. I should be able to translate the c code though if thats all you you can provide. That vertices are stored in the array as folllows; x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3,x0,y0..... pretty obvious I know but just in case. Also on another note does anyone know anything about CVAs (Compiled Vertex Arrays) their suppose to increas the render speed of a program considerablby, espically for lighting and fog. I konw there is a command in Ogl for rendering them but I don''t know how to make them, also I don''t know how they deal with normals or textures. Thanks in advance and thanks to NeHe for all this great work.
Hello, seeing as I fought long and hard with this too in the beginning, I am happy to help!

Take any three points on your quad, say (x0, y0, z0) and (x1, y1,z1), (x2, y2, z2).

Find two vectors (nv1 and nv2) from these three points.
i.e.
nv1.x = x0 - x1;
nv1.y = y0 - y1;
nv1.z = z0 - z1;

nv2.x = x1 - x2;
nv2.y = y1 - y2;
nv2.z = z1 - z2;

Take the cross product of these two vectors
i.e.
let (nx, ny, nz) be the normal coordinates

nx = (nv1.y * nv2.z) - (nv1.z * nv2.y);
ny = (nv1.z * nv2.x) - (nv1.x * nv2.z);
nz = (nv1.x * nv2.y) - (nv1.y * nv2.x);

Now you have your surface normal!
This will not be much use for lighting though, as it will have to be normalized before use. You can do this by calling initialising glNormalize(); in your init() function. But this will normalize all you vectors, and you may not want this.
You can normalize the vector by hand, by doing the following:
(this is ripped straight from my code, Im lazy...)

float length = (float) sqrt(( nx * nx) + ( ny * ny) + ( nz * nz) );

// Keep the program from blowing up from
// vectors that may calculated too close to zero.
if(length == 0.0)
length = 1.0;

// Dividing each element by the length will result in a
// unit normal vector.
nx /= length;
ny /= length;
nz /= length;


voila. Hope this helps.
CVA are used the same as normal vertex arrays, the only difference (in OpenGL) is the initialisation gl_ext_compiled....
The point of them is to keep vertices that will be reused in a scene in video-memory or to stop reused vertices being copied to video-memory twice. I _think_

Paul
3d kid
Advertisement
Thanks for your help. I''ve got it working now.

This topic is closed to new replies.

Advertisement