lib: add mini tests framework
This commit is contained in:
parent
bca01fe3cd
commit
c463904a6b
41
Makefile
41
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)
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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
14
include/batch.h
Normal 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
|
|
@ -5,7 +5,7 @@
|
|||
#include <cglm/mat4.h>
|
||||
|
||||
#define CAMERA_PERSPECTIVE 0x1
|
||||
#define CAMERA_ORTHOGONAL 0x2
|
||||
#define CAMERA_ORTHOGRAPHIC 0x2
|
||||
|
||||
struct camera {
|
||||
int type;
|
||||
|
|
|
@ -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
|
||||
|
|
48
include/shaders.h
Normal file
48
include/shaders.h
Normal 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
7
src/batch.c
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include "batch.h"
|
||||
|
||||
int start_batch(struct batch **ab) {
|
||||
if (*ab != NULL) {
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
#include <string.h>
|
||||
#include <cglm/cam.h>
|
||||
#include <GL/glew.h>
|
||||
#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;
|
|
@ -1,5 +1,5 @@
|
|||
#include "math.h"
|
||||
#include "include/object.h"
|
||||
#include "object.h"
|
||||
#include <cglm/cglm.h>
|
||||
|
||||
float frand48(void) {
|
|
@ -1,6 +1,6 @@
|
|||
#include "include/object.h"
|
||||
#include "object.h"
|
||||
|
||||
#include "include/math.h"
|
||||
#include "math.h"
|
||||
#include <math.h>
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
|
@ -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);
|
|
@ -5,9 +5,10 @@
|
|||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cglm/cglm.h>
|
||||
#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;
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user