2024-10-07 22:15:42 +00:00
|
|
|
#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"
|
2024-10-08 17:43:14 +00:00
|
|
|
#include "include/camera.h"
|
2024-10-07 22:15:42 +00:00
|
|
|
#include "include/object.h"
|
|
|
|
|
|
|
|
// global variables
|
|
|
|
int window_width = 960;
|
|
|
|
int window_height = 540;
|
|
|
|
char *window_title = "rendlib window";
|
|
|
|
|
2024-10-08 17:43:14 +00:00
|
|
|
struct camera *ac;
|
|
|
|
|
2024-10-08 21:36:14 +00:00
|
|
|
void (*keyboard_probe)(int key, int scancode, int action, int mods);
|
|
|
|
void (*mouse_probe)(double x, double y);
|
|
|
|
void (*update_probe)(void);
|
|
|
|
|
2024-10-08 17:43:14 +00:00
|
|
|
float top_movement_speed = 0.4f;
|
2024-10-07 22:15:42 +00:00
|
|
|
vec3 speed = { 0.0f, 0.0f, 0.0f };
|
2024-10-08 15:27:09 +00:00
|
|
|
GLint screen_viewport[4];
|
2024-10-07 22:15:42 +00:00
|
|
|
|
|
|
|
struct object *objects;
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
2024-10-08 15:27:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-10-07 22:15:42 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-10-08 15:27:09 +00:00
|
|
|
update_screen_viewport(0, 0, width, height);
|
2024-10-07 22:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-10-08 17:43:14 +00:00
|
|
|
int ret = bake_camera(ac);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "--error: failed baking camera\n");
|
|
|
|
return;
|
|
|
|
}
|
2024-10-07 22:15:42 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2024-10-08 17:43:14 +00:00
|
|
|
glUniformMatrix4fv(view_uniform, 1, GL_FALSE, (float *) ac->view);
|
|
|
|
glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, (float *) ac->projection);
|
2024-10-07 22:15:42 +00:00
|
|
|
|
|
|
|
for (struct object *obj = objects; obj != NULL; obj = obj->next) {
|
|
|
|
mat4 translation_matrix;
|
|
|
|
glm_mat4_identity(translation_matrix);
|
|
|
|
|
2024-10-08 17:43:14 +00:00
|
|
|
struct model *obj_model = obj->model;
|
2024-10-07 22:15:42 +00:00
|
|
|
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) {
|
2024-10-08 21:36:14 +00:00
|
|
|
if (keyboard_probe == NULL) {
|
|
|
|
return;
|
2024-10-07 22:15:42 +00:00
|
|
|
}
|
2024-10-08 21:36:14 +00:00
|
|
|
|
|
|
|
(keyboard_probe)(key, scancode, action, mods);
|
2024-10-07 22:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mouse_motion(GLFWwindow *window, double x, double y) {
|
2024-10-08 21:36:14 +00:00
|
|
|
if (mouse_probe == NULL) {
|
2024-10-07 22:15:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-08 21:36:14 +00:00
|
|
|
(mouse_probe)(x, y);
|
2024-10-07 22:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2024-10-08 15:27:09 +00:00
|
|
|
glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
2024-10-07 22:15:42 +00:00
|
|
|
|
|
|
|
ret = glewInit();
|
|
|
|
if (ret != GLEW_OK) {
|
|
|
|
return err_glew_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = load_shaders();
|
|
|
|
if (ret < 0) {
|
|
|
|
return err_shaders_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-08 17:43:14 +00:00
|
|
|
int setup_default_camera(void) {
|
|
|
|
ac = create_camera(CAMERA_PERSPECTIVE);
|
|
|
|
if (ac == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-07 22:15:42 +00:00
|
|
|
int rendlib_render(void) {
|
2024-10-08 17:43:14 +00:00
|
|
|
if (ac == NULL) {
|
|
|
|
if (setup_default_camera() < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 22:15:42 +00:00
|
|
|
rendlib_bake_graphics();
|
|
|
|
for (;;) {
|
2024-10-08 21:36:14 +00:00
|
|
|
if (update_probe != NULL) {
|
|
|
|
(update_probe)();
|
|
|
|
}
|
|
|
|
|
2024-10-07 22:15:42 +00:00
|
|
|
display();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|