summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--balls.c33
-rw-r--r--balls.h11
-rw-r--r--geo.c18
-rw-r--r--rand.c14
4 files changed, 40 insertions, 36 deletions
diff --git a/balls.c b/balls.c
index 13c3ddd..c342b40 100644
--- a/balls.c
+++ b/balls.c
@@ -59,7 +59,7 @@ void freeGL(void);
void initShaders(void);
char *readFile(const char *filename, size_t *size);
void compileShader(GLint shader);
-float2 *noOverlapPositions(int n);
+Vector *noOverlapPositions(int n);
void frameCount(void);
void drawString(const char *str);
@@ -214,43 +214,48 @@ createKernel(cl_program prog, const char *kernelFunc) {
void
setPositions(void) {
- float2 *hostPositions;
+ Vector *hostPositions;
+ int *hostPositionBuf;
int err;
/* Generate initial ball positions. */
hostPositions = noOverlapPositions(nBalls);
+ hostPositionBuf = flatten(hostPositions);
+ free(hostPositions);
/* Create device-side buffer. */
- positions = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*sizeof(float2), hostPositions, &err);
+ positions = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*2*sizeof(float), hostPositionBuf, &err);
if (err < 0)
sysfatal("Failed to allocate position buffer.\n");
/* Copy positions to device. */
- err = clEnqueueWriteBuffer(queue, positions, CL_TRUE, 0, nBalls*sizeof(float2), hostPositions, 0, NULL, NULL);
+ err = clEnqueueWriteBuffer(queue, positions, CL_TRUE, 0, nBalls*2*sizeof(float), hostPositionBuf, 0, NULL, NULL);
if (err < 0)
sysfatal("Failed to copy ball positions to device.\n");
- free(hostPositions);
+ free(hostPositionBuf);
}
void
setVelocities(void) {
- float2 *hostVelocities;
+ float *hostVelocities;
int i, err;
/* Generate initial ball velocities. */
- if ((hostVelocities = malloc(nBalls*sizeof(float2))) == NULL)
+ if ((hostVelocities = malloc(nBalls*2*sizeof(float))) == NULL)
sysfatal("Failed to allocate velocity array.\n");
- for (i = 0; i < nBalls; i++)
- hostVelocities[i] = randVec(-VMAX_INIT, VMAX_INIT, -VMAX_INIT, VMAX_INIT);
+ for (i = 0; i < nBalls; i++) {
+ hostVelocities[2*i] = randFloat(-VMAX_INIT, VMAX_INIT);
+ hostVelocities[2*i+1] = randFloat(-VMAX_INIT, VMAX_INIT);
+ }
/* Create device-side buffer. */
- velocities = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*sizeof(float2), hostVelocities, &err);
+ velocities = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, nBalls*2*sizeof(float), hostVelocities, &err);
if (err < 0)
sysfatal("Failed to allocate velocity buffer.\n");
/* Copy velocities to device. */
- err = clEnqueueWriteBuffer(queue, velocities, CL_TRUE, 0, nBalls*sizeof(float2), hostVelocities, 0, NULL, NULL);
+ err = clEnqueueWriteBuffer(queue, velocities, CL_TRUE, 0, nBalls*2*sizeof(float), hostVelocities, 0, NULL, NULL);
if (err < 0)
sysfatal("Failed to copy ball velocities to device.\n");
@@ -589,13 +594,13 @@ compileShader(GLint shader) {
* Generate n circle coordinates such that none overlap even if all circles
* have the maximum radius.
*/
-float2 *
+Vector *
noOverlapPositions(int n) {
- float2 *ps;
+ Vector *ps;
Rectangle r;
int i, j;
- if ((ps = malloc(n*sizeof(float2))) == NULL)
+ if ((ps = malloc(n*sizeof(Vector))) == NULL)
sysfatal("Failed to allocate position array.\n");
r = insetRect(bounds, RMAX);
diff --git a/balls.h b/balls.h
index c19b948..aaf90e2 100644
--- a/balls.h
+++ b/balls.h
@@ -1,7 +1,9 @@
-typedef float float2 __attribute__ ((vector_size (2*sizeof(float))));
+typedef struct {
+ int x, y;
+} Vector;
typedef struct {
- float2 min, max;
+ Vector min, max;
} Rectangle;
/*
@@ -21,9 +23,8 @@ Partition partitionCollisions(size_t nBalls);
void freePartition(Partition part);
void printPartition(Partition part);
-int isCollision(float2 p1, float r1, float2 p2, float r2);
+int isCollision(Vector p1, float r1, Vector p2, float r2);
Rectangle insetRect(Rectangle r, float n);
float randFloat(float lo, float hi);
-float2 randPtInRect(Rectangle r);
-float2 randVec(float xmin, float xmax, float ymin, float ymax);
+Vector randPtInRect(Rectangle r);
diff --git a/geo.c b/geo.c
index 6178bf6..5bf7e39 100644
--- a/geo.c
+++ b/geo.c
@@ -3,18 +3,22 @@
#include "balls.h"
int
-isCollision(float2 p1, float r1, float2 p2, float r2) {
- float2 dist;
- float rhs;
+isCollision(Vector p1, float r1, Vector p2, float r2) {
+ float dx, dy, rhs;
- dist = p1 - p2;
+ dx = p1.x - p2.x;
+ dy = p1.y - p2.y;
rhs = r1 + r2;
- return (dist[0]*dist[0] + dist[1]*dist[1]) <= rhs*rhs;
+ return (dx*dx + dy*dy) <= rhs*rhs;
}
Rectangle
insetRect(Rectangle r, float n) {
- r.min += n;
- r.max -= n;
+ r.min.x += n;
+ r.min.y += n;
+
+ r.max.y -= n;
+ r.max.y -= n;
+
return r;
}
diff --git a/rand.c b/rand.c
index f070fd1..b679600 100644
--- a/rand.c
+++ b/rand.c
@@ -18,17 +18,11 @@ randFloat(float lo, float hi) {
return lo + r*diff;
}
-float2
+Vector
randPtInRect(Rectangle r) {
- float2 pt = {
- randFloat(r.min[0], r.max[0]),
- randFloat(r.min[1], r.max[1])
+ Vector pt = {
+ randFloat(r.min.x, r.max.x),
+ randFloat(r.min.y, r.max.y)
};
return pt;
}
-
-float2
-randVec(float xmin, float xmax, float ymin, float ymax) {
- float2 v = {randFloat(xmin, xmax), randFloat(ymin, ymax)};
- return v;
-}