summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--balls.c116
-rw-r--r--balls.cl47
-rw-r--r--balls.frag3
-rw-r--r--balls.vert15
4 files changed, 70 insertions, 111 deletions
diff --git a/balls.c b/balls.c
index c4f4e2a..beec976 100644
--- a/balls.c
+++ b/balls.c
@@ -17,6 +17,7 @@
#define FRAGMENT_SHADER "balls.frag"
enum { WIDTH = 640, HEIGHT = 480 };
+enum { NVERTICES = 256 };
void initGL(int argc, char *argv[]);
void initCL(void);
@@ -34,8 +35,9 @@ static cl_context context;
cl_program prog;
static cl_command_queue queue;
static cl_kernel kernel;
-GLuint vao[3], vbo[6];
-cl_mem memObjs[nelem(vbo)];
+GLuint vao, vbo;
+cl_mem vertexBuf;
+float tick;
int
main(int argc, char *argv[]) {
@@ -45,16 +47,15 @@ main(int argc, char *argv[]) {
configureSharedData();
- execKernel();
-
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
freeCL();
-
freeGL();
+
+ return 0;
}
void
@@ -144,95 +145,66 @@ initCL(void) {
void
configureSharedData(void) {
- int i, err;
-
- /* Create 3 vertex array objects, one for each square. */
- glGenVertexArrays(nelem(vao), vao);
- glBindVertexArray(vao[0]);
+ int err;
- /* Create 6 vertex buffer objects, one for each set of coordinates and colors. */
- glGenBuffers(nelem(vbo), vbo);
+ /* Create vertex array object. */
+ glGenVertexArrays(1, &vao);
+ glBindVertexArray(vao);
- /* VBO for coordinates of first square */
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(0);
+ /* Create vertex buffer. */
+ glGenBuffers(1, &vbo);
- /* VBO for colors of first square */
- glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
- glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(1);
-
- /* VBO for coordinates of second square */
- glBindVertexArray(vao[1]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
- glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ /* Create VBO coordinates. */
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER, 4 * NVERTICES * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
+ glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
- /* VBO for colors of second square */
- glBindBuffer(GL_ARRAY_BUFFER, vbo[3]);
- glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(1);
-
- /* VBO for coordinates of third square */
- glBindVertexArray(vao[2]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[4]);
- glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(0);
+ /* Create memory objects from VBOs. */
+ vertexBuf = clCreateFromGLBuffer(context, CL_MEM_WRITE_ONLY, vbo, &err);
+ if (err < 0)
+ sysfatal("Failed to create buffer object from VBO.\n");
- /* VBO for colors of third square */
- glBindBuffer(GL_ARRAY_BUFFER, vbo[5]);
- glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- /* Create OpenCL memory objects for the VBOs. */
- for (i = 0; i < nelem(vbo); i++) {
- memObjs[i] = clCreateFromGLBuffer(context, CL_MEM_WRITE_ONLY, vbo[i], &err);
- if (err < 0)
- sysfatal("Failed to create buffer object from VBO.\n");
- err = clSetKernelArg(kernel, i, sizeof(cl_mem), &memObjs[i]);
- if (err < 0)
- sysfatal("Failed to set kernel argument.\n");
- }
+ /* Set kernel arguments. */
+ err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &vertexBuf);
+ err |= clSetKernelArg(kernel, 1, sizeof(float), &tick);
+ if (err < 0)
+ sysfatal("Failed to set kernel arguments.\n");
}
void
execKernel(void) {
int err;
+ size_t globalSize;
cl_event kernelEvent;
glFinish();
- err = clEnqueueAcquireGLObjects(queue, nelem(memObjs), memObjs, 0, NULL, NULL);
+ err = clEnqueueAcquireGLObjects(queue, 1, &vertexBuf, 0, NULL, NULL);
if (err < 0)
sysfatal("Couldn't acquire the GL objects.\n");
- err = clEnqueueTask(queue, kernel, 0, NULL, &kernelEvent);
+ err = clSetKernelArg(kernel, 1, sizeof(float), &tick);
if (err < 0)
- sysfatal("Couldn't enqueue the kernel.\n");
+ sysfatal("Failed to set kernel argument.\n");
+
+ globalSize = NVERTICES;
+ err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &globalSize, NULL, 0, NULL, &kernelEvent);
+ if (err < 0)
+ sysfatal("Couldn't enqueue kernel.\n");
err = clWaitForEvents(1, &kernelEvent);
if (err < 0)
sysfatal("Couldn't enqueue the kernel.\n");
- clEnqueueReleaseGLObjects(queue, nelem(memObjs), memObjs, 0, NULL, NULL);
+ clEnqueueReleaseGLObjects(queue, 1, &vertexBuf, 0, NULL, NULL);
clFinish(queue);
clReleaseEvent(kernelEvent);
}
void
freeCL(void) {
- int i;
-
- for (i = 0; i < nelem(memObjs); i++)
- clReleaseMemObject(memObjs[i]);
+ clReleaseMemObject(vertexBuf);
clReleaseKernel(kernel);
clReleaseCommandQueue(queue);
clReleaseProgram(prog);
@@ -241,7 +213,8 @@ freeCL(void) {
void
freeGL(void) {
- glDeleteBuffers(nelem(vbo), vbo);
+ glDeleteBuffers(1, &vbo);
+ glDeleteBuffers(1, &vao);
}
void
@@ -316,17 +289,18 @@ compileShader(GLint shader) {
void
display(void) {
- int i;
-
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
- for (i = nelem(vao)-1; i >= 0; i--) {
- glBindVertexArray(vao[i]);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- }
+ execKernel();
+
+ glBindVertexArray(vao);
+ glDrawArrays(GL_LINE_LOOP, 0, NVERTICES);
+
+ tick += 0.0005f;
glBindVertexArray(0);
glutSwapBuffers();
+ glutPostRedisplay();
}
void
diff --git a/balls.cl b/balls.cl
index 5ed8ab7..eb668e2 100644
--- a/balls.cl
+++ b/balls.cl
@@ -1,29 +1,22 @@
+#define RADIUS 0.75
+
__kernel void
-balls(__global float4 *coords1, __global float4 *colors1,
- __global float4 *coords2, __global float4 *colors2,
- __global float4 *coords3, __global float4 *colors3
-) {
- coords1[0] = (float4)(-0.15f, -0.15f, 1.00f, -0.15f);
- coords1[1] = (float4)( 0.15f, 1.00f, 0.15f, 0.15f);
- coords1[2] = (float4)( 1.00f, 0.15f, -0.15f, 1.00f);
-
- colors1[0] = (float4)(0.00f, 0.00f, 0.00f, 0.25f);
- colors1[1] = (float4)(0.00f, 0.00f, 0.50f, 0.00f);
- colors1[2] = (float4)(0.00f, 0.75f, 0.00f, 0.00f);
-
- coords2[0] = (float4)(-0.30f, -0.30f, 0.00f, -0.30f);
- coords2[1] = (float4)( 0.30f, 0.00f, 0.30f, 0.30f);
- coords2[2] = (float4)( 0.00f, 0.30f, -0.30f, 0.00f);
-
- colors2[0] = (float4)(0.00f, 0.00f, 0.00f, 0.00f);
- colors2[1] = (float4)(0.25f, 0.00f, 0.00f, 0.50f);
- colors2[2] = (float4)(0.00f, 0.00f, 0.75f, 0.00f);
-
- coords3[0] = (float4)(-0.45f, -0.45f, -1.00f, -0.45f);
- coords3[1] = (float4)( 0.45f, -1.00f, 0.45f, 0.45f);
- coords3[2] = (float4)(-1.00f, 0.45f, -0.45f, -1.00f);
-
- colors3[0] = (float4)(0.00f, 0.00f, 0.00f, 0.00f);
- colors3[1] = (float4)(0.00f, 0.25f, 0.00f, 0.00f);
- colors3[2] = (float4)(0.50f, 0.00f, 0.00f, 0.75f);
+balls(__global float4 *vertices, float tick) {
+ uint id;
+ int longitude, latitude;
+ float sign, phi, theta;
+
+ id = get_global_id(0);
+
+ longitude = id / 16;
+ latitude = id % 16;
+
+ sign = -2.0f * (longitude % 2) + 1.0f;
+ phi = 2.0f * M_PI_F * longitude / 16 + tick;
+ theta = M_PI_F * latitude / 16;
+
+ vertices[id].x = RADIUS * sin(theta) * cos(phi);
+ vertices[id].y = RADIUS * sign * cos(theta);
+ vertices[id].z = RADIUS * sin(theta) * sin(phi);
+ vertices[id].w = 1.0f;
}
diff --git a/balls.frag b/balls.frag
index f1fb57a..0aaa49d 100644
--- a/balls.frag
+++ b/balls.frag
@@ -5,6 +5,5 @@ out vec4 out_color;
void
main(void) {
- vec3 tmp_color = new_color + vec3(0.25f, 0.25f, 0.25f);
- out_color = vec4(tmp_color, 1.0);
+ out_color = vec4(0.0, 0.0, 1.0, 1.0);
}
diff --git a/balls.vert b/balls.vert
index fc00b1d..8313b06 100644
--- a/balls.vert
+++ b/balls.vert
@@ -1,15 +1,8 @@
#version 130
-in vec3 in_coords;
-in vec3 in_color;
-out vec3 new_color;
+in vec4 in_coords;
-void main(void) {
- new_color = in_color;
- mat3x3 rot_matrix = mat3x3(
- 0.707, 0.641, -0.299,
- -0.707, 0.641, -0.299,
- -0.000, 0.423, 0.906);
- vec3 coords = rot_matrix * in_coords;
- gl_Position = vec4(coords, 1.0);
+void
+main(void) {
+ gl_Position = in_coords;
}