/
part2.cl
43 lines (37 loc) · 1.56 KB
/
part2.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define STRINGIFY(A) #A
std::string kernel_source = STRINGIFY(
__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)
{
p = pos_gen[i];
v = vel_gen[i];
life = 1.0;
}
//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.8*dt;
//update the position with the new velocity
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;
}
);