Improving the appearance of 3D OpenGL scenes Brian Farrimond Robina Hetherington
What we shall do Hidden line removal Lighting and shading Materials Enable nearer objects to hide distant objects Lighting and shading Improve the sense of depth with shading Materials Add colour Importing 3DSMax models Add realistic models
Hidden line removal Unless told otherwise, OpenGL draws on top of other objects regardless of how far away they are Suppose we want to draw this -----------
First attempt
The effect Because OpenGL draws the cube then the sphere, the sphere is drawn on top even though it is further away Ex07
Hidden line removal OpenGL uses depth-buffering (also known as z-buffering) Depth buffer is created It records for each pixel, the distance from the viewer Initially all set to a very large value
Hidden line removal As each object drawn each pixel is generated its distance from the viewer is compared with the corresponding value in the depth buffer If smaller than the value already there then Pixel is updated Its distance is recorded in the depth buffer Otherwise Pixel is not drawn
Coding in OpenGL 1. In main – create a depth buffer
Coding in OpenGL 2. In reshape – enable depth testing
Coding in OpenGL 3. In display – set the depth buffer to high values
Results – Ex08
Lighting 3D drawing aims to look realistic Realism includes realistic lighting effects Light in the real world is very complicated Optics is a whole branch of Physics Light involves quantum mechanics! OpenGL uses simplified light calculations Results are acceptable
Default lighting OpenGL default is to have no lighting Switch on the lighting with: glEnable(GL_LIGHTING); OpenGL has 8 lights available named GL_LIGHT0, GL_LIGHT1, .., GL_LIGHT7 Switch on a light like this: glEnable(GL_LIGHT0);
Ex09 Here is init modified to switch on GL_LIGHT0
Ex09 Ex08 – no lighting Ex09 – with lighting
Ex09 Sphere has its polygons visible No colours – glColor3f is ignored in a lit scene
Setting the shade model We can make the sphere show its polygons with glShadeModel(GL_FLAT); When drawing a polygon, OpenGL chooses one of the polygons vertices and colours all the polygon's pixels the same colour as this vertex Put the command into init
Setting the shade model We can make the sphere look smoother with glShadeModel(GL_SMOOTH); When drawing a polygon, OpenGL computes a colour for each vertex then colours the polygon's interior pixels by interpolating the vertex values to provide the smooth effect. This is the OpenGL default. Put the command into init
Defining materials to get colour When we use lights we need to specify an object’s colour in a more sophisticated way Interaction of a surface with light is scary physics In OpenGL we simplify by using the concept of material properties
Lights with colour
OpenGL material properties Material property Description Ambient Light scattered by the rest of the scene. It is omnidirectional. Diffuse Reflected light from light sources. The more directly the surface faces a light, the more light is reflected. Emissiveness Light generated by the object. Used mainly to simulate lamps or the Sun in a scene Specular Specular reflection produces highlights which are brightest at the angle of reflection between the light and the viewpoint. Shininess The brightness and sharpness of the highlight.
Specifying diffuse colour Defining colours as 4 element arrays of floating point numbers: red, green, blue, alpha
Specifying diffuse colour Each array element is a floating point number
Specifying diffuse colour Indicates the variable is an array instead of a single value
Specifying diffuse colour Puts values into the array
Specifying diffuse colour Setting the colour for the red cube
Specifying diffuse colour Setting the colour for the green sphere
Notes We need to use arrays to define material colours GL_FRONT specifies that the colour should be applied to the front of the object’s polygons Alternatives are GL_FRONT and GL_FRONT_AND_BACK
Defining the light Default colour is white Default position is (0, 0, 1) Change the position like this …
Array containing x, y, z coords of light position Defining the light Default colour is white Default position is (0, 0, 1) Change the position like this … Array containing x, y, z coords of light position plus w value w = 0 : light at specified point w = 1 : light is at infinity in direction from origin through (x, y, z)
Light is moved to the new position Defining the light Default colour is white Default position is (0, 0, 1) Change the position like this … Light is moved to the new position
Nate Robbins tutorial LightMaterial
ExLoad3DS Illustrates changing the light position interactively
Using 3DS Models in OpenGL Complex models can be imported into OpenGL if the file format is understood 3DSMax has a facility for exporting models as .3ds files. Web sites contain clues as to the structure of this file format In this module we shall use the vertex and polygon information found there
Using 3DS Models in OpenGL We need to Load the 3DS data into a suitable data structure Use the data structure to draw OpenGL polygons
Procedure: adding files Put a copy of the files load3dsbtf.cpp load3dsbtf.h into your project folder Add load3dsbtf.cpp to your project tree
Procedure: programming the OpenGL Add a variable that forms the data structure Use the call loadBTF3DS to load a model from the .3ds file Use the call display3DSObject to display the model in OpenGL
Include file with definitions needed for 3DS ExLoad3ds Include file with definitions needed for 3DS
3DS file may contain more than one object ExLoad3ds 3DS file may contain more than one object
Array of pointers to 3ds object data structures ExLoad3ds Array of pointers to 3ds object data structures
Count of the number of 3ds objects ExLoad3ds Count of the number of 3ds objects
A function to set the colour
Here we load the 3DS object from its file init Here we load the 3DS object from its file
display
Display each of the objects read from the 3DS file