GR2 Advanced Computer Graphics AGR Lecture 5 Getting Started with OpenGL A Simple Reflection Model GR2-00 1 What is OpenGL? OpenGL provides a set of routines for advanced 3D graphics – derived from Silicon Graphics GL – acknowledged industry standard, even on PCs (OpenGL graphics cards available) – integrates 3D drawing into X (and other window systems such as Windows NT) – draws simple primitives (points, lines, polygons) but NOT complex primitives such as spheres – provides control over transformations, lighting, etc GR2-00 2 Geometric Primitives Defined by a group of vertices - for example to draw a triangle: glBegin (GL_POLYGON); glVertex3i (0, 0, 0); glVertex3i (0, 1, 0); glVertex3i (1, 0, 1); glEnd(); GR2-00 See Chapter 2 of the OpenGL Programming Guide 3 Viewing OpenGL maintains two matrix transformation modes – MODELVIEW to specify modelling transformations, and transformations to align camera – PROJECTION to specify the type of projection (parallel or perspective) and clipping planes GR2-00 See Chapter 3 of OpenGL Programming Guide 4 OpenGL Utility Library (GLU) Useful set of higher level utility routines to make some tasks easier – written in terms of OpenGL and provided with the OpenGL implementation – for example, gluLookAt() is a way of specifying the viewing transformation GR2-00 See Appendix C of OpenGL Programming Guide 5 OpenGL Utility Toolkit (GLUT) Set of routines to provide an interface to the underlying windowing system - plus many useful high-level primitives (even a teapot - glutSolidTeapot()!) Improved version of the ‘aux’ library described in Appendix E of the Guide Allows you to write ‘event driven’ applications – you specify call back functions which are executed when an event (eg window resize) occurs GR2-00 6 How to Get Started Look at the GR2 practicals page: – http://www.scs.leeds.ac.uk/kwb/GR2/ practicals.html Points you to: – example programs – information about GLUT – information about OpenGL – a simple exercise GR2-00 7 A Simple Reflection Model GR2-00 8 What is a Reflection Model? A reflection model (also called lighting or illumination model) describes the interaction between light and a surface, in terms of: – surface properties – nature of incident light Computer graphics uses a simplification of accurate physical models – objective is to mimic reality to an acceptable degree GR2-00 9 Phong Reflection Model The most common reflection model in computer graphics is due to Bui-Tuong Phong - in 1975 Has proved an acceptable compromise between simplicity and accuracy Largely empirical GR2-00 10 Diffuse Reflection and Specular Reflection - Phong Approach white light specular reflection (white) diffuse reflection (yellow) microscopic view GR2-00 yellow pigment particles Some light reflected directly from surface. Other light passes into material. Particles of pigment absorb certain wavelengths from the incident light, but also scatter the light through multiple reflections - some light emerges back through surface as diffuse reflection. 11 Ambient Reflection In addition to diffuse and specular reflection, a scene will also include ambient reflection This is caused by light falling on an object after reflection off other surfaces – eg in a room with a light above a table, the floor below the table will not be totally black, despite having no direct illumination - this is reflection of ambient light GR2-00 12 Reflection Model - Ambient Light hemisphere of ambient light surface P I ( )= Ka ( )Ia() GR2-00 Ia = Intensity of ambient light Ka = Ambient-reflection coefficient I = Reflected intensity = wavelength of light 13 Reflection Model - Diffuse Reflection light source light source P Light reflected equally in all directions - intensity dependent on angle between light source and surface normal Lambert’s cosine law: I = I* cos where I* is intensity of light source GR2-00 P N L light source surface 14 Reflection Model - Diffuse Reflection light source N L Light reflected equally in all directions, with intensity depending on angle between light and surface normal: I = Kd ( cos ) I* GR2-00 surface I* = Intensity of light source N = Surface normal L = Direction of light source Kd = Diffuse-reflection coefficient I = Reflected intensity 15 Reflection Model - Diffuse Reflection light source N L surface The angle between two vectors is given by their dot product: cos = L . N (assume L, N are unit length) The coefficient Kd depends on the wavelength of the incoming light I ( ) = Kd() ( L . N ) I*() GR2-00 16 Reflection Model - Specular Reflection N light source R P In perfect specular reflection, light is only reflected along the unique direction symmetric to the incoming light GR2-00 17 Reflection Model - Specular Reflection N light source R P In practice, light is reflected within a small angle of the perfect reflection direction - the intensity of the reflection tails off at the outside of the cone. This gives a narrow highlight for shiny surfaces, and a broad highlight for dull surfaces. GR2-00 18 Reflection Model - Specular Reflection Thus we want to model intensity, I, as a function of angle between viewer and R, say , like this: I with a sharper peak for shinier surfaces, and broader peak for dull surfaces. GR2-00 19 Reflection Model - Specular Reflection Phong realised this effect can be modelled by: (cos )n with a sharper peak for larger n I n=1 n=10 GR2-00 20 Reflection Model - Specular Reflection eye R light source N V L surface Intensity depends on angle between eye and reflected light ray: I = Ks( cos )n I* GR2-00 n varies with material large n : shiny small n : dull I* = Intensity of light source V = View direction R = Direction of perfect reflected light Ks = Specular-reflection coefficient I = Reflected intensity 21 Reflection Model - Specular Reflection eye R V light source N L surface Using cos = R . V (R, V unit vectors), we have: I () = Ks ( R . V )n I()* Note: Ks does not depend on the wavelength - hence colour of highlight is same as source GR2-00 22 Reflection Model Ambient, Diffuse and Specular eye R V light source N L surface I() = Ka()Ia() + ( Kd()( L . N ) + Ks( R . V )n ) I*() GR2-00 23 Reflection Model - Effect of Distance light source d surface The intensity of light reaching a surface decreases with distance - so we use typically: I* K1 + K2*d + K3*d2 GR2-00 K1, K2, K3 constant - often K2=1, K3=0 24 Final Reflection Model eye R V light source N L d surface I() = Ka()Ia() + ( Kd()( L . N ) + Ks( R . V )n ) I*() K1 + K2*d + K3*d2 This needs to be applied for every light source in the scene GR2-00 25 Phong Model in Practice In practice, some simplifications are made to the model for sake of efficiency For example, ambient light is sometimes assumed to be a constant Other simplifications are: – lights at infinity – simple colour model GR2-00 26 Practicalities - Effect of Distance There are advantages in assuming light source and viewer are at infinity – L and V are then fixed for whole scene and calculations become simpler Lights at infinity are called directional lights Lights at a specified position are called positional, or point, lights GR2-00 27 Practicalities - Calculating R R + L = 2 ( N.L ) N hence R = 2 ( N.L )N - L In practice, implementations often compute H = ( L + V ) / 2 and replace (R.V) with (H.N) N GR2-00 – these are not the same, but compensation is made with choice of n (angle between N and H is half angle between R and V) R V R R L N H L 28 Practicalities - Calculating R As noted, if viewer and light source both sufficiently far from surface, then V and L are constant over scene - and also H Then, for nonplanar surfaces, the calculation: N.H is faster than R . V because R needs to be evaluated at each point in terms of N. GR2-00 29 Practicalities - Effect of Colour The Phong reflection model gives reflection for each wavelength in visible spectrum In practice, we assume light to be composed as a mixture of RGB (red, green, blue) components - and reflection model is applied for each component Coefficients of ambient-reflection (Ka) and diffuse-reflection (Kd) have separate components for RGB Coefficient of specular-reflection (Ks) is independent of colour GR2-00 30 Example - Ambient Reflection GR2-00 31 Example - Ambient and Diffuse GR2-00 32 Ambient, Diffuse and Specular GR2-00 33