summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-26 19:14:59 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-26 19:14:59 -0400
commit7c6a1d395492e8e26c0c091e83f69f54477f5d15 (patch)
treedff5d213173ca76fd489ee94dee1839971f530ce
parentda60f356e3c6404672cbc35b85b309d21fca896e (diff)
downloadballs-7c6a1d395492e8e26c0c091e83f69f54477f5d15.zip
shaders
-rw-r--r--Makefile2
-rw-r--r--balls.c71
-rw-r--r--balls.frag10
-rw-r--r--balls.vert15
4 files changed, 85 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index ec42712..6aacfdf 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -std=c99 -Wall -pedantic -Wno-deprecated-declarations
-LDFLAGS = -lOpenCL -lglut -lGLU -lGL -lGLX
+LDFLAGS = -lGLEW -lGL -lX11 -lGLU -lOpenGL -lOpenCL -lglut -lGLX
SRC = balls.c sysfatal.c
OBJ = ${SRC:.c=.o}
diff --git a/balls.c b/balls.c
index ecb649e..bfa2f4b 100644
--- a/balls.c
+++ b/balls.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glx.h>
#include <CL/cl_gl.h>
@@ -10,12 +11,16 @@
#define PROG_FILE "balls.cl"
#define KERNEL_FUNC "balls"
+#define VERTEX_SHADER "balls.vert"
+#define FRAGMENT_SHADER "balls.frag"
enum { WIDTH = 640, HEIGHT = 480 };
void initGL(int argc, char *argv[]);
void initCL(void);
+void initShaders(void);
char *readFile(const char *filename, size_t *size);
+void compileShader(GLint shader);
static cl_context context;
static cl_command_queue queue;
@@ -32,11 +37,18 @@ 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
@@ -109,6 +121,36 @@ initCL(void) {
sysfatal("Failed to create kernel: %d\n", err);
}
+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);
+}
+
char *
readFile(const char *filename, size_t *size) {
FILE *f;
@@ -130,16 +172,21 @@ readFile(const char *filename, size_t *size) {
}
void
-configSharedData(void) {
-
-}
-
-void
-execKernel(void) {
-
-}
-
-void
-display(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);
+ }
}
diff --git a/balls.frag b/balls.frag
new file mode 100644
index 0000000..f1fb57a
--- /dev/null
+++ b/balls.frag
@@ -0,0 +1,10 @@
+#version 130
+
+in vec3 new_color;
+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);
+}
diff --git a/balls.vert b/balls.vert
new file mode 100644
index 0000000..fc00b1d
--- /dev/null
+++ b/balls.vert
@@ -0,0 +1,15 @@
+#version 130
+
+in vec3 in_coords;
+in vec3 in_color;
+out vec3 new_color;
+
+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);
+}