opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit 944c21151c565baef62ca1101b5b9d60d3479f6e
parent dd9042cea8550135e40dc7e1dd9b90a63096e89e
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 25 Jun 2023 19:33:43 +0900

split file

Diffstat:
MMakefile | 5+++--
Mmain.c | 135++-----------------------------------------------------------------------------
Amain.h | 15+++++++++++++++
Ashader.c | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 156 insertions(+), 135 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,6 @@ CC = tcc -SRC = main.c src/glad.c +SRC = main.c shader.c src/glad.c +HDR = main.h OUT = main INCS = -I./include @@ -8,7 +9,7 @@ CFLAGS = -Wall LDFLAGS = -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl -all: $(SRC) +all: $(SRC) $(HDR) $(CC) -o $(OUT) $(CFLAGS) $(INCS) $(LDFLAGS) $(SRC) run: all diff --git a/main.c b/main.c @@ -1,4 +1,3 @@ -#include <errno.h> #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -7,6 +6,8 @@ #include <glad/glad.h> #include <GLFW/glfw3.h> +#include "main.h" + float vertices[] = { 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.5, -0.5, -0.0, 0.0, 1.0, 0.0, @@ -14,143 +15,11 @@ float vertices[] = { -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, }; -char errstr[512]; - unsigned int indices[] = { 0, 1, 3, 1, 2, 3, }; -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 = 128; - size_t bytesRead = 0; - char *c; - c = (char *)malloc(bufsize); - if (c == NULL) { - snprintf(errstr, sizeof(errstr), - "allocate memory: errno = %d", errno); - return NULL; - } - - for (;;) { - size_t read = fread(&c[bytesRead], sizeof(c[0]), - bufsize-bytesRead, f); - bytesRead += read; - if (read < bufsize - bytesRead) { - break; - } - bufsize *= 2; // TODO: check overflow. - char* newc = (char *)realloc(c, bufsize); - if (newc == NULL) { - snprintf(errstr, sizeof(errstr), - "allocate memory: errno = %d", errno); - free(c); - return NULL; - } - c = newc; - } - if (!feof(f) || ferror(f)) { - snprintf(errstr, sizeof(errstr), - "read from file %s: errno = %d", file, errno); - free(c); - return NULL; - } - - c[bytesRead] = '\0'; - return c; -} - -// ShaderInit create Shader struct with vertexShader specified by vs and -// fragmentShader specified by fs. vs and fs are path names to the shader -// files. It returns the pointer to the resulting struct. -Shader * -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]; - vertexShader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); - glCompileShader(vertexShader); - glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(vertexShader, sizeof(log), NULL, log); - snprintf(errstr, sizeof(errstr), - "compile vertex shader: %s", log); - return NULL; - } - unsigned int fragmentShader; - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); - glCompileShader(fragmentShader); - glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(fragmentShader, sizeof(log), NULL, log); - snprintf(errstr, sizeof(errstr), - "compile fragment shader: %s", log); - return NULL; - } - unsigned int shaderProgram; - shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertexShader); - glAttachShader(shaderProgram, fragmentShader); - glLinkProgram(shaderProgram); - glGetShaderiv(shaderProgram, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(shaderProgram, sizeof(log), NULL, log); - snprintf(errstr, sizeof(errstr), - "link shaders: %s", log); - return NULL; - } - - free(vertexShaderSource); - free(fragmentShaderSource); - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); - - Shader *shader = (Shader *) malloc(sizeof(Shader)); - shader->ID = shaderProgram; - return shader; -} - -void -ShaderUse(Shader *s) { - glUseProgram(s->ID); -} - -void -ShaderDelete(Shader *s) { - glDeleteProgram(s->ID); -} void framebuffer_size_callback(GLFWwindow *window, int width, int height) diff --git a/main.h b/main.h @@ -0,0 +1,15 @@ +char errstr[512]; + +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); +// ShaderInit create Shader struct with vertexShader specified by vs and +// fragmentShader specified by fs. vs and fs are path names to the shader +// files. It returns the pointer to the resulting struct. +Shader *ShaderInit(const char *vs, const char *fs); +void ShaderUse(Shader *s); +void ShaderDelete(Shader *s); diff --git a/shader.c b/shader.c @@ -0,0 +1,136 @@ +// Shader.c provides utilities for shader management. +// GLFW and GLAD must be initialized before any functions can be used. +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <glad/glad.h> +#include <GLFW/glfw3.h> + +#include "main.h" + +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 = 128; + size_t bytesRead = 0; + char *c; + c = (char *)malloc(bufsize); + if (c == NULL) { + snprintf(errstr, sizeof(errstr), + "allocate memory: errno = %d", errno); + return NULL; + } + + for (;;) { + size_t read = fread(&c[bytesRead], sizeof(c[0]), + bufsize-bytesRead, f); + bytesRead += read; + if (read < bufsize - bytesRead) { + break; + } + bufsize *= 2; // TODO: check overflow. + char* newc = (char *)realloc(c, bufsize); + if (newc == NULL) { + snprintf(errstr, sizeof(errstr), + "allocate memory: errno = %d", errno); + free(c); + return NULL; + } + c = newc; + } + if (!feof(f) || ferror(f)) { + snprintf(errstr, sizeof(errstr), + "read from file %s: errno = %d", file, errno); + free(c); + return NULL; + } + + c[bytesRead] = '\0'; + return c; +} + +Shader * +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]; + vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShader); + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertexShader, sizeof(log), NULL, log); + snprintf(errstr, sizeof(errstr), + "compile vertex shader: %s", log); + return NULL; + } + unsigned int fragmentShader; + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragmentShader, sizeof(log), NULL, log); + snprintf(errstr, sizeof(errstr), + "compile fragment shader: %s", log); + return NULL; + } + unsigned int shaderProgram; + shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + glGetShaderiv(shaderProgram, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(shaderProgram, sizeof(log), NULL, log); + snprintf(errstr, sizeof(errstr), + "link shaders: %s", log); + return NULL; + } + + free(vertexShaderSource); + free(fragmentShaderSource); + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + Shader *shader = (Shader *) malloc(sizeof(Shader)); + shader->ID = shaderProgram; + return shader; +} + +void +ShaderUse(Shader *s) +{ + glUseProgram(s->ID); +} + +void +ShaderDelete(Shader *s) +{ + glDeleteProgram(s->ID); +} +