1

I have four normals which I am attempting to average by adding the x, y, and z values, then normalizing the result. However, when I add the different axis values, the resulting vector has a length of zero. I get the feeling that this issue is very easily solved, but I'm relatively new to OpenGL, and don't know vectors in and out yet.

Code for getting the face and vertex normals, respectively.

public Vector3f getNormal(Vector3f p1, Vector3f p2, Vector3f p3){
        Vector3f v = new Vector3f();

        Vector3f calU = new Vector3f(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z);
        Vector3f calV = new Vector3f(p3.x - p1.x, p3.y - p1.y, p3.z - p1.z);

        v.setX(calU.getY() * calV.getZ() - calU.getZ() * calV.getY());
        v.setY(calU.getZ() * calV.getX() - calU.getX() * calV.getZ());
        v.setZ(calU.getX() * calV.getY() - calU.getY() * calV.getX());

        return (Vector3f)v.normalise();
    }

    public Vector3f getVectorNormal(Vector3f p){
        Vector3f t1 = getNormal(p, new Vector3f(p.getX() - 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() - 1));
        Vector3f t2 = getNormal(p, new Vector3f(p.getX() + 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() - 1));
        Vector3f t3 = getNormal(p, new Vector3f(p.getX() + 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() + 1));
        Vector3f t4 = getNormal(p, new Vector3f(p.getX() - 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() + 1));

        float x = t1.getX() + t2.getX() + t3.getX() + t4.getX();
        float y = t1.getY() + t2.getY() + t3.getY() + t4.getY();
        float z = t1.getZ() + t2.getZ() + t3.getZ() + t4.getZ();

        Vector3f v = new Vector3f(x, y, z);

        return (Vector3f)v.normalise();
    }
Max Roncace
  • 1,277
  • 2
  • 15
  • 40

1 Answers1

1

This doesn't make sense

    Vector3f t1 = getNormal(p, new Vector3f(p.getX() - 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() - 1));
    Vector3f t2 = getNormal(p, new Vector3f(p.getX() + 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() - 1));
    Vector3f t3 = getNormal(p, new Vector3f(p.getX() + 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() + 1));
    Vector3f t4 = getNormal(p, new Vector3f(p.getX() - 1, p.getY(), p.getZ()), new Vector3f(p.getX(), p.getY(), p.getZ() + 1));

You can't extract a normal from a single point. Whatever you're adding and subtracting there, this is not what you're supposed to do.

UPDATE

I think you're mostly strugling with linear algebra basics here. This is not really OpenGL specific. This is math. I can't give you better advice than grabbing some good textbook on the topic and, well, learn it.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • @mproncace: Give me a moment I've written down this in a details StackOverflow answer months ago, I'm looking for it right now. – datenwolf Apr 06 '13 at 19:41
  • @mproncace: I hope you find this usefull: http://stackoverflow.com/a/6661242/524368 – datenwolf Apr 06 '13 at 19:43
  • @mproncace: A single vector by definition has no normal. A normal is the single vector perpendicular to a plane. But for just a vector there's an infinite amount of vectors perpendicular to it. – datenwolf Apr 06 '13 at 19:44
  • @mproncace: If you want to use OpenGL it's mandatory that you learn the math it's based on: Linear Algebra. You must become fluent in terms like "vector spaces", "linear transformations", "vectors", "matrices" and the mathematical operations defined on them. You will have a *very* hard time trying to use OpenGL without knowing those things. – datenwolf Apr 06 '13 at 19:46
  • Whoops, meant to say "vertex" rather than "vector." I read through the post you linked me to, but I still don't understand what I'm doing wrong in my code. My code takes a vertex and uses it to find the surrounding- Ohh... I think I see part of the problem now. My code attempted to find the average of the normals of four congruent triangles sharing a vertex by manipulating the x and z of their intersection. The problem is, they weren't coplanar. But, I still don't exactly see how that's causing a zero-length vector to be generated. How do I average normals? I fail to understand that aspect. – Max Roncace Apr 06 '13 at 19:52
  • @mproncace: First you calculate the normal of each face separately. Then at vertices you just sum up the normals of the faces sharing the vertex and normalize the result. There's no need to add "offsets" or similar stuff. The process is explained **very** detailed in the answer I linked, all you have to do is write it down in the form of code. – datenwolf Apr 06 '13 at 20:17