feat: reorganize source code + cleanup
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
#include "file_manager.h"
|
||||
#include "IO/file_manager.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
@ -1,6 +0,0 @@
|
||||
#include "block.h"
|
||||
|
||||
Block::Block(glm::vec3 position, glm::vec4 color) {
|
||||
this->m_position = position;
|
||||
this->m_color = m_color;
|
||||
}
|
14
src/main.cpp
14
src/main.cpp
@ -5,6 +5,10 @@
|
||||
#include <cmath>
|
||||
// #endif
|
||||
|
||||
#ifndef WIN32
|
||||
#define GLEW_STATIC
|
||||
#endif
|
||||
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/quaternion_geometric.hpp>
|
||||
@ -13,12 +17,10 @@
|
||||
#include <GL/glew.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "shader.h"
|
||||
#include "file_manager.h"
|
||||
#include "prelude.h"
|
||||
#include "block.h"
|
||||
#include "vertex.h"
|
||||
#include "model.h"
|
||||
#include "renderer/shader.h"
|
||||
#include "IO/file_manager.h"
|
||||
#include "renderer/debug.h"
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
#define WIDTH 1024
|
||||
#define HEIGHT 768
|
||||
|
192
src/prelude.cpp
192
src/prelude.cpp
@ -1,192 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "prelude.h"
|
||||
|
||||
#ifndef WIN32
|
||||
#define GLEW_STATIC
|
||||
#endif
|
||||
#include <GL/glew.h>
|
||||
|
||||
#define SCREEN_WIDTH 1024
|
||||
#define SCREEN_HEIGHT 768
|
||||
|
||||
void panic_errno(const char *fmt, ...)
|
||||
{
|
||||
fprintf(stderr, "ERROR: ");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, ": %s\n", strerror(errno));
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *slurp_file(const char *file_path)
|
||||
{
|
||||
#define SLURP_FILE_PANIC panic_errno("Could not read file `%s`", file_path)
|
||||
FILE *f = fopen(file_path, "r");
|
||||
if (f == NULL) SLURP_FILE_PANIC;
|
||||
if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
long size = ftell(f);
|
||||
if (size < 0) SLURP_FILE_PANIC;
|
||||
|
||||
char *buffer = (char*)malloc(size + 1);
|
||||
if (buffer == NULL) SLURP_FILE_PANIC;
|
||||
|
||||
if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
fread(buffer, 1, size, f);
|
||||
if (ferror(f) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
buffer[size] = '\0';
|
||||
|
||||
if (fclose(f) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
return buffer;
|
||||
#undef SLURP_FILE_PANIC
|
||||
}
|
||||
|
||||
bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader)
|
||||
{
|
||||
*shader = glCreateShader(shader_type);
|
||||
glShaderSource(*shader, 1, &source, NULL);
|
||||
glCompileShader(*shader);
|
||||
|
||||
GLint compiled = 0;
|
||||
glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled);
|
||||
|
||||
if (!compiled) {
|
||||
GLchar message[1024];
|
||||
GLsizei message_size = 0;
|
||||
glGetShaderInfoLog(*shader, sizeof(message), &message_size, message);
|
||||
fprintf(stderr, "%.*s\n", message_size, message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader)
|
||||
{
|
||||
char *source = slurp_file(file_path);
|
||||
bool err = compile_shader_source(source, shader_type, shader);
|
||||
free(source);
|
||||
return err;
|
||||
}
|
||||
|
||||
bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
|
||||
{
|
||||
*program = glCreateProgram();
|
||||
|
||||
glAttachShader(*program, vert_shader);
|
||||
glAttachShader(*program, frag_shader);
|
||||
glLinkProgram(*program);
|
||||
|
||||
GLint linked = 0;
|
||||
glGetProgramiv(*program, GL_LINK_STATUS, &linked);
|
||||
if (!linked) {
|
||||
GLsizei message_size = 0;
|
||||
GLchar message[1024];
|
||||
|
||||
glGetProgramInfoLog(*program, sizeof(message), &message_size, message);
|
||||
fprintf(stderr, "Program Linking: %.*s\n", message_size, message);
|
||||
}
|
||||
|
||||
glDeleteShader(vert_shader);
|
||||
glDeleteShader(frag_shader);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
void reload_shaders(RenderContext* context)
|
||||
{
|
||||
glDeleteProgram(context->program);
|
||||
|
||||
context->program_failed = false;
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
GLuint vert = 0;
|
||||
if (!compile_shader_file("./main.vert", GL_VERTEX_SHADER, &vert)) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
context->program_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
GLuint frag = 0;
|
||||
if (!compile_shader_file("./main.frag", GL_FRAGMENT_SHADER, &frag)) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
context->program_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!link_program(vert, frag, &context->program)) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
context->program_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(context->program);
|
||||
|
||||
context->resolution_location = glGetUniformLocation(context->program, "resolution");
|
||||
context->time_location = glGetUniformLocation(context->program, "time");
|
||||
|
||||
printf("Successfully Reload the Shaders\n");
|
||||
}
|
||||
|
||||
void window_size_callback(SDL_Window* window, int width, int height)
|
||||
{
|
||||
(void) window;
|
||||
glViewport(
|
||||
width / 2 - SCREEN_WIDTH / 2,
|
||||
height / 2 - SCREEN_HEIGHT / 2,
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT);
|
||||
}
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam)
|
||||
{
|
||||
(void) source;
|
||||
(void) id;
|
||||
(void) length;
|
||||
(void) userParam;
|
||||
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
||||
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
||||
type, severity, message);
|
||||
}
|
||||
|
||||
void process_prelude(RenderContext *context)
|
||||
{
|
||||
// glfwSetKeyCallback(window, key_callback);
|
||||
// glfwSetFramebufferSizeCallback(window, window_size_callback);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
if (!context->program_failed) {
|
||||
glUniform2f(context->resolution_location,
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT);
|
||||
glUniform1f(context->time_location, context->time);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
int64_t cur_time = SDL_GetTicks();
|
||||
if (!context->pause) {
|
||||
context->time += cur_time - context->prev_time;
|
||||
}
|
||||
context->prev_time = cur_time;
|
||||
}
|
22
src/renderer/debug.cpp
Normal file
22
src/renderer/debug.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "renderer/debug.h"
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam)
|
||||
{
|
||||
(void) source;
|
||||
(void) id;
|
||||
(void) length;
|
||||
(void) userParam;
|
||||
std::cerr << "GL CALLBACK: " << (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "")
|
||||
<< " type = 0x" << type
|
||||
<< ", severity = 0x" << severity
|
||||
<< ", message = " << message << std::endl;
|
||||
// std::cerr << "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
||||
// (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
||||
// type, severity, message);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
#include "shader.h"
|
||||
#include "renderer/shader.h"
|
||||
|
||||
Shader::Shader()
|
||||
{
|
@ -1,7 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include "texture.h"
|
||||
#include "renderer/texture.h"
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
@ -5,7 +5,7 @@
|
||||
#include <filesystem>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "model.h"
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
ObjElement toElement(const std::string &s) {
|
||||
if (s == "#") return ObjElement::OHASH;
|
||||
@ -47,11 +47,6 @@ void Vertex::DefineAttrib()
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
|
||||
void Face::PushItem(const FaceItem& item)
|
||||
{
|
||||
m_items.push_back(item);
|
||||
}
|
||||
|
||||
inline int Object::NormalizeIndex(const std::string &s, int baseCount) {
|
||||
if (s.empty()) return -1;
|
||||
int idx = std::stoi(s);
|
||||
@ -284,8 +279,6 @@ Object Object::LoadFile(const std::string& filename) {
|
||||
{
|
||||
auto& mesh = obj.GetLastMesh();
|
||||
std::string token;
|
||||
|
||||
Face fv;
|
||||
|
||||
while (iss >> token) {
|
||||
std::string a, b, c;
|
151
src/vertex.cpp
151
src/vertex.cpp
@ -1,151 +0,0 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "vertex.h"
|
||||
|
||||
#define BLOCK_SIZE 0.5f
|
||||
|
||||
Point::Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color)
|
||||
{
|
||||
m_position = position;
|
||||
m_normal = normal;
|
||||
m_color = color;
|
||||
}
|
||||
|
||||
Vertices::Vertices()
|
||||
{
|
||||
m_items = std::vector<Point>();
|
||||
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
glGenBuffers(1, &m_vbo);
|
||||
glGenBuffers(1, &m_ebo);
|
||||
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
// VBO (vertex buffer)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
// EBO (index buffer)
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// Call this after you finish adding vertices (or call it each time after PushBlock)
|
||||
void Vertices::Upload()
|
||||
{
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_items.size() * sizeof(Point), m_items.data(), GL_DYNAMIC_DRAW);
|
||||
|
||||
// Upload indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned int), m_indices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// void Vertices::PushBlock(const Block& block)
|
||||
// {
|
||||
// // 1 face
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 2 face
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 3 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
|
||||
// // 4 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
|
||||
// // 5 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 6 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 7 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 8 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 9 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
|
||||
// // 10 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
|
||||
// // 11 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 12 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color());
|
||||
// }
|
||||
|
||||
void Vertices::PushVertex(const Point& point)
|
||||
{
|
||||
m_items.push_back(point);
|
||||
}
|
||||
|
||||
void Vertices::PushIndex(unsigned int index)
|
||||
{
|
||||
m_indices.push_back(index);
|
||||
}
|
||||
|
||||
void Vertices::Bind()
|
||||
{
|
||||
glBindVertexArray(m_vao);
|
||||
}
|
||||
|
||||
void Vertices::Unbind()
|
||||
{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Vertices::Draw()
|
||||
{
|
||||
Bind();
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()), GL_UNSIGNED_INT, 0);
|
||||
Unbind();
|
||||
}
|
Reference in New Issue
Block a user