Normals

September 22nd, 2009 experiment,research 1 comment

Normals Sphere
Finally got normals working for the height map. Brad helped me a ton with the math, thanks brad :). After making sure I had the right points its was pretty simple.

Here is an example to calculate normals for a grid in OpenFrameworks. Obviously this can be applied to any other language.
grid

The image above we have four points on a plane. We need three of those points:

ofxVec3f p1(x1, y1, z1);
ofxVec3f p2(x2, y2, z2);
ofxVec3f p3(x3, y3, z3);

We now need a vector from p1 to p2 and p1 to p3.

ofxVec3f v1 = p1 - p2;
ofxVec3f v2 = p1 - p3;

We need the cross product of v1 and v2. This will give us our normal vector. Next we normalize that vector scale it and add p1.

ofxVec3f normalVec = v1.cross(v2);
normalVec.normalize();
normalVec *= 50.0;
normalVec += p1;

Now render from p1 to normal vector.

ofSetColor(255, 0, 0);
glVertex3f(normalVec.x, normalVec.y, normalVec.z);
glVertex3f(p1.x, p1.y, p1.z);

One Response to “Normals”

  1. Andy Li

    Sorry I’m not so clear with those math stuff…
    But you say “a vector from p1 to p2″, isn’t it should be p2 – p1 instead of p1 – p2?

Say Something