Download Techniques for Finding Normals & Lighting in 3D Graphics and more Slides Fundamentals of E-Commerce in PDF only on Docsity!
Vector Math: Sample code ^ The dot product: float^ operator
*(const^ Vec
&V) {^ return^ m_x*V.x()
+^ m_y*V.y()
+^ m_z*V.z();
}
^ The cross product: Vec^ operator
^(const^ Vec
&V) { Vec^ crossProduct;crossProduct.m_x
=^ m_y*V.m_z
-^ m_z*V.m_y; crossProduct.m_y
=^ m_z*V.m_x
-^ m_x*V.m_z; crossProduct.m_z
=^ m_x*V.m_y
-^ m_y*V.m_x; return^ crossProduct;}
Vector Math ^ Working with points in space:^ ^ To find the angle between two vectors, you dot the vectorsand take the
inverse cosine
( acos() ) of the dot. ^ You can find the angle between two intersecting lines bysubtracting the intersection point from points on the lines,normalising the vectors, and taking the acos() of the dot. ^ To find the line perpendicularto the two lines, subtract thepoints, normalise the vectorswhich result, then cross them. ^ V=B-A goes from A to B!
Surface Normals ^ Sometimes your data isn’t stored as a set ofpolygons.^ ^ If you’re working with an implicit surface, you can still find thesurface normal. You just need to calculate the nearby pointson the surface by nudging u and v a tiny bit.^ ^ Take a look at the GL_QUADS code on week 6’s optionalhandout for an example.
Normal P
P1^ P Normal =(P2-P1) x (P3-P1)Docsity.com
Surface Normals: The Right-Hand Rule ^ One question is: okay, so you can find the normalto a surface. Which side of the surface does itpoint away from? ^ The answer is: use the
Right-Hand Rule
. Curl
your right hand clockwise, from the first vectorbeing crossed towards the second vector beingcrossed. Your thumb points in the direction ofthe normal. The OpenGL default is to assume that thevertices in a polygon are arranged in counterclockwise
(anticlockwise) order around
the normal.
Surface Normals - Why bother? ^ So what’s the point of finding the normal to thesurface at a point or on a face?^ ^ (1) This is the lynchpin to
lighting. ^ (2) It can also be used for
backface culling
, an optimisation
often used to speed up rendering times. Lighting uses surface normals to figure out howthe light should bounce off of a surface at anyparticular point. Backface culling uses normals to test whetherthe face is visible to the user at all; using thisquick test, if the face couldn’t be visible, it won’tbe rendered or processed at all. Speedy!
Lighting ^ Light sources in computer graphics break downinto two categories:^ ^ Point lights^ ^ Directional lights ^ Light itself breaks down into three types of light:^ ^ Ambient light- The omnidirectional, floating, everywhere lighting^ ^ Diffuse light- Light that gets scattered around by the surface^ ^ Specular light- Shiny light that bounces to the eye of the viewer
Lighting
Positioning the light ^ The position of the light is irrelevant in theambient lighting calculations. ^ The position of the light is relevant in calculatingdiffuse lighting, but the position of the camera isnot. ^ The position of the light AND the position of thecamera are both used in calculating specularhighlights.^ Ambient
Diffuse^
Specular