Download - Improving the appearance of 3D OpenGL scenes
![Page 1: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/1.jpg)
1
Improving the appearance of 3D OpenGL scenes
Brian FarrimondRobina Hetherington
![Page 2: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/2.jpg)
2
What 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
![Page 3: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/3.jpg)
3
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 -----------
![Page 4: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/4.jpg)
4
First attempt
![Page 5: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/5.jpg)
5
The effect
• Because OpenGL draws the cube then the sphere, the sphere is drawn on top even though it is further away
• Ex07
![Page 6: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/6.jpg)
6
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
![Page 7: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/7.jpg)
7
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
![Page 8: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/8.jpg)
8
Coding in OpenGL
• 1. In main – create a depth buffer
![Page 9: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/9.jpg)
9
Coding in OpenGL
• 2. In reshape – enable depth testing
![Page 10: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/10.jpg)
10
Coding in OpenGL• 3. In display – set the depth buffer to high values
![Page 11: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/11.jpg)
11
Results – Ex08
![Page 12: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/12.jpg)
12
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
![Page 13: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/13.jpg)
13
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);
![Page 14: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/14.jpg)
14
Ex09
• Here is init modified to switch on GL_LIGHT0
![Page 15: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/15.jpg)
15
Ex09
Ex09 – with lightingEx08 – no lighting
![Page 16: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/16.jpg)
16
Ex09
• Sphere has its polygons visible
• No colours – glColor3f is ignored in a lit scene
![Page 17: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/17.jpg)
17
Setting the shade model
• We can make the sphere show its polygons withglShadeModel(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
![Page 18: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/18.jpg)
18
Setting the shade model• We can make the sphere look
smoother withglShadeModel(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
![Page 19: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/19.jpg)
19
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
![Page 20: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/20.jpg)
20
Lights with colour
![Page 21: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/21.jpg)
21
OpenGL material propertiesMaterial 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.
![Page 22: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/22.jpg)
22
Specifying diffuse colour
Defining coloursas 4 element arrays of floating point numbers:
red,green,blue,alpha
![Page 23: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/23.jpg)
23
Specifying diffuse colour
Each array element is a floating point
number
![Page 24: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/24.jpg)
24
Specifying diffuse colour
Indicates the variable is an
array instead of a single value
![Page 25: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/25.jpg)
25
Specifying diffuse colour
Puts values into the array
![Page 26: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/26.jpg)
26
Specifying diffuse colour
Setting the colour for the red
cube
![Page 27: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/27.jpg)
27
Specifying diffuse colour
Setting the colour for the green sphere
![Page 28: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/28.jpg)
28
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
![Page 29: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/29.jpg)
29
Defining the light
• Default colour is white• Default position is (0, 0, 1)• Change the position like this …
![Page 30: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/30.jpg)
30
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 valuew = 0 : light at specified pointw = 1 : light is at infinity in direction from origin through (x, y, z)
![Page 31: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/31.jpg)
31
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
![Page 32: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/32.jpg)
32
Nate Robbins tutorial
• LightMaterial
![Page 33: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/33.jpg)
33
ExLoad3DS
• Illustrates changing the light position interactively
![Page 34: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/34.jpg)
34
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
![Page 35: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/35.jpg)
35
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
![Page 36: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/36.jpg)
36
Procedure: adding files
• Put a copy of the filesload3dsbtf.cppload3dsbtf.h
into your project folder• Add load3dsbtf.cpp to your project tree
![Page 37: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/37.jpg)
37
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
![Page 38: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/38.jpg)
38
ExLoad3ds
Include file with definitions needed for 3DS
![Page 39: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/39.jpg)
39
ExLoad3ds
3DS file may contain more than one object
![Page 40: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/40.jpg)
40
ExLoad3ds
Array of pointers to 3ds object data structures
![Page 41: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/41.jpg)
41
ExLoad3ds
Count of the number of 3ds objects
![Page 42: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/42.jpg)
42
A function to set the colour
![Page 43: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/43.jpg)
43
init
Here we load the 3DS object from its
file
![Page 44: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/44.jpg)
44
display
![Page 45: Improving the appearance of 3D OpenGL scenes](https://reader033.vdocuments.site/reader033/viewer/2022050806/5a4d1b187f8b9ab0599927e2/html5/thumbnails/45.jpg)
45
display
Display each of the objects read from the
3DS file