improving the appearance of 3d opengl scenes

Post on 17-Jan-2018

241 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

1

Improving the appearance of 3D OpenGL scenes

Brian FarrimondRobina Hetherington

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

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 -----------

4

First attempt

5

The effect

• Because OpenGL draws the cube then the sphere, the sphere is drawn on top even though it is further away

• Ex07

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

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

8

Coding in OpenGL

• 1. In main – create a depth buffer

9

Coding in OpenGL

• 2. In reshape – enable depth testing

10

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

11

Results – Ex08

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

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);

14

Ex09

• Here is init modified to switch on GL_LIGHT0

15

Ex09

Ex09 – with lightingEx08 – no lighting

16

Ex09

• Sphere has its polygons visible

• No colours – glColor3f is ignored in a lit scene

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

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

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

20

Lights with colour

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.

22

Specifying diffuse colour

Defining coloursas 4 element arrays of floating point numbers:

red,green,blue,alpha

23

Specifying diffuse colour

Each array element is a floating point

number

24

Specifying diffuse colour

Indicates the variable is an

array instead of a single value

25

Specifying diffuse colour

Puts values into the array

26

Specifying diffuse colour

Setting the colour for the red

cube

27

Specifying diffuse colour

Setting the colour for the green sphere

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

29

Defining the light

• Default colour is white• Default position is (0, 0, 1)• Change the position like this …

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)

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

32

Nate Robbins tutorial

• LightMaterial

33

ExLoad3DS

• Illustrates changing the light position interactively

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

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

36

Procedure: adding files

• Put a copy of the filesload3dsbtf.cppload3dsbtf.h

into your project folder• Add load3dsbtf.cpp to your project tree

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

38

ExLoad3ds

Include file with definitions needed for 3DS

39

ExLoad3ds

3DS file may contain more than one object

40

ExLoad3ds

Array of pointers to 3ds object data structures

41

ExLoad3ds

Count of the number of 3ds objects

42

A function to set the colour

43

init

Here we load the 3DS object from its

file

44

display

45

display

Display each of the objects read from the

3DS file

top related