Refactor out all the rendering structs into their own files
This commit is contained in:
parent
04e82c754c
commit
0123bb5527
@ -1,8 +1,8 @@
|
|||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
|
||||||
Matrix4 get_camera_view_matrix(Camera *camera, Render_Data *render_data) {
|
Matrix4 get_camera_view_matrix(Camera *camera, Render_Data *render_data) {
|
||||||
f32 azimimuth_radians = glm::radians(render_data.view_azimuth);
|
f32 azimuth_radians = glm::radians(render_data->view_azimuth);
|
||||||
f32 elevation_radians = glm::radians(render_data.view_elevation);
|
f32 elevation_radians = glm::radians(render_data->view_elevation);
|
||||||
|
|
||||||
f32 sin_azimuth = glm::sin(azimuth_radians);
|
f32 sin_azimuth = glm::sin(azimuth_radians);
|
||||||
f32 cos_azimuth = glm::cos(azimuth_radians);
|
f32 cos_azimuth = glm::cos(azimuth_radians);
|
||||||
@ -11,7 +11,7 @@ Matrix4 get_camera_view_matrix(Camera *camera, Render_Data *render_data) {
|
|||||||
|
|
||||||
camera->view_direction = glm::normalize(Vector3(sin_azimuth * cos_elevation, sin_elevation, -cos_azimuth * cos_elevation));
|
camera->view_direction = glm::normalize(Vector3(sin_azimuth * cos_elevation, sin_elevation, -cos_azimuth * cos_elevation));
|
||||||
|
|
||||||
Matrix4 look_at = glm::lookAt(camera->world_position, camera->world_position + camera->view_direction, camera->word_up_vector);
|
Matrix4 look_at = glm::lookAt(camera->world_position, camera->world_position + camera->view_direction, camera->world_up_vector);
|
||||||
|
|
||||||
return look_at;
|
return look_at;
|
||||||
}
|
}
|
||||||
|
|||||||
84
src/frame_buffer.cpp
Normal file
84
src/frame_buffer.cpp
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
#include "frame_buffer.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
bool init_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height) {
|
||||||
|
frame_buffer->width = width;
|
||||||
|
frame_buffer->height = height;
|
||||||
|
|
||||||
|
glGenFramebuffers(1, &frame_buffer->buffer);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer->buffer);
|
||||||
|
glEnable(GL_FRAMEBUFFER_SRGB);
|
||||||
|
|
||||||
|
glGenTextures(1, &frame_buffer->color_tex);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, frame_buffer->color_tex);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, frame_buffer->color_tex, 0);
|
||||||
|
log(1, "%s: added color buffer\n", __FUNCTION__);
|
||||||
|
|
||||||
|
glGenRenderbuffers(1, &frame_buffer->depth_buffer);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, frame_buffer->depth_buffer);
|
||||||
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, frame_buffer->depth_buffer);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||||
|
log(1, "%s: added depth render buffer\n", __FUNCTION__);
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
|
return check_frame_buffer_complete(frame_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check_frame_buffer_complete(Frame_Buffer *frame_buffer) {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer->buffer);
|
||||||
|
|
||||||
|
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||||
|
if (result != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
log(1, "%s error: framebuffer is NOT complete\n", __FUNCTION__);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
log(1, "%s: framebuffer is complete\n", __FUNCTION__);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool resize_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height) {
|
||||||
|
frame_buffer->width = width;
|
||||||
|
frame_buffer->height = height;
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||||
|
glDeleteTextures(1, &frame_buffer->color_tex);
|
||||||
|
glDeleteRenderbuffers(1, &frame_buffer->depth_buffer);
|
||||||
|
glDeleteFramebuffers(1, &frame_buffer->buffer);
|
||||||
|
|
||||||
|
return init_frame_buffer(frame_buffer, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_frame_buffer(Frame_Buffer *frame_buffer) {
|
||||||
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, frame_buffer->buffer);
|
||||||
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||||
|
glBlitFramebuffer(0, 0, frame_buffer->width, frame_buffer->height, 0, 0, frame_buffer->width, frame_buffer->height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||||
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind_frame_buffer(Frame_Buffer *frame_buffer) {
|
||||||
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frame_buffer->buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unbind_frame_buffer(Frame_Buffer *frame_buffer) {
|
||||||
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_frame_buffer(Frame_Buffer *frame_buffer) {
|
||||||
|
unbind_frame_buffer(frame_buffer);
|
||||||
|
|
||||||
|
glDeleteTextures(1, &frame_buffer->color_tex);
|
||||||
|
glDeleteRenderbuffers(1, &frame_buffer->depth_buffer);
|
||||||
|
glDeleteFramebuffers(1, &frame_buffer->buffer);
|
||||||
|
}
|
||||||
21
src/frame_buffer.h
Normal file
21
src/frame_buffer.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "basic.h"
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
struct Frame_Buffer {
|
||||||
|
u32 width = 640;
|
||||||
|
u32 height = 480;
|
||||||
|
u32 buffer = 0;
|
||||||
|
u32 color_tex = 0;
|
||||||
|
u32 depth_buffer = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool init_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height);
|
||||||
|
bool resize_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height);
|
||||||
|
bool check_frame_buffer_complete(Frame_Buffer *frame_buffer);
|
||||||
|
void bind_frame_buffer(Frame_Buffer *frame_buffer);
|
||||||
|
void unbind_frame_buffer(Frame_Buffer *frame_buffer);
|
||||||
|
void draw_frame_buffer(Frame_Buffer *frame_buffer);
|
||||||
|
void cleanup_frame_buffer(Frame_Buffer *frame_buffer);
|
||||||
270
src/renderer.cpp
270
src/renderer.cpp
@ -1,5 +1,3 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
|
||||||
#include "file.h"
|
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
@ -165,271 +163,3 @@ void cleanup_renderer(Renderer *renderer) {
|
|||||||
cleanup_frame_buffer(&renderer->frame_buffer);
|
cleanup_frame_buffer(&renderer->frame_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Frame buffer
|
|
||||||
*/
|
|
||||||
bool init_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height) {
|
|
||||||
frame_buffer->width = width;
|
|
||||||
frame_buffer->height = height;
|
|
||||||
|
|
||||||
glGenFramebuffers(1, &frame_buffer->buffer);
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer->buffer);
|
|
||||||
glEnable(GL_FRAMEBUFFER_SRGB);
|
|
||||||
|
|
||||||
glGenTextures(1, &frame_buffer->color_tex);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, frame_buffer->color_tex);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
|
|
||||||
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, frame_buffer->color_tex, 0);
|
|
||||||
log(1, "%s: added color buffer\n", __FUNCTION__);
|
|
||||||
|
|
||||||
glGenRenderbuffers(1, &frame_buffer->depth_buffer);
|
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, frame_buffer->depth_buffer);
|
|
||||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
|
|
||||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, frame_buffer->depth_buffer);
|
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
||||||
log(1, "%s: added depth render buffer\n", __FUNCTION__);
|
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
||||||
|
|
||||||
return check_frame_buffer_complete(frame_buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool check_frame_buffer_complete(Frame_Buffer *frame_buffer) {
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer->buffer);
|
|
||||||
|
|
||||||
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
||||||
if (result != GL_FRAMEBUFFER_COMPLETE) {
|
|
||||||
log(1, "%s error: framebuffer is NOT complete\n", __FUNCTION__);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
||||||
log(1, "%s: framebuffer is complete\n", __FUNCTION__);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool resize_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height) {
|
|
||||||
frame_buffer->width = width;
|
|
||||||
frame_buffer->height = height;
|
|
||||||
|
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
||||||
glDeleteTextures(1, &frame_buffer->color_tex);
|
|
||||||
glDeleteRenderbuffers(1, &frame_buffer->depth_buffer);
|
|
||||||
glDeleteFramebuffers(1, &frame_buffer->buffer);
|
|
||||||
|
|
||||||
return init_frame_buffer(frame_buffer, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw_frame_buffer(Frame_Buffer *frame_buffer) {
|
|
||||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, frame_buffer->buffer);
|
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
||||||
glBlitFramebuffer(0, 0, frame_buffer->width, frame_buffer->height, 0, 0, frame_buffer->width, frame_buffer->height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
|
||||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bind_frame_buffer(Frame_Buffer *frame_buffer) {
|
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frame_buffer->buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unbind_frame_buffer(Frame_Buffer *frame_buffer) {
|
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cleanup_frame_buffer(Frame_Buffer *frame_buffer) {
|
|
||||||
unbind_frame_buffer(frame_buffer);
|
|
||||||
|
|
||||||
glDeleteTextures(1, &frame_buffer->color_tex);
|
|
||||||
glDeleteRenderbuffers(1, &frame_buffer->depth_buffer);
|
|
||||||
glDeleteFramebuffers(1, &frame_buffer->buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Vertex buffer
|
|
||||||
*/
|
|
||||||
bool init_vertex_buffer(Vertex_Buffer *vertex_buffer) {
|
|
||||||
glGenVertexArrays(1, &vertex_buffer->vao);
|
|
||||||
glGenBuffers(1, &vertex_buffer->vbo);
|
|
||||||
|
|
||||||
glBindVertexArray(vertex_buffer->vao);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer->vbo);
|
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, position));
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, color));
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, uv));
|
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void cleanup_vertex_buffer(Vertex_Buffer *vertex_buffer) {
|
|
||||||
glDeleteBuffers(1, &vertex_buffer->vbo);
|
|
||||||
glDeleteVertexArrays(1, &vertex_buffer->vao);
|
|
||||||
}
|
|
||||||
|
|
||||||
void upload_vertex_buffer_data(Vertex_Buffer *vertex_buffer, Mesh *vertex_data) {
|
|
||||||
glBindVertexArray(vertex_buffer->vao);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer->vbo);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, vertex_data->vertices.size() * sizeof(Vertex), &vertex_data->vertices.at(0), GL_DYNAMIC_DRAW);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bind_vertex_buffer(Vertex_Buffer *vertex_buffer) {
|
|
||||||
glBindVertexArray(vertex_buffer->vao);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unbind_vertex_buffer() {
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw_vertex_buffer(u32 mode, u32 start, u32 num) {
|
|
||||||
glDrawArrays(mode, start, num);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Uniform buffer
|
|
||||||
*/
|
|
||||||
bool init_uniform_buffer(Uniform_Buffer *uniform_buffer) {
|
|
||||||
glGenBuffers(1, &uniform_buffer->ubo_buffer);
|
|
||||||
glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer->ubo_buffer);
|
|
||||||
glBufferData(GL_UNIFORM_BUFFER, 2 * sizeof(Matrix4), nullptr, GL_STATIC_DRAW);
|
|
||||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void upload_uniform_buffer_data(Uniform_Buffer *uniform_buffer, Matrix4 view_matrix, Matrix4 projection_matrix) {
|
|
||||||
glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer->ubo_buffer);
|
|
||||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(Matrix4), glm::value_ptr(view_matrix));
|
|
||||||
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(Matrix4), sizeof(Matrix4), glm::value_ptr(projection_matrix));
|
|
||||||
glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniform_buffer->ubo_buffer, 0, 2 * sizeof(Matrix4));
|
|
||||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cleanup(Uniform_Buffer *uniform_buffer) {
|
|
||||||
glDeleteBuffers(1, &uniform_buffer->ubo_buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Texture
|
|
||||||
*/
|
|
||||||
bool load_texture(Texture *texture, string filepath) {
|
|
||||||
texture->name = filepath;
|
|
||||||
|
|
||||||
stbi_set_flip_vertically_on_load(true);
|
|
||||||
u8* texture_data = stbi_load(filepath.c_str(), &texture->width, &texture->height, &texture->number_of_channels, 0);
|
|
||||||
|
|
||||||
if (!texture_data) {
|
|
||||||
stbi_image_free(texture_data);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
glGenTextures(1, &texture->texture);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture->texture);
|
|
||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
|
|
||||||
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
|
|
||||||
stbi_image_free(texture_data);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bind_texture(Texture *texture) {
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture->texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unbind_texture() {
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cleanup_texture(Texture *texture) {
|
|
||||||
glDeleteTextures(1, &texture->texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Shaders
|
|
||||||
*/
|
|
||||||
bool load_shaders(Shader *shader, string vertex_shader_filepath, string fragment_shader_filepath) {
|
|
||||||
u32 vertex_shader = load_shader(vertex_shader_filepath, GL_VERTEX_SHADER);
|
|
||||||
if (!vertex_shader) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 fragment_shader = load_shader(fragment_shader_filepath, GL_FRAGMENT_SHADER);
|
|
||||||
if (!fragment_shader) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
shader->program = glCreateProgram();
|
|
||||||
glAttachShader(shader->program, vertex_shader);
|
|
||||||
glAttachShader(shader->program, fragment_shader);
|
|
||||||
glLinkProgram(shader->program);
|
|
||||||
|
|
||||||
s32 is_program_linked;
|
|
||||||
glGetProgramiv(shader->program, GL_LINK_STATUS, &is_program_linked);
|
|
||||||
if (!is_program_linked) {
|
|
||||||
glDeleteShader(vertex_shader);
|
|
||||||
glDeleteShader(fragment_shader);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
s32 ubo_index = glGetUniformBlockIndex(shader->program, "Matrices");
|
|
||||||
glUniformBlockBinding(shader->program, ubo_index, 0);
|
|
||||||
|
|
||||||
glDeleteShader(vertex_shader);
|
|
||||||
glDeleteShader(fragment_shader);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void cleanup_shader(Shader *shader) {
|
|
||||||
glDeleteProgram(shader->program);
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 load_shader(string filepath, u32 shader_type) {
|
|
||||||
string shader_as_text;
|
|
||||||
shader_as_text = load_file_to_string(filepath);
|
|
||||||
|
|
||||||
const char*shader_source = shader_as_text.c_str();
|
|
||||||
u32 shader = glCreateShader(shader_type);
|
|
||||||
glShaderSource(shader, 1, (const GLchar**) &shader_source, 0);
|
|
||||||
glCompileShader(shader);
|
|
||||||
|
|
||||||
s32 is_shader_compiled;
|
|
||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &is_shader_compiled);
|
|
||||||
if (!is_shader_compiled) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
void use_shader(Shader *shader) {
|
|
||||||
glUseProgram(shader->program);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -3,43 +3,17 @@
|
|||||||
#include "basic.h"
|
#include "basic.h"
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <stb_image.h>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
#include "render_data.h"
|
#include "render_data.h"
|
||||||
#include "user_interface.h"
|
#include "user_interface.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
#include "frame_buffer.h"
|
||||||
|
#include "vertex_buffer.h"
|
||||||
|
#include "uniform_buffer.h"
|
||||||
|
#include "texture.h"
|
||||||
|
#include "shaders.h"
|
||||||
|
|
||||||
|
|
||||||
struct Frame_Buffer {
|
|
||||||
u32 width = 640;
|
|
||||||
u32 height = 480;
|
|
||||||
u32 buffer = 0;
|
|
||||||
u32 color_tex = 0;
|
|
||||||
u32 depth_buffer = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Vertex_Buffer {
|
|
||||||
u32 vao = 0;
|
|
||||||
u32 vbo = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Uniform_Buffer {
|
|
||||||
u32 ubo_buffer = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Texture {
|
|
||||||
u32 texture = 0;
|
|
||||||
s32 width = 0;
|
|
||||||
s32 height = 0;
|
|
||||||
s32 number_of_channels = 0;
|
|
||||||
string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Shader {
|
|
||||||
u32 program = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Renderer {
|
struct Renderer {
|
||||||
Shader shader;
|
Shader shader;
|
||||||
Shader alt_shader;
|
Shader alt_shader;
|
||||||
@ -62,31 +36,3 @@ void draw_renderer(Renderer *renderer);
|
|||||||
void upload_renderer_data(Renderer *renderer, Mesh *vertex_data);
|
void upload_renderer_data(Renderer *renderer, Mesh *vertex_data);
|
||||||
void cleanup_renderer(Renderer *renderer);
|
void cleanup_renderer(Renderer *renderer);
|
||||||
|
|
||||||
bool init_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height);
|
|
||||||
bool resize_frame_buffer(Frame_Buffer *frame_buffer, u32 width, u32 height);
|
|
||||||
bool check_frame_buffer_complete(Frame_Buffer *frame_buffer);
|
|
||||||
void bind_frame_buffer(Frame_Buffer *frame_buffer);
|
|
||||||
void unbind_frame_buffer(Frame_Buffer *frame_buffer);
|
|
||||||
void draw_frame_buffer(Frame_Buffer *frame_buffer);
|
|
||||||
void cleanup_frame_buffer(Frame_Buffer *frame_buffer);
|
|
||||||
|
|
||||||
bool init_vertex_buffer(Vertex_Buffer *vertex_buffer);
|
|
||||||
void cleanup_vertex_buffer(Vertex_Buffer *vertex_buffer);
|
|
||||||
void upload_vertex_buffer_data(Vertex_Buffer *vertex_buffer, Mesh *vertex_data);
|
|
||||||
void bind_vertex_buffer(Vertex_Buffer *vertex_buffer);
|
|
||||||
void unbind_vertex_buffer();
|
|
||||||
void draw_vertex_buffer(u32 mode, u32 start, u32 num);
|
|
||||||
|
|
||||||
bool init_uniform_buffer(Uniform_Buffer *uniform_buffer);
|
|
||||||
void upload_uniform_buffer_data(Uniform_Buffer *uniform_buffer, Matrix4 view_matrix, Matrix4 projection_matrix);
|
|
||||||
void cleanup(Uniform_Buffer *uniform_buffer);
|
|
||||||
|
|
||||||
bool load_texture(Texture *texture, string filepath);
|
|
||||||
void bind_texture(Texture *texture);
|
|
||||||
void unbind_texture();
|
|
||||||
void cleanup_texture(Texture *texture);
|
|
||||||
|
|
||||||
bool load_shaders(Shader *shader, string vertex_shader_filepath, string fragment_shader_filepath);
|
|
||||||
u32 load_shader(string filepath, u32 shader_type);
|
|
||||||
void use_shader(Shader *shader);
|
|
||||||
void cleanup_shader(Shader *shader);
|
|
||||||
|
|||||||
62
src/shaders.cpp
Normal file
62
src/shaders.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#include "shaders.h"
|
||||||
|
#include "file.h"
|
||||||
|
|
||||||
|
|
||||||
|
bool load_shaders(Shader *shader, string vertex_shader_filepath, string fragment_shader_filepath) {
|
||||||
|
u32 vertex_shader = load_shader(vertex_shader_filepath, GL_VERTEX_SHADER);
|
||||||
|
if (!vertex_shader) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 fragment_shader = load_shader(fragment_shader_filepath, GL_FRAGMENT_SHADER);
|
||||||
|
if (!fragment_shader) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
shader->program = glCreateProgram();
|
||||||
|
glAttachShader(shader->program, vertex_shader);
|
||||||
|
glAttachShader(shader->program, fragment_shader);
|
||||||
|
glLinkProgram(shader->program);
|
||||||
|
|
||||||
|
s32 is_program_linked;
|
||||||
|
glGetProgramiv(shader->program, GL_LINK_STATUS, &is_program_linked);
|
||||||
|
if (!is_program_linked) {
|
||||||
|
glDeleteShader(vertex_shader);
|
||||||
|
glDeleteShader(fragment_shader);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
s32 ubo_index = glGetUniformBlockIndex(shader->program, "Matrices");
|
||||||
|
glUniformBlockBinding(shader->program, ubo_index, 0);
|
||||||
|
|
||||||
|
glDeleteShader(vertex_shader);
|
||||||
|
glDeleteShader(fragment_shader);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_shader(Shader *shader) {
|
||||||
|
glDeleteProgram(shader->program);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 load_shader(string filepath, u32 shader_type) {
|
||||||
|
string shader_as_text;
|
||||||
|
shader_as_text = load_file_to_string(filepath);
|
||||||
|
|
||||||
|
const char*shader_source = shader_as_text.c_str();
|
||||||
|
u32 shader = glCreateShader(shader_type);
|
||||||
|
glShaderSource(shader, 1, (const GLchar**) &shader_source, 0);
|
||||||
|
glCompileShader(shader);
|
||||||
|
|
||||||
|
s32 is_shader_compiled;
|
||||||
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &is_shader_compiled);
|
||||||
|
if (!is_shader_compiled) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
|
||||||
|
void use_shader(Shader *shader) {
|
||||||
|
glUseProgram(shader->program);
|
||||||
|
}
|
||||||
16
src/shaders.h
Normal file
16
src/shaders.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "basic.h"
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct Shader {
|
||||||
|
u32 program = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool load_shaders(Shader *shader, string vertex_shader_filepath, string fragment_shader_filepath);
|
||||||
|
u32 load_shader(string filepath, u32 shader_type);
|
||||||
|
void use_shader(Shader *shader);
|
||||||
|
void cleanup_shader(Shader *shader);
|
||||||
|
|
||||||
44
src/texture.cpp
Normal file
44
src/texture.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "texture.h"
|
||||||
|
|
||||||
|
|
||||||
|
bool load_texture(Texture *texture, string filepath) {
|
||||||
|
texture->name = filepath;
|
||||||
|
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
u8* texture_data = stbi_load(filepath.c_str(), &texture->width, &texture->height, &texture->number_of_channels, 0);
|
||||||
|
|
||||||
|
if (!texture_data) {
|
||||||
|
stbi_image_free(texture_data);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
glGenTextures(1, &texture->texture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture->texture);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
|
||||||
|
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
stbi_image_free(texture_data);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind_texture(Texture *texture) {
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture->texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unbind_texture() {
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_texture(Texture *texture) {
|
||||||
|
glDeleteTextures(1, &texture->texture);
|
||||||
|
}
|
||||||
19
src/texture.h
Normal file
19
src/texture.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "basic.h"
|
||||||
|
#include <stb_image.h>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
struct Texture {
|
||||||
|
u32 texture = 0;
|
||||||
|
s32 width = 0;
|
||||||
|
s32 height = 0;
|
||||||
|
s32 number_of_channels = 0;
|
||||||
|
string name;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool load_texture(Texture *texture, string filepath);
|
||||||
|
void bind_texture(Texture *texture);
|
||||||
|
void unbind_texture();
|
||||||
|
void cleanup_texture(Texture *texture);
|
||||||
|
|
||||||
23
src/uniform_buffer.cpp
Normal file
23
src/uniform_buffer.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "uniform_buffer.h"
|
||||||
|
|
||||||
|
|
||||||
|
bool init_uniform_buffer(Uniform_Buffer *uniform_buffer) {
|
||||||
|
glGenBuffers(1, &uniform_buffer->ubo_buffer);
|
||||||
|
glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer->ubo_buffer);
|
||||||
|
glBufferData(GL_UNIFORM_BUFFER, 2 * sizeof(Matrix4), nullptr, GL_STATIC_DRAW);
|
||||||
|
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void upload_uniform_buffer_data(Uniform_Buffer *uniform_buffer, Matrix4 view_matrix, Matrix4 projection_matrix) {
|
||||||
|
glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer->ubo_buffer);
|
||||||
|
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(Matrix4), glm::value_ptr(view_matrix));
|
||||||
|
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(Matrix4), sizeof(Matrix4), glm::value_ptr(projection_matrix));
|
||||||
|
glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniform_buffer->ubo_buffer, 0, 2 * sizeof(Matrix4));
|
||||||
|
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup(Uniform_Buffer *uniform_buffer) {
|
||||||
|
glDeleteBuffers(1, &uniform_buffer->ubo_buffer);
|
||||||
|
}
|
||||||
15
src/uniform_buffer.h
Normal file
15
src/uniform_buffer.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "basic.h"
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
struct Uniform_Buffer {
|
||||||
|
u32 ubo_buffer = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool init_uniform_buffer(Uniform_Buffer *uniform_buffer);
|
||||||
|
void upload_uniform_buffer_data(Uniform_Buffer *uniform_buffer, Matrix4 view_matrix, Matrix4 projection_matrix);
|
||||||
|
void cleanup(Uniform_Buffer *uniform_buffer);
|
||||||
@ -27,28 +27,28 @@ void create_user_interface_frame(User_Interface *user_interface, Render_Data *re
|
|||||||
|
|
||||||
ImGui::Text("FPS:");
|
ImGui::Text("FPS:");
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text(std::to_string(user_interface->frames_per_second).c_str());
|
ImGui::Text("%s", std::to_string(user_interface->frames_per_second).c_str());
|
||||||
ImGui::Separator();
|
ImGui::Separator();
|
||||||
|
|
||||||
ImGui::Text("UI generation time:");
|
ImGui::Text("UI generation time:");
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text(std::to_string(render_data->user_interface_generate_time).c_str());
|
ImGui::Text("%s", std::to_string(render_data->user_interface_generate_time).c_str());
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text("ms");
|
ImGui::Text("ms");
|
||||||
ImGui::Separator();
|
ImGui::Separator();
|
||||||
|
|
||||||
ImGui::Text("Triangles:");
|
ImGui::Text("Triangles:");
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text(std::to_string(render_data->triangle_count).c_str());
|
ImGui::Text("%s", std::to_string(render_data->triangle_count).c_str());
|
||||||
string window_dims = std::to_string(render_data->width) + "x" +
|
string window_dims = std::to_string(render_data->width) + "x" +
|
||||||
std::to_string(render_data->height);
|
std::to_string(render_data->height);
|
||||||
ImGui::Text("Window dimensions:");
|
ImGui::Text("Window dimensions:");
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text(window_dims.c_str());
|
ImGui::Text("%s", window_dims.c_str());
|
||||||
string img_window_position = std::to_string(static_cast<s32>(ImGui::GetWindowPos().x))+ "/" + std::to_string(static_cast<s32>(ImGui::GetWindowPos().y));
|
string img_window_position = std::to_string(static_cast<s32>(ImGui::GetWindowPos().x))+ "/" + std::to_string(static_cast<s32>(ImGui::GetWindowPos().y));
|
||||||
ImGui::Text("ImGui Window Position:");
|
ImGui::Text("ImGui Window Position:");
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text(img_window_position.c_str());
|
ImGui::Text("%s", img_window_position.c_str());
|
||||||
|
|
||||||
// Checkbox
|
// Checkbox
|
||||||
static bool checkbox_is_checked = false;
|
static bool checkbox_is_checked = false;
|
||||||
|
|||||||
48
src/vertex_buffer.cpp
Normal file
48
src/vertex_buffer.cpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include "vertex_buffer.h"
|
||||||
|
|
||||||
|
bool init_vertex_buffer(Vertex_Buffer *vertex_buffer) {
|
||||||
|
glGenVertexArrays(1, &vertex_buffer->vao);
|
||||||
|
glGenBuffers(1, &vertex_buffer->vbo);
|
||||||
|
|
||||||
|
glBindVertexArray(vertex_buffer->vao);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer->vbo);
|
||||||
|
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, position));
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, color));
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, uv));
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_vertex_buffer(Vertex_Buffer *vertex_buffer) {
|
||||||
|
glDeleteBuffers(1, &vertex_buffer->vbo);
|
||||||
|
glDeleteVertexArrays(1, &vertex_buffer->vao);
|
||||||
|
}
|
||||||
|
|
||||||
|
void upload_vertex_buffer_data(Vertex_Buffer *vertex_buffer, Mesh *vertex_data) {
|
||||||
|
glBindVertexArray(vertex_buffer->vao);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer->vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, vertex_data->vertices.size() * sizeof(Vertex), &vertex_data->vertices.at(0), GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind_vertex_buffer(Vertex_Buffer *vertex_buffer) {
|
||||||
|
glBindVertexArray(vertex_buffer->vao);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unbind_vertex_buffer() {
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_vertex_buffer(u32 mode, u32 start, u32 num) {
|
||||||
|
glDrawArrays(mode, start, num);
|
||||||
|
}
|
||||||
20
src/vertex_buffer.h
Normal file
20
src/vertex_buffer.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "basic.h"
|
||||||
|
#include "render_data.h"
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct Vertex_Buffer {
|
||||||
|
u32 vao = 0;
|
||||||
|
u32 vbo = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool init_vertex_buffer(Vertex_Buffer *vertex_buffer);
|
||||||
|
void cleanup_vertex_buffer(Vertex_Buffer *vertex_buffer);
|
||||||
|
void upload_vertex_buffer_data(Vertex_Buffer *vertex_buffer, Mesh *vertex_data);
|
||||||
|
void bind_vertex_buffer(Vertex_Buffer *vertex_buffer);
|
||||||
|
void unbind_vertex_buffer();
|
||||||
|
void draw_vertex_buffer(u32 mode, u32 start, u32 num);
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user