From c463904a6b5c47b249643f48b151d3dbce86c675 Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Mon, 14 Oct 2024 22:40:35 +0200 Subject: [PATCH] lib: add mini tests framework --- Makefile | 41 ++++++---- assets/shaders/shader.frag | 21 ----- assets/shaders/shader.vert | 21 ----- include/batch.h | 14 ++++ include/camera.h | 2 +- include/object.h | 3 +- include/shaders.h | 48 +++++++++++ src/batch.c | 7 ++ camera.c => src/camera.c | 8 +- math.c => src/math.c | 2 +- object.c => src/object.c | 7 +- rendlib.c => src/rendlib.c | 82 +++++++++++++------ {assets/models => tests/assets-01}/sphere.obj | 0 dev.c => tests/test-01.c | 14 ++-- 14 files changed, 167 insertions(+), 103 deletions(-) delete mode 100644 assets/shaders/shader.frag delete mode 100644 assets/shaders/shader.vert create mode 100644 include/batch.h create mode 100644 include/shaders.h create mode 100644 src/batch.c rename camera.c => src/camera.c (91%) rename math.c => src/math.c (89%) rename object.c => src/object.c (94%) rename rendlib.c => src/rendlib.c (87%) rename {assets/models => tests/assets-01}/sphere.obj (100%) rename dev.c => tests/test-01.c (94%) diff --git a/Makefile b/Makefile index e9201c1..6f36118 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,35 @@ CC=gcc -CFLAGS=-DFREEGLUT_STATIC -g3 +CFLAGS=-DFREEGLUT -g3 -iquote include/ LFLAGS=-lglfw -L/usr/lib64 -lGLEW -lGL -lX11 -lGLU -lassimp -lm LD=ld -OUT=build -DEV=dev.c +SRCDIR=src +BINDIR=build +TSTDIR=tests -all: - mkdir -p $(OUT) - $(CC) $(CFLAGS) -c rendlib.c -o $(OUT)/rendlib.o $(LFLAGS) - $(CC) $(CFLAGS) -c math.c -o $(OUT)/math.o $(LFLAGS) - $(CC) $(CFLAGS) -c object.c -o $(OUT)/object.o $(LFLAGS) - $(CC) $(CFLAGS) -c camera.c -o $(OUT)/camera.o $(LFLAGS) - $(LD) -relocatable $(OUT)/rendlib.o $(OUT)/math.o $(OUT)/object.o $(OUT)/camera.o -o $(OUT)/rendlib +LIB_SRCS = $(wildcard $(SRCDIR)/*.c) +LIB_OBJS = $(addprefix $(BINDIR)/, $(notdir $(LIB_SRCS:.c=.o))) +TST_SRCS = $(wildcard $(TSTDIR)/*.c) +TST_OBJS = $(addprefix $(BINDIR)/$(TSTDIR)/, $(notdir $(TST_SRCS:.c=.o))) +TST_ASST = $(shell find $(TSTDIR) -maxdepth 1 -mindepth 1 -type d) -dev: all - $(CC) $(CFLAGS) dev.c $(OUT)/rendlib -o $(OUT)/dev $(LFLAGS) +init: + mkdir -p $(BINDIR) + mkdir -p $(BINDIR)/tests -devall: dev - $(OUT)/dev +all: init $(LIB_OBJS) + $(LD) -relocatable $(LIB_OBJS) -o $(BINDIR)/rendlib +$(BINDIR)/%.o: $(SRCDIR)/%.c + $(CC) $(CFLAGS) -c $< -o $@ $(LFLAGS) + +$(BINDIR)/$(TSTDIR)/test-%.o: $(TSTDIR)/test-%.c $(TSTDIR)/assets-% | all + $(CC) $(CFLAGS) $< $(BINDIR)/rendlib -o $@ $(LFLAGS) + cp -rf $(word 2,$^) $(BINDIR)/$(TSTDIR)/ + +test: $(TST_OBJS) + +runt%: $(BINDIR)/$(TSTDIR)/test-%.o + cd $(BINDIR)/$(TSTDIR); ./test-$*.o clean: - rm -rf $(OUT) + rm -rf $(BINDIR) diff --git a/assets/shaders/shader.frag b/assets/shaders/shader.frag deleted file mode 100644 index 00983e4..0000000 --- a/assets/shaders/shader.frag +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 core -in vec4 frag_pos; -in vec4 frag_normal; -in vec3 object_color; - -out vec4 outy; - -void main() { - vec4 norm = normalize(frag_normal); - vec4 ambient = vec4(0.7, 0.7788, 0.46, 1.0); - vec4 light_color = vec4(0.7, 0.7, 0.7, 1.0); - vec4 color = vec4(object_color.xyz, 1.0f); - - vec4 light_location = vec4(0.0, 100.0, 0.0, 1.0); - vec4 light_direction = normalize(light_location - frag_pos); - float diff = max(dot(norm.xyz, light_direction.xyz), 0.0); - - vec4 diffuse = diff * light_color; - - outy = color + diffuse; -} diff --git a/assets/shaders/shader.vert b/assets/shaders/shader.vert deleted file mode 100644 index 028d53a..0000000 --- a/assets/shaders/shader.vert +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 core -layout (location = 0) in vec3 pos; -layout (location = 1) in vec3 normal; - -uniform mat4 view; -uniform mat4 projection; -uniform mat4 translation; -uniform mat4 rotation; -uniform vec3 color; -uniform float scale; - -out vec4 frag_pos; -out vec4 frag_normal; -out vec3 object_color; - -void main() { - gl_Position = projection * view * translation * (vec4(pos.xyz, 1.0) * vec4(scale, scale, scale, 1.0)); - frag_pos = gl_Position; - frag_normal = translation * vec4(normal.xyz, 1.0); - object_color = color; -} \ No newline at end of file diff --git a/include/batch.h b/include/batch.h new file mode 100644 index 0000000..706c807 --- /dev/null +++ b/include/batch.h @@ -0,0 +1,14 @@ +#ifndef BATCH_H +#define BATCH_H + +#include + +struct batch { + int id; + GLint vertex_array; + GLint vertex_buffer; + GLint indices_buffer; + GLint normals_buffer; +}; + +#endif diff --git a/include/camera.h b/include/camera.h index ce37dc2..4b3ccec 100644 --- a/include/camera.h +++ b/include/camera.h @@ -5,7 +5,7 @@ #include #define CAMERA_PERSPECTIVE 0x1 -#define CAMERA_ORTHOGONAL 0x2 +#define CAMERA_ORTHOGRAPHIC 0x2 struct camera { int type; diff --git a/include/object.h b/include/object.h index b091869..399964c 100644 --- a/include/object.h +++ b/include/object.h @@ -19,7 +19,6 @@ struct object { vec4 translation_force; vec4 position; vec3 color; - float mass; void *next; struct model *model; @@ -33,6 +32,6 @@ struct object { struct model *load_model(const char *path); int record_path(struct object *obj); -struct object *create_object(struct object **o, float mass, struct model *model); +struct object *create_object(struct object **o, struct model *model); #endif diff --git a/include/shaders.h b/include/shaders.h new file mode 100644 index 0000000..204673f --- /dev/null +++ b/include/shaders.h @@ -0,0 +1,48 @@ +#ifndef SHADERS_H +#define SHADERS_H + +#define DEFAULT_VSHADER "#version 330 core\n" \ + "layout (location = 0) in vec3 pos;\n" \ + "layout (location = 1) in vec3 normal;\n" \ + "\n" \ + "uniform mat4 view;\n" \ + "uniform mat4 projection;\n" \ + "uniform mat4 translation;\n" \ + "uniform mat4 rotation;\n" \ + "uniform vec3 color;\n" \ + "uniform float scale;\n" \ + "\n" \ + "out vec4 frag_pos;\n" \ + "out vec4 frag_normal;\n" \ + "out vec3 object_color;\n" \ + "\n" \ + "void main() {\n" \ + " gl_Position = projection * view * translation * (vec4(pos.xyz, 1.0) * vec4(scale, scale, scale, 1.0));\n" \ + " frag_pos = gl_Position;\n" \ + " frag_normal = translation * vec4(normal.xyz, 1.0);\n" \ + " object_color = color;\n" \ + "}" + +#define DEFAULT_FSHADER "#version 330 core\n" \ + "in vec4 frag_pos;\n" \ + "in vec4 frag_normal;\n" \ + "in vec3 object_color;\n" \ + "\n" \ + "out vec4 outy;\n" \ + "\n" \ + "void main() {\n" \ + " vec4 norm = normalize(frag_normal);\n" \ + " vec4 ambient = vec4(0.7, 0.7788, 0.46, 1.0);\n" \ + " vec4 light_color = vec4(0.7, 0.7, 0.7, 1.0);\n" \ + " vec4 color = vec4(object_color.xyz, 1.0f);\n" \ + "\n" \ + " vec4 light_location = vec4(0.0, 100.0, 0.0, 1.0);\n" \ + " vec4 light_direction = normalize(light_location - frag_pos);\n" \ + " float diff = max(dot(norm.xyz, light_direction.xyz), 0.0);\n" \ + "\n" \ + " vec4 diffuse = diff * light_color;\n" \ + "\n" \ + " outy = color + diffuse;\n" \ + "}" + +#endif diff --git a/src/batch.c b/src/batch.c new file mode 100644 index 0000000..be68695 --- /dev/null +++ b/src/batch.c @@ -0,0 +1,7 @@ +#include "batch.h" + +int start_batch(struct batch **ab) { + if (*ab != NULL) { + return -1; + } +} diff --git a/camera.c b/src/camera.c similarity index 91% rename from camera.c rename to src/camera.c index 204a46d..c3ef520 100644 --- a/camera.c +++ b/src/camera.c @@ -1,11 +1,11 @@ #include #include #include -#include "include/camera.h" +#include "camera.h" struct camera *create_camera(int type) { if (type != CAMERA_PERSPECTIVE && - type != CAMERA_ORTHOGONAL) { + type != CAMERA_ORTHOGRAPHIC) { return NULL; } @@ -24,7 +24,7 @@ struct camera *create_camera(int type) { c->pitch = 0.0f; c->sensitivity = 0.02f; - if (c->type == CAMERA_ORTHOGONAL) { + if (c->type == CAMERA_ORTHOGRAPHIC) { c->viewport_modifier = 0.5f; } @@ -57,7 +57,7 @@ int bake_camera(struct camera *c) { (float) ((float)screen_viewport[2]/(float)screen_viewport[3]), c->near_z, c->far_z, c->projection); - } else if (c->type == CAMERA_ORTHOGONAL) { + } else if (c->type == CAMERA_ORTHOGRAPHIC) { float left = -screen_viewport[2]/2 * c->viewport_modifier; float right = screen_viewport[2]/2 * c->viewport_modifier; float top = screen_viewport[3]/2 * c->viewport_modifier; diff --git a/math.c b/src/math.c similarity index 89% rename from math.c rename to src/math.c index 88424cf..3ecfc9e 100644 --- a/math.c +++ b/src/math.c @@ -1,5 +1,5 @@ #include "math.h" -#include "include/object.h" +#include "object.h" #include float frand48(void) { diff --git a/object.c b/src/object.c similarity index 94% rename from object.c rename to src/object.c index 72a665e..cce4e8d 100644 --- a/object.c +++ b/src/object.c @@ -1,6 +1,6 @@ -#include "include/object.h" +#include "object.h" -#include "include/math.h" +#include "math.h" #include #include #include @@ -93,14 +93,13 @@ struct model *load_model(const char *path) { return nm; } -struct object *create_object(struct object **o, float mass, struct model *model) { +struct object *create_object(struct object **o, struct model *model) { struct object *no = malloc(sizeof(struct object)); if (no == NULL) { return NULL; } memset(no, 0, sizeof(struct object)); - no->mass = mass; no->scale = 1.0f; no->model = model; glm_vec4_one(no->position); diff --git a/rendlib.c b/src/rendlib.c similarity index 87% rename from rendlib.c rename to src/rendlib.c index e379672..b94fe41 100644 --- a/rendlib.c +++ b/src/rendlib.c @@ -5,9 +5,10 @@ #include #include #include -#include "include/rendlib.h" -#include "include/camera.h" -#include "include/object.h" +#include "rendlib.h" +#include "camera.h" +#include "object.h" +#include "shaders.h" // global variables int window_width = 960; @@ -15,6 +16,7 @@ int window_height = 540; char *window_title = "rendlib window"; struct camera *ac; +struct batch *ab; void (*keyboard_probe)(int key, int scancode, int action, int mods); void (*mouse_probe)(double x, double y); @@ -30,8 +32,10 @@ unsigned int shader_program; unsigned int vertex_shader; unsigned int fragment_shader; -const char *object_vertex_shader_location = "assets/shaders/shader.vert"; -const char *object_fragment_shader_location = "assets/shaders/shader.frag"; +extern char *default_vshader; +extern char *default_fshader; +extern int default_vshader_len; +extern int default_fshader_len; void update_screen_viewport(int x, int y, int width, int height) { GLFWwindow *w = glfwGetCurrentContext(); @@ -40,7 +44,27 @@ void update_screen_viewport(int x, int y, int width, int height) { glGetIntegerv(GL_VIEWPORT, screen_viewport); } -int load_shader(const char *path, unsigned int shader) { +int load_shader(char **s, int *n, unsigned int shader) { + glShaderSource(shader, 1, (const char **) s, n); + glCompileShader(shader); + + int success; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (success != GL_TRUE) { + int log_length; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length); + + char log[log_length]; + glGetShaderInfoLog(shader, log_length, NULL, log); + + fprintf(stderr, "Shader Compilation Error: %s\n", log); + return -1; + } + + return 0; +} + +int fload_shader(char *path, unsigned int shader) { FILE *fp = fopen(path, "r"); if (fp == NULL) { fprintf(stderr, "error: cannot open file '%s'\n", path); @@ -70,41 +94,45 @@ int load_shader(const char *path, unsigned int shader) { } while (rb < len); fclose(fp); - glShaderSource(shader, 1, (const char **) &fc, &rb); - glCompileShader(shader); - - int success; - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (success != GL_TRUE) { - int log_length; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length); - - char log[log_length]; - glGetShaderInfoLog(shader, log_length, NULL, log); - - fprintf(stderr, "Shader Compilation Error: %s\n", log); - return -1; - } - + int ret = load_shader(&fc, &rb, shader); free(fc); - return 0; + return ret; } -int load_shaders(void) { +int load_default_shaders(void) { + int ret = 0; glDeleteProgram(shader_program); shader_program = glCreateProgram(); vertex_shader = glCreateShader(GL_VERTEX_SHADER); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); - if (load_shader(object_vertex_shader_location, vertex_shader) == -1) { + int vshader_len = strlen(DEFAULT_VSHADER); + char *vshader = strndup(DEFAULT_VSHADER, vshader_len); + if (vshader == NULL) { return -1; } - if (load_shader(object_fragment_shader_location, fragment_shader) == -1) { + ret = load_shader(&vshader, &vshader_len, vertex_shader); + if (ret < 0) { return -1; } + free(vshader); + + int fshader_len = strlen(DEFAULT_FSHADER); + char *fshader = strndup(DEFAULT_FSHADER, fshader_len); + if (fshader == NULL) { + return -1; + } + + ret = load_shader(&fshader, &fshader_len, fragment_shader); + if (ret < 0) { + return -1; + } + + free(fshader); + glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glLinkProgram(shader_program); @@ -257,7 +285,7 @@ int rendlib_start_window(int argc, char *argv[]) { return err_glew_init; } - ret = load_shaders(); + ret = load_default_shaders(); if (ret < 0) { return err_shaders_init; } diff --git a/assets/models/sphere.obj b/tests/assets-01/sphere.obj similarity index 100% rename from assets/models/sphere.obj rename to tests/assets-01/sphere.obj diff --git a/dev.c b/tests/test-01.c similarity index 94% rename from dev.c rename to tests/test-01.c index 1f56abd..4d5f546 100644 --- a/dev.c +++ b/tests/test-01.c @@ -1,9 +1,9 @@ #include #include #include -#include "include/rendlib.h" -#include "include/camera.h" -#include "include/object.h" +#include "rendlib.h" +#include "camera.h" +#include "object.h" #define INPUT_ESCAPE 0b00000001 #define INPUT_RELOAD 0b00000010 @@ -158,20 +158,20 @@ int main(int argc, char *argv[]) { mouse_probe = &mousep; update_probe = &updatep; - struct model *m = load_model("assets/models/sphere.obj"); + struct model *m = load_model("assets-01/sphere.obj"); if (m == NULL) { fprintf(stderr, "--error: loading model\n"); return EXIT_FAILURE; } - struct object *o = create_object(&objects, 100.0f, m); + struct object *o = create_object(&objects, m); if (o == NULL) { fprintf(stderr, "--error: creating object\n"); return EXIT_FAILURE; } - struct camera *c = create_camera(CAMERA_PERSPECTIVE); - if (c == NULL) { + ac = create_camera(CAMERA_PERSPECTIVE); + if (ac == NULL) { fprintf(stderr, "--error: creating camera\n"); return EXIT_FAILURE; }