Hidden Surface & Lighting

27
Hidden Surface & Lighting Presentation modified from Presentation modified from a presentation at a presentation at OpenGL.org OpenGL.org

description

Hidden Surface & Lighting. Presentation modified from a presentation at OpenGL.org. Hidden Surface. Hidden Surface Removal is very important in 3D scenes Only surfaces closest to the eye should be seen and objects that are hidden by others should be eliminated. - PowerPoint PPT Presentation

Transcript of Hidden Surface & Lighting

Page 1: Hidden Surface & Lighting

Hidden Surface & LightingHidden Surface & Lighting

Presentation modified from Presentation modified from a presentation at a presentation at

OpenGL.orgOpenGL.org

Page 2: Hidden Surface & Lighting

2

Hidden SurfaceHidden Surface

Hidden Surface Removal is very Hidden Surface Removal is very important in 3D scenesimportant in 3D scenes

Only surfaces closest to the eye Only surfaces closest to the eye should be seen and objects that are should be seen and objects that are hidden by others should be hidden by others should be eliminated.eliminated.

The use of a depth buffer facilitates The use of a depth buffer facilitates hidden surface removal.hidden surface removal.

Page 3: Hidden Surface & Lighting

3

Hidden Surface RemovalHidden Surface Removal

To use depth-buffering, you need to To use depth-buffering, you need to enable it:enable it:

glutInitDisplayMode (GLUT_DEPTH glutInitDisplayMode (GLUT_DEPTH | ..);| ..);

glEnable (GL_DEPTH_TEST);glEnable (GL_DEPTH_TEST);

Page 4: Hidden Surface & Lighting

4

Hidden Surface RemovalHidden Surface Removal

Initialize the depth buffer and color Initialize the depth buffer and color buffer by using:buffer by using:

glClear(GL_DEPTH_BUFFER_BIT |glClear(GL_DEPTH_BUFFER_BIT |

GL_COLOR_BUFFER_BIT);GL_COLOR_BUFFER_BIT);

Page 5: Hidden Surface & Lighting

5

Hidden Surface RemovalHidden Surface Removal

glutInitDisplayMode (GLUT_DEPTH | ..);glutInitDisplayMode (GLUT_DEPTH | ..);glEnable (GL_DEPTH_TEST);glEnable (GL_DEPTH_TEST);......//in display function//in display functionglClear(GL_COLOR_BUFFER_BIT | glClear(GL_COLOR_BUFFER_BIT |

GL_DEPTH_BUFFER_BIT);GL_DEPTH_BUFFER_BIT);draw3DObjectA();draw3DObjectA();draw3DObjectB();draw3DObjectB();

Page 6: Hidden Surface & Lighting

6

Lighting PrinciplesLighting Principles

Lighting simulates how objects reflect Lighting simulates how objects reflect lightlight•Lighting properties

•Light’s color and position

•Material composition of object

•Global lighting parameters

•ambient light•two sided lighting

•available in RGBA mode

Page 7: Hidden Surface & Lighting

7

Lighting Properties:Lighting Properties:

Light comes from light sources that Light comes from light sources that can be turned on/offcan be turned on/off

AmbientAmbient lighting comes from light lighting comes from light that is so scattered there is no way that is so scattered there is no way to tell its original locationto tell its original location

Example: Backlighting in a roomExample: Backlighting in a room

Page 8: Hidden Surface & Lighting

8

Lighting Properties:Lighting Properties:

The The diffuse componentdiffuse component of lighting is of lighting is light that comes from one direction.light that comes from one direction.

Once it hits a surface, it is scattered Once it hits a surface, it is scattered equally in all directionsequally in all directions

Page 9: Hidden Surface & Lighting

9

Lighting Properties:Lighting Properties:

Specular lightSpecular light comes from a comes from a particular direction.particular direction.

It bounces off the surface in a It bounces off the surface in a particular directionparticular direction

Page 10: Hidden Surface & Lighting

10

RGB Values for LightsRGB Values for Lights

A light source is characterized by the A light source is characterized by the amount of red, green, & blue light it amount of red, green, & blue light it emits.emits.

Examples: If R=G=B=1.0, the light is Examples: If R=G=B=1.0, the light is the brightest possible white.the brightest possible white.

If R=G=B=.5, the color is still white, but If R=G=B=.5, the color is still white, but only at half intensity, so it appears only at half intensity, so it appears gray.gray.

If R=G=1.0 and B=0.0, the light appears If R=G=1.0 and B=0.0, the light appears yellow.yellow.

Page 11: Hidden Surface & Lighting

11

Setting Lighting PropertiesSetting Lighting Properties

glLightfv( glLightfv( light, property, valuelight, property, value ); );•light specifies which light

•multiple lights (at least 8), starting with GL_LIGHT0

•properties•Colors for ambient, diffuse, & specular

components•position and type•attenuation

Page 12: Hidden Surface & Lighting

12

Types of LightsTypes of Lights

OpenGL supports two types of LightsOpenGL supports two types of Lights•Local (Point) light sources

• Infinite (Directional) light sources

Type of light controlled by Type of light controlled by ww coordinatecoordinate

wzwy

wxw

zyxw

at positioned Light Local

along directed LightInfinite

0

0

Page 13: Hidden Surface & Lighting

13

Light Sources (cont.)

Light color propertiesLight color properties•GL_AMBIENT

•GL_DIFFUSE

•GL_SPECULAR

Light color propertiesLight color properties•GL_AMBIENT

•GL_DIFFUSE

•GL_SPECULAR

Page 14: Hidden Surface & Lighting

14

Turning on the LightsTurning on the Lights

Flip each light’s switchFlip each light’s switchglEnable( GL_LIGHTn );

Turn on the powerTurn on the powerglEnable( GL_LIGHTING );

Page 15: Hidden Surface & Lighting

15

Controlling a Light’s PositionControlling a Light’s Position

Modelview matrix affects a light’s Modelview matrix affects a light’s positionposition•Different effects based on when position is

specified

•eye coordinates

•world coordinates

•model coordinates

•Push and pop matrices to uniquely control a light’s position

Page 16: Hidden Surface & Lighting

16

Advanced Lighting FeaturesAdvanced Lighting Features

SpotlightsSpotlights• localize lighting affects

•GL_SPOT_DIRECTION•GL_SPOT_CUTOFF•GL_SPOT_EXPONENT

Page 17: Hidden Surface & Lighting

17

Advanced Lighting FeaturesAdvanced Lighting Features

Light attenuationLight attenuation•decrease light intensity with distance

•GL_CONSTANT_ATTENUATION•GL_LINEAR_ATTENUATION•GL_QUADRATIC_ATTENUATION

2

1

dkdkkf

qlci

Page 18: Hidden Surface & Lighting

18

Light Model PropertiesLight Model Properties

glLightModelfv( glLightModelfv( property, valueproperty, value ); );

Enabling two sided lightingEnabling two sided lightingGL_LIGHT_MODEL_TWO_SIDE

Global ambient colorGlobal ambient colorGL_LIGHT_MODEL_AMBIENT

Local viewer modeLocal viewer modeGL_LIGHT_MODEL_LOCAL_VIEWER

Separate specular colorSeparate specular colorGL_LIGHT_MODEL_COLOR_CONTROL

Page 19: Hidden Surface & Lighting

19

How OpenGL Simulates LightsHow OpenGL Simulates Lights

Gourad lighting modelGourad lighting model•Computed at vertices

Lighting contributorsLighting contributors•Light properties

•Lighting model properties

•Surface material properties

Page 20: Hidden Surface & Lighting

20

Material PropertiesMaterial Properties

Material’s color depends on % of incoming R,G,B light Material’s color depends on % of incoming R,G,B light it reflects. it reflects.

Material Properties:Material Properties:• Ambient is color of the object from all the undirected light in

a scene.

• Diffuse is the base color of the object under current lighting. There must be a light shining on the object to get a diffuse contribution.

• Specular is the contribution of the shiny highlights on the object.

• Emission is the contribution added in if the object emits light (i.e. glows)

Page 21: Hidden Surface & Lighting

21

Material PropertiesMaterial Properties

Color Components for lights mean Color Components for lights mean something different than for something different than for materials.materials.

Example: if R = 1, G = 0.5, and B = 0 Example: if R = 1, G = 0.5, and B = 0 for a material, that material for a material, that material reflects all the incoming red light, reflects all the incoming red light, half the incoming green light, and half the incoming green light, and none of the incoming blue light. none of the incoming blue light.

Page 22: Hidden Surface & Lighting

22

Material PropertiesMaterial Properties

Define the surface properties of a Define the surface properties of a primitiveprimitiveglMaterialfv( glMaterialfv( face, property, valueface, property, value ); );

• separate materials for front and back

GL_DIFFUSE Base color

GL_SPECULAR Highlight Color

GL_AMBIENT Low-light Color

GL_EMISSION Glow Color

GL_SHININESS Surface Smoothness

Page 23: Hidden Surface & Lighting

23

Light Material TutorialLight Material Tutorial

Page 24: Hidden Surface & Lighting

24

Light Position TutorialLight Position Tutorial

Page 25: Hidden Surface & Lighting

25

SurfaceNormalsSurfaceNormals

Normals define how a surface reflects Normals define how a surface reflects lightlight

glNormal3f( glNormal3f( x, y, zx, y, z ) )

•Current normal is used to compute vertex’s color

•Use unit normals for proper lighting

•scaling affects a normal’s length glEnable( GL_NORMALIZE )

orglEnable( GL_RESCALE_NORMAL )

CPUCPU

DLDL

Poly.Poly. Per

Vertex

PerVertex

RasterRaster

FragFrag

FBFB

PixelPixel

TextureTexture

Page 26: Hidden Surface & Lighting

26

Steps for adding lightingSteps for adding lighting

1. Define normal vectors for each vertex of 1. Define normal vectors for each vertex of every object. These normals determine every object. These normals determine the orientation of the object relative to the orientation of the object relative to the light source.the light source.

2.2. Create, select, and position one or more Create, select, and position one or more light sources.light sources.

3.3. Create and select a lighting model, Create and select a lighting model, which defines the level of global which defines the level of global ambient light and the effective location ambient light and the effective location of the viewpoint.of the viewpoint.

4.4. Define material properties for the Define material properties for the objects in the scene. objects in the scene.

Page 27: Hidden Surface & Lighting

27

Tips for Better LightingTips for Better Lighting

Recall lighting computed only at Recall lighting computed only at verticesvertices•model tessellation heavily affects lighting

results

•better results but more geometry to process

Use a single infinite light for fastest Use a single infinite light for fastest lightinglighting•minimal computation per vertex