Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC Graphics Programming

Similar presentations


Presentation on theme: "CSC Graphics Programming"— Presentation transcript:

1 CSC 307 1.0 Graphics Programming
Budditha Hettige Department of Statistics and Computer Science

2 08 Lighting

3 Real-World and OpenGL Lighting

4 Using a Light Source

5 An Interactive Introduction to OpenGL Programming
Lighting Principles Lighting simulates how objects reflect light material composition of object light’s color and position global lighting parameters ambient light two sided lighting available in both color index and RGBA mode Lighting is an important technique in computer graphics. Without lighting, objects tend to look like they’re made out of plastic. OpenGL divides lighting into three parts: material properties, light properties and global lighting parameters. Lighting is available in both RGBA mode and color index mode. RGBA is more flexible and less restrictive than color index mode lighting.

6 How OpenGL Simulates Lights
An Interactive Introduction to OpenGL Programming How OpenGL Simulates Lights Phong lighting model Computed at vertices Lighting contributors Surface material properties Light properties Lighting model properties OpenGL lighting is based on the Phong lighting model. At each vertex in the primitive, a color is computed using that primitives material properties along with the light settings. The color for the vertex is computed by adding four computed colors for the final vertex color. The four contributors to the vertex color are: Ambient is color of the object from all the undirected light in a scene. Diffuse is the base color of the object under current lighting. There must be a light shining on the object to get a diffuse contribution. Specular is the contribution of the shiny highlights on the object. Emission is the contribution added in if the object emits light (i.e. glows)

7 OpenGL Lights Common Properties Types Ambient Diffuse Specular
Directional Light Point Light Spotlight

8 Lighting….

9 An Interactive Introduction to OpenGL Programming
Surface Normals CPU DL Poly. Per Vertex Raster Frag FB Pixel Texture Normals define how a surface reflects light glNormal3f( x, y, z ) Current normal is used to compute vertex’s color Use unit normals for proper lighting scaling affects a normal’s length glEnable( GL_NORMALIZE ) or glEnable( GL_RESCALE_NORMAL ) The lighting normal tells OpenGL how the object reflects light around a vertex. If you imagine that there is a small mirror at the vertex, the lighting normal describes how the mirror is oriented, and consequently how light is reflected. glNormal*() sets the current normal, which is used in the lighting computation for all vertices until a new normal is provided. Lighting normals should be normalized to unit length for correct lighting results. glScale*() affects normals as well as vertices, which can change the normal’s length, and cause it to no longer be normalized. OpenGL can automatically normalize normals, by enabling glEnable(GL_NORMALIZE). or glEnable(GL_RESCALE_NORMAL). GL_RESCALE_NORMAL is a special mode for when your normals are uniformly scaled. If not, use GL_NORMALIZE which handles all normalization situations, but requires the computation of a square root, which can potentially lower performance OpenGL evaluators and NURBS can provide lighting normals for generated vertices automatically.

10 An Interactive Introduction to OpenGL Programming
Material Properties Define the surface properties of a primitive glMaterialfv( face, property, value ); separate materials for front and back GL_DIFFUSE Base color GL_SPECULAR Highlight Color GL_AMBIENT Low-light Color GL_EMISSION Glow Color GL_SHININESS Surface Smoothness Material properties describe the color and surface properties of a material (dull, shiny, etc.). OpenGL supports material properties for both the front and back of objects, as described by their vertex winding. The OpenGL material properties are: GL_DIFFUSE - base color of object GL_SPECULAR - color of highlights on object GL_AMBIENT - color of object when not directly illuminated GL_EMISSION - color emitted from the object (think of a firefly) GL_SHININESS - concentration of highlights on objects. Values range from 0 (very rough surface - no highlight) to 128 (very shiny) Material properties can be set for each face separately by specifying either GL_FRONT or GL_BACK, or for both faces simultaneously using GL_FRONT_AND_BACK.

11 An Interactive Introduction to OpenGL Programming
Light Properties glLightfv( light, property, value ); light specifies which light multiple lights, starting with GL_LIGHT0 glGetIntegerv( GL_MAX_LIGHTS, &n ); properties colors position and type attenuation The glLight() call is used to set the parameters for a light. OpenGL implementations must support at least eight lights, which are named GL_LIGHT0 through GL_LIGHTn, where n is one less than the maximum number supported by an implementation. OpenGL lights have a number of characteristics which can be changed from their default values. Color properties allow separate interactions with the different material properties. Position properties control the location and type of the light and attenuation controls the natural tendency of light to decay over distance.

12 Surface lighting Surface lighting properties
Values defined in RGB colorspace Ambient Light reaching all points on a surface Diffuse Light scattering from rough surfaces Specular Light reflection from shiny surfaces Shininess Defines the spread of the specular term Larger values have more “mirror” like specularity

13 Lighting model Light Properties Ambient Diffuse Specular
Color and intensity of light that interacts with a surface materials ambient property Diffuse Color and intensity of light that interacts with a surface materials diffuse property Specular Color and intensity of light that interacts with a surface materials specular property

14 Lighting model Ambient Diffuse + Specular + Combination =

15 Controlling a Light’s Position
An Interactive Introduction to OpenGL Programming Controlling a Light’s Position Modelview matrix affects a light’s position Different effects based on when position is specified eye coordinates world coordinates model coordinates Push and pop matrices to uniquely control a light’s position As mentioned previously, a light’s position is transformed by the current ModelView matrix when it is specified. As such, depending on when you specify the light’s position, and what’s in the ModelView matrix, you can obtain different lighting affects. In general, there are three coordinate systems where you can specify a light’s position/direction 1) Eye coordinates - which is represented by an identity matrix in the ModelView. In this case, when the light’s position/direction is specified, it remains fixed to the imaging plane. As such, regardless of how the objects are manipulated, the highlights remain in the same location relative to the eye. 2) World Coordinates - when only the viewing transformation is in the ModelView matrix. In this case, a light’s position/direction appears fixed in the scene, as if the light were on a lamppost. 3) Model Coordinates - any combination of viewing and modeling transformations is in the ModelView matrix. This method allows arbitrary, and even animated, position of a light using modeling transformations.

16 GLUT simple geometry GLUT can render simple geometry
Sphere, Box, Cone, Torus, Teapot, etc void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks); radius: radius of the sphere slices: number of longitudinal subdivitions stacks: number of latitudinal subdivitions void glutSolidCube(GLdouble size); size: length of all sides of the cube

17 Tips for Better Lighting
An Interactive Introduction to OpenGL Programming Tips for Better Lighting Recall lighting computed only at vertices model tessellation heavily affects lighting results better results but more geometry to process Use a single infinite light for fastest lighting minimal computation per vertex As with all of computing, time versus space is the continual tradeoff. To get the best results from OpenGL lighting, your models should be finely tessellated to get the best specular highlights and diffuse color boundaries. This yields better results, but usually at a cost of more geometric primitives, which could slow application performance. To achieve maximum performance for lighting in your applications, use a single infinite light source. This minimizes the amount of work that OpenGL has to do to light every vertex.

18 Setting up state for lighting
Lighting and lights are disabled by default Enabling lighting overrides the use of assigned colors from glColor calls glShadeModel(GL_SMOOTH); Set Gouraud shading glShadeModel(GL_FLAT); Set Flat Shading glEnable(GL_LIGHTING); Enable fixed function lighting glEnable(GL_LIGHT0); Enable light 0 in the scene

19 Hidden Surface removal
First Enable Culling glEnable(GL_CULL_FACE); Define which face to cull glCullFace(GLenum face); Face: The polygon facing GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

20 Assigning Material Properties
void glMaterialfv( GLenum face, GLenum pname, const GLfloat * params); face: The geometry facing to assign GL_FRONT,GL_BACK, or GL_FRONT_AND_BACK pname: The material property to assign GL_AMBIENT,GL_DIFFUSE,GL_SPECULAR, and others params: pointer to an array of values

21 OpenGl calls array pointers
Some sets of OpenGL methods take multiple numbers and types of arguments glVertex2i glMaterialfv Number of arguments Type of argument Method takes an array pointer argument GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);

22 Assign Material Property
//Define GLfloat arrays to hold material data GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; //Shininess will be an array of length 1 GLfloat mat_shininess[] = { 50.0 }; //Apply the material properties to the front facing glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

23 Assigning Light Properties
Similar to Materials void glLightfv(GLenum light, GLenum pname, const GLfloat * params); light: The light to assign GL_LIGHT0, GL_LIGHT1, …, GL_LIGHT8 pname: The light property to assign GL_AMBIENT,GL_DIFFUSE, GL_SPECULAR, and others params: pointer to an array of values

24 Assigning Light Properties
//Define GLfloat arrays to hold light data GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat light_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; //Apply the light properties to LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

25 For Positions this should be 1.0
Point Light A light source originating from a zero-volume point in the scene Casts light in all directions For Positions this should be 1.0 Position //Light position GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; //Apply the light position glLightfv(GL_LIGHT0, GL_POSITION, light_position);

26 Directional Light A light infinitely far away from the drawn scene
Used most often for emulating sunlight Distance from sun to earth is large Light direction can be considered the same Direction -should be normalized- For Directions this should be 0.0 //Light direction down the negative x axis GLfloat light_direction[] = { -1.0, 0.0, 0.0, 0.0 }; //Apply the light direction glLightfv(GL_LIGHT0, GL_POSITION, light_ambient);

27 Spotlights

28 Spotlight A light source originating from a zero-volume point in the scene Direction Direction the light is focused on Cutoff angle that defines light cone Exponent Concentration of the light Brightest around the center

29 Spotlight example //Spotlight properties GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; GLfloat light_spot_direction[] = { 0.0, 0.0, -1.0}; GLfloat light_spot_cutoff[] = { 25.0 }; GLfloat light_spot_exp[] = { 2.0 }; //Apply the light position glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION , light_spot_direction); glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF , light_spot_cufoff); glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT , light_spot_exp);

30 Shading Flat Shading Flat shading selects the computed color of just one vertex

31 Shading Gouraud Shading Lighting color calculated per-vertex
saves computation Vertex color is bilinearly interpolated across polygon

32 Flat vs Gouraud shading

33 Drawing a Smooth-Shaded Triangle
// Enable smooth shading glShadeModel(GL_SMOOTH); // Draw the triangle glBegin(GL_TRIANGLES); // Red Apex glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0); glVertex3f(0.0f,200.0f,0.0f); // Green on the right bottom corner glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0); glVertex3f(200.0f,-70.0f,0.0f); // Blue on the left bottom corner glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255); glVertex3f(-200.0f, -70.0f, 0.0f); glEnd();

34 Example Lighting01.cpp


Download ppt "CSC Graphics Programming"

Similar presentations


Ads by Google