Presentation is loading. Please wait.

Presentation is loading. Please wait.

CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360.

Similar presentations


Presentation on theme: "CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360."— Presentation transcript:

1 CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360

2 Overview “Survival kits” Shading (and illumination) Texture mapping

3 Shading and Lights Introduce OpenGL shading functions and lights
Discuss polygon shading a bit Flat Smooth Gouraud

4 (Local) Polygon Mesh Shading
Any surface can be illuminated/shaded/lighted by: 1. calculating surface normal at each visible point and 2. applying illumination model Where efficiency is consideration, e.g., for interactivity (vs. photorealism) approximations are used Three methods - each treats a single polygon independently of others (non-global) Constant (flat) Gouraud (intensity interpolation) Interpolated Phong (normal-vector interpolation)

5 Flat/Constant Shading
Single illumination value per polygon Illumination model evaluated just once for each polygon (1 value for all polygon) Which is as fast as it gets As “sampling” value of illumination equation (at just 1 point) Pretty coarse – below is flat vs. smooth (Gouraud) shading If polygon mesh is an approximation to curved surface, faceted look is a problem Also, facets exaggerated by mach band effect For fast, can (and do) store normal with each surface Or can, of course, compute from vertices

6 Gouraud Shading For flat/constant shading, single illumination value per polygon Gouraud (or smooth or interpolated intensity) shading overcomes problem of discontinuity at edge “Smooths” where polygons meet Linearly interpolate intensity along scan lines eliminates intensity discontinuities at polygon edges

7 Phong Shading The first best “good enough” model Ambient
From everywhere Diffuse Reflected from surface Specular “shiny spot”

8 Steps in OpenGL shading
Enable shading and select model Specify normals Specify material properties Specify lights

9 Enabling Shading Shading calculations are enabled by:
glEnable(GL_LIGHTING) Once lighting is enabled, glColor() ignored Must enable each light source individually: glEnable(GL_LIGHTi) i=0,1….. Can choose light model parameters: glLightModeli(parameter, GL_TRUE) GL_LIGHT_MODEL_LOCAL_VIEWER do not use simplifying distant viewer assumption in calculation GL_LIGHT_MODEL_TWO_SIDED shades both sides of polygons independently

10 (opt.) Normals In OpenGL normal vector is part of state
Set by glNormal*() glNormal3f(x, y, z); glNormal3fv(p); Usually want to set normal to have unit length so cosine calculations are correct Length can be affected by transformations Note that scaling does not preserved length glEnable(GL_NORMALIZE) allows for autonormalization at a performance penalty

11 (opt.) Normal for Triangle
plane n ·(p - p0 ) = 0 n = (p2 - p0 ) ×(p1 - p0 ) p p1 normalize n  n/ |n| p0 Note that right-hand rule determines outward face

12 Light: Defining a Point Source
For each light source, we can set an RGBA for the diffuse, specular, and ambient components, and for the position Setting light values for specular, etc. is model Code is pretty crude Can have multiple lights with multiple colors that move – go for it GL float diffuse0[]={1.0, 0.0, 0.0, 1.0}; GL float ambient0[]={1.0, 0.0, 0.0, 1.0}; GL float specular0[]={1.0, 0.0, 0.0, 1.0}; Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightv(GL_LIGHT0, GL_POSITION, light0_pos); glLightv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightv(GL_LIGHT0, GL_SPECULAR, specular0);

13 Light: Distance, Direction, Ambient
Again, source colors are specified in RGBA Position is given in homogeneous coordinates If w =1.0, we are specifying a finite location If w =0.0, we are specifying a parallel source with the given direction vector Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0}; Coefficients in distance terms (for attenuation) are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms). Change by: a= 0.80; glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a); Ambient light depends on color of light sources A red light in a white room will cause a red ambient term that disappears when the light is turned off OpenGL also allows a global ambient term that is often helpful for testing glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)

14 Light: Spotlights f Use glLightv to set Direction GL_SPOT_DIRECTION
Cutoff GL_SPOT_CUTOFF Attenuation GL_SPOT_EXPONENT Proportional to cosaf f -q q

15 Moving Light Sources Light sources are geometric objects whose positions or directions are affected by the model-view matrix Depending on where we place the position (direction) setting function, we can Move the light source(s) with the object(s) Fix the object(s) and move the light source(s) Fix the light source(s) and move the object(s) Move the light source(s) and object(s) independently

16 Material Properties Material properties are also part of OpenGL state and match terms in modified Phong model Set by glMaterialv() GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat shine = 100.0 glMaterialf(GL_FRONT, GL_AMBIENT, ambient); glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialf(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, shine);

17 Texture Mapping Responsible for much of today’s photorealistic cg
Puts an image on a facet (polygon) Using some geometry Will see example Lots of variations OpenGL texture functions and options x y z image geometry display

18 Textures in OpenGL Texture is 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective Three steps to applying a texture: Specify the texture A. read or generate image B. assign to texture C. enable texturing Assign texture coordinates to vertices Mapping function is set in application Specify texture parameters wrapping, filtering

19 Texture Mapping and OpenGL Pipeline
Big idea … Images and geometry flow through separate pipelines that join during fragment processing “complex” textures do not affect geometric complexity geometry pipeline vertices pixel pipeline image Fragment processor

20 1. Specifying a Texture Image
Define texture image from array of texels (texture elements) in memory Glubyte my_texels[512][512]; Define as any other pixel map Scanned image, generate by application code, etc. Enable texture mapping glEnable(GL_TEXTURE_2D) OpenGL supports 1-4 dimensional texture maps Define image as texture: glTexImage2D( target, level, components,w, h, border, format, type, texels ); target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) components: elements per texel w, h: width and height of texels in pixels border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);

21 May Need to Convert Texture Image …
OpenGL requires texture dimensions to be powers of 2 If dimensions of image are not powers of 2 gluScaleImage( format, w_in, h_in,type_in, *data_in, w_out, h_out,type_out, *data_out ); data_in is source image data_out is for destination image Image interpolated and filtered during scaling If

22 2. Mapping Texture -> Polygon
Based on texture coordinates glTexCoord*() specified at each vertex Extraordinary flexibility glBegin(GL_POLYGON); glColor3f(r0, g0, b0); //no shading used glNormal3f(u0, v0, w0); // shading used glTexCoord2f(s0, t0); glVertex3f(x0, y0, z0); glColor3f(r1, g1, b1); glNormal3f(u1, v1, w1); glTexCoord2f(s1, t1); glVertex3f(x1, y1, z1); . glEnd(); t Texture Space Object Space 1, 1 (s, t) = (0.2, 0.8) 0, 1 A a c (0.4, 0.2) b B C 0, 0 (0.8, 0.4) 1, 0 s

23 Interpolation OpenGL uses interpolation to find proper texels from specified texture coordinates Can be distortions texture stretched over trapezoid showing effects of bilinear interpolation good selection of tex coordinates poor selection of tex coordinates

24 3. Texture Parameters OpenGL has a variety of parameters that determine how texture is applied: Wrapping parameters determine what happens if s and t are outside the (0,1) range Filter modes allow us to use area averaging instead of point samples Mipmapping allows us to use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading

25 Magnification and Minification
More than one texel can cover a pixel (minification) or more than one pixel can cover a texel (magnification) Can use point sampling (nearest texel) or linear filtering( 2 x 2 filter) to obtain texture values Texture Polygon Magnification Minification

26 glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param )
Texture Functions Controls how texture is applied: glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param ) GL_TEXTURE_ENV_MODE modes GL_MODULATE: modulates with computed shade GL_BLEND: blends with an environmental color GL_REPLACE: use only texture color GL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); Set blend color with GL_TEXTURE_ENV_COLOR

27 End .


Download ppt "CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360."

Similar presentations


Ads by Google