Top Banner
Outline Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors 2
31

Outline

Feb 23, 2016

Download

Documents

Kyrie

Outline. Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors. Motivation. We know how to rasterize Given a 3D triangle and a 3D viewpoint, we know which pixels represent the triangle But what color should those pixels be?. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Outline

2

Outline

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors

Page 2: Outline

3

Motivation We know how to rasterize

- Given a 3D triangle and a 3D viewpoint, we know which pixels represent the triangle

But what color should those pixels be?

Page 3: Outline

4

Motivation – Why we need light?

Adds realism to our objects

Suppose we create a model of a sphere using many polygons and color it with glColor.

But our objective is

Page 4: Outline

5

Why we need light? - Light example 1

Keyboard key l turns on and off the lights. Keyboard key d turns on and off depth test. The keys 1,2,3 allow turn on and of 3 light

sources. Without lights, it looks like a simple 2d red circle. When we use lights definitions, we can see its a

sphere.

Page 5: Outline

6

Depth Test - Light Example 1

In this version of OpenGL, the sphere is drawn from the front to the back.

So if the depth test is cancelled, what we will see is the back of the sphere.

In another version of OpenGL the results can be different.

Page 6: Outline

7

Motivation – (cont’d)

Light-material interactions affect vertices appearance

Due to this interaction, each vertex would have different color and shade

Need to consider - Light sources- Material properties - Location of viewer - Surface orientation

Page 7: Outline

8

Outline

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors

Page 8: Outline

9

What is Color? Color is simply a

wavelength of light that is visible to the human eye

Reflection/absorption

Where is the black?

What about white?

Light Spectrum

glColor)…(

Page 9: Outline

10

What is Color? – (cont’d)

glColor3f(red, green, blue)glColor4f(red, green, blue, alpha)

Page 10: Outline

11

What determines vertex color in OpenGL?

Is OpenGL lighting enabled ?

Color determined by glColor3f(…)

Ignoring:• normals• lights• material

properties

Color determined by shades

Using:• normals• lights• material

properties

YESNO

Page 11: Outline

12

Outline

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors

Page 12: Outline

13

Polygonal Shading

Shading is defined as a smooth transition from one color to another

Curved surfaces are approximated by polygons

Types of shading are:- Flat shading- Smooth Shading- Gouraud Shading

Page 13: Outline

14

Flat Shading Enabled with glShadeModel(GL_FLAT) Shading is constant across polygon Color of last vertex determines interior

color Only suitable for very small polygons

v1v0

v2

Page 14: Outline

15

Flat Shading – (cont’d)

Inexpensive to compute Appropriate for objects with flat faces Less suitable for smooth surfaces

Page 15: Outline

16

Smooth Shading Enable with glShadeModel(GL_SMOOTH) Interpolate color in interior Computed during rasterization Much better than flat shading More expensive to calculate

v1v0

v2

Page 16: Outline

17

Gouraud Shading Special case of smooth shading Need to calculate vertices normals, HOW? Average all adjacent face normals Requires knowledge about which faces

share a vertex

Page 17: Outline

18

Outline

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors

Page 18: Outline

19

Enabling Light Lighting in general must be enabled

glEnable)GL_LIGHTING(

Each individual light must be enabledglEnable)GL_LIGHT0(

OpenGL supports at least 8 light sources

There are 3 types of effects of light source: Ambient Diffuse Specular

Page 19: Outline

20

Light effects Ambient light :

Directionless Objects are evenly lit on all surfaces in all directions Objects are evenly shaded regardless of their viewing

angle

Diffuse light: Comes from a particular direction Reflected evenly off a surface

Specular light: Comes from a particular direction Reflected sharply and in a particular direction To make objects specular, both light source and object

must have specular property set

Page 20: Outline

21

Light effects – (cont’d)

Page 21: Outline

22

Defining light source Use vectors (r, g, b, a) for light properties Beware: light positions will be transformed by the

modelview matrix

GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0};GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};

GLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0};

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Page 22: Outline

23

Point Source vs. Directional Source Directional light given by position vectorGLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0};glLightfv)GL_LIGHT0, GL_POSITION, light_position(;

Point source given by position point

GLfloat light_position[] = {-1.0, 1.0, -1.0, 1.0};glLightfv)GL_LIGHT0, GL_POSITION, light_position(;

Page 23: Outline

24

Moving Light Sources Light sources are geometric objects whose

positions or directions are affected by the modelview matrix

It is possible to: Move the light source(s) with the object(s) Move the light source(s) and object(s)

independently Fix the object(s) and move the light source(s) Fix the light source(s) and move the object(s)

Page 24: Outline

25

Outline

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors

Page 25: Outline

26

Material Properties Material have certain reflective color properties Put a blue ball in a dark room with only yellow light,

how would it looks like? Material properties stay in effect until changed

GLfloat mat_a[] = {0.1, 0.5, 0.8, 1.0};GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0};GLfloat mat_s[] = {1.0, 1.0, 1.0, 1.0};GLfloat low_sh[] = {5.0};

glMaterialfv)GL_FRONT, GL_AMBIENT, mat_a(;glMaterialfv)GL_FRONT, GL_DIFFUSE, mat_d(;glMaterialfv)GL_FRONT, GL_SPECULAR, mat_s(;glMaterialfv)GL_FRONT, GL_SHININESS, low_sh(;

Page 26: Outline

27

Material Properties – (cont’d)

Can shortcut material properties using glColor

Must be explicitly enabled and disabled

glEnable)GL_COLOR_MATERIAL(;

/* affect front face, diffuse reflection properties */glColorMaterial)GL_FRONT, GL_DIFFUSE(;

glColor3f)0.0, 0.0, 0.8(;/* draw some objects here in blue */

glColor3f)1.0, 0.0, 0.0(;/* draw some objects here in red */

glDisable)GL_COLOR_MATERIAL(;

Page 27: Outline

28

Outline

Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors

Page 28: Outline

29

Defining normals Define unit normal before each vertex

glNormal3f(nx1, ny1, nz1);glVertex3f(x1, y1, z1);glNormal3f(nx2, ny2, nz2);glVertex3f(x2, y2, z2);glNormal3f(nx3, ny3, nz3);glVertex3f(x3, y3, z3);

different normals

glNormal3f(nx, ny, nz);glVertex3f(x1, y1, z1);glVertex3f(x2, y2, z2);glVertex3f(x3, y3, z3);

same normalfor all vertices

Page 29: Outline

30

Maintain Normals Length of normals changes under some

modelview transformations (but not under translations and rotations)

Ask OpenGL to automatically re-normalize

glEnable)GL_NORMALIZE(;

Page 30: Outline

31

Assignment 2 Gray scale image Using BMPLoader to create a terrain – height

map Objects in OFF files Locating objects somewhere on the terrain

Page 31: Outline

32

Loading BMP files

By this class, you can load bmp images and access the cells directly

BMPClass bmp;BMPLoad)imgName,bmp(;

_width = bmp.width;_hight = bmp.height;