CS代考计算机代写 data structure algorithm Computer Graphics CSI4130 – Winter 2019
Computer Graphics CSI4130 – Winter 2019
Jochen Lang
EECS, University of Ottawa Canada
This Lecture: Other Texturing Techniques
• Bump Mapping
• Displacement Mapping
• Environment Maps
• Shadow Mapping
CSI4130 Computer Graphics
Programmable Hardware makes Texturing Techniques Simpler
• Examples of Common Techniques – Bump Mapping
• Disturb normal to simulate rough surface – DisplacementMapping
• Add an offset in the normal direction to a surface – Environment Maps
• Use an image panorama as a background (skybox)
• Color an object with an environment map for a mirror reflection effect
– Shadow Mapping
• Two pass rendering to detect which pixels are in shadow
CSI4130 Computer Graphics
Bump Mapping
• Disturb normal to simulate rough surface
– sometimes also referred to as normal mapping – use a texture to change the texel orientation
– techniques works best in local coordinates
• Local Coordinates
– independent from global orientation of the object – tangent , bi-tangent , normal
• Use tangent as and normal as with and hence
– Map eye and light vector into local coordinates
CSI4130 Computer Graphics
Bump Mapping Example
• Example adapted from Randi Rost, OpenGL Shading Language 2nd ed.
– Works in local coordinates
– Uses procedurally calculated bumps
• Could use texture instead
– Uses slightly simplified reflection model in local coordinates
• Specular reflection in mirror direction dotted with eye vector (no exponent)
• Diffuse reflection based on Lambert reflection, i.e., dot product between normal and light direction
– Bumps increase coordinates of normal while is scaled for unit length
CSI4130 Computer Graphics
Procedural Bump Mapping: Vertex Program
in vec4 position; in vec3 normal; in vec2 textureCoord; in vec3
tangent;
out vec3 LightDir; out vec3 EyeDir; out vec4 texCoord;
uniform vec3 LightPosition;
uniform mat4 ModelViewMatrix; uniform mat4 ProjectionMatrix;
uniform mat4 NormalMatrix;
void main() {
EyeDir = vec3(ModelViewMatrix * position);
gl_Position = ProjectionMatrix * vec4(EyeDir,1.0);
texCoord = vec4(textureCoord.x, textureCoord.y, 0, 1.0);
vec3 NN = normalize(NormalMatrix * normal); // Local coordinates
vec3 TN = normalize(NormalMatrix * tangent);
vec3 BN = cross(NN, t);
vec3 v;
v.x = dot(LightPosition, TN); v.y = dot(LightPosition, BN);
v.z = dot(LightPosition, NN);
LightDir = normalize(v); // Light dir in local coordinates
v.x = dot(EyeDir, TN); v.y = dot(EyeDir, BN);
v.z = dot(EyeDir, NN);
EyeDir = normalize(v); // Eye dir in local coordinates
}
CSI4130 Computer Graphics
Procedural Bump Mapping: Fragment Program
in vec3 LightDir; in vec3 EyeDir; in vec4 texCoord;
out vec4 color;
uniform vec3 SurfColor;
uniform float BumpDensity;
uniform float BumpSize;
uniform float SpecularFactor; // = 0.5
void main() {
vec2 c = BumpDensity * texCoord.st;
vec2 p = fract(c) – vec2(0.5);
float d = p.x * p.x + p.y * p.y;
float f = 1.0 / sqrt(d + 1.0);
if (d >= BumpSize)
// = (0.7, 0.6, 0.18)
// = 16.0
// = 0.15
Adapted from Randi Rost,OpenGL Shading Language 2nd ed.
Copyright (c)
2002-2006 3Dlabs
Inc. Ltd.
}
vec3 normDelta = vec3(p.x, p.y, 1.0) * f;
vec3 litColor = SurfColor * max(dot(normDelta, LightDir), 0.0);
vec3 reflectDir = reflect(LightDir, normDelta);
float spec = SpecularFactor * max(dot(EyeDir, reflectDir), 0.0);
litColor = min(litColor + spec, vec3(1.0));
color = vec4(litColor, 1.0);
{ p = vec2(0.0); f = 1.0; }
CSI4130 Computer Graphics
Environment Mapping
• Image of the scene from a point in all directions
– Use for lighting
– Store as a cube-map – Record with
• Panoramic cameras
• Specular probes
• Rotating cameras (static scenes)
• Examples
– Uffizi Galery, Florence by
P. Debevec
CSI4130 Computer Graphics
Environment Mapping
• Use of CubeMaps in OpenGL and WebGL 2
– Use an image panorama as a background (skybox)
– Color an object with an environment map for a mirror reflection effect
• Similar texture loading than with 2D textures
– Use of target gl.TextureCubeMap
– Load 6 images corresponding to the 6 sides of the cube
• Texture coordinates can be calculated
– going from the center of the cube to the 3D vertex and then
intersecting the cube
– Only orientation of the cubemap changes
• There should not be any depth buffer used • Cubemap is always at infinity
CSI4130 Computer Graphics
Loading a Cubemap in OpenGL
// Generate a texture object
glGenTextures( 1, &texObject );
// Bind texture object glBindTexture(GL_TEXTURE_CUBE_MAP, texObject ); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, im.columns(), im.rows(), 0, GL_BGRA, GL_UNSIGNED_SHORT, im.getPixels(0,0,im.columns(),im.rows()));
… // 5 more: -x, y, -y, z, -z
// Make sure to clamp to edge of cube glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
CSI4130 Computer Graphics
Using the Cube Map Texture
• Similar to 2D textures
– Texture coordinates need to calculates from vertex
coordinates
– Texture coordinates are 3D (direction vector) – Special Sampler for cube map
#version 330 core
in vec3 texCoordFrag;
out vec4 color;
uniform samplerCube cMap;
void main() {
color = texture(cMap,texCoordFrag);
}
CSI4130 Computer Graphics
Shadow Mapping
• Multi-Pass Algorithm [Williams 1978] – Renderscenefromlightsource(s)
• Store shadow map
– Renderscenefromviewpoint
• Use shadow map to decide if pixel is lit
• need to transform points from shadow map to canonical view volume
• Principle
– Rendering scene as viewed from lightsource – Only points which are lit are seen
CSI4130 Computer Graphics
Shadow-Lookup – Shadowed
CSI4130 Computer Graphics
Shadow-Lookup – Not Shadowed
CSI4130 Computer Graphics
Generating the Shadowmap
– Set the viewpoint to the lightsource position and the direction centered into the scene
– Define the lightsource field of view (e.g., use gluPerspective)
– Render (here: fixed pipeline)
– Copy depth buffer to shadow map texturing unit
CSI4130 Computer Graphics
ShadowMap Transformation
• Pretend the light is a camera
Store it in a texture: texture coordinates are
u = 0…1 and v = 0…1
Scene/World in world coordinates
Modelview into “light coordinates”
Projection into canonical (clip) coordinates
x = -1…1 , y = -1…1 and z = -1…1
CSI4130 Computer Graphics
Shadow Map Lookup
– Calculate texture matrix
– Render scene (Shader program)
– Transform object into shadow map – Compare depth value
Use of eye coordinates → render as usual
Must explicitly map into range u = 0…1 and v = 0…1
CSI4130 Computer Graphics
Creating the Shadow Map
• Different options
– Dedicated shadow mapping extension (fixed pipeline) – Offscreen pBuffer (old render to texture approach)
– Frame Buffer Object (FBO)
• Extension prior to 2.1
– Use back-buffer of frame buffer directly
• Easy and general
• Copy frame buffer into a texture
• Disadvantage: Shadow map cannot be larger than viewport
CSI4130 Computer Graphics
Looking up shadow map
– Use texture
– Special depth sampler – returns 0 or 1 in s coordinate
– Texture look-up based on s and t coordinate
– Depth comparison between p coordinate and value from texture map
uniform sampler2DShadow ShadowMap; vec3 shadowTexCoord;
float test = texture(ShadowMap,shadowTexCoord).s;
CSI4130 Computer Graphics
Next Lecture
• Graphics Pipeline, Hidden Surface Removal and Spatial Data Structures
– Graphicspipeline
– Clipping
– Z-buffer
– Perspectivecorrecttexturemapping – Spatial data structures
– Binary Space Partioning tree
CSI4130 Computer Graphics