From 62de6a18cb37e0385928c66ce2d6c4f80fc52021 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Wed, 15 Oct 2025 15:52:48 -0500 Subject: [PATCH] add single_color_translate --- build.sh | 4 +- src/single_color_translate.c | 209 +++++++++++++++++++++++++++++ src/single_color_translate.fp.glsl | 6 + src/single_color_translate.vp.glsl | 10 ++ src/vertex_color_translate.c | 2 +- 5 files changed, 229 insertions(+), 2 deletions(-) create mode 100644 src/single_color_translate.c create mode 100644 src/single_color_translate.fp.glsl create mode 100644 src/single_color_translate.vp.glsl diff --git a/build.sh b/build.sh index 254d930..f0f9cc3 100644 --- a/build.sh +++ b/build.sh @@ -6,4 +6,6 @@ CFLAGS="-Wall -Werror -Wfatal-errors -g -Og -std=gnu23 -Iinclude $(pkg-config -- #gcc $CFLAGS src/vertex_color.c -gcc $CFLAGS src/vertex_color_translate.c +#gcc $CFLAGS src/vertex_color_translate.c + +gcc $CFLAGS src/single_color_translate.c diff --git a/src/single_color_translate.c b/src/single_color_translate.c new file mode 100644 index 0000000..de71a1d --- /dev/null +++ b/src/single_color_translate.c @@ -0,0 +1,209 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static void * read_file(const char * filename) +{ + int fd = open(filename, O_RDONLY); + if (fd == -1) { + fprintf(stderr, "open(%s): %s\n", filename, strerror(errno)); + return NULL; + } + + off_t size = lseek(fd, 0, SEEK_END); + if (size == (off_t)-1) { + fprintf(stderr, "lseek(%s, SEEK_END): %s\n", filename, strerror(errno)); + return NULL; + } + + off_t start = lseek(fd, 0, SEEK_SET); + if (start == (off_t)-1) { + fprintf(stderr, "lseek(%s, SEEK_SET): %s\n", filename, strerror(errno)); + return NULL; + } + + void * buf = malloc(size+1); + + ssize_t read_size = read(fd, buf, size); + if (read_size == -1) { + fprintf(stderr, "read(%s): %s\n", filename, strerror(errno)); + return NULL; + } + ((char*)buf)[read_size] = 0; + + return buf; +} + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + glViewport(0, 0, width, height); +} + +unsigned int compile_shaders() +{ + void * vertexShaderSource = read_file("src/single_color_translate.vp.glsl"); + unsigned int vertexShader; + vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, (const char **)&vertexShaderSource, NULL); + glCompileShader(vertexShader); + { + int success; + char infoLog[512]; + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); + printf("vertex shader compile failed:\n%s\n", infoLog); + } + } + free(vertexShaderSource); + + void * fragmentShaderSource = read_file("src/single_color_translate.fp.glsl"); + unsigned int fragmentShader; + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, (const char **)&fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + { + int success; + char infoLog[512]; + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); + printf("fragment shader compile failed:\n%s\n", infoLog); + } + } + free(fragmentShaderSource); + + unsigned int shaderProgram; + shaderProgram = glCreateProgram(); + + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + + { + int success; + char infoLog[512]; + glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); + printf("program link failed:\n%s\n", infoLog); + } + } + + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + return shaderProgram; +} + +static int make_buffer(unsigned int target, + const void * data, + size_t size) +{ + unsigned int buffer; + glGenBuffers(1, &buffer); + glBindBuffer(target, buffer); + glBufferData(target, size, data, GL_STATIC_DRAW); + return buffer; +} + +static const float vertex_buffer_data[] = { + // position + 0.5f, -0.5f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, // bottom left + 0.0f, 0.5f, 0.0f, // top +}; + +struct location { + struct { + unsigned int position; + } attrib; + struct { + unsigned int translate; + } uniform; +}; + +int main() +{ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); + + GLFWwindow* window = glfwCreateWindow(1600, 1200, "LearnOpenGL", NULL, NULL); + if (window == NULL) { + const char* description; + glfwGetError(&description); + printf("Failed to create GLFW window: %s\n", description); + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + + glViewport(0, 0, 1600, 1200); + //glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + unsigned int vertex_buffer = make_buffer(GL_ARRAY_BUFFER, + vertex_buffer_data, + (sizeof (vertex_buffer_data))); + + printf("compile start\n"); + unsigned int shaderProgram = compile_shaders(); + printf("compile end\n"); + + struct location location; + + location.attrib.position = glGetAttribLocation(shaderProgram, "position"); + printf("attributes:\n position %u\n", + location.attrib.position); + + location.uniform.translate = glGetUniformLocation(shaderProgram, "translate"); + printf("uniforms:\n translate %u\n\n", + location.uniform.translate); + + float time = 0; + + while(!glfwWindowShouldClose(window)) { + if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + + // + // render + // + glUseProgram(shaderProgram); + + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glVertexAttribPointer(location.attrib.position, + 3, + GL_FLOAT, + GL_FALSE, + (sizeof (float)) * 3, + (void*)0 + ); + glEnableVertexAttribArray(location.attrib.position); + + glUniform1f(location.uniform.translate, sinf(time) * 0.5f); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glfwSwapBuffers(window); + glfwPollEvents(); + + glClearColor(0.1f, 0.1f, 0.1f, 0.1f); + glClear(GL_COLOR_BUFFER_BIT); + + time += 0.05; + } + + glfwTerminate(); + + return 0; +} diff --git a/src/single_color_translate.fp.glsl b/src/single_color_translate.fp.glsl new file mode 100644 index 0000000..66e4a5f --- /dev/null +++ b/src/single_color_translate.fp.glsl @@ -0,0 +1,6 @@ +#version 120 + +void main() +{ + gl_FragColor = vec4(1, 1, 0, 0); +} diff --git a/src/single_color_translate.vp.glsl b/src/single_color_translate.vp.glsl new file mode 100644 index 0000000..9e10eee --- /dev/null +++ b/src/single_color_translate.vp.glsl @@ -0,0 +1,10 @@ +#version 120 + +attribute vec3 position; + +uniform float translate; + +void main() +{ + gl_Position = vec4(translate + position.x, position.yz, 1); +} diff --git a/src/vertex_color_translate.c b/src/vertex_color_translate.c index 4860b80..526c4bc 100644 --- a/src/vertex_color_translate.c +++ b/src/vertex_color_translate.c @@ -169,7 +169,7 @@ int main() location.attrib.color); location.uniform.translate = glGetUniformLocation(shaderProgram, "translate"); - printf("attributes:\n translate %u\n\n", + printf("uniforms:\n translate %u\n\n", location.uniform.translate); float time = 0;