Drawing normals in LWJGL messes with lighting

青春壹個敷衍的年華 提交于 2019-12-06 07:07:18
Abaab

When lighting is enabled in OpenGL, all polygons will be lit according to their normals. If no normals are given to a polygon, that object's normal will default to the vec3 {0, 0, 1}. Basically, enable lighting, draw your model, then disable lighting and draw the rest of your scene.

Calculating per-face normals is fairly easy, here is a little getNormal() function I wrote:

//Feel free to use this for whatever you want, no licenses applied or anything.

//p1, p2, p3 - Vertices of triangle
public Vector3f getNormal(Vector3f p1, Vector3f p2, Vector3f p3) {

    //Create normal vector we are going to output.
    Vector3f output = new Vector3f();

    //Calculate vectors used for creating normal (these are the edges of the triangle).
    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);

    //The output vector is equal to the cross products of the two edges of the triangle
    output.x = calU.y*calV.z - calU.z*calV.y;
    output.y = calU.z*calV.x - calU.x*calV.z;
    output.z = calU.x*calV.y - calU.y*calV.x;

    //Return the resulting vector.
    return output.normalise();
}

[OpenGL Documentation]

[Another similar question]

[More on the cross product]

But... per-face normals can look sort of... bad. If you want a smoother result, you should use per-vertex normals. Here's the difference:

Per-Face:

Per-Vertex:

Make sure not to use per-face normals for things like cubes, cubes are flat, not curved. Per-Vertex normals are to be used in situations where you want to make something look more smooth and realistic (skin, potato, pillow, etc.).

For calculating per-vertex normals, you basically average all the normals of the joined polygons like so:

If you happen to get the model looking entirely black or the parts facing you are invisible, try reversing the order that you put the points into the getNormal() function (or negating the normal, the error is due to the fact that the normal is facing inward, not outward).

Hoped this helped!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!