lib: add mini tests framework

This commit is contained in:
Kevin J. 2024-10-14 22:40:35 +02:00
parent bca01fe3cd
commit c463904a6b
14 changed files with 167 additions and 103 deletions

View File

@ -1,24 +1,35 @@
CC=gcc CC=gcc
CFLAGS=-DFREEGLUT_STATIC -g3 CFLAGS=-DFREEGLUT -g3 -iquote include/
LFLAGS=-lglfw -L/usr/lib64 -lGLEW -lGL -lX11 -lGLU -lassimp -lm LFLAGS=-lglfw -L/usr/lib64 -lGLEW -lGL -lX11 -lGLU -lassimp -lm
LD=ld LD=ld
OUT=build SRCDIR=src
DEV=dev.c BINDIR=build
TSTDIR=tests
all: LIB_SRCS = $(wildcard $(SRCDIR)/*.c)
mkdir -p $(OUT) LIB_OBJS = $(addprefix $(BINDIR)/, $(notdir $(LIB_SRCS:.c=.o)))
$(CC) $(CFLAGS) -c rendlib.c -o $(OUT)/rendlib.o $(LFLAGS) TST_SRCS = $(wildcard $(TSTDIR)/*.c)
$(CC) $(CFLAGS) -c math.c -o $(OUT)/math.o $(LFLAGS) TST_OBJS = $(addprefix $(BINDIR)/$(TSTDIR)/, $(notdir $(TST_SRCS:.c=.o)))
$(CC) $(CFLAGS) -c object.c -o $(OUT)/object.o $(LFLAGS) TST_ASST = $(shell find $(TSTDIR) -maxdepth 1 -mindepth 1 -type d)
$(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
dev: all init:
$(CC) $(CFLAGS) dev.c $(OUT)/rendlib -o $(OUT)/dev $(LFLAGS) mkdir -p $(BINDIR)
mkdir -p $(BINDIR)/tests
devall: dev all: init $(LIB_OBJS)
$(OUT)/dev $(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: clean:
rm -rf $(OUT) rm -rf $(BINDIR)

View File

@ -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;
}

View File

@ -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;
}

14
include/batch.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef BATCH_H
#define BATCH_H
#include <GL/glew.h>
struct batch {
int id;
GLint vertex_array;
GLint vertex_buffer;
GLint indices_buffer;
GLint normals_buffer;
};
#endif

View File

@ -5,7 +5,7 @@
#include <cglm/mat4.h> #include <cglm/mat4.h>
#define CAMERA_PERSPECTIVE 0x1 #define CAMERA_PERSPECTIVE 0x1
#define CAMERA_ORTHOGONAL 0x2 #define CAMERA_ORTHOGRAPHIC 0x2
struct camera { struct camera {
int type; int type;

View File

@ -19,7 +19,6 @@ struct object {
vec4 translation_force; vec4 translation_force;
vec4 position; vec4 position;
vec3 color; vec3 color;
float mass;
void *next; void *next;
struct model *model; struct model *model;
@ -33,6 +32,6 @@ struct object {
struct model *load_model(const char *path); struct model *load_model(const char *path);
int record_path(struct object *obj); 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 #endif

48
include/shaders.h Normal file
View File

@ -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

7
src/batch.c Normal file
View File

@ -0,0 +1,7 @@
#include "batch.h"
int start_batch(struct batch **ab) {
if (*ab != NULL) {
return -1;
}
}

View File

@ -1,11 +1,11 @@
#include <string.h> #include <string.h>
#include <cglm/cam.h> #include <cglm/cam.h>
#include <GL/glew.h> #include <GL/glew.h>
#include "include/camera.h" #include "camera.h"
struct camera *create_camera(int type) { struct camera *create_camera(int type) {
if (type != CAMERA_PERSPECTIVE && if (type != CAMERA_PERSPECTIVE &&
type != CAMERA_ORTHOGONAL) { type != CAMERA_ORTHOGRAPHIC) {
return NULL; return NULL;
} }
@ -24,7 +24,7 @@ struct camera *create_camera(int type) {
c->pitch = 0.0f; c->pitch = 0.0f;
c->sensitivity = 0.02f; c->sensitivity = 0.02f;
if (c->type == CAMERA_ORTHOGONAL) { if (c->type == CAMERA_ORTHOGRAPHIC) {
c->viewport_modifier = 0.5f; c->viewport_modifier = 0.5f;
} }
@ -57,7 +57,7 @@ int bake_camera(struct camera *c) {
(float) ((float)screen_viewport[2]/(float)screen_viewport[3]), (float) ((float)screen_viewport[2]/(float)screen_viewport[3]),
c->near_z, c->far_z, c->near_z, c->far_z,
c->projection); c->projection);
} else if (c->type == CAMERA_ORTHOGONAL) { } else if (c->type == CAMERA_ORTHOGRAPHIC) {
float left = -screen_viewport[2]/2 * c->viewport_modifier; float left = -screen_viewport[2]/2 * c->viewport_modifier;
float right = 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; float top = screen_viewport[3]/2 * c->viewport_modifier;

View File

@ -1,5 +1,5 @@
#include "math.h" #include "math.h"
#include "include/object.h" #include "object.h"
#include <cglm/cglm.h> #include <cglm/cglm.h>
float frand48(void) { float frand48(void) {

View File

@ -1,6 +1,6 @@
#include "include/object.h" #include "object.h"
#include "include/math.h" #include "math.h"
#include <math.h> #include <math.h>
#include <assimp/cimport.h> #include <assimp/cimport.h>
#include <assimp/scene.h> #include <assimp/scene.h>
@ -93,14 +93,13 @@ struct model *load_model(const char *path) {
return nm; 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)); struct object *no = malloc(sizeof(struct object));
if (no == NULL) { if (no == NULL) {
return NULL; return NULL;
} }
memset(no, 0, sizeof(struct object)); memset(no, 0, sizeof(struct object));
no->mass = mass;
no->scale = 1.0f; no->scale = 1.0f;
no->model = model; no->model = model;
glm_vec4_one(no->position); glm_vec4_one(no->position);

View File

@ -5,9 +5,10 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cglm/cglm.h> #include <cglm/cglm.h>
#include "include/rendlib.h" #include "rendlib.h"
#include "include/camera.h" #include "camera.h"
#include "include/object.h" #include "object.h"
#include "shaders.h"
// global variables // global variables
int window_width = 960; int window_width = 960;
@ -15,6 +16,7 @@ int window_height = 540;
char *window_title = "rendlib window"; char *window_title = "rendlib window";
struct camera *ac; struct camera *ac;
struct batch *ab;
void (*keyboard_probe)(int key, int scancode, int action, int mods); void (*keyboard_probe)(int key, int scancode, int action, int mods);
void (*mouse_probe)(double x, double y); void (*mouse_probe)(double x, double y);
@ -30,8 +32,10 @@ unsigned int shader_program;
unsigned int vertex_shader; unsigned int vertex_shader;
unsigned int fragment_shader; unsigned int fragment_shader;
const char *object_vertex_shader_location = "assets/shaders/shader.vert"; extern char *default_vshader;
const char *object_fragment_shader_location = "assets/shaders/shader.frag"; 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) { void update_screen_viewport(int x, int y, int width, int height) {
GLFWwindow *w = glfwGetCurrentContext(); GLFWwindow *w = glfwGetCurrentContext();
@ -40,7 +44,27 @@ void update_screen_viewport(int x, int y, int width, int height) {
glGetIntegerv(GL_VIEWPORT, screen_viewport); 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"); FILE *fp = fopen(path, "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "error: cannot open file '%s'\n", path); 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); } while (rb < len);
fclose(fp); fclose(fp);
glShaderSource(shader, 1, (const char **) &fc, &rb); int ret = load_shader(&fc, &rb, shader);
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;
}
free(fc); free(fc);
return 0; return ret;
} }
int load_shaders(void) { int load_default_shaders(void) {
int ret = 0;
glDeleteProgram(shader_program); glDeleteProgram(shader_program);
shader_program = glCreateProgram(); shader_program = glCreateProgram();
vertex_shader = glCreateShader(GL_VERTEX_SHADER); vertex_shader = glCreateShader(GL_VERTEX_SHADER);
fragment_shader = glCreateShader(GL_FRAGMENT_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; 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; 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, vertex_shader);
glAttachShader(shader_program, fragment_shader); glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program); glLinkProgram(shader_program);
@ -257,7 +285,7 @@ int rendlib_start_window(int argc, char *argv[]) {
return err_glew_init; return err_glew_init;
} }
ret = load_shaders(); ret = load_default_shaders();
if (ret < 0) { if (ret < 0) {
return err_shaders_init; return err_shaders_init;
} }

View File

@ -1,9 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "include/rendlib.h" #include "rendlib.h"
#include "include/camera.h" #include "camera.h"
#include "include/object.h" #include "object.h"
#define INPUT_ESCAPE 0b00000001 #define INPUT_ESCAPE 0b00000001
#define INPUT_RELOAD 0b00000010 #define INPUT_RELOAD 0b00000010
@ -158,20 +158,20 @@ int main(int argc, char *argv[]) {
mouse_probe = &mousep; mouse_probe = &mousep;
update_probe = &updatep; update_probe = &updatep;
struct model *m = load_model("assets/models/sphere.obj"); struct model *m = load_model("assets-01/sphere.obj");
if (m == NULL) { if (m == NULL) {
fprintf(stderr, "--error: loading model\n"); fprintf(stderr, "--error: loading model\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
struct object *o = create_object(&objects, 100.0f, m); struct object *o = create_object(&objects, m);
if (o == NULL) { if (o == NULL) {
fprintf(stderr, "--error: creating object\n"); fprintf(stderr, "--error: creating object\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
struct camera *c = create_camera(CAMERA_PERSPECTIVE); ac = create_camera(CAMERA_PERSPECTIVE);
if (c == NULL) { if (ac == NULL) {
fprintf(stderr, "--error: creating camera\n"); fprintf(stderr, "--error: creating camera\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }