Compare commits
4 Commits
a176afab7e
...
c463904a6b
Author | SHA1 | Date | |
---|---|---|---|
c463904a6b | |||
bca01fe3cd | |||
950fb4d353 | |||
5d19366642 |
12
INSTALL
Normal file
12
INSTALL
Normal file
|
@ -0,0 +1,12 @@
|
|||
+=============+
|
||||
| INSTALL |
|
||||
+=============+
|
||||
|
||||
DEBIAN PACKAGES
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install libglew-dev
|
||||
sudo apt-get install libglfw3-dev
|
||||
sudo apt-get install libcglm-dev
|
||||
sudo apt-get install libassimp-dev
|
||||
|
40
Makefile
40
Makefile
|
@ -1,23 +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)
|
||||
$(LD) -relocatable $(OUT)/rendlib.o $(OUT)/math.o $(OUT)/object.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;
|
||||
}
|
34
dev.c
34
dev.c
|
@ -1,34 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "include/rendlib.h"
|
||||
#include "include/object.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int ret = rendlib_start_window(argc, argv);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "--error: %d\n", ret);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
struct model *m = load_model("assets/models/sphere.obj");
|
||||
if (m == NULL) {
|
||||
fprintf(stderr, "--error: loading model\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
struct object *o = create_object(&objects, 100.0f, m);
|
||||
if (o == NULL) {
|
||||
fprintf(stderr, "--error: creating object\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ret = rendlib_render();
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "--error: %d\n", ret);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fprintf(stdout, "--rendering\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
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
|
32
include/camera.h
Normal file
32
include/camera.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include <cglm/vec3.h>
|
||||
#include <cglm/mat4.h>
|
||||
|
||||
#define CAMERA_PERSPECTIVE 0x1
|
||||
#define CAMERA_ORTHOGRAPHIC 0x2
|
||||
|
||||
struct camera {
|
||||
int type;
|
||||
float fov;
|
||||
float near_z;
|
||||
float far_z;
|
||||
float yaw;
|
||||
float pitch;
|
||||
float sensitivity;
|
||||
float viewport_modifier;
|
||||
|
||||
vec3 pos;
|
||||
vec3 front;
|
||||
vec3 center;
|
||||
vec3 up;
|
||||
|
||||
mat4 view;
|
||||
mat4 projection;
|
||||
};
|
||||
|
||||
struct camera *create_camera(int type);
|
||||
int bake_camera(struct camera *c);
|
||||
|
||||
#endif
|
|
@ -5,6 +5,5 @@
|
|||
#include <cglm/cglm.h>
|
||||
|
||||
float frand48(void);
|
||||
void calculate_gravity(struct object *src, struct object *target, vec3 force);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -19,27 +19,19 @@ struct object {
|
|||
vec4 translation_force;
|
||||
vec4 position;
|
||||
vec3 color;
|
||||
float mass;
|
||||
void *next;
|
||||
|
||||
float *paths;
|
||||
int paths_num;
|
||||
int paths_max;
|
||||
|
||||
struct model *model;
|
||||
float scale;
|
||||
|
||||
unsigned int vao; // array object for the actual object
|
||||
unsigned int vbo; // buffer for vertices
|
||||
unsigned int ebo; // buffer for indices
|
||||
unsigned int nbo; // buffer for normals
|
||||
|
||||
unsigned int pvao; // array object for paths
|
||||
unsigned int pbo; // buffer for paths
|
||||
unsigned int vao;
|
||||
unsigned int vbo; // vertices
|
||||
unsigned int ebo; // indices
|
||||
unsigned int nbo; // normals
|
||||
};
|
||||
|
||||
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
|
||||
|
|
|
@ -8,7 +8,13 @@ enum errs {
|
|||
err_shaders_init = -4,
|
||||
};
|
||||
|
||||
extern struct camera *ac;
|
||||
extern struct object *objects;
|
||||
|
||||
extern void (*keyboard_probe)(int key, int scancode, int action, int mods);
|
||||
extern void (*mouse_probe)(double x, double y);
|
||||
extern void (*update_probe)(void);
|
||||
|
||||
int rendlib_start_window(int argc, char *argv[]);
|
||||
int rendlib_render(void);
|
||||
|
||||
|
|
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
|
42
math.c
42
math.c
|
@ -1,42 +0,0 @@
|
|||
#include "math.h"
|
||||
#include "include/object.h"
|
||||
#include <cglm/cglm.h>
|
||||
|
||||
float frand48(void) {
|
||||
float number = (float) rand() / (float) (RAND_MAX + 1.0);
|
||||
float side = rand() % 2;
|
||||
if (side == 0) {
|
||||
number = -number;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
void calculate_gravity(struct object *src, struct object *target, vec3 force) {
|
||||
vec4 v4distance;
|
||||
glm_vec4_sub(target->position, src->position, v4distance);
|
||||
|
||||
vec3 v3distance;
|
||||
glm_vec3(v4distance, v3distance);
|
||||
|
||||
float distance_xy = sqrt((v3distance[0] * v3distance[0]) + (v3distance[1] * v3distance[1]));
|
||||
float distance_xyz = sqrt((distance_xy * distance_xy) + (v3distance[2] * v3distance[2]));
|
||||
float force_scale = 4.0f;
|
||||
|
||||
float g = 6.67f * 1e-11f;
|
||||
float top = g * src->mass * target->mass;
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
v3distance[i] = (v3distance[i] * v3distance[i] * v3distance[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (v3distance[i] == 0) {
|
||||
force[i] = 0.0f;
|
||||
continue;
|
||||
}
|
||||
|
||||
force[i] = (top / (distance_xyz / (target->position[i] - src->position[i]))) * force_scale;
|
||||
}
|
||||
}
|
||||
|
129
object.c
129
object.c
|
@ -1,129 +0,0 @@
|
|||
#include "include/object.h"
|
||||
|
||||
#include "include/math.h"
|
||||
#include <math.h>
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
struct model *load_model(const char *path) {
|
||||
struct model *new_model = (struct model *) calloc(1, sizeof(struct model));
|
||||
const struct aiScene *scene = aiImportFile(path, aiProcess_Triangulate);
|
||||
|
||||
if (scene == NULL) {
|
||||
fprintf(stderr, "Error: failed importing file from path '%s'", path);
|
||||
}
|
||||
|
||||
for (int mesh_index = 0; mesh_index < scene->mNumMeshes; mesh_index++) {
|
||||
struct aiMesh *mesh = scene->mMeshes[mesh_index];
|
||||
|
||||
// fetch vertices
|
||||
for (int vertex_index = 0; vertex_index < mesh->mNumVertices; vertex_index++) {
|
||||
struct aiVector3D *vertex = &(mesh->mVertices[vertex_index]);
|
||||
long start = new_model->vertices_num*3;
|
||||
|
||||
new_model->vertices_num++;
|
||||
new_model->vertices = (float *) realloc(new_model->vertices, new_model->vertices_num*3*sizeof(float));
|
||||
if (new_model->vertices == NULL) {
|
||||
fprintf(stderr, "Error: failed allocating memory for vertices\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
memcpy(&new_model->vertices[start], vertex, sizeof(float)*3);
|
||||
}
|
||||
|
||||
// fetch indices
|
||||
for (int face_index = 0; face_index < mesh->mNumFaces; face_index++) {
|
||||
struct aiFace *face = &(mesh->mFaces[face_index]);
|
||||
long start = new_model->indices_num;
|
||||
|
||||
new_model->indices_num += face->mNumIndices;
|
||||
new_model->indices = (unsigned int *) realloc(new_model->indices, sizeof(unsigned int)*new_model->indices_num);
|
||||
if (new_model->indices == NULL) {
|
||||
fprintf(stderr, "Error: failed allocating memory for indices\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
memcpy(&new_model->indices[start], face->mIndices, sizeof(unsigned int)*face->mNumIndices);
|
||||
}
|
||||
|
||||
// fetch normals
|
||||
for (int normal_index = 0; normal_index < mesh->mNumVertices; normal_index++) {
|
||||
struct aiVector3D *normal = &(mesh->mNormals[normal_index]);
|
||||
long start = new_model->normals_num*3;
|
||||
|
||||
new_model->normals_num++;
|
||||
new_model->normals = (float *) realloc(new_model->normals, new_model->normals_num*3*sizeof(float));
|
||||
if (new_model->normals == NULL) {
|
||||
fprintf(stderr, "Error: failed allocating memory for normals\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
memcpy(&new_model->normals[start], normal, sizeof(float)*3);
|
||||
}
|
||||
}
|
||||
|
||||
return new_model;
|
||||
|
||||
error:
|
||||
aiReleaseImport(scene);
|
||||
free(new_model->vertices);
|
||||
free(new_model->indices);
|
||||
free(new_model->normals);
|
||||
free(new_model);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int record_path(struct object *obj) {
|
||||
if (obj->paths_num <= obj->paths_max) {
|
||||
obj->paths = (float *) reallocarray(obj->paths, (obj->paths_num+1)*3, sizeof(float));
|
||||
}
|
||||
|
||||
if (obj->paths == NULL) {
|
||||
fprintf(stderr, "Error: failed allocating memory for paths of object\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(obj->paths+(obj->paths_num*3), obj->position, 3*sizeof(float));
|
||||
|
||||
if (obj->paths_num < obj->paths_max) {
|
||||
obj->paths_num++;
|
||||
goto end;
|
||||
}
|
||||
|
||||
// pop first element
|
||||
memmove(obj->paths, &obj->paths[3], (obj->paths_num)*3*sizeof(float));
|
||||
|
||||
end:
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct object *create_object(struct object **o, float mass, 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->paths_max = MAX_PATHS;
|
||||
no->model = model;
|
||||
glm_vec4_one(no->position);
|
||||
glm_vec3_one(no->color);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
no->color[i] = 0.5f + (fabs(frand48()) / 2);
|
||||
}
|
||||
|
||||
if (*o == NULL) {
|
||||
*o = no;
|
||||
return no;
|
||||
}
|
||||
|
||||
struct object *previous_object = *o;
|
||||
no->next = previous_object;
|
||||
*o = no;
|
||||
|
||||
return no;
|
||||
}
|
473
rendlib.c
473
rendlib.c
|
@ -1,473 +0,0 @@
|
|||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cglm/cglm.h>
|
||||
#include "include/rendlib.h"
|
||||
#include "include/math.h"
|
||||
#include "include/object.h"
|
||||
|
||||
// global variables
|
||||
int window_width = 960;
|
||||
int window_height = 540;
|
||||
char *window_title = "rendlib window";
|
||||
|
||||
// global settings
|
||||
float fov = 80.0f; // default fov
|
||||
float fov_change = 1.0f;
|
||||
vec3 camera_pos = { 0.0f, 0.0f, 20.0f };
|
||||
vec3 camera_front = { 0.0f, 0.0f, -1.0f };
|
||||
vec3 camera_up = { 0.0f, 1.0f, 0.0f };
|
||||
struct object *camera_lock = NULL; // is camera locked to any object?
|
||||
float camera_yaw = -90.0f; // x rotation
|
||||
float camera_pitch = 0.0f; // y rotation
|
||||
float camera_sensitivity = 0.01f;
|
||||
float top_movement_speed = 0.2f;
|
||||
vec3 speed = { 0.0f, 0.0f, 0.0f };
|
||||
|
||||
GLint screen_viewport[4]; // viewport: x,y,width,height
|
||||
int toggle_tracing = 0; // true or false
|
||||
long added_particles = 0;
|
||||
|
||||
// tmp input
|
||||
int inputs[8] = {0,0,0,0,0,0,};
|
||||
|
||||
// tmp
|
||||
struct model *sphere_model;
|
||||
struct object *objects;
|
||||
|
||||
// opengl
|
||||
unsigned int shader_program;
|
||||
unsigned int vertex_shader;
|
||||
unsigned int fragment_shader;
|
||||
|
||||
// shaders
|
||||
const char *object_vertex_shader_location = "assets/shaders/shader.vert";
|
||||
const char *object_fragment_shader_location = "assets/shaders/shader.frag";
|
||||
|
||||
int load_shader(const char *path, unsigned int shader) {
|
||||
FILE *fp = fopen(path, "r");
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "error: cannot open file '%s'\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
int len = ftell(fp);
|
||||
if (len == -1) {
|
||||
fprintf(stderr, "error: cannot fetch length of file '%s'\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_SET);
|
||||
char *fc = (char *) malloc(len);
|
||||
if (fc == NULL) {
|
||||
fprintf(stderr, "error: not enough dynamic memory\n");
|
||||
return -1;
|
||||
}
|
||||
memset(fc, 0, len);
|
||||
int rb = 0;
|
||||
do {
|
||||
rb += fread(fc+rb, sizeof(char), len-rb, fp);
|
||||
if (rb == 0) {
|
||||
break;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
free(fc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int load_shaders(void) {
|
||||
glDeleteProgram(shader_program);
|
||||
shader_program = glCreateProgram();
|
||||
|
||||
// create and load new shaders
|
||||
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
if (load_shader(object_vertex_shader_location, vertex_shader) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (load_shader(object_fragment_shader_location, fragment_shader) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// compile object shader program
|
||||
glAttachShader(shader_program, vertex_shader);
|
||||
glAttachShader(shader_program, fragment_shader);
|
||||
glLinkProgram(shader_program);
|
||||
|
||||
int success;
|
||||
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
|
||||
|
||||
if (success != GL_TRUE) {
|
||||
int log_length;
|
||||
glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &log_length);
|
||||
|
||||
char log[log_length];
|
||||
glGetProgramInfoLog(shader_program, log_length, NULL, log);
|
||||
|
||||
fprintf(stderr, "[object program] Shader Compilation Error: %s\n", log);
|
||||
return -1;
|
||||
}
|
||||
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_input(void) {
|
||||
int ret = 0;
|
||||
if (inputs[0]) {
|
||||
exit(EXIT_SUCCESS);
|
||||
inputs[0] = 0;
|
||||
}
|
||||
|
||||
if (inputs[1]) {
|
||||
ret = load_shaders();
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "--error: reloading shaders\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
inputs[1] = 0;
|
||||
}
|
||||
|
||||
if (inputs[2]) {
|
||||
vec3 side_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
|
||||
vec3 camera_side;
|
||||
glm_cross(camera_front, camera_up, camera_side);
|
||||
glm_normalize(camera_side);
|
||||
glm_vec3_mul(camera_side, side_scalar, camera_side);
|
||||
glm_vec3_sub(camera_pos, camera_side, camera_pos);
|
||||
}
|
||||
|
||||
if (inputs[3]) {
|
||||
vec3 side_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
|
||||
vec3 camera_side;
|
||||
glm_cross(camera_front, camera_up, camera_side);
|
||||
glm_normalize(camera_side);
|
||||
glm_vec3_mul(camera_side, side_scalar, camera_side);
|
||||
glm_vec3_add(camera_pos, camera_side, camera_pos);
|
||||
}
|
||||
|
||||
if (inputs[4]) {
|
||||
vec3 front_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
|
||||
glm_vec3_mul(front_scalar, camera_front, front_scalar);
|
||||
glm_vec3_sub(camera_pos, front_scalar, camera_pos);
|
||||
}
|
||||
|
||||
if (inputs[5]) {
|
||||
vec3 front_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
|
||||
glm_vec3_mul(front_scalar, camera_front, front_scalar);
|
||||
glm_vec3_add(camera_pos, front_scalar, camera_pos);
|
||||
}
|
||||
}
|
||||
|
||||
void window_size(GLFWwindow *w, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void display(void) {
|
||||
handle_input();
|
||||
GLFWwindow *w = glfwGetCurrentContext();
|
||||
|
||||
mat4 view;
|
||||
mat4 projection;
|
||||
|
||||
GLint translation_uniform;
|
||||
GLint view_uniform;
|
||||
GLint projection_uniform;
|
||||
GLint color_uniform;
|
||||
GLint scale_uniform;
|
||||
|
||||
glClearColor(0.13f, 0.13f, 0.13f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||
glUseProgram(shader_program);
|
||||
|
||||
glm_mat4_identity(view);
|
||||
vec3 camera_center;
|
||||
glm_vec3_add(camera_pos, camera_front, camera_center);
|
||||
glm_lookat(camera_pos, camera_center, camera_up, view);
|
||||
|
||||
glm_mat4_identity(projection);
|
||||
glm_perspective(glm_rad(fov), (float) screen_viewport[2]/(float) screen_viewport[3], 0.01f, 100000.0f, projection);
|
||||
|
||||
view_uniform = glGetUniformLocation(shader_program, "view");
|
||||
projection_uniform = glGetUniformLocation(shader_program, "projection");
|
||||
translation_uniform = glGetUniformLocation(shader_program, "translation");
|
||||
color_uniform = glGetUniformLocation(shader_program, "color");
|
||||
scale_uniform = glGetUniformLocation(shader_program, "scale");
|
||||
|
||||
glUniformMatrix4fv(view_uniform, 1, GL_FALSE, (float *) view);
|
||||
glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, (float *) projection);
|
||||
|
||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||
mat4 translation_matrix;
|
||||
glm_mat4_identity(translation_matrix);
|
||||
struct model *obj_model = obj->model;
|
||||
|
||||
// calculate gravity
|
||||
for (struct object *target = objects; target != NULL; target = target->next) {
|
||||
if (target == obj) {
|
||||
continue;
|
||||
}
|
||||
|
||||
vec3 force;
|
||||
glm_vec3_zero(force);
|
||||
calculate_gravity(obj, target, force);
|
||||
|
||||
vec4 force_new;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
force_new[i] = force[i];
|
||||
}
|
||||
force_new[3] = 0.0f;
|
||||
|
||||
float n = obj->mass;
|
||||
vec4 scaler = {n,n,n,1.0f};
|
||||
glm_vec4_div(force_new, scaler, force_new);
|
||||
|
||||
glm_vec4_add(force_new, obj->translation_force, obj->translation_force);
|
||||
}
|
||||
|
||||
glm_vec4_add(obj->position, obj->translation_force, obj->position);
|
||||
|
||||
// follow object if camera locked
|
||||
if (camera_lock == obj) {
|
||||
glm_vec3_add(camera_pos, obj->translation_force, camera_pos);
|
||||
}
|
||||
|
||||
// record path
|
||||
if (toggle_tracing == 1) {
|
||||
if (record_path(obj) == -1) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
glm_translate(translation_matrix, obj->position);
|
||||
|
||||
glUniformMatrix4fv(translation_uniform, 1, GL_FALSE, (float *) translation_matrix);
|
||||
|
||||
glUniform3fv(color_uniform, 1, (float *) obj->color);
|
||||
glUniform1f(scale_uniform, obj->scale);
|
||||
|
||||
glBindVertexArray(obj->vao);
|
||||
glDrawElements(GL_TRIANGLES, obj_model->indices_num, GL_UNSIGNED_INT, (void *) 0);
|
||||
|
||||
glBindVertexArray(obj->pvao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, obj->pbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, obj->paths_num*3*sizeof(float),obj->paths, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glm_mat4_identity(translation_matrix);
|
||||
glUniformMatrix4fv(translation_uniform, 1, GL_FALSE, (float *) translation_matrix);
|
||||
glUniform1f(scale_uniform, 1.0f);
|
||||
|
||||
glDrawArrays(GL_LINE_STRIP, 0, obj->paths_num);
|
||||
}
|
||||
|
||||
/*glutPostRedisplay();
|
||||
glutSwapBuffers();*/
|
||||
glfwSwapBuffers(w);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void rendlib_bake_graphics(void) {
|
||||
// setup default mouse position
|
||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||
|
||||
|
||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||
struct model *obj_model = obj->model;
|
||||
glGenVertexArrays(1, &obj->vao);
|
||||
glGenVertexArrays(1, &obj->pvao);
|
||||
glGenBuffers(1, &obj->vbo);
|
||||
glGenBuffers(1, &obj->ebo);
|
||||
glGenBuffers(1, &obj->nbo);
|
||||
glGenBuffers(1, &obj->pbo);
|
||||
|
||||
glBindVertexArray(obj->vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER,obj->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER,obj_model->vertices_num*3*sizeof(float),obj_model->vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, obj->nbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, obj_model->normals_num*3*sizeof(float), obj_model->normals, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,obj->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, obj_model->indices_num*sizeof(unsigned int), obj_model->indices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
void keyboard(GLFWwindow *w, int key, int scancode, int action, int mods) {
|
||||
if (action == GLFW_PRESS) {
|
||||
switch (key) {
|
||||
case GLFW_KEY_ESCAPE:
|
||||
inputs[0] = 1;
|
||||
break;
|
||||
case GLFW_KEY_R:
|
||||
inputs[1] = 1;
|
||||
break;
|
||||
case GLFW_KEY_A:
|
||||
inputs[2] = 1;
|
||||
break;
|
||||
case GLFW_KEY_D:
|
||||
inputs[3] = 1;
|
||||
break;
|
||||
case GLFW_KEY_S:
|
||||
inputs[4] = 1;
|
||||
break;
|
||||
case GLFW_KEY_W:
|
||||
inputs[5] = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
} else if (action == GLFW_RELEASE) {
|
||||
switch (key) {
|
||||
case GLFW_KEY_A:
|
||||
inputs[2] = 0;
|
||||
break;
|
||||
case GLFW_KEY_D:
|
||||
inputs[3] = 0;
|
||||
break;
|
||||
case GLFW_KEY_S:
|
||||
inputs[4] = 0;
|
||||
break;
|
||||
case GLFW_KEY_W:
|
||||
inputs[5] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mouse(int button, int state, int x, int y) {
|
||||
switch (button) {
|
||||
case 3:
|
||||
if (fov-fov_change < 0.0f) {
|
||||
break;
|
||||
}
|
||||
|
||||
fov -= fov_change;
|
||||
break;
|
||||
case 4:
|
||||
if (fov+fov_change > 180.0f) {
|
||||
break;
|
||||
}
|
||||
|
||||
fov += fov_change;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int warped_pointer = 0;
|
||||
int first_pointer = 1;
|
||||
void mouse_motion(GLFWwindow *window, double x, double y) {
|
||||
if (warped_pointer == 1) {
|
||||
warped_pointer = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
warped_pointer = 1;
|
||||
GLFWwindow *w = glfwGetCurrentContext();
|
||||
glfwSetCursorPos(w, (screen_viewport[2]/2), (screen_viewport[3]/2));
|
||||
|
||||
if (first_pointer == 1) {
|
||||
first_pointer = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
float offset_x = (float) (x - (screen_viewport[2]/2)) * camera_sensitivity;
|
||||
float offset_y = (float) (y - (screen_viewport[3]/2)) * camera_sensitivity;
|
||||
|
||||
camera_yaw += offset_x;
|
||||
camera_pitch -= offset_y;
|
||||
|
||||
// limit view rotation
|
||||
if (camera_pitch < -89.9f) {
|
||||
camera_pitch = -89.9f;
|
||||
}
|
||||
if (camera_pitch > 89.9f) {
|
||||
camera_pitch = 89.9f;
|
||||
}
|
||||
|
||||
vec3 view_direction;
|
||||
view_direction[0] = cos(glm_rad(camera_yaw)) * cos(glm_rad(camera_pitch));
|
||||
view_direction[1] = sin(glm_rad(camera_pitch));
|
||||
view_direction[2] = sin(glm_rad(camera_yaw)) * cos(glm_rad(camera_pitch));
|
||||
glm_normalize_to(view_direction, camera_front);
|
||||
}
|
||||
|
||||
int rendlib_start_window(int argc, char *argv[]) {
|
||||
int ret = glfwInit();
|
||||
if (!ret) {
|
||||
return err_glfw_init;
|
||||
}
|
||||
|
||||
GLFWwindow *w = glfwCreateWindow(window_width, window_height, window_title, NULL, NULL);
|
||||
if (w == NULL) {
|
||||
return err_glfw_win;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(w);
|
||||
glfwSetWindowSizeCallback(w, window_size);
|
||||
glfwSetCursorPosCallback(w, mouse_motion);
|
||||
glfwSetKeyCallback(w, keyboard);
|
||||
|
||||
ret = glewInit();
|
||||
if (ret != GLEW_OK) {
|
||||
return err_glew_init;
|
||||
}
|
||||
|
||||
ret = load_shaders();
|
||||
if (ret < 0) {
|
||||
return err_shaders_init;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rendlib_render(void) {
|
||||
rendlib_bake_graphics();
|
||||
for (;;) {
|
||||
display();
|
||||
}
|
||||
return 0;
|
||||
}
|
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;
|
||||
}
|
||||
}
|
72
src/camera.c
Normal file
72
src/camera.c
Normal file
|
@ -0,0 +1,72 @@
|
|||
#include <string.h>
|
||||
#include <cglm/cam.h>
|
||||
#include <GL/glew.h>
|
||||
#include "camera.h"
|
||||
|
||||
struct camera *create_camera(int type) {
|
||||
if (type != CAMERA_PERSPECTIVE &&
|
||||
type != CAMERA_ORTHOGRAPHIC) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct camera *c = malloc(sizeof(struct camera));
|
||||
if (c == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(c, 0, sizeof(struct camera));
|
||||
|
||||
c->type = type;
|
||||
c->fov = 80.0f;
|
||||
c->near_z = 0.01f;
|
||||
c->far_z = 1000.0f;
|
||||
c->yaw = -90.0f;
|
||||
c->pitch = 0.0f;
|
||||
c->sensitivity = 0.02f;
|
||||
|
||||
if (c->type == CAMERA_ORTHOGRAPHIC) {
|
||||
c->viewport_modifier = 0.5f;
|
||||
}
|
||||
|
||||
c->pos[0] = 0.0f;
|
||||
c->pos[1] = 0.0f;
|
||||
c->pos[2] = 10.0f;
|
||||
|
||||
c->front[0] = 0.0f;
|
||||
c->front[1] = 0.0f;
|
||||
c->front[2] = -1.0f;
|
||||
|
||||
c->up[0] = 0.0f;
|
||||
c->up[1] = 1.0f;
|
||||
c->up[2] = 0.0f;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
int bake_camera(struct camera *c) {
|
||||
glm_mat4_identity(c->view);
|
||||
glm_vec3_add(c->pos, c->front, c->center);
|
||||
glm_lookat(c->pos, c->center, c->up, c->view);
|
||||
|
||||
glm_mat4_identity(c->projection);
|
||||
|
||||
int screen_viewport[4];
|
||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||
if (c->type == CAMERA_PERSPECTIVE) {
|
||||
glm_perspective(glm_rad(c->fov),
|
||||
(float) ((float)screen_viewport[2]/(float)screen_viewport[3]),
|
||||
c->near_z, c->far_z,
|
||||
c->projection);
|
||||
} 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;
|
||||
float bottom = -screen_viewport[3]/2 * c->viewport_modifier;
|
||||
glm_ortho(left, right, bottom, top, c->near_z, c->far_z, c->projection);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
13
src/math.c
Normal file
13
src/math.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "math.h"
|
||||
#include "object.h"
|
||||
#include <cglm/cglm.h>
|
||||
|
||||
float frand48(void) {
|
||||
float number = (float) rand() / (float) (RAND_MAX + 1.0);
|
||||
float side = rand() % 2;
|
||||
if (side == 0) {
|
||||
number = -number;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
122
src/object.c
Normal file
122
src/object.c
Normal file
|
@ -0,0 +1,122 @@
|
|||
#include "object.h"
|
||||
|
||||
#include "math.h"
|
||||
#include <math.h>
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
static int load_vertices(struct model *m, struct aiMesh *ms) {
|
||||
m->vertices_num = ms->mNumVertices;
|
||||
int len = m->vertices_num * sizeof(struct aiVector3D);
|
||||
m->vertices = malloc(len);
|
||||
if (m->vertices == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(m->vertices, 0, len);
|
||||
memcpy(m->vertices, ms->mVertices, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_indices(struct model *m, struct aiMesh *ms) {
|
||||
for (int face_index = 0; face_index < ms->mNumFaces; face_index++) {
|
||||
struct aiFace *face = &(ms->mFaces[face_index]);
|
||||
long start = m->indices_num;
|
||||
|
||||
m->indices_num += face->mNumIndices;
|
||||
m->indices = (unsigned int *) realloc(m->indices, sizeof(unsigned int)*m->indices_num);
|
||||
if (m->indices == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(&m->indices[start], face->mIndices, sizeof(unsigned int)*face->mNumIndices);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_normals(struct model *m, struct aiMesh *ms) {
|
||||
m->normals_num = ms->mNumVertices;
|
||||
int len = m->vertices_num * sizeof(struct aiVector3D);
|
||||
m->normals = malloc(len);
|
||||
if (m->vertices == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memset(m->normals, 0, len);
|
||||
memcpy(m->normals, ms->mNormals, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct model *load_model(const char *path) {
|
||||
int ret = 0;
|
||||
struct model *nm = malloc(sizeof(struct model));
|
||||
if (nm == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(nm, 0, sizeof(struct model));
|
||||
|
||||
const struct aiScene *scene = aiImportFile(path, aiProcess_Triangulate);
|
||||
if (scene == NULL) {
|
||||
free(nm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int mesh_index = 0; mesh_index < scene->mNumMeshes; mesh_index++) {
|
||||
struct aiMesh *mesh = scene->mMeshes[mesh_index];
|
||||
|
||||
ret = load_vertices(nm, mesh);
|
||||
if (ret < 0) {
|
||||
free(nm);
|
||||
aiReleaseImport(scene);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = load_indices(nm, mesh);
|
||||
if (ret < 0) {
|
||||
free(nm->vertices);
|
||||
free(nm);
|
||||
aiReleaseImport(scene);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = load_normals(nm, mesh);
|
||||
if (ret < 0) {
|
||||
free(nm->vertices);
|
||||
free(nm->indices);
|
||||
free(nm);
|
||||
aiReleaseImport(scene);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return nm;
|
||||
}
|
||||
|
||||
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->scale = 1.0f;
|
||||
no->model = model;
|
||||
glm_vec4_one(no->position);
|
||||
glm_vec3_one(no->color);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
no->color[i] = 0.5f + (fabs(frand48()) / 2);
|
||||
}
|
||||
|
||||
if (*o == NULL) {
|
||||
*o = no;
|
||||
return no;
|
||||
}
|
||||
|
||||
struct object *previous_object = *o;
|
||||
no->next = previous_object;
|
||||
*o = no;
|
||||
|
||||
return no;
|
||||
}
|
319
src/rendlib.c
Normal file
319
src/rendlib.c
Normal file
|
@ -0,0 +1,319 @@
|
|||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cglm/cglm.h>
|
||||
#include "rendlib.h"
|
||||
#include "camera.h"
|
||||
#include "object.h"
|
||||
#include "shaders.h"
|
||||
|
||||
// global variables
|
||||
int window_width = 960;
|
||||
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);
|
||||
void (*update_probe)(void);
|
||||
|
||||
float top_movement_speed = 0.4f;
|
||||
vec3 speed = { 0.0f, 0.0f, 0.0f };
|
||||
GLint screen_viewport[4];
|
||||
|
||||
struct object *objects;
|
||||
|
||||
unsigned int shader_program;
|
||||
unsigned int vertex_shader;
|
||||
unsigned int fragment_shader;
|
||||
|
||||
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();
|
||||
glfwSetCursorPos(w, (width/2), (height/2));
|
||||
glViewport(x, y, width, height);
|
||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||
}
|
||||
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
int len = ftell(fp);
|
||||
if (len == -1) {
|
||||
fprintf(stderr, "error: cannot fetch length of file '%s'\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_SET);
|
||||
char *fc = (char *) malloc(len);
|
||||
if (fc == NULL) {
|
||||
fprintf(stderr, "error: not enough dynamic memory\n");
|
||||
return -1;
|
||||
}
|
||||
memset(fc, 0, len);
|
||||
int rb = 0;
|
||||
do {
|
||||
rb += fread(fc+rb, sizeof(char), len-rb, fp);
|
||||
if (rb == 0) {
|
||||
break;
|
||||
}
|
||||
} while (rb < len);
|
||||
|
||||
fclose(fp);
|
||||
int ret = load_shader(&fc, &rb, shader);
|
||||
free(fc);
|
||||
return ret;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
int vshader_len = strlen(DEFAULT_VSHADER);
|
||||
char *vshader = strndup(DEFAULT_VSHADER, vshader_len);
|
||||
if (vshader == NULL) {
|
||||
return -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);
|
||||
|
||||
int success;
|
||||
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
|
||||
|
||||
if (success != GL_TRUE) {
|
||||
int log_length;
|
||||
glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &log_length);
|
||||
|
||||
char log[log_length];
|
||||
glGetProgramInfoLog(shader_program, log_length, NULL, log);
|
||||
|
||||
fprintf(stderr, "[object program] Shader Compilation Error: %s\n", log);
|
||||
return -1;
|
||||
}
|
||||
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_input(void) {
|
||||
}
|
||||
|
||||
void window_size(GLFWwindow *w, int width, int height) {
|
||||
update_screen_viewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void display(void) {
|
||||
handle_input();
|
||||
GLFWwindow *w = glfwGetCurrentContext();
|
||||
|
||||
GLint translation_uniform;
|
||||
GLint view_uniform;
|
||||
GLint projection_uniform;
|
||||
GLint color_uniform;
|
||||
GLint scale_uniform;
|
||||
|
||||
glClearColor(0.13f, 0.13f, 0.13f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glUseProgram(shader_program);
|
||||
|
||||
int ret = bake_camera(ac);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "--error: failed baking camera\n");
|
||||
return;
|
||||
}
|
||||
|
||||
view_uniform = glGetUniformLocation(shader_program, "view");
|
||||
projection_uniform = glGetUniformLocation(shader_program, "projection");
|
||||
translation_uniform = glGetUniformLocation(shader_program, "translation");
|
||||
color_uniform = glGetUniformLocation(shader_program, "color");
|
||||
scale_uniform = glGetUniformLocation(shader_program, "scale");
|
||||
|
||||
glUniformMatrix4fv(view_uniform, 1, GL_FALSE, (float *) ac->view);
|
||||
glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, (float *) ac->projection);
|
||||
|
||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||
mat4 translation_matrix;
|
||||
glm_mat4_identity(translation_matrix);
|
||||
|
||||
struct model *obj_model = obj->model;
|
||||
glm_translate(translation_matrix, obj->position);
|
||||
|
||||
glUniformMatrix4fv(translation_uniform, 1, GL_FALSE, (float *) translation_matrix);
|
||||
|
||||
glUniform3fv(color_uniform, 1, (float *) obj->color);
|
||||
glUniform1f(scale_uniform, obj->scale);
|
||||
|
||||
glBindVertexArray(obj->vao);
|
||||
glDrawElements(GL_TRIANGLES, obj_model->indices_num, GL_UNSIGNED_INT, (void *) 0);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(w);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void rendlib_bake_graphics(void) {
|
||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||
|
||||
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
||||
struct model *obj_model = obj->model;
|
||||
glGenVertexArrays(1, &obj->vao);
|
||||
glGenBuffers(1, &obj->vbo);
|
||||
glGenBuffers(1, &obj->ebo);
|
||||
glGenBuffers(1, &obj->nbo);
|
||||
|
||||
glBindVertexArray(obj->vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER,obj->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER,obj_model->vertices_num*3*sizeof(float),obj_model->vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, obj->nbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, obj_model->normals_num*3*sizeof(float), obj_model->normals, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *) 0);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,obj->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, obj_model->indices_num*sizeof(unsigned int), obj_model->indices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
void keyboard(GLFWwindow *w, int key, int scancode, int action, int mods) {
|
||||
if (keyboard_probe == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
(keyboard_probe)(key, scancode, action, mods);
|
||||
}
|
||||
|
||||
void mouse_motion(GLFWwindow *window, double x, double y) {
|
||||
if (mouse_probe == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
(mouse_probe)(x, y);
|
||||
}
|
||||
|
||||
int rendlib_start_window(int argc, char *argv[]) {
|
||||
int ret = glfwInit();
|
||||
if (!ret) {
|
||||
return err_glfw_init;
|
||||
}
|
||||
|
||||
GLFWwindow *w = glfwCreateWindow(window_width, window_height, window_title, NULL, NULL);
|
||||
if (w == NULL) {
|
||||
return err_glfw_win;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(w);
|
||||
glfwSetWindowSizeCallback(w, window_size);
|
||||
glfwSetCursorPosCallback(w, mouse_motion);
|
||||
glfwSetKeyCallback(w, keyboard);
|
||||
glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
ret = glewInit();
|
||||
if (ret != GLEW_OK) {
|
||||
return err_glew_init;
|
||||
}
|
||||
|
||||
ret = load_default_shaders();
|
||||
if (ret < 0) {
|
||||
return err_shaders_init;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int setup_default_camera(void) {
|
||||
ac = create_camera(CAMERA_PERSPECTIVE);
|
||||
if (ac == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rendlib_render(void) {
|
||||
if (ac == NULL) {
|
||||
if (setup_default_camera() < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
rendlib_bake_graphics();
|
||||
for (;;) {
|
||||
if (update_probe != NULL) {
|
||||
(update_probe)();
|
||||
}
|
||||
|
||||
display();
|
||||
}
|
||||
return 0;
|
||||
}
|
188
tests/test-01.c
Normal file
188
tests/test-01.c
Normal file
|
@ -0,0 +1,188 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "rendlib.h"
|
||||
#include "camera.h"
|
||||
#include "object.h"
|
||||
|
||||
#define INPUT_ESCAPE 0b00000001
|
||||
#define INPUT_RELOAD 0b00000010
|
||||
#define INPUT_LEFT 0b00000100
|
||||
#define INPUT_RIGHT 0b00001000
|
||||
#define INPUT_DOWN 0b00010000
|
||||
#define INPUT_UP 0b00100000
|
||||
|
||||
#define MOVEMENT_SPEED 0.2f
|
||||
|
||||
char input = 0;
|
||||
|
||||
void keyboardp(int key, int scancode, int action, int mods) {
|
||||
if (action == GLFW_PRESS) {
|
||||
switch (key) {
|
||||
case GLFW_KEY_Q:
|
||||
case GLFW_KEY_ESCAPE:
|
||||
input |= INPUT_ESCAPE;
|
||||
break;
|
||||
case GLFW_KEY_R:
|
||||
input |= INPUT_RELOAD;
|
||||
break;
|
||||
case GLFW_KEY_A:
|
||||
case GLFW_KEY_LEFT:
|
||||
input |= INPUT_LEFT;
|
||||
break;
|
||||
case GLFW_KEY_D:
|
||||
case GLFW_KEY_RIGHT:
|
||||
input |= INPUT_RIGHT;
|
||||
break;
|
||||
case GLFW_KEY_S:
|
||||
case GLFW_KEY_DOWN:
|
||||
input |= INPUT_DOWN;
|
||||
break;
|
||||
case GLFW_KEY_W:
|
||||
case GLFW_KEY_UP:
|
||||
input |= INPUT_UP;
|
||||
break;
|
||||
}
|
||||
} else if (action == GLFW_RELEASE) {
|
||||
switch (key) {
|
||||
case GLFW_KEY_A:
|
||||
case GLFW_KEY_LEFT:
|
||||
input &= ~INPUT_LEFT;
|
||||
break;
|
||||
case GLFW_KEY_D:
|
||||
case GLFW_KEY_RIGHT:
|
||||
input &= ~INPUT_RIGHT;
|
||||
break;
|
||||
case GLFW_KEY_S:
|
||||
case GLFW_KEY_DOWN:
|
||||
input &= ~INPUT_DOWN;
|
||||
break;
|
||||
case GLFW_KEY_W:
|
||||
case GLFW_KEY_UP:
|
||||
input &= ~INPUT_UP;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int warped_pointer = 0;
|
||||
int first_pointer = 1;
|
||||
void mousep(double x, double y) {
|
||||
GLint screen_viewport[4];
|
||||
if (warped_pointer == 1) {
|
||||
warped_pointer = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
warped_pointer = 1;
|
||||
glGetIntegerv(GL_VIEWPORT, screen_viewport);
|
||||
GLFWwindow *w = glfwGetCurrentContext();
|
||||
glfwSetCursorPos(w, (screen_viewport[2]/2), (screen_viewport[3]/2));
|
||||
|
||||
if (first_pointer == 1) {
|
||||
first_pointer = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
float offset_x = (float) (x - (screen_viewport[2]/2)) * ac->sensitivity;
|
||||
float offset_y = (float) (y - (screen_viewport[3]/2)) * ac->sensitivity;
|
||||
|
||||
ac->yaw += offset_x;
|
||||
ac->pitch -= offset_y;
|
||||
|
||||
if (ac->pitch < -89.9f) {
|
||||
ac->pitch = -89.9f;
|
||||
}
|
||||
if (ac->pitch > 89.9f) {
|
||||
ac->pitch = 89.9f;
|
||||
}
|
||||
|
||||
vec3 view_direction = { cos(glm_rad(ac->yaw)) * cos(glm_rad(ac->pitch)),
|
||||
sin(glm_rad(ac->pitch)),
|
||||
sin(glm_rad(ac->yaw)) * cos(glm_rad(ac->pitch))
|
||||
};
|
||||
glm_normalize_to(view_direction, ac->front);
|
||||
}
|
||||
|
||||
void periodic_input(void) {
|
||||
int ret = 0;
|
||||
if (input & INPUT_ESCAPE) {
|
||||
exit(EXIT_SUCCESS);
|
||||
input &= ~INPUT_ESCAPE;
|
||||
}
|
||||
|
||||
if (input & INPUT_LEFT) {
|
||||
vec3 side_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED};
|
||||
vec3 camera_side;
|
||||
glm_cross(ac->front, ac->up, camera_side);
|
||||
glm_normalize(camera_side);
|
||||
glm_vec3_mul(camera_side, side_scalar, camera_side);
|
||||
glm_vec3_sub(ac->pos, camera_side, ac->pos);
|
||||
}
|
||||
|
||||
if (input & INPUT_RIGHT) {
|
||||
vec3 side_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED};
|
||||
vec3 camera_side;
|
||||
glm_cross(ac->front, ac->up, camera_side);
|
||||
glm_normalize(camera_side);
|
||||
glm_vec3_mul(camera_side, side_scalar, camera_side);
|
||||
glm_vec3_add(ac->pos, camera_side, ac->pos);
|
||||
}
|
||||
|
||||
if (input & INPUT_DOWN) {
|
||||
vec3 front_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED};
|
||||
glm_vec3_mul(front_scalar, ac->front, front_scalar);
|
||||
glm_vec3_sub(ac->pos, front_scalar, ac->pos);
|
||||
}
|
||||
|
||||
if (input & INPUT_UP) {
|
||||
vec3 front_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED};
|
||||
glm_vec3_mul(front_scalar, ac->front, front_scalar);
|
||||
glm_vec3_add(ac->pos, front_scalar, ac->pos);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void updatep(void) {
|
||||
periodic_input();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int ret = rendlib_start_window(argc, argv);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "--error: %d\n", ret);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
keyboard_probe = &keyboardp;
|
||||
mouse_probe = &mousep;
|
||||
update_probe = &updatep;
|
||||
|
||||
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, m);
|
||||
if (o == NULL) {
|
||||
fprintf(stderr, "--error: creating object\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ac = create_camera(CAMERA_PERSPECTIVE);
|
||||
if (ac == NULL) {
|
||||
fprintf(stderr, "--error: creating camera\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ret = rendlib_render();
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "--error: %d\n", ret);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fprintf(stdout, "--rendering\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user