Illumination and Shading Sang Il Park Sejong University
코딩 연습 Phone Illumination Model을 직접 구현해 본다 (GL_LIGHTING 을 끈 상태로 직접 색을 계산하여 그려본다) 조명을 설정한다. (위치(lx,ly,lz) 및 Intensity) 도형의 색을 설정 (ka, kd, ks) 카메라의 위치 설정 (vx, vy, vz) 그리고 있는 삼각형의 Normal Vector 계산 Phong Model의 각각의 성분 계산 (ambient, diffuse, specular) 계산
OpenGL Lighting Functions Using Illumination/Shading Model: Turn on/off Lights glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glEnable (GL_LIGHT1); … glEnable (GL_LIGHT7); glDisable (GL_LIGHT0); glDisable (GL_LIGHT1); … glDisable (GL_LIGHT7);
OpenGL Lighting Functions glLight * (lightName,lightProperty, propertyValue) lightName: GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, …., GL_LIGHT7
OpenGL Lighting Functions Point Light Source: GLfloat light1pos [] = {2.0, 0.0, 3.0, 1.0}; GLfloat light2pos [] = {0.0, 1.0, 0.0, 0.0}; glLightfv (GL_LIGHT0, GL_POSITION, light1pos); glEnable (GL_LIGHT0); glLightfv (GL_LIGHT1, GL_POSITION, light2pos); glEnable (GL_LIGHT1); Default value: GLfloat light0pos [] = {0.0, 0.0, 1.0, 0.0};
OpenGL Lighting Functions Point Light Source Colors: GLfloat ambientColor[]={0.0,0.0,0.0,1.0}; GLfloat diffuseColor[]={1.0,1.0,1.0,1.0}; GLfloat specularColor[]={1.0,1.0,1.0,1.0}; glLightfv (GL_LIGHT0, GL_AMBIENT, ambientColor); glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuseColor); glLightfv (GL_LIGHT0, GL_SPECULAR, specularColor);
OpenGL Lighting Functions Global Light Setting GLfloat globalAmbient[] = {0.2,0.2,0.2,1.0}; glLightModelfv (GL_LIGHT_MODEL_AMBIENT, globalAmbient);
OpenGL Lighting Functions Point Light Source Attenuation: glLightf (GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.5); glLightf (GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.75); glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.4);
OpenGL Lighting Functions Spot Light Source: Glfloat dirVector [] = {1.0, 0.0, 0.0}; glLightfv (GL_LIGHT0, GL_SPOT_DIRECTION, dirVector); glLightfv (GL_LIGHT0, GL_SPOT_CUTOFF, 3.0); glLightfv (GL_LIGHT0, GL_SPOT_EXPONENT, 2.5);
OpenGL Material Functions glMaterial * (surface, sufProperty, propertyValue) GLfloat diffuseCoeff [] = {0.2,0.4,0.9,1.0}; GLfloat specularCoeff [] ={1.0,1.0,1.0,1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuseCoeff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularCoeff); glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 25.0);
OpenGL Material Functions How to apply some of the properties by glMeterial glColorMaterial(surface, mode) surface: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK mode: GL_EMISSION, GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_AMBIENT_AND_DIFFUSE. ? glEnable(GL_COLOR_MATERIAL) VS. glDisable(GL_COLOR_MATERIAL)
OpenGL Lighting Model
Tips for a quick lighting setting 1 Set GL_LIGHT_0's position to something like 45 degrees to the 'vertical'. Coordinate (1,1,0) should work nicely in most cases. Set GL_LIGHT_0's Ambient color to 0,0,0,1 Set GL_LIGHT_0's Diffuse color to 1,1,1,1 Set GL_LIGHT_0's Specular color to 1,1,1,1 Set the glLightModel's global ambient to 0.2,0.2,0.2,1 (this is the default). Don't set any other glLight or glLightModel options - just let them default. Enable GL_LIGHTING and GL_LIGHT_0.
Tips for a quick lighting setting 1 Enable GL_COLOR_MATERIAL and set glColorMaterial to GL_AMBIENT_AND_DIFFUSE. This means that glMaterial will control the polygon's specular and emission colours and the ambient and diffuse will both be set using glColor. Set the glMaterial's Specular colour to 1,1,1,1 Set the glMaterial's Emission colour to 0,0,0,1 Set the glColor to whatever colour you want each polygon to basically appear to be. That sets the Ambient and Diffuse to the same value which is what you generally want.
Atmospheric Effects A hazy atmosphere makes colors fade and objects appear dimmer Hazy-atmosphere effect is often simulated with an exponential attenuation function such as Higher values for r produce a denser atmosphere
A solution for the trackball interface
How to use your mouse There is a callback function for mouse in GLUT Registration: Implementation button: GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON action: GLUT_DOWN, GLUT_UP glutMouseFunc ( myMouse); void myMouse (GLint button, GLint action, GLint xpos, GLint ypos ); 교재 749page 참조
How to Rotate? Virtual trackball A trackball translates 2D mouse movements into 3D rotations This is done by projecting the position of the mouse on to an imaginary sphere behind the viewport As the mouse is moved the camera (or scene) is rotated to keep the same point on the sphere underneath the mouse pointer
How to Rotate? Virtual trackball
Review: OpenGL Geometric Transformations Getting the current matrix value: glGetFloatv (GL_MODELVIEW_MATRIX, GLfloat elems[16]); Column major 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15 GLfloat mat [16]; glGetFloatv (GL_MODELVIEW_MATRIX, mat);
Review: OpenGL Geometric Transformations Matrix Direct Manipulation: glLoadMatrixf(GLfloat elems[16]); Column major glMultMatrixf(GLfloat elems[16]); The current matrix is postmultiplied by the matrix 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15 glLoadIdentity(); glMultMatrixf (M1); glMultMatrixf (M2); M = M1∙M2
Sang Il Park SEjong University Ray Tracing Sang Il Park SEjong University With lots of slides stolen from Jehee Lee, Doug James, Steve Seitz, Shree Nayar, Alexei Efros, Fredo Durand and others
Local vs. Global Illumination Models Local illumination models Object illuminations are independent No light scattering between objects No real shadows, reflection, transmission Global illumination models Ray tracing (highlights, reflection, transmission) Radiosity (Surface interreflections) Photon mapping
Forward Ray Tracing Rays as paths of photons in world space Follow photon from light sources to viewer Problem: Many rays will not contribute to image
Backward Ray Tracing Trace rays backward from viewer to light sources One ray from center of projection through each pixel in image plane Ray casting Simplest form of ray tracing No recursion
Backward Ray Tracing Illumination Phong illumination Shadow rays Specular reflection Specular refraction Specular reflection and refraction are recursive
Shadow Rays Determine if light “really” hits surface point Cast shadow ray from surface point to light If shadow ray hits opaque object, no contribution
Specular Refraction (Snell’s law) , : index of refraction of each material (averaged over wavelengths and temperature)
Specular Refraction (Snell’s law) , : index of refraction of each material (averaged over wavelengths and temperature)
Specular Refraction Path shifts are ignored for thin objects From Snell’s law, we can obtain the unit transmission vector T in the direction
Interpolated transparency : transmission coefficient (0 for opaque objects, 1 for totally transparent objects) 2 1 line of sight
Binary Ray-Tracing Tree
Ray-surface Intersections Specialized algorithm for most commonly occurring shapes Sphere Polygon Quadric Splines Many shapes are represented in either implicit or parametric form
Ray-Implicit Surface Intersections Parametric ray equation Initial position Unit direction vector Implicit surface Consists of all points such that Substitute ray equation for Solve for s (univariate root finding)
Ray-Sphere Intersections Sphere equation Substitution Letting Solution
Ray-Polygon Intersections Bounding sphere and back face culling is useful Ray-Plane Intersections Plane equation containing the polygon Substitution Solution Perform inside-outside test to determine whether the intersection is inside the polygon
Acceleration Techniques Space-subdivision Uniform subdivision Adaptive subdivision (Octrees)