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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#define G_FACTOR 3600.0
#define G (9.81 / G_FACTOR)
float
min(float a, float b) {
if (a < b)
return a;
return b;
}
float
max(float a, float b) {
if (a > b)
return a;
return b;
}
float
clamp(float x, float lo, float hi) {
return min(hi, max(x, lo));
}
__kernel void
move(__global float2 *positions, __global float2 *velocities) {
size_t id;
id = get_global_id(0);
velocities[id].y -= G;
positions[id] += velocities[id];
}
__kernel void
collideWalls(__global float2 *positions, __global float2 *velocities, __global float *radii) {
size_t id;
float2 p, v, min, max;
float r;
id = get_global_id(0);
p = positions[id];
v = velocities[id];
r = radii[id];
/* Set bounds. */
min.x = -1.0 + r;
min.y = -1.0 + r;
max.x = 1.0 - r;
max.y = 1.0 - r;
/* Check for collision with bounds. */
if (p.x <= min.x || p.x >= max.x) {
p.x = clamp(p.x, min.x, max.x);
v.x = -v.x;
}
if (p.y <= min.y || p.y >= max.y) {
p.y = clamp(p.y, min.y, max.y);
v.y = -v.y;
}
/* Write back. */
positions[id] = p;
velocities[id] = v;
}
__kernel void
genVertices(__global float2 *positions, __global float *radii, __global float2 *vertices) {
size_t ball, nsegs;
float2 center;
float r, theta;
ball = get_group_id(0);
center = positions[ball];
r = radii[ball];
nsegs = get_local_size(0)-2; /* Number of edge segments. */
theta = 2.0f * M_PI_F * get_local_id(0) / nsegs;
vertices[get_global_id(0)].x = center.x + r * cos(theta);
vertices[get_global_id(0)].y = center.y + r * sin(theta);
vertices[ball*get_local_size(0)] = center;
}
|