diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-13 12:27:58 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-13 12:27:58 -0500 |
| commit | 5011ea1b44c055893c691e923f4e82cdaebd5b0c (patch) | |
| tree | 5b6d421b2287d0ddf9f733be9ce1c928a2822e7d | |
| parent | 44792fd5369cfe2ad1478b72bf22b0d1829f291b (diff) | |
| download | balls-5011ea1b44c055893c691e923f4e82cdaebd5b0c.zip | |
move gl functions out of balls.c
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | balls.c | 146 | ||||
| -rw-r--r-- | config.h | 9 | ||||
| -rw-r--r-- | gl.c | 143 | ||||
| -rw-r--r-- | gl.h | 3 |
5 files changed, 160 insertions, 145 deletions
@@ -2,7 +2,7 @@ CC = gcc CFLAGS = -std=c99 -Wall -pedantic -Wno-deprecated-declarations LDFLAGS = -lGLEW -lGL -lX11 -lGLU -lOpenGL -lOpenCL -lglut -lGLX -SRC = balls.c sysfatal.c geo.c rand.c partition.c io.c +SRC = balls.c sysfatal.c geo.c rand.c partition.c gl.c io.c OBJ = ${SRC:.c=.o} balls: ${OBJ} @@ -14,4 +14,4 @@ balls: ${OBJ} clean: rm -f *.o balls -${OBJ}: sysfatal.h balls.h config.h +${OBJ}: sysfatal.h balls.h config.h gl.h @@ -11,8 +11,9 @@ #include <GL/glx.h> #endif -#include "sysfatal.h" #include "balls.h" +#include "sysfatal.h" +#include "gl.h" #define nelem(arr) (sizeof(arr) / sizeof(arr[0])) #ifdef WINDOWS @@ -36,28 +37,20 @@ #define COLLIDE_WALLS_KERNEL_FUNC "collideWalls" #define COLLIDE_BALLS_KERNEL_FUNC "collideBalls" #define GEN_VERTICES_KERNEL_FUNC "genVertices" -#define VERTEX_SHADER "balls.vert" -#define FRAGMENT_SHADER "balls.frag" #define RMIN 0.05 /* Minimum radius. */ #define RMAX 0.15 /* Maximum radius. */ #define VMAX_INIT 5.0 /* Maximum initial velocity. */ enum { - WIDTH = 640, - HEIGHT = 640, -}; -enum { MS_PER_S = 1000, FRAME_TIME_MS = MS_PER_S / FPS, }; enum { KEY_QUIT = 'q' }; enum { NBALLS_DEFAULT = 3 }; -enum { CIRCLE_POINTS = 24 }; /* Number of vertices per circle. */ const Rect bounds = { {-1.0, -1.0}, {1.0, 1.0} }; -void initGL(int argc, char *argv[]); void initCL(void); int getDevicePlatform(cl_platform_id platforms[], int nPlatforms, cl_device_type devType, cl_device_id *device); void printPlatform(cl_platform_id platform); @@ -68,9 +61,6 @@ void setPositions(void); void setVelocities(void); void setRadii(void); void setCollisions(void); -void genBuffers(void); -void genVertexBuffer(void); -void setColors(void); void configSharedData(void); void setKernelArgs(void); void animate(int v); @@ -83,7 +73,6 @@ void copyPositionsToGpu(cl_event cpuEvent); void reshape(int w, int h); void keyboard(unsigned char key, int x, int y); void freeCL(void); -void freeGL(void); void initShaders(void); void compileShader(GLint shader); void frameCount(void); @@ -118,8 +107,8 @@ main(int argc, char *argv[]) { setRadii(); setCollisions(); - genBuffers(); - setColors(); + genBuffers(&vertexVAO, &vertexVBO, &colorVBO, nBalls); + configSharedData(); setKernelArgs(); @@ -132,7 +121,7 @@ main(int argc, char *argv[]) { glutMainLoop(); freeCL(); - freeGL(); + freeGL(vertexVAO, vertexVBO, colorVBO); freePartition(collisionPartition); free(positionsHostBuf); @@ -140,22 +129,6 @@ main(int argc, char *argv[]) { } void -initGL(int argc, char *argv[]) { - GLenum err; - - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); - glutInitWindowSize(WIDTH, HEIGHT); - glutCreateWindow("Balls"); - glClearColor(1, 1, 1, 1); - - if ((err = glewInit()) != GLEW_OK) - sysfatal("Failed to initialize GLEW.\n"); - - initShaders(); -} - -void initCL(void) { cl_uint nPlatforms; cl_platform_id *platforms, cpuPlatform, gpuPlatform; @@ -449,57 +422,6 @@ setCollisions(void) { } } -/* Create GL vertex and color buffers. */ -void -genBuffers(void) { - glGenVertexArrays(1, &vertexVAO); - glBindVertexArray(vertexVAO); - - /* Generate vertex buffer. */ - genVertexBuffer(); - - /* Generate color buffer. */ - glGenBuffers(1, &colorVBO); -} - -/* Generate GL vertex buffer. */ -void -genVertexBuffer(void) { - glGenBuffers(1, &vertexVBO); - glBindBuffer(GL_ARRAY_BUFFER, vertexVBO); - glBufferData(GL_ARRAY_BUFFER, nBalls*CIRCLE_POINTS*2*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(0); -} - -/* Set ball colors in the GL vertex color buffer. */ -void -setColors(void) { - GLfloat (*colors)[3]; - GLfloat color[3]; - int i, j; - - if ((colors = malloc(nBalls*CIRCLE_POINTS*3*sizeof(GLfloat))) == NULL) - sysfatal("Failed to allocate color array.\n"); - for (i = 0; i < nBalls; i++) { - color[0] = randFloat(0, 1); - color[1] = randFloat(0, 1); - color[2] = randFloat(0, 1); - for (j = 0; j < CIRCLE_POINTS; j++) { - colors[i*CIRCLE_POINTS + j][0] = color[0]; - colors[i*CIRCLE_POINTS + j][1] = color[1]; - colors[i*CIRCLE_POINTS + j][2] = color[2]; - } - } - - glBindBuffer(GL_ARRAY_BUFFER, colorVBO); - glBufferData(GL_ARRAY_BUFFER, nBalls*CIRCLE_POINTS*3*sizeof(GLfloat), colors, GL_STATIC_DRAW); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(1); - - free(colors); -} - /* Create CL memory object from vertex buffer. */ void configSharedData(void) { @@ -691,64 +613,6 @@ freeCL(void) { } void -freeGL(void) { - glDeleteBuffers(1, &vertexVBO); - glDeleteBuffers(1, &vertexVAO); - glDeleteBuffers(1, &colorVBO); -} - -void -initShaders(void) { - GLuint vs, fs, prog; - char *vSrc, *fSrc; - size_t vLen, fLen; - - vs = glCreateShader(GL_VERTEX_SHADER); - fs = glCreateShader(GL_FRAGMENT_SHADER); - - vSrc = readFile(VERTEX_SHADER, &vLen); - fSrc = readFile(FRAGMENT_SHADER, &fLen); - - glShaderSource(vs, 1, (const char **) &vSrc, (GLint *) &vLen); - glShaderSource(fs, 1, (const char **) &fSrc, (GLint *) &fLen); - - compileShader(vs); - compileShader(fs); - - prog = glCreateProgram(); - - glBindAttribLocation(prog, 0, "in_coords"); - glBindAttribLocation(prog, 1, "in_colors"); - - glAttachShader(prog, vs); - glAttachShader(prog, fs); - - glLinkProgram(prog); - glUseProgram(prog); -} - - -void -compileShader(GLint shader) { - GLint success; - GLsizei logSize; - GLchar *log; - - glCompileShader(shader); - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize); - if ((log = malloc((logSize+1) * sizeof(GLchar))) == NULL) - sysfatal("Failed to allocate space for shader compile log.\n"); - glGetShaderInfoLog(shader, logSize+1, NULL, log); - log[logSize] = '\0'; - fprintf(stderr, "%s\n", log); - free(log); - exit(1); - } -} - -void frameCount(void) { static int fps = 0; static int nFrames = 0; @@ -1,5 +1,10 @@ -#define CL_TARGET_OPENCL_VERSION 110 - /* #define WINDOWS 1 */ +#define CL_TARGET_OPENCL_VERSION 110 +#define WINDOW_TITLE "Balls" enum { FPS = 60 }; /* Frames per second. */ +enum window { + WIDTH = 640, + HEIGHT = 640, +}; +enum { CIRCLE_POINTS = 24 }; /* Number of vertices per circle. */ @@ -0,0 +1,143 @@ +#include <stdio.h> +#include <GL/glew.h> +#include <GL/glut.h> + +#include "gl.h" +#include "balls.h" +#include "config.h" +#include "sysfatal.h" + +#define VERTEX_SHADER "balls.vert" +#define FRAGMENT_SHADER "balls.frag" + +static void initShaders(void); +static void compileShader(GLint shader); +static void genVertexBuffer(GLuint *vertexVBO, int nBalls); +static void genColorBuffer(GLuint *colorVBO, int nBalls); + +void +initGL(int argc, char *argv[]) { + GLenum err; + + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); + glutInitWindowSize(WIDTH, HEIGHT); + glutCreateWindow(WINDOW_TITLE); + glClearColor(1, 1, 1, 1); + + if ((err = glewInit()) != GLEW_OK) + sysfatal("Failed to initialize GLEW.\n"); + + initShaders(); +} + +/* Create GL vertex and color buffers. */ +void +genBuffers(GLuint *vertexVAO, GLuint *vertexVBO, GLuint *colorVBO, int nBalls) { + glGenVertexArrays(1, vertexVAO); + glBindVertexArray(*vertexVAO); + genVertexBuffer(vertexVBO, nBalls); + genColorBuffer(colorVBO, nBalls); +} + +void +freeGL(GLuint vertexVAO, GLuint vertexVBO, GLuint colorVBO) { + glDeleteBuffers(1, &vertexVBO); + glDeleteBuffers(1, &vertexVAO); + glDeleteBuffers(1, &colorVBO); +} + +static void +initShaders(void) { + GLuint vs, fs, prog; + int err; + char *vSrc, *fSrc; + size_t vLen, fLen; + + vs = glCreateShader(GL_VERTEX_SHADER); + fs = glCreateShader(GL_FRAGMENT_SHADER); + + err = readFile(VERTEX_SHADER, &vSrc, &vLen); + if (err != 0) + sysfatal("Failed to read '%s'\n", VERTEX_SHADER); + err = readFile(FRAGMENT_SHADER, &fSrc, &fLen); + if (err != 0) + sysfatal("Failed to read '%s'\n", FRAGMENT_SHADER); + + glShaderSource(vs, 1, (const char **) &vSrc, (GLint *) &vLen); + glShaderSource(fs, 1, (const char **) &fSrc, (GLint *) &fLen); + + compileShader(vs); + compileShader(fs); + + free(vSrc); + free(fSrc); + + prog = glCreateProgram(); + + glBindAttribLocation(prog, 0, "in_coords"); + glBindAttribLocation(prog, 1, "in_colors"); + + glAttachShader(prog, vs); + glAttachShader(prog, fs); + + glLinkProgram(prog); + glUseProgram(prog); +} + +static void +compileShader(GLint shader) { + GLint success; + GLsizei logSize; + GLchar *log; + + glCompileShader(shader); + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize); + if ((log = malloc((logSize+1) * sizeof(GLchar))) == NULL) + sysfatal("Failed to allocate space for shader compile log.\n"); + glGetShaderInfoLog(shader, logSize+1, NULL, log); + log[logSize] = '\0'; + fprintf(stderr, "%s\n", log); + free(log); + exit(1); + } +} + +static void +genVertexBuffer(GLuint *vertexVBO, int nBalls) { + glGenBuffers(1, vertexVBO); + glBindBuffer(GL_ARRAY_BUFFER, *vertexVBO); + glBufferData(GL_ARRAY_BUFFER, nBalls*CIRCLE_POINTS*2*sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(0); +} + +static void +genColorBuffer(GLuint *colorVBO, int nBalls) { + GLfloat (*colors)[3]; + GLfloat color[3]; + int i, j; + + if ((colors = malloc(nBalls*CIRCLE_POINTS*3*sizeof(GLfloat))) == NULL) + sysfatal("Failed to allocate color array.\n"); + for (i = 0; i < nBalls; i++) { + color[0] = randFloat(0, 1); + color[1] = randFloat(0, 1); + color[2] = randFloat(0, 1); + for (j = 0; j < CIRCLE_POINTS; j++) { + colors[i*CIRCLE_POINTS + j][0] = color[0]; + colors[i*CIRCLE_POINTS + j][1] = color[1]; + colors[i*CIRCLE_POINTS + j][2] = color[2]; + } + } + + glGenBuffers(1, colorVBO); + glBindBuffer(GL_ARRAY_BUFFER, *colorVBO); + glBufferData(GL_ARRAY_BUFFER, nBalls*CIRCLE_POINTS*3*sizeof(GLfloat), colors, GL_STATIC_DRAW); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(1); + + free(colors); +} @@ -0,0 +1,3 @@ +void initGL(int argc, char *argv[]); +void genBuffers(GLuint *vertexVAO, GLuint *vertexVBO, GLuint *colorVBO, int nBalls); +void freeGL(GLuint vertexVAO, GLuint vertexVBO, GLuint colorVBO); |