380 likes | 460 Views
Image Synthesis. Reflections. 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. Planar reflections.
E N D
Image Synthesis Reflections
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
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
Planar reflections • The reflection matrix • p[3]: point on plane • n[3]: plane normal • d: n·p
Planar reflections • Algorithm A) Initialize ModelView matrix B) 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 matrix F) Render unreflected scene
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
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)
Planar reflections Algorithm 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
Planar reflections Recursive reflections • Reflect scene recursively through reflected mirrors • Generate reflection matrix for reflected mirrors • Successively increment stencil bit in recursive reflections • Masking within previous masks
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-E E: view point N: normal of mirror plane d: dist of E to mirror plane
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
Environment maps • Cube maps • Capture reflected scene in 6 projected faces
Environment maps • Cube maps • General layout • Generated by rendering the scene as seen from the objects center through each face YPos ZPos XPos ZNeg XNeg YNeg
Environment maps • Cube map example
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
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
Environment maps / fixed function glActiveTextureARB(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 Mapping glActiveTextureARB(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-space FLOATMATRIX4 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);
Environment maps / D3D10 HLSL TextureCube 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 ); [...] }
Planar reflections + Env Maps Combination 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
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
Fresnel effects Transparency with Fresnel term
Fresnel effects • The Fresnel equation (simplified) • F = 2 2 n cosi - t cosi – n t + 0.5 cosi +n t n cosi +t i: angle of incidence relative to N n: index of refraction t: n2+cosi-1 / n
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
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
Bump mapping • Normal pertubation • Displacement values perturb the surface normal • The lighting at each pixel on the surface is changed accordingly
Bump mapping Surface 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)
Normal Mapping • Similar idea to bump mapping • Store normals instead of displacements • Avoid difference computation • Requires Tangent Space transformation
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
Steep Parallax Mapping // tsE = tangent space eye vector float4 PS(VERTEX2PIXEL input) { const intnumSteps = 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 [...] }