Understanding lighting in OpenGL

后端 未结 1 755
北海茫月
北海茫月 2020-12-07 02:51

I am trying to create a 3D sphere out of a bunch of triangles with Haskell / GLUT. It works quite nicely: The green one is \"my\" sphere, the red one is done with GLUT\'s re

相关标签:
1条回答
  • 2020-12-07 03:34
    1. Surface normals are crucial for lighting equations

      Normal to surface is vector perpendicular to surface. For triangle is computed by cross product of any its 2 vertices vectors so if triangle points are p0,p1,p2 then normal is n=cross(p1-p0,p2-p1) or any other combination.

      Normals tells which way is pixel/face/polygon turned usually dot product with light direction is computed by render engine that gives a cos(angle_between light and surface normal). This number is the scale of amount of light hitting the surface when multiplied with light source strength you got the light color ... with combination of surface color render get the pixel color there are many light models this one was very simple (normal shading).

      To make the dot product work the normal should be unit vector so divide it by its length n=n/|n|

      Here small example of normals

      example

      For sphere the normal is easy normal n for any point p is n=(p-center)/radius

    2. If normal does not correspond with surface

      then you can do light effects like visually smooth sharp edges of mesh. for example how Look here:

      • smoothing normals

      also the exact opposite can be achieved (smooth mesh but sharp edge render)

    3. OpenGL interface

      old style gl uses something like glNormal3f(nx,ny,nz); The VBO/VAO/arrays knows normals too. In new style glNormal is depreceated like most parameters so you need to bind it to your custom layout on your own

    4. Normal direction

      any surface has 2 possible direction of perpendicular normal to it. Usually the one pointing outwards from mesh is used. Sometimes for 3D curves is double sided material used that means that the dot product is handled as abs value so it does not matter which way the normal is pointing. Without this the opposite side of surface will be always dark

      So if you have normals and no lighting is visible then try to negate normals

    0 讨论(0)
提交回复
热议问题