improving the appearance of 3d opengl scenes

45
1 Improving the appearance of 3D OpenGL scenes Brian Farrimond Robina Hetherington

Upload: sabrina-logan

Post on 17-Jan-2018

239 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Improving the appearance of 3D OpenGL scenes

1

Improving the appearance of 3D OpenGL scenes

Brian FarrimondRobina Hetherington

Page 2: Improving the appearance of 3D OpenGL scenes

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

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

4

First attempt

Page 5: Improving the appearance of 3D OpenGL scenes

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

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

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

8

Coding in OpenGL

• 1. In main – create a depth buffer

Page 9: Improving the appearance of 3D OpenGL scenes

9

Coding in OpenGL

• 2. In reshape – enable depth testing

Page 10: Improving the appearance of 3D OpenGL scenes

10

Coding in OpenGL• 3. In display – set the depth buffer to high values

Page 11: Improving the appearance of 3D OpenGL scenes

11

Results – Ex08

Page 12: Improving the appearance of 3D OpenGL scenes

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

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

14

Ex09

• Here is init modified to switch on GL_LIGHT0

Page 15: Improving the appearance of 3D OpenGL scenes

15

Ex09

Ex09 – with lightingEx08 – no lighting

Page 16: Improving the appearance of 3D OpenGL scenes

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

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

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

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

20

Lights with colour

Page 21: Improving the appearance of 3D OpenGL scenes

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

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

23

Specifying diffuse colour

Each array element is a floating point

number

Page 24: Improving the appearance of 3D OpenGL scenes

24

Specifying diffuse colour

Indicates the variable is an

array instead of a single value

Page 25: Improving the appearance of 3D OpenGL scenes

25

Specifying diffuse colour

Puts values into the array

Page 26: Improving the appearance of 3D OpenGL scenes

26

Specifying diffuse colour

Setting the colour for the red

cube

Page 27: Improving the appearance of 3D OpenGL scenes

27

Specifying diffuse colour

Setting the colour for the green sphere

Page 28: Improving the appearance of 3D OpenGL scenes

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

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

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

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

32

Nate Robbins tutorial

• LightMaterial

Page 33: Improving the appearance of 3D OpenGL scenes

33

ExLoad3DS

• Illustrates changing the light position interactively

Page 34: Improving the appearance of 3D OpenGL scenes

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

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

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

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

38

ExLoad3ds

Include file with definitions needed for 3DS

Page 39: Improving the appearance of 3D OpenGL scenes

39

ExLoad3ds

3DS file may contain more than one object

Page 40: Improving the appearance of 3D OpenGL scenes

40

ExLoad3ds

Array of pointers to 3ds object data structures

Page 41: Improving the appearance of 3D OpenGL scenes

41

ExLoad3ds

Count of the number of 3ds objects

Page 42: Improving the appearance of 3D OpenGL scenes

42

A function to set the colour

Page 43: Improving the appearance of 3D OpenGL scenes

43

init

Here we load the 3DS object from its

file

Page 44: Improving the appearance of 3D OpenGL scenes

44

display

Page 45: Improving the appearance of 3D OpenGL scenes

45

display

Display each of the objects read from the

3DS file