Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>{0, 0, 1}</code>. Basically, enable lighting, draw your model, then disable lighting and draw the rest of your scene.</p> <p>Calculating per-face normals is fairly easy, here is a little getNormal() function I wrote:</p> <pre><code>//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(); } </code></pre> <p><a href="http://www.opengl.org/wiki/Calculating_a_Surface_Normal" rel="nofollow noreferrer">[OpenGL Documentation]</a></p> <p><a href="https://math.stackexchange.com/questions/305642/how-to-find-surface-normal-of-a-triangle">[Another similar question]</a></p> <p><a href="http://freespace.virgin.net/hugo.elias/routines/r_cross.htm" rel="nofollow noreferrer">[More on the cross product]</a></p> <p>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:</p> <p>Per-Face: <img src="https://i.stack.imgur.com/LzD9z.jpg" alt="You can see the triangles..."></p> <p>Per-Vertex: <img src="https://i.stack.imgur.com/QuuU1.jpg" alt="enter image description here"></p> <p>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.).</p> <p>For calculating per-vertex normals, you basically average all the normals of the joined polygons like so:</p> <p><img src="https://i.stack.imgur.com/9l3xF.gif" alt="It&#39;s de average"></p> <p>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).</p> <p>Hoped this helped!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload