Docsity

More GPU Programming Tricks - Lecture Slides | CS 418, Study notes of Computer Graphics

Partial preview of the text

Download More GPU Programming Tricks - Lecture Slides | CS 418 and more Study notes Computer Graphics in PDF only on Docsity!

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

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

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

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

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

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;

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

Keeping track of transformations

Camera^ Projector

Object Space – i.e. glVertex(..) (^) Object Space – i.e. glVertex(..)

Model Matrix (^) Translate, Rotate, etc… Model Matrix

gluLookAt(…)

gluPerspective(…)

Camera View Matrix Projector View Matrix

Camera Projection Matrix

Projector Projection Matrix

[0,1] Range Mapping

World Space World Space

Eye Space Projector Space

Clip Space

Perspective Divide Normalized Device Coordinates

cont.

Projector Clip Space

Texture Space

Getting our Matrices Straight

  • 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

0

0

0

0

w

z

y

x

Matrix

Modeling

Matrix

lookat

View

Light

Matrix

projection

Frustum

Light

q

r

t

s

What’s Missing?

  • Occlusion
    • No shadows
    • Textures applied to all surfaces within the range of the projector
    • Solution: Combine projective textures and shadow mapping
  • Lighting
    • The demo currently does no lighting calculations
    • Solution: Adjust the color from the texture
  • Back-projection
    • Applying the texture to objects behind the projector
    • Similar to the problem we face in shadowing
    • Solution: Check that object is in front of the projector before applying the texture color