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

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

Embed Size (px)

Citation preview

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

CG Summary:OpenGL Shading

andTexturesAngel, Chapters 5, 7; “Red Book”

slides from AW, red book, etc.

CSCI 6360/4360

Page 2: 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

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

Shading and Lights

• Introduce OpenGL shading functions and lights

• Discuss polygon shading a bit– Flat– Smooth– Gouraud

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

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

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

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

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

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

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

Phong Shading

• The first best “good enough” model

• Ambient– From everywhere

• Diffuse– Reflected from surface

• Specular– “shiny spot”

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

Steps in OpenGL shading

1. Enable shading and select model

2. Specify normals

3. Specify material properties

4. Specify lights

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

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

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

(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

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

(opt.) Normal for Triangle

p0

p1

p2

n

plane n ·(p - p0 ) = 0

n = (p2 - p0 ) ×(p1 - p0 )

normalize n n/ |n|

p

Note that right-hand rule determines outward face

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

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

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

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)

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

Light: Spotlights

• Use glLightv to set – Direction GL_SPOT_DIRECTION– Cutoff GL_SPOT_CUTOFF– Attenuation GL_SPOT_EXPONENT

• Proportional to cos

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

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

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

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.0glMaterialf(GL_FRONT, GL_AMBIENT, ambient);glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);glMaterialf(GL_FRONT, GL_SPECULAR, specular);glMaterialf(GL_FRONT, GL_SHININESS, shine);

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

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

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

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:

1. Specify the texture• A. read or generate image• B. assign to texture• C. enable texturing

2. Assign texture coordinates to vertices• Mapping function is set in application

3. Specify texture parameters• wrapping, filtering

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

Texture Mapping and OpenGL Pipeline

geometry pipelinevertices

pixel pipelineimage

Fragmentprocessor

• Big idea …

• Images and geometry flow through separate pipelines that join during fragment processing– “complex” textures do not affect geometric complexity

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

• 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:

1. Specifying a Texture Image

glTexImage2D( target, level, components,w, h, border, format, type, texels );

target: type of texture, e.g. GL_TEXTURE_2Dlevel: used for mipmapping (discussed later)components: elements per texelw, h: width and height of texels in pixelsborder: used for smoothing (discussed later)format and type: describe texelstexels: pointer to texel array

glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);

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

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

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

• Based on texture coordinates

• glTexCoord*() specified at each vertex

• Extraordinary flexibility

s

t1, 1

0, 1

0, 0 1, 0

(s, t) = (0.2, 0.8)

(0.4, 0.2)

(0.8, 0.4)

A

B C

a

b

c

Texture Space Object Space

2. Mapping Texture -> PolygonglBegin(GL_POLYGON);

glColor3f(r0, g0, b0); //no shading usedglNormal3f(u0, v0, w0); // shading usedglTexCoord2f(s0, t0);glVertex3f(x0, y0, z0);glColor3f(r1, g1, b1);glNormal3f(u1, v1, w1);glTexCoord2f(s1, t1);glVertex3f(x1, y1, z1);

.

.glEnd();

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

Interpolation

• OpenGL uses interpolation to find proper texels from specified texture coordinates

• Can be distortions

good selectionof tex coordinates

poor selectionof tex coordinates

texture stretchedover trapezoid showing effects of bilinear interpolation

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

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

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

Magnification and Minification

Texture Polygon

Magnification Minification

PolygonTexture

•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

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

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

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

End

• .