Docsity

GPU Particles: Simulation and Rendering Techniques, Study notes of Computer Science

Partial preview of the text

Download GPU Particles: Simulation and Rendering Techniques and more Study notes Computer Science in PDF only on Docsity!

Particles on the GPU

 Particle simulation

 (^) Vertex processor  (^) Fragment processor

Particle simulation on the vertex processor

 Most intuitive approach

 (^) Particles are point primitives  (^) Can use vertex attributes to store particle state 

No write access to memory

 (^) gl_Vertex does not change

 Geometry shader can write to memory...

FBO

Ping-ponging buffers

No simultaneous read-write access to texture memory

 Cannot do this:

Can't read from texture attached to currently bound FBO.

 Can't render to a texture currently bound to

GL_TEXTURE_2D

VP FP Texture

FBO

Ping-ponging buffers

No simultaneous read-write access to texture memory

 (^) No error, but results of the read are undefined  (^) Use ping-ponging  (^) Drawbacks:  (^) storage requirements double  (^) Overhead of swapping VP FP Tex 2 Tex 1 FBO VP FP Tex 1 Tex 2 Even iterations Odd iterations

Updating particle positions

 Particle state stored in texels of a FP texture

We want to only update each particle once per frame

 Need a 1-1 correspondence between texels and fragments

Draw a view aligned textured quadrilateral

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f); glViewport(0, 0, TexSizeU, TexSizeV); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0); glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0); glEnd();

Particle simulation on the fragment processor

 In fragment shader

 (^) Read attributes  (^) Update attributes  (^) Write attributes to FBO attached textures. uniform sampler2D Pos2D; uniform sampler2D Vel2D; uniform float StepSize; uniform mat4 ScaleBiasP; uniform mat4 ScaleBiasV; void main() { vec4 pos0 = ScaleBiasPtexture2D(Pos2D, gl_TexCoord[0].st); vec4 vel0 = ScaleBiasVtexture2D(Dir2D, gl_TexCoord[0].st); vec4 vel1 = UpdateVelocity(vel0); vec4 pos1 = pos0 + StepSize*vel1; gl_FragData[0] = pos1; gl_FragData[1] = vel1; }

Render-to-vertex buffer

 Approach one: glReadPixels()

 (^) Slow GPU-CPU copy 

Approach two: Pixel buffer objects (PBO)

 (^) Fast GPU-GPU copy

 Approach three: Vertex Texture Fetch

 (^) Look up position in texture during vertex processing

Render-to-vertex buffer : Approach 1

 Copy from GPU-CPU,

Then copy from CPU-GPU

 Slowest - don't need to do this anymore

 (^) glReadBuffer(GL_COLOR_ATTACHMENTn_EXT);  (^) glReadPixels(0, 0, w, h, format, type, &data);  (^) glBindBuffer(GL_ARRAY_BUFFER, vbo_id);  (^) glBufferData(GL_ARRAY_BUFFER, size, &data, GL_STREAM_DRAW);

Render-to-vertex buffer : Approach 2

 Fast GPU-GPU copy using PBO

 (^) glReadBuffer(GL_COLOR_ATTACHMENTn_EXT);  (^) glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, vbo_id);  (^) glReadPixels(0, 0, w, h, format, type, 0);  (^) glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, 0);

Render-to-vertex buffer : Approach 3

 Vertex texture fetch (VTF)

 (^) In shader model 3.0 (GeForce 6 Series and newer) 

Set up

 (^) Create a VBO for 2D points  (^) Set point locations to be texture coordinates const int w = 256; const int h = 256; float data = new float[2wh]; for(int i=0; i<w; i++) for(int j=0; j<h; j++) { data[2(ih+j)] = (float)i/w; data[2(ih+j)+1] = (float)j/h; } glBindBuffer(GL_ARRAY_BUFFER, vbo_id); glBufferData(GL_ARRAY_BUFFER, wh2sizeof(float), data, GL_STATIC_DRAW); glVertexPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));

GPU Particles Overview

 Init: FBO, “old” and “new” floating point textures for particle

attributes, VBO

Pass 1 : Compute positions

 (^) OpenGL: render view-aligned quadrilateral  (^) FP : Read particle position from “old” texture  (^) FP : Write particle position to “new” texture

 Pass 2: Render particles

 (^) VP : Read gl_Position from “new” texture  (^) FP : Render particles 

Swap “old”, “new” texture IDs

 Goto Pass 1

Water simulation and rendering

 Water simulation / animation

 (^) Water and fluid simulation  (^) Surface waves  (^) Sine waves  (^) Modified sine waves  (^) Gerstner waves 

Water rendering

 (^) Fresnel's Law  (^) Snell's Law  (^) Lambert-Beer Law  (^) Caustics

Water animation

 Water surface = mesh displaced by height map

 (^) No splashes  (^) Simple model for real-time water  (^) No FFT M. Finch, Effective Water Simulation from Physical Models, GPU Gems,

J. Guardado, Rendering Water Caustics, GPU Gems, 2004. (online http://developer.download.nvidia.com/books/HTML/gpugems/gpugems_ch01.html) Hinsinger, D. and Neyret, F. and Cani, M.P., Interactive animation of ocean waves, Proceedings of the 2002 ACM SIGGRAPH/Eurographics symposium on Computer animation, pp. 161-166, 2002. Tessendorf, J., Simulating ocean water, Siggraph Course Notes, 1999.

Waves

 Sum of sine waves

  (^) A : amplitude  (^) w: frequency  (^) D : direction crest travels in x-y plane  (^) phi: speed  (^) Simple to compute tangent, bitangent, normal

H  x , y ,t =∑

i = 1 n Ai sin  wi Di ⋅

[

x

y ]

 i t