opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit 59fbab53ee580386a1601fea22d01f39ef861113
parent d295d2de6f1c23fc2da63688f028e445dc1449d6
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 25 Jun 2023 17:50:35 +0900

divide shaders to separate files

Diffstat:
Mmain.c | 83+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Ashaders/fragment.sl | 9+++++++++
Ashaders/vertex.sl | 11+++++++++++
3 files changed, 70 insertions(+), 33 deletions(-)

diff --git a/main.c b/main.c @@ -1,6 +1,8 @@ +#include <errno.h> +#include <math.h> #include <stdio.h> #include <stdlib.h> -#include <math.h> +#include <string.h> #include <glad/glad.h> #include <GLFW/glfw3.h> @@ -23,9 +25,48 @@ typedef struct Shader { unsigned int ID; } Shader; +// Readfile read from file to a char* and return that pointer. +// Caller is responsible to free the returned pointer. +char * +readfile(const char *file) +{ + FILE *f = fopen(file, "r"); + if (f == NULL) { + snprintf(errstr, sizeof(errstr), "fopen: errno = %d", errno); + return NULL; + } + + size_t bufsize = 1024; + char *c; + c = (char *)malloc(bufsize); + + size_t read = fread(c, sizeof(c[0]), bufsize, f); + if (read == sizeof(c)) { + snprintf(errstr, sizeof(errstr), + "read file: file too big: %s", file); + return NULL; + } + return c; +} + Shader * -ShaderInit(const char *vertexShaderSource, const char *fragmentShaderSource) +ShaderInit(const char *vs, const char *fs) { + char *vertexShaderSource = readfile(vs); + if (vertexShaderSource == NULL) { + char *olderr = strdup(errstr); + snprintf(errstr, sizeof(errstr), + "read vertex shader: %s", olderr); + return NULL; + } + char *fragmentShaderSource = readfile(fs); + if (vertexShaderSource == NULL) { + char *olderr = strdup(errstr); + snprintf(errstr, sizeof(errstr), + "read fragment shader: %s", olderr); + return NULL; + } + unsigned int vertexShader; int success; char log[512]; @@ -36,7 +77,7 @@ ShaderInit(const char *vertexShaderSource, const char *fragmentShaderSource) if (!success) { glGetShaderInfoLog(vertexShader, sizeof(log), NULL, log); snprintf(errstr, sizeof(errstr), - "compile vertex shader: %s\n", log); + "compile vertex shader: %s", log); return NULL; } unsigned int fragmentShader; @@ -47,7 +88,7 @@ ShaderInit(const char *vertexShaderSource, const char *fragmentShaderSource) if (!success) { glGetShaderInfoLog(fragmentShader, sizeof(log), NULL, log); snprintf(errstr, sizeof(errstr), - "compile fragment shader: %s\n", log); + "compile fragment shader: %s", log); return NULL; } unsigned int shaderProgram; @@ -59,9 +100,12 @@ ShaderInit(const char *vertexShaderSource, const char *fragmentShaderSource) if (!success) { glGetShaderInfoLog(shaderProgram, sizeof(log), NULL, log); snprintf(errstr, sizeof(errstr), - "link shaders: %s\n", log); + "link shaders: %s", log); return NULL; } + + free(vertexShaderSource); + free(fragmentShaderSource); glDeleteShader(vertexShader); glDeleteShader(fragmentShader); @@ -80,33 +124,6 @@ ShaderDelete(Shader *s) { glDeleteProgram(s->ID); } -const char *vertexShaderSource = " - #version 330 core - layout (location = 0) in vec3 aPos; - layout (location = 1) in vec3 aColor; - out vec3 ourColor; - - void - main() - { - gl_Position = vec4(aPos, 1.0); - ourColor = aColor; - } -"; - -const char *fragmentShaderSource = " - #version 330 core - in vec3 ourColor; - out vec4 FragColor; - - void - main() - { - FragColor = vec4(ourColor, 1.0); - } -"; - - void framebuffer_size_callback(GLFWwindow *window, int width, int height) { @@ -148,7 +165,7 @@ main(void) glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // setup shaders - Shader *shader = ShaderInit(vertexShaderSource, fragmentShaderSource); + Shader *shader = ShaderInit("shaders/vertex.sl", "shaders/fragment.sl"); if (shader == NULL) { fprintf(stderr, "init shader: %s\n", errstr); exit(1); diff --git a/shaders/fragment.sl b/shaders/fragment.sl @@ -0,0 +1,9 @@ +#version 330 core +in vec3 ourColor; +out vec4 FragColor; + +void +main() +{ + FragColor = vec4(ourColor, 1.0); +} diff --git a/shaders/vertex.sl b/shaders/vertex.sl @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; +out vec3 ourColor; + +void +main() +{ + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; +}