Announcement
Lots of CS Scholarship opportunities Textures I Deadline: April 15, 2008.
Final exam effects Final exam effects
Lighting
Neon lights
Textures / refelction
Strawberry
Snapple bottle
Water surface
CD surface
Volumetric
PET scan visualization
Postprocessing
“Classic” film look
Impressionistic painting
Final exam effects Final exam effects
Grads Please register your preference
Research and implementation plan Mycourses quiz.
Teams
Implementation
RenderMan
GLSL
Documentations
1
Notes for Lab 1 Notes for Lab 1
GLSL coordinate spaces In Renderman, premultiply opacity…I.e. object world eye clip Os = Oi; Model Projection transformation View Cs = Ci * Os;
gl_ModelViewMatrix
gl_ModelViewProjection
Plan Texture Mapping
Define surface characteristics of Textures an object using an image This week Basic concept: associate value texture space (u,v) at some coordinates in texture Texture mapping (from files) with pixel at some coordinates in parameterization Bump Mapping geometry Coordinate spaces used in Environment Mapping object space (xo,yo, zo) texture mapping: Simple Toon Shading Texture space (u, v) projection Next week Object space (xo,yo,zo) Screen space (x, y) screen space (x,y) Procedural shading
Texture Mapping Texture Mapping
y
z x geometry screen
v image
u
2
Textures in shaders Texture pipeline
Textures are just data
Texture coordinates available to the shader
Global variable / semantic Akenine-Moller / Haines Actual mapping determined by underlying scene model.
Texture pipeline example Textures in shaders
Tasks:
Make texture data available to rendering
Grab texture data for a given point
Use texture coord from shading point
Indexed by some other means
Apply texture data as appropriate
Color, normal, opacity, whatever. Akenine-Moller / Haines
Texture mapping in RenderMan Textures in RenderMan
Texture coordinates range from 0-1 in Access texture values from file using texture space the texture() function
Values of texture coords corresponding texture (filename, q, r) -- Return to P given in global parameters s and value(s) at texture coords (q,r) t. texture (filename) -- Return value(s) at standard texture coords (s, t). Filtered texture lookup
Free antialiasing.
3
Textures in RenderMan Textures in RenderMan
Can access color or floating point values texture() function provides manual from file control over filtering Blur f = float texture (“foo.tex”); Filter width c = color texture (“bar.tex”); Type of filter Can access specific channels from file Fill data (for missing channels) f = texture (“foo.tex” [1]);
See Section 8.1.4 in text.
Texture format Textures in RenderMan
surface paintedplastic ( float Ka = 1, Kd = .5, Ks = prman has a proprietary format for textures. .5, roughness = .1; txmake [args] infile outfile color speccolor = 1; string texname = “”;) Converts images into this format { color Ct = Cs; Args: if (texname != “”) -mode [black clamp periodic] Ct *= color texture (texname);
-smode, -tmode normal Nf = faceforward (normalize(N), I); -short, -float vector V = -normalize (I); Ci = Ct * (Ka*ambient() + Kd*diffuse(Nf)) + -resize up- speccolor * Ks * specular (Nf, V, roughness); Oi = Os; Ci *= Oi; }
Textures in RenderMan Textures in GLSL
Questions? Texture access in GLSL is almost as easy
EXCEPT…
Texture state must be set up in OpenGL program!
Places texture into texture memory for use by shaders
4
Setting up OpenGL Texture Setting up the OpenGL texture state state
Select a texture unit and make it active Notes: ( glActiveTexture()) No need to enable texture (glEnable()) or Create a tecture object and bind to the set the up the texture function active texture (glBindTexture()). (glTexEnv())
Set texture parameters (wrapping, Texture data must be read in by aux filtering etc) (glTexParam()). functions. Define the texture (glTexImage())
Sample setup code Sample code setup
Initializing the texture Using the texture
glActiveTexture (GL_TEXTURE0) glBindTexture (GL_TEXTURE_2D, myTexName);
Sample code setup On the shader side
Passing texture to a shader Textures are grabbed from texture memory by use of samplers
- 1D texture texLoc = glGetUniformLocation (myProgram, sampler1D “paramname); sampler2D - 2D texture glUniform1i (texLoc, 0); sampler3D - 3D texture samplerCube - cube map texture samplerShadow1D/2D - shadow maps
5
Shader texture functions Texture access in GLSL
texture1D Access to texture values: use textureNNNN[proj/Lod] () functions texture2D shadowNNNN[proj/Lod] () functions texture3D NNNN = 1D / 2D / 3D - using texture coords (0-1) textureCube Cube - for environment mapping [proj] - projective texture map shadow1D Proj = As if projected by slide projector/Useful in shadowing
Lod = Use of textures in vertex shaders shadow2D
See section 5.7 for complete list
Textures in GLSL Simple example
varying float Intensity; Projective texture mapping Uniform sampler2D myTexture;
void main() { vec3 light = vec3 (texture2D (myTexture, glTexCoord [0].st; glFragColor = vec4 (light * Intensity, 1.0); }
What you can do with texture Textures in GLSL maps
Textures are data Color
Can be used as values Bump Mapping Individual components can be accessed via Environment Mapping swizzling / masking Toon Shading.
Questions / Break
6
Texture Mapping- Bump Mapping Texture Mapping- Bump Mapping
Perturbing surface normal . Adds roughness to surfaces Texture map represents displacements from the . Quick way to add detail to normal an object Use perturbed normal in illumination model
. Polygon remains physically flat, but appears bumpy
Jim Blinn
Texture mapping – Bump Mapping Issues with Bump Mapping
How much to perturb
Common coordinate system
Using eye or even object may be cumbersome.
Normal map defined on object
Tangent Space Bump mapping in RenderMan
Build a local coordinate system around Performed using the displacement shader: the normal at a point Modifies global variables P and N P+= bumpheight * normalize(N); N = calculatenormal (P);
Recall that displacement shaders are executed before surface shaders.
bumpheight can be read from a texture file, or generated procedurally, or both…
7
Bump vs displacement mapping Bump mapping in RenderMan
Displacement mapping Bump mapping calculations should be
Moves actual point done in “shader” space
P+= bumpheight * normalize(N); Coordinate Description N = calculatenormal (P); System
Bump mapping "shader" The coordinate system in which the shader was defined. This is the "object" coordinate Just changes normal system when the shader is defined. N = calculatenormal (P + amp*normalize(N)); "current" The coordinate system in which the shading calculations are being performed. This is normally the "camera" or "world" coordinate system.
Bump Mapping in Renderman Bump Mapping in GLSL
vector Nshad = vtransform (“shader”, N); No convenient displacement shader point Pshad = transform (“shader”, P); Pshad += amp * normalize (Nshad); Just vertex and fragment shaders P = transform (“shader”, “current”, Pshad); N = calculatenormal (P); Must be done in fragment shader Works just as well (see Section 8.2.3 in text for derivation) vector Nn = normalize (N); P += Nn * (amp / length (vtransform(“shader”, Nn));
Phong as a Vertex Shader - Phong as a Vertex Shader - diffuse specular
Lighthouse3d.com
8
Issues with Bump Mapping Tangent Space
How much to perturb Build a local coordinate system around
Common coordinate system the normal at a point
Using eye or even object may be cumbersome.
Normal map defined on object
GLSL does not have a calculatenormal() function
Tangent Space Matrix Multiplication
Converting to tangent space " % "S x% Tx Ty Tz "O x% $ ' $ '$ ' " % Sy = Bx By Bz Oy "S x% Tx Ty Tz "O x% $ ' $ '$ ' $ ' $ '$ ' $ S ' $N N N '$ O ' S B B B O # z & # x y z z& $ y' = $ x y z '$ y' $ S ' $N N N '$ O ' # z & # x y z z& Sx = TxOx + TyOy + TzOz
S - vector in tangent space ! T - tangent vector S = T • O !N - normal vector x B - binormal vector ! Sy = B • O O - vector in object space Sz = N • O
Bump Mapping Bump Mapping
For a modified Phong For a modified Phong:
Vertex shader takes as args: Fragment Shader will: Normal (built-in) Modify normal Tangent (user defined) Do lighting calculation. Bi-normal (can be calculated)
Will pass to fragment: Set fragment color. Light vector
Eye position
BOTH IN Tangent Space
9
Bump Mapping Toon Shading
Questions? Mimic the look of cartoons
Intentionally 2-dimensional
Use of a set solid colors (rather than a gradiant)
Hard edges
Phong Model Toon Shading
= + • + • ke L(V ) ka La kd ∑i Li (Si N) ks ∑i Li (Ri V) ambient diffuse specular
Note: Ln are radiance terms, include both light and material info [Lake, et. al. 2000]
Toon Shading Toon Shading
Much like diffuse shading but… To avoid aliasing
Rather than using N • L to determine Use large map with gradient between color shade color transitions.
Use N • L as index to a 1D texture map.
10
Toon Shading Environment mapping
toon shading in print: Create an image, representing the reflection of
[Lake, et. al. 2000] the world onto an object X-Toon [Barla, et. al. 2004] Use surrounding sphere or box, image is texture map indexed by direction of reflection ray
Poor-man’s ray tracing - cheaper
Environment mapping Environment Mapping
Not associated with a particular object but with an imaginary surface surrounding the scene
Specular Reflection – indexed by reflected ray
Diffuse - by surface normal
Transparency – refracted ray direction
Texture Mapping Environment Mapping
Environment mapping
spherical Cube map
11
Environment Mapping Environment Map
Both RenderMan and GLSL have basic Map provides data based on direction
support for cube mapping Reflection, refract, etc.
Environment Mapping Environment Mapping in RenderMan
Major assumption Use environment function: Environment is infinitely distant from object environment (string filename, vector R, …) This is a hack…but a good hack Like texture() Getting away with stuff Can return color or float Every object should have its own environment map Can access separate channels using [] Envrionment maps should change as objects are moved Automatic filtering
Environment maps don’t self reflect. Best on convex objects
Works best on curved rather than flat surfaces.
Environment mapping in RenderMan Environment Mapping in RenderMan
Typical usage: Creating a cube map:
normal Nf = normalize (faceforward (N,I)); From RIB file: vector R = normalize (reflect(I,N)); color Crefl = color environment (mapname, R); MakeCubeFaceEnvironment Using txmake txmake -envcube Parameters front, back, top, bottom, left, right cubemapfile
12
Environment Mapping in GLSL Environment Mapping in Cg
Calculates color based on reflection found Cube Map must be constructed in in CUBE map OpenGL. vec4 c = texureCube (environmentMap, R); See pp. 430-431 in Red Book.
Where http://developer.nvidia.com/object/cube_map_ogl_tutorial.html samplerCUBE environmentMap; R is reflection direction
Questions?
Spherical Environment Map Cylindrical mapping
Alternative to cube mapping 1 cos#1(y) d = 2" (x 2 + z2) u = 0.5 + xd ! v = 0.5 + zd
! !
Summary
Texturing from files
Basic textures
Bump Mapping
Toon shading
Environment Mapping
Will get to do 3 out of the 4 in lab.
13