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

Slides:



Advertisements
Similar presentations
Polygon Rendering Flat Rendering Goraud Rendering Uses Phong Reflectance Phong Rendering.
Advertisements

Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Texture Mapping. Texturing  process that modifies the appearance of each point on a surface using an image or function  any aspect of appearance can.
CSPC 352: Computer Graphics
Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
CSC345: Advanced Graphics & Virtual Environments
OpenGL Texture Mapping
OpenGL Texture Mapping Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
OpenGL Texture Mapping April 16, Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002 Basic Stragegy Three steps to applying a texture.
CS 4731: Computer Graphics Lecture 17: Texturing Emmanuel Agu.
Computer Graphics Lighting.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 6: Shading Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL.
Shading in OpenGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Chapter 6 Shading. 2 Objectives Learn to shade objects so their images appear three-dimensional Introduce the types of light-material interactions Build.
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
Illumination and Shading Angel, Chapter 6 slides from AW, etc.
Texture Mapping. Scope Buffers Buffers Various of graphics image Various of graphics image Texture mapping Texture mapping.
Texture Mapping Course: Computer Graphics Presented by Fan Chen
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
Mapping method Texture Mapping Environmental mapping (sphere mapping) (cube mapping)
An Interactive Introduction to OpenGL Programming Ed Angel
Computer Graphics I, Fall 2010 Shading in OpenGL.
CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
Texture Mapping. 2 Motivation A typical modern graphics card can handle 10s of millions of polygons a second. How many individual blades of grass are.
OpenGL Texture Mapping. 2 Objectives Introduce the OpenGL texture functions and options.
Texture Mapping Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS 480/680 Computer Graphics OpenGL Texture Mapping Dr. Frederick C Harris, Jr. Fall 2011.
Texture Mapping Software College, Shandong University Instructor: Zhou Yuanfeng
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
Computing & Information Sciences Kansas State University Lecture 12 of 42CIS 636/736: (Introduction to) Computer Graphics CIS 636/736 Computer Graphics.
11/5/2002 (c) University of Wisconsin, CS 559 Last Time Local Shading –Diffuse term –Specular term –All together –OpenGL brief overview.
Where We Stand So far we know how to: –Transform between spaces –Rasterize –Decide what’s in front Next –Deciding its intensity and color.
Texture Mapping and NURBS Week 7 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico.
Lighting Dave Shreiner. 2 Lighting Principles Lighting simulates how objects reflect light Lighting simulates how objects reflect light material composition.
Lighting and Reflection Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 Chapter 7 Texture Mapping. 2 The Limits of Geometric Modeling Although graphics cards can render over 10 million polygons per second, that number is.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 55 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 55 Computer Graphics Three-Dimensional Graphics V.
Madhulika (18010), Assistant Professor, LPU.
Shading To determine the correct shades of color on the surface of graphical objects.
OpenGL Texture Mapping
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
OpenGL Texture Mapping
Introduction to Computer Graphics with WebGL
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
CSC461: Lecture 24 Lighting and Shading in OpenGL
Lighting Phong's Lighting Model normals
3D Game Programming Texture Mapping
Lighting and Materials
Introduction to Computer Graphics with WebGL
CS5500 Computer Graphics April 10, 2006.
OpenGL Texture Mapping
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
OpenGL Texture Mapping
3D Game Programming Texture Mapping
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
OpenGL Texture Mapping
Presentation transcript:

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

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

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

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

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

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

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

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

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

(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

(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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

End .