Created
March 22, 2011 20:41
-
-
Save enjalot/882003 to your computer and use it in GitHub Desktop.
OpenCL Kernel for a simple particle system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__kernel void part2(__global float4* pos, | |
__global float4* color, | |
__global float4* vel, | |
__global float4* pos_gen, | |
__global float4* vel_gen, | |
float dt) | |
{ | |
//get our index in the array | |
unsigned int i = get_global_id(0); | |
//copy position and velocity for this iteration to a local variable | |
//note: if we were doing many more calculations we would want to have opencl | |
//copy to a local memory array to speed up memory access (this will be the subject of a later tutorial) | |
float4 p = pos[i]; | |
float4 v = vel[i]; | |
//we've stored the life in the fourth component of our velocity array | |
float life = vel[i].w; | |
//decrease the life by the time step (this value could be adjusted to lengthen or shorten particle life | |
life -= dt; | |
//if the life is 0 or less we reset the particle's values back to the original values and set life to 1 | |
if(life <= 0.f) | |
{ | |
p = pos_gen[i]; | |
v = vel_gen[i]; | |
life = 1.0f; | |
} | |
//we use a first order euler method to integrate the velocity and position (i'll expand on this in another tutorial) | |
//update the velocity to be affected by "gravity" in the z direction | |
v.z -= 9.8f*dt; | |
//update the position with the new velocity | |
p.x += v.x*dt; | |
p.y += v.y*dt; | |
p.z += v.z*dt; | |
//store the updated life in the velocity array | |
v.w = life; | |
//update the arrays with our newly computed values | |
pos[i] = p; | |
vel[i] = v; | |
//you can manipulate the color based on properties of the system | |
//here we adjust the alpha | |
color[i].w = life; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Named the file with .c extension for better syntax highlighting.