Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

35
Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell

Transcript of Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Page 1: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Color in OpenGL(Chapter 4)

Presented by: Stacy C. Lovell

Page 2: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

How do we perceive color?

• Our eyes contain only 3 types of photosentitive cells tuned to three frequencies– Red, Green, and Blue (RGB)

• Sensation of color is created in our brains by averaging and recognizing millions of combinations of red, green, and blue

• Different spectra can produce the same color– By mixing two frequencies (blue and yellow)– By providing a single frequency (a green light)– We cannot distinguish the color difference even though the

corresponding spectra are totally different

Page 3: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

The eye

Retina contains cones that register different photons

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 4: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Newton observed that color is not inherent in objects, rather, the surface of an object reflects some colors and absorbs others.

• We only perceive the reflected colors.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 5: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

RGB (and sometimes A) Color model

• Red, green, and blue pixels are assigned brightness values – 0 to 1 where 0 is none or dark

– (0,0,0) = black (1,1,1) = white

– You can then specify the exact color to be mixed

– A computer monitor is only capable of producing a certain range of color, and is further reduced by the limitations of computer hardware. It can display up to 16.7 million colors (with 24 bitplanes)

Page 6: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

RGBA mode continued

• A certain amount of color data is stored at each pixel, determined by number of bitplanes in framebuffer

• Bitplane contains 1 bit of data for each pixel

• 8 color bitplanes and 8 color bits per pixel– 256 different values or colors can be stored at

the pixel

Page 7: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Alpha

• Alpha value has no direct affect on color displayed on screen

• Used for blending and transparency

Page 8: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Blending

– Green and blue creates shades of cyan– Blue and red creates magenta– Red and green creates yellow

Color cubeQuickTime™ and a

TIFF (Uncompressed) decompressorare needed to see this picture.

Page 9: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Specifying color in OpenGL

glColor3f(1.0, 0.0, 0.0); //sets RGB color to red

glBegin(GL_POINTS);

glVertex3f(….) //draw some vertices

glEnd();

The color drawn will be red until it is changed

glColor4f (1.0,0.0,0.0,0.5) ; //to set an alpha value

Page 10: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• There are two modes, RGBA and color-index mode -- we won’t be dealing with color-index mode, but feel free to read up on it if you are interested

If lighting is enabled, color is determined from the interaction of the transformation matrices with the surface normals and other material properties

Page 11: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Dithering

• The technique of using combinations of some colors to create the effect of other colors– For example, to display pink, the hardware can

fill the region by alternating red and white pixels.

– glEnable(GL_DITHER); or glDisable• Enabled by default

Page 12: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Specifying a shading model

• Lines and filled polygons can be drawn with a single color (flat shading) or with many different colors (smooth shading)– glShadeModel(GL_SMOOTH); //default

• Or GL_FLAT

– For smooth shading, colors along the line segment are interpolated between vertex colors

– For polygons, colors along the interior are interpolated between vertex colors

Page 13: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Three vertices at corners drawn in red, green, and blue, and the rest is smooth shaded between these colors

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 14: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Color cube

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 15: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Lighting

(Chapter 5)

Page 16: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Overview

• 2 different types of light sources – Directional - infinite distance away from

objects in scene, rays are considered parallel by the time they reach the object

– Positional - near or within scene, direction of rays taken into account in lighting calculations

Page 17: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Overview continued

• glLightfv() used to specify position of light regardless of type, and is also used to specify color components

• Normal vectors and material properties must be defined after light sources are defined

Page 18: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Hidden-surface removal

• Uses the depth buffer (or z-buffer)– Associated a depth from the view plane with each pixel

on the window.• Set to the largest possible distance initially using

glClear(GL_DEPTH_BUFFER_BIT)

• Objects are then drawn in any order

– If enabled, before each pixel is drawn a comparison is done with the depth value already stored at the pixel, if a new pixel is closer, then its value replace those currently written

Page 19: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Depth-buffer testing and hidden-surface removal can increase performance

glutInitDisplayMode (GLUT_DEPTH | .... );glEnable(GL_DEPTH_TEST); ... while (1) { glClear(GL_COLOR_BUFFER_BIT |

GL_DEPTH_BUFFER_BIT); get_viewing_point_from_mouse_position(); draw_3d_object_A(); draw_3d_object_B(); }

Page 20: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Different types of light

• Ambient - light that comes from all directions• Diffuse - light that comes from one directions

– Once it hits a surface, it is scattered equally

• Specular - comes from a particular directions and tends to bounce off the surface in a preferred direction– Ex: a laser bouncing off a mirror produces almost

100% specular reflection

– Think of as “shininess”

Page 21: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Lighting model

• OpenGL makes approximation that a color depends on the percentages of incoming light it reflects. Materials have both ambient, diffuse, and specular reflectance properties and emissive properties which simulate light originating from an object– Like a light bulb– Emissive color is unaffected by any light

sources

Page 22: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

RGB, light, materials• All 1.0 = brightest possible white light

• All 0.5 = half intensity of white light (gray)

• R=G=1 B=0?– Full red, full green, no blue = yellow light

• For materials, numbers correspond to reflected proportions of color

• R=1, G=0.5, B=0 means material reflects all the incoming red light, half incoming green, and no blue.

• If light has component (LR, LG, LB) and a material has (MR, MG, MB) then light that arrives at eye is given by: ( LR*MR, LG*MG, LB*MB)

Page 23: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

A lit and unlit sphere

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 24: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Defining normal vectors

• An object’s normals determine its orientation relative to light sources– Very important if you want your object to

display correctly– For each vertex, it uses the normal to determine

how much light that vertex receives from each light source

Page 25: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Normal review

• A normal vector is a vector that points in a direction that is perpendicular to a surface

• “Right hand rule”– Use glNormal() to set current normalglBegin (GL_POLYGON); glNormal3fv(n0); glVertex3fv(v0); glNormal3fv(n1); glVertex3fv(v1); glNormal3fv(n2); glVertex3fv(v2); glNormal3fv(n3); glVertex3fv(v3);glEnd();

Page 26: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Normals continued…

• The cross product or vector product produces a resulting vector that is orthogonal to both u and v, where n is the direction of the resulting vector.

Remember?u x v is the notation which is defined as:n |u||v| sin

where n is the unit vector that is perpendicular to u and v and is the angle between the vectors u and v

Page 27: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

Defining properties

• Use glEnable(GL_LIGHTING)– glEnable(GL_LIGHT0) //explicitly enable each light source

that you define after you’ve specified the parameters

• Select a light model• glLightModelfv(GL_LIGHT_MODEL_AMBIENT)

• Define material properties– glMaterialfv(GL_DIFFUSE);

• (GL_AMBIENT_AND_DIFFUSE)• (GL_SPECULAR)• (GL_EMISSION)• (GL_AMBIENT)

Page 28: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Creating light sources– glLightfv

• (GL_AMBIENT)• DIFFUSE, SPECULAR, POSITION, ETC…

• To specify a blue ambient light, for exampleGLfloat light_ambient[ ] = { 0.0 , 0.0 , 1.0 , 1.0 } ;

glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient );

GL_DIFFUSE can be thought of as “the color of light”GL_SPECULAR affects the color of the specular

highlight (often same color as light shining on it)-like light reflecting off of a glass• Changing light position

– Pass GL_POSITION to glLightfv

Page 29: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Left - pale blue ambient light, white diffuse light source

• Right - pale blue ambient light source, no ambient light

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 30: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Left - infinite light source

• Right - local light source

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 31: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• Teapots drawn with increasing ambient light

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 32: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 33: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 34: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

• The color produced by lighting a vertex is computed as follows:– Vertex color =

• The material emission at that vertex +

• The global ambient light scaled by materials ambient property at that vertex +

• The ambient, diffuse, and specualr contributions from all the light sources

Page 35: Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.

The End!