refactoring

This commit is contained in:
2025-08-17 12:40:31 -05:00
parent 47564bb3cf
commit 2a1654e80c
13 changed files with 120 additions and 1901 deletions

View File

@@ -1,28 +1,19 @@
#include <array>
#include <cmath>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdio.h>
#include <optional>
#include "util.hpp"
#include "shaders.hpp"
using namespace std;
static GLFWwindow* window;
template<class T>
struct SimpleArray {
T* data;
size_t size;
};
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
@@ -32,7 +23,7 @@ bool glfw_window_setup() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW windoww" << std::endl;
@@ -49,7 +40,7 @@ enum class ParserState {
FACE,
FACE_SKIP,
};
optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
std::optional<std::pair<array<float>, array<unsigned int>>> load_icosphere() {
std::FILE* f = NULL;
if (fopen_s(&f, "Icosphere.obj", "r")) {
@@ -64,7 +55,7 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
unsigned int* indices_data = (unsigned int*)malloc(sizeof(int) * 256);
size_t indices_data_size = 0;
ParserState state = ParserState::PREFIX;
ParserState state = ParserState::PREFIX;
char ln[128];
int len = 0;
@@ -75,11 +66,9 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
if (len > 0 && ln[len - 1] == 'v' && ln[len] == ' ') {
state = ParserState::VERTEX;
start = len + 1;
}
else if (len > 0 && ln[len - 1] == 'f' && ln[len] == ' ') {
} else if (len > 0 && ln[len - 1] == 'f' && ln[len] == ' ') {
state = ParserState::FACE;
start = len + 1;
}
break;
case ParserState::VERTEX:
@@ -110,27 +99,26 @@ optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
len++;
}
SimpleArray<float> verts = { vert_data, vert_data_size };
SimpleArray<unsigned int> indices = { indices_data, indices_data_size };
array<float> verts = { vert_data, vert_data_size };
array<unsigned int> indices = { indices_data, indices_data_size };
return make_pair(verts, indices);
return std::make_pair(verts, indices);
}
int main()
{
if (!glfw_window_setup()) return -1;
int main() {
if (!glfw_window_setup())
return -1;
auto data = load_icosphere();
if (!data) {
std::cout << "ERROR loading the icosphere obj file failed" << std::endl;
return -1;
}
auto verts = data.value().first;
auto indices = data.value().second;
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
@@ -150,27 +138,32 @@ int main()
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verts.size * sizeof(float), verts.data, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, verts.len * sizeof(float), verts.data, GL_STATIC_DRAW);
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size * sizeof(unsigned int), indices.data, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.len * sizeof(unsigned int), indices.data, GL_STATIC_DRAW);
uint program_id;
if (!shader::load(&program_id, "src/shaders/vertex.vs", "src/shaders/fragment.fs")) return -1;
const char* vertex_filepath = "src/shaders/vertex.glsl";
const char* fragment_filepath = "src/shaders/fragment.glsl";
if (!shader::load(&program_id, vertex_filepath, fragment_filepath))
return -1;
shader::use(program_id);
// Tell opengl which attribute our data is for (i.e. location)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window))
{
while (!glfwWindowShouldClose(window)) {
float time = glfwGetTime();
shader::set_uniform(program_id, "color", vec4 { 1, sin(time), 1, 1.0 });
shader::use(program_id);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, indices.size, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, indices.len, GL_UNSIGNED_INT, 0);
glBindVertexArray(VAO);
processInput(window);

View File

@@ -6,7 +6,6 @@
#define MAX_ERR_MSG_LEN 256
// Visit/Variant overload pattern
// https://www.modernescpp.com/index.php/visiting-a-std-variant-with-the-overload-pattern/
template<typename ... Ts>
@@ -57,6 +56,12 @@ void set_uniform(uint id, const char* name, uniform_variant value) {
[id, name](int i) { glUniform1i(glGetUniformLocation(id, name), i); },
[id, name](float f) { glUniform1f(glGetUniformLocation(id, name), f); },
[id, name](bool b) { glUniform1i(glGetUniformLocation(id, name), b); },
[id, name](vec4 v) { glUniform4f(glGetUniformLocation(id, name), v.x, v.y, v.z, v.w); },
[id, name](vec3 v) { glUniform3f(glGetUniformLocation(id, name), v.x, v.y, v.z); },
[id, name](vec2 v) { glUniform2f(glGetUniformLocation(id, name), v.x, v.y); },
[id, name](vec4i v) { glUniform4i(glGetUniformLocation(id, name), v.x, v.y, v.z, v.w); },
[id, name](vec3i v) { glUniform3i(glGetUniformLocation(id, name), v.x, v.y, v.z); },
[id, name](vec2i v) { glUniform2i(glGetUniformLocation(id, name), v.x, v.y); },
};
visit(visitor, value);
}
@@ -64,12 +69,12 @@ void set_uniform(uint id, const char* name, uniform_variant value) {
// Privates
bool _compile_shader(uint *out_id, const char* filepath, int shader_type) {
Array<char> source;
array<char> source;
if (!read_file(&source, filepath))
return false;
uint id = glCreateShader(shader_type);
glShaderSource(id, 1, &source._data, (int*)&source.len);
glShaderSource(id, 1, &source.data, (int*)&source.len);
glCompileShader(id);
int status;
@@ -78,7 +83,7 @@ bool _compile_shader(uint *out_id, const char* filepath, int shader_type) {
char err_msg[MAX_ERR_MSG_LEN];
glGetShaderInfoLog(id, MAX_ERR_MSG_LEN, NULL, err_msg);
printf("Error compiling shader %s\n", filepath);
printf("%.*s\n", (int)source.len, source._data);
printf("%.*s\n", (int)source.len, source.data);
printf("Error msg: %s\n", err_msg);
}
*out_id = id;

View File

@@ -1,7 +1,9 @@
#version 330 core
uniform vec4 color;
out vec4 frag_color;
void main() {
frag_color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
frag_color = color;
}

8
src/shaders/vertex.glsl Normal file
View File

@@ -0,0 +1,8 @@
#version 330 core
layout (location = 0) in vec3 pos;
uniform vec4 color;
void main() {
gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
}

View File

@@ -1,6 +0,0 @@
#version 330 core
layout (location = 0) in vec3 aPos;
void main() {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

View File

@@ -3,21 +3,21 @@
#include "util.hpp"
bool read_file(Array<char>* out, const char* filepath) {
bool read_file(array<char>* out, const char* filepath) {
FILE* fp = NULL;
if (fopen_s(&fp, filepath, "r") != 0) {
if (fopen_s(&fp, filepath, "rb") != 0) {
printf("ERROR Failed to open file %s\n", filepath);
return false;
}
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
fseek(fp, 0L, 0L);
fseek(fp, 0, SEEK_SET);
char *data = (char*)malloc(sizeof(char)*sz);
fread(data, sizeof(char), sz, fp);
fclose(fp);
out->_data = data;
out->data = data;
out->len = sz;
return true;
}