computer graphics & visualization reflections. computer graphics & visualization image...
TRANSCRIPT
computer graphics & visualization
Image Synthesis
Reflections
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• Reflect the scene about a planar mirror
– Environment maps won‘t work in general
• Two basic algorithms– 1) flip scene through the mirror and re-render– 2) flip point of view through the mirror and re-render
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• Reflect scene through the mirror plane
– Need to find reflection matrix
• Re-render reflected scene– Restrict drawing to the mirrors visible part
Mirror
Reflected scene
Wall
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• The reflection matrix
– p[3]: point on plane– n[3]: plane normal– d: n·p
1000
]2[2]2[]2[21]1[]2[2]0[]2[2
]1[2]2[]1[2]1[]1[21]0[]1[2
]0[2]2[]0[2]1[]0[2]0[]0[21
ndnnnnnn
ndnnnnnn
ndnnnnnn
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• Algorithm
A) Initialize ModelView matrixB) Multiply ModelView matrix with reflection matrix
- Flips the scene
C) Render scene !!!D) Render mirror plane
• Sets depth values correctly• Don‘t draw color buffer
E) Pull ModelView matrixF) Render unreflected scene
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• Problems
– Mirror is not infinite– Reflected scene should not be seen through walls
• Solution– Use stencil buffer to restrict drawing of reflected
scene– Render mirror polygons and set stencil bit– Restrict drawing of restricted scene to stenciled
pixels
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• Algorithm
A) Draw scene without mirrors • glEnable(GL_DEPTH_BUFFER_BIT)
B)Draw mirror• glColorMask(0,0,0,0)• glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)• glStencilFunc(GL_ALWAYS, 1, ~0)
C)Draw mirror again to reset depth in mirror projection- glDepthRange(1,1)- glDepthFunc(GL_ALWAYS)- glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)- glStencilFunc(GL_EQUAL, 1, ~0)
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflectionsAlgorithm cont.
D) Draw reflected scene (clip everything in front of the mirror using glClipPlane(…) )
• glDepthRange(0,1)• glDepthFunc(GL_LESS)• glColorMask(1,1,1,1)
E)Draw mirror again to reset stencil and depth values- glColorMask(0,0,0,0)- glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO)- glDepthFunc(GL_ALWAYS)
Repeat for all other mirrors
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflectionsRecursive reflections
– Reflect scene recursively through reflected mirrors• Generate reflection matrix for reflected mirrors
– Successively increment stencil bit in recursive reflections
• Masking within previous masks
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections• Reflections using texture mapping
– Stores image of reflected environment– Generated by flipping scene or view point through
mirror • Image is mapped onto the reflector
• Rendering from reflected view point
• E‘ = E+2d(-N)
• R‘ = 2(NE)N-EE: view point
N: normal of mirror plane
d: dist of E to mirror plane
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Non-Planar Reflections
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps• Used to simulate reflections of the surrounding
scene in an object• Easily realized in software by tracing ´secondary
rays´• Alternative approach:
– Image of the 3D reflected scene is stored in a 2D environment (texture) map
– Secondary rays are looked up in the environment map during rendering
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps• Cube maps
– Capture reflected scene in 6 projected faces
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps• Cube maps
– General layout
– Generated by rendering the scene as seen from the objects center through each face
XNeg ZPos
YPos
YNeg
XPos ZNeg
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps• Cube map example
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps• Cube maps
– Introduce anisotropic sampling• Larger solid angles are subtended near the edges
– View independent• Update only when surrounding scene changes
– Texture coordinate calculation easy to do• Access via 3D texture coordinates, i.e. the reflection vector• Select largest component of reflection vector
– Selects cube face
• Divide other components through– Selects 2D texture coordinate
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps / fixed function• Cube maps hardware support
– OpenGL support for cube maps• glTexImage2D(GL_CUBE_MAP_ENUM, ......)• GL_CUBE_MAP_ENUM one of GL_TEXTURE_CUBE_MAP_POSITIVE(NEGATIVE)_X(Y,Z)• glEnable(GL_TEXTURE_CUBE_MAP)
– Hardware suported calculation of the reflection vector
• reflection_vector
– New automatic texture coordinate generation function
• GL_REFLECTION_MAP• GL_NORMAL_MAP
– Often used to re-normalize directions
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps / fixed functionglActiveTextureARB(GL_TEXTURE0_ARB);glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);glBindTexture(GL_TEXTURE_2D,g_uiCheesBoardTexture);glEnable(GL_TEXTURE_2D);
// Texture unit #1 contains the cubic environment map with Reflection MappingglActiveTextureARB(GL_TEXTURE1_ARB);glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);glBindTexture(GL_TEXTURE_CUBE_MAP_ARB,g_uiCubemap);glEnable(GL_TEXTURE_CUBE_MAP_ARB);glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_REFLECTION_MAP_ARB);glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_REFLECTION_MAP_ARB);glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_REFLECTION_MAP_ARB);glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R);
// Setup texture matrix to transform world-space reflections back to object-spaceFLOATMATRIX4 invM;invM.getModelview();invM = invM.inverse();invM[12]=0; invM[13]=0; invM[14]=0; glMatrixMode(GL_TEXTURE);glLoadMatrixf(invM.array);glMatrixMode(GL_MODELVIEW);
glCallList(g_iBigSphere);
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Environment maps / D3D10 HLSLTextureCube g_txEnvMap;
SamplerState g_samCube{ Filter = ANISOTROPIC; AddressU = Clamp; AddressV = Clamp;};
[..]
float4 PS_EnvMappedScene( VS_OUTPUT_SCENEENV vin ) : SV_Target{ wN = ( mul( vin.Normal, (float3x3)mWorld ) );ac float3 I = vin.wPos.xyz - vEye; float3 wR = I - 2.0f * dot( I, wN ) * wN; float4 CubeSample = lightMul*g_txEnvMap.Sample( g_samCube, wR );
[...]}
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Planar reflections + Env MapsCombination of planar mirror reflections and environment maps
– Loop across all reflective objects– Render reflected scene as is– Store result as environment map or planar
reflection map– Repeat loop
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Fresnel effects• The Fresnel term defines the ratio of incident
light to reflected light– F = f(,i,material properties)– It specifies the attenuation of light– For some materials like glass or plastic the
attenuation is independent of • Little reflected light at normal angles• Most light is reflected at glancing angles
– For metallic surfaces (high refraction index) the Fresnel term is close to 1
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Fresnel effects Transparency with Fresnel term
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Fresnel effects• The Fresnel equation (simplified)
– F =
n cosi - t n cosi +t
cosi – n t cosi +n t
+0.5
i: angle of incidence relative to N
n: index of refraction
t: n2+cosi-1 / n
2 2
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Fresnel effects• Environment maps with fresnel term
Additive blend of diffuse and Fresnel
weighted mirror term
refraction indices: 1.5, 200
-blending of diffuse and Fresnel
weighted mirror term
refraction indices:1.5, 200
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Bump Mapping
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Bump mapping• Bumps could be simulated by geometric displacement
– They are detected by relative darkness or lightness of surface features
• Displacement can be avoided by computing the light distribution and by mapping these values onto the surface– Surface details (texture) are interpreted as a heightfield
and its interaction with light is simulated– Bumpmap defines the displacement of details from the
base surface
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Bump mapping• Normal pertubation
– Displacement values perturb the surface normal– The lighting at each pixel on the surface is changed
accordingly
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Bump mappingSurface displacement
– p´(u,v) = p(u,v) + f(u,v) • N(u,v)
Now the perturbed normal is needed for shading– N´= (Tu x Tv) / |Tu x Tv|
– Tu and Tv are the tangents to the perturbed surface points
– Tu = (px´/u, py
´/u, pz´/u)
– Tv = (px´/v, py
´/v, pz´/v)
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Normal mapping
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Normal Mapping
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Normal Mapping• Similar idea to bump mapping
– Store normals instead of displacements– Avoid difference computation– Requires Tangent Space transformation
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Tangent Space• Normals are stored as if the face lies in the x/z
plane and the normal points toward (0,0,1)• During runtime the faces are most likely
oriented differently tangent space transformation
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Parallax mapping
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Normal vs. Parallax Mapping
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Steep Parallax Mapping
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Steep Parallax Mapping// tsE = tangent space eye vector
float4 PS(VERTEX2PIXEL input) { const int numSteps = 30; float height = 1.0, step = 1.0 / numSteps;
float2 offset = input.texCoord.xy; float4 NormalBump = txNB.Sample(smLinear, offset); float2 delta = float2(-tsE.x, tsE.y) / (tsE.z * numSteps);
while (NormalBump.a < height) { height -= step; offset += delta; NormalBump = txNB.Sample(smLinear, offset); }
float4 color = txColor.Sample(smLinear, offset); float3 normal = NB.xyz * 2 - 1;
// same for light ray [...]}
computer graphics & visualization
Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group
Combination of Techniques