Title: Improving the appearance of 3D OpenGL scenes
1Improving the appearance of 3D OpenGL scenes
- Brian Farrimond
- Robina Hetherington
2What we shall do
- Hidden line removal
- 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
3Hidden 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 -----------?
4First attempt
5The effect
- Because OpenGL draws the cube then the sphere,
the sphere is drawn on top even though it is
further away - Ex07
6Hidden 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
7Hidden 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
8Coding in OpenGL
- 1. In main create a depth buffer
9Coding in OpenGL
- 2. In reshape enable depth testing
10Coding in OpenGL
- 3. In display set the depth buffer to high
values
11Results Ex08
12Lighting
- 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
13Default 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)
14Ex09
- Here is init modified to switch on GL_LIGHT0
15Ex09
Ex08 no lighting
16Ex09
- Sphere has its polygons visible
- No colours glColor3f is ignored in a lit scene
17Setting 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
18Setting 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
19Defining materials to get colour
- When we use lights we need to specify an objects
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
20Lights with colour
21OpenGL material properties
22Specifying diffuse colour
Defining colours as 4 element arrays of floating
point numbers red, green, blue, alpha
23Specifying diffuse colour
Each array element is a floating point number
24Specifying diffuse colour
Indicates the variable is an array instead of a
single value
25Specifying diffuse colour
Puts values into the array
26Specifying diffuse colour
Setting the colour for the red cube
27Specifying diffuse colour
Setting the colour for the green sphere
28Notes
- We need to use arrays to define material colours
- GL_FRONT specifies that the colour should be
applied to the front of the objects polygons - Alternatives are GL_FRONT and GL_FRONT_AND_BACK
29Defining the light
- Default colour is white
- Default position is (0, 0, 1)
- Change the position like this
30Defining 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)
31Defining the light
- Default colour is white
- Default position is (0, 0, 1)
- Change the position like this
Light is moved to the new position
32Nate Robbins tutorial
33ExLoad3DS
- Illustrates changing the light position
interactively
34Using 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
35Using 3DS Models in OpenGL
- We need to
- Load the 3DS data into a suitable data structure
- Use the data structure to draw OpenGL polygons
36Procedure adding files
- Put a copy of the files
- load3dsbtf.cpp
- load3dsbtf.h
- into your project folder
- Add load3dsbtf.cpp to your project tree
37Procedure 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
38ExLoad3ds
Include file with definitions needed for 3DS
39ExLoad3ds
3DS file may contain more than one object
40ExLoad3ds
Array of pointers to 3ds object data structures
41ExLoad3ds
Count of the number of 3ds objects
42A function to set the colour
43init
Here we load the 3DS object from its file
44display
45display
Display each of the objects read from the 3DS file