More GPU Programming Tricks William Baker CS 418 Fall 2005 Lecture Overview Less GLSL specific syntax than last time High level look at some useful algorithms A few cool effects you might want to try 1 Animation Last time it was briefly mentioned that we can perform animation using GLSL shaders Basic concept Move each vertex in our vertex shader Calculation of where and how to move in vertex shader We can control movement with uniform variables and vertex attributes Time Velocity Gravity Fragment shaders do the same work as always Application 1 Breathing Pulsating Meshes Uses Make a character look like they re breathing Warp an alien into another dimension Put a cow marshmallow in a microwave What we need to write our shader A direction to move each vertex The distance to move each vertex 2 Direction Vertex Normals Source The Cg Tutorial Distance Where do we get our distance from Animation we should be using time somehow We could do float displacement time Problem with this approach The solution sin float displacement 0 5 sin time 1 0 5 and 1 to change range from 1 1 to 0 1 Additional extensions Base the displacement on other variables e g y coordinate as well Special note Last time we said we almost always end with calling ftransform We can no longer do this need to use gl ModelViewProjectionMatrix with our new position 3 Application 2 Particle Systems Assumptions that make our life easier Particles do not collide with each other Particles do not reflect light usually they emit light Particles do not cast shadows on other particles Usual particle attributes Position Color Transparency Velocity Size Shape Lifetime Use as many of these as is necessary Image www 3dshaders com Vertex Fragment Work Breakdown Why do this work on the GPU CPU Update one particle at a time GPU Update several at a time Parallel Vertex Shaders Calculate how to move the particles Move the particles Particle gl Vertex Fragment Shaders Compute color transparency Usually simple and straightforward 4 Choosing Initial Particle Attributes Once the application sets some attributes all updates performed in the shader How we choose these initial parameters depends on what we re simulating Explosion Initial position Current vertex locations Initial velocity We could use normals or some variant Fountain Initial position Fountain source Initial velocity Direction the fountain is facing if its moving it s current direction Updating Particle Attributes Just like the breathing mesh use time supplied from the application Implement a physics equation of your choice in the vertex shader Color and transparency can be updated based on time as well vertex or fragment shader Some attributes e g lifetime may have to be processed by the application 5 Uniform Fog Why use fog If we are in a swamp If it s earlier in the morning and we are on a lake If we want to hide small artifacts in our scene Wait doesn t OpenGL do fog already Yes We might want to combine fog with other effects We might want to calculate fog in a new way Some standard parameters Fog color smog Fog density Fog start end distances Fog Attributes from Cg Tutorial Fog Color Aggregate color of the scattered light that reaches your eye Fog Density Measure of how much light is scattered extinguished over a unit of distance Fog Distance Distance along the view ray from an object to the eye Larger the distance foggier the object appears We will assume that color and density are uniform and constant 6 Fog Light Interaction Source The Cg Tutorial Fog Mathematics Cleave Center Creduction Cincrease Cleave Color intensity of light leaving that we see along a ray Center Color intensity entering a unit segment along a ray Creduction Color intensity of all the light absorbed or scattered Cincrease Color intensity of additional light redirected in The amount of light being scattered absorbed cannot be greater than the amount of entering light 0 Creduction Center Uniform Creduction h Center 0 h 1 Same goes for additional light Cincrease h Cfog Cfog constant fog color 7 Fog Mathematics cont Cleave Center Creduction Cincrease Cleave Center h Center h Cfog Cleave 1 h Center h Cfog Expressed as a linear blend Cleave f Center 1 f Cfog f 1 h f fog factor color mix vec3 gl Fog color color fog We will look at 3 ways of computing the fog factor Linear GL LINEAR Exponential GL EXP Squared Exponential GL EXP2 Linear Fog Factor Based on 3 values start distance from the eye where fog starts end distance from the eye where fog ends z depth of fragment distance of fragment from eye f end z end start 1 end start constant precompute 1 end start gl Fog scale fog gl Fog end gl FogFragCoord gl Fog scale 8 Exponential Fog Factor f e density z density thickness of fog gl Fog density z distance same as before GLSL does not have a built in exp base e function ex 2 x ln 2 Code const float LOG2E 1 442695 1 ln 2 fog exp2 gl Fog density gl FogFragCoord LOG2E Exponential Squared Fog Factor f e density z 2 density thickness of fog gl Fog density z distance same as before GLSL code const float LOG2E 1 442695 1 ln 2 fog exp2 gl Fog density gl Fog density gl FogFragCoord gl FogFragCoord LOG2E Can improve performance by precomputing gl Fog density gl Fog density LOG2E 9 Projective Texturing What is Projective Texturing and why should we care Project textures onto objects Closely related to shadow mapping Uses If you have a projector in your scene Interesting light effects flashlight star shaped light etc Image source www cs tufts edu 10 Keeping track of transformations Projector Camera Object Space i e glVertex Model Matrix Translate Rotate etc World Space Model Matrix World Space gluLookAt Camera View Matrix Eye Space Projector View Matrix Projector Space Camera Projection Matrix gluPerspective Clip Space Projector Projection Matrix Projector Clip Space 0 1 Range Mapping Perspective Divide cont Object Space i e glVertex Normalized Device Coordinates Texture Space Getting our Matrices Straight 1 s 2 t r q 1 2 1 2 1 x0 2 Light Light 1 Frustum View Modeling y 0 2 z0 projection look at Matrix 1 w0 2 Matrix Matrix 1 Takes us from object space to texture coordinates Send the matrix in on the texture stack Before looking up a color in the texture map convert out of homogeneous coordinates s q t q 11 Vertex and Fragment Work Breakdown Vertex Shader All the standard work Transform vertex into clip space Lighting calculations Transform vertex into projector space Use the texture matrix Make sure you are using the right
View Full Document
Unlocking...