Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell."— Presentation transcript:

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

2 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

3 The eye Retina contains cones that register different photons

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

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

6 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

7 Alpha Alpha value has no direct affect on color displayed on screen Used for blending and transparency

8 Blending –Green and blue creates shades of cyan –Blue and red creates magenta –Red and green creates yellow Color cube

9 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

10 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

11 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

12 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

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

14 Color cube

15 Lighting (Chapter 5)

16 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

17 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

18 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

19 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(); }

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

21 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

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

23 A lit and unlit sphere

24 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

25 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 normal glBegin (GL_POLYGON); glNormal3fv(n0); glVertex3fv(v0); glNormal3fv(n1); glVertex3fv(v1); glNormal3fv(n2); glVertex3fv(v2); glNormal3fv(n3); glVertex3fv(v3); glEnd();

26 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

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

28 Creating light sources –glLightfv (GL_AMBIENT) DIFFUSE, SPECULAR, POSITION, ETC… To specify a blue ambient light, for example GLfloat 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

29 Left - pale blue ambient light, white diffuse light source Right - pale blue ambient light source, no ambient light

30 Left - infinite light source Right - local light source

31 Teapots drawn with increasing ambient light

32

33

34 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

35 The End!


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

Similar presentations


Ads by Google