From 68af17b81a1cb384717aa75a55211b2559b8899c Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Sun, 21 Jan 2024 22:33:55 +1100 Subject: [PATCH] things are rendering --- CMakeLists.txt | 2 +- src/graphics/arrays.cpp | 42 +------------- src/graphics/arrays.hpp | 11 ++-- src/graphics/model.cpp | 122 ++++++++++++++++++++++++++++++++++++++++ src/graphics/model.hpp | 25 ++++++++ src/graphics/resize.cpp | 10 ++++ src/graphics/resize.hpp | 4 ++ src/graphics/shader.cpp | 15 ++++- src/graphics/shader.hpp | 2 + src/graphics/window.cpp | 30 +++++++--- 10 files changed, 209 insertions(+), 54 deletions(-) create mode 100644 src/graphics/model.cpp create mode 100644 src/graphics/model.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 074a5de..2649105 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,5 +8,5 @@ set(CMAKE_CXX_FLAGS "-g -lncurses -I/usr/include/freetype2") file(GLOB_RECURSE SOURCES src/*.cpp) add_executable(FastNuclearSim ${SOURCES}) -target_link_libraries(FastNuclearSim PUBLIC stdc++ m GLEW glfw GL freetype) +target_link_libraries(FastNuclearSim PUBLIC stdc++ m GLEW glfw GL freetype assimp) diff --git a/src/graphics/arrays.cpp b/src/graphics/arrays.cpp index 41aae77..ced4c60 100644 --- a/src/graphics/arrays.cpp +++ b/src/graphics/arrays.cpp @@ -4,6 +4,7 @@ #include +#include "shader.hpp" #include "arrays.hpp" #include "font.hpp" @@ -16,46 +17,9 @@ static void* ptr_diff(void* a, void* b) return (void*)((size_t)a - (size_t)b); } -void processNode(aiNode *node, const aiScene *scene) -{ - -} - -Mesh processMesh(aiMesh *mesh, const aiScene *scene) -{ - -} - -unsigned int arrays::init() +void arrays::vertex_attrib_pointers() { vertex v; - unsigned long handle = font::chars['*'].handle; - - Assimp::Importer importer; - const aiScene *scene = importer.ReadFile("monkey.obj", aiProcess_Triangulate | aiProcess_FlipUVs); - - arrays::vertex vertices[4] = { - {handle, {0.0f, 1.0f}, {-0.5f, -0.5f, 0.0f}}, - {handle, {0.0f, 0.0f}, {-0.5f, 0.5f, 0.0f}}, - {handle, {1.0f, 1.0f}, { 0.5f, -0.5f, 0.0f}}, - {handle, {1.0f, 0.0f}, { 0.5f, 0.5f, 0.0f}} - }; - - int indices[6] = { - 0, 1, 3, 0, 2, 3 - }; - - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - glGenBuffers(1, &vbo); - glGenBuffers(1, &ebo); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribLPointer(0, 1, GL_UNSIGNED_INT64_ARB, sizeof(v), ptr_diff(&v.texid, &v)); glEnableVertexAttribArray(0); @@ -65,7 +29,5 @@ unsigned int arrays::init() glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof(v), ptr_diff(&v.pos, &v)); glEnableVertexAttribArray(2); - - return vao; } diff --git a/src/graphics/arrays.hpp b/src/graphics/arrays.hpp index 14dba03..2f8bc26 100644 --- a/src/graphics/arrays.hpp +++ b/src/graphics/arrays.hpp @@ -9,12 +9,15 @@ namespace sim::graphics::arrays struct vertex { - unsigned long texid; - glm::vec2 texpos; - glm::vec3 pos; + unsigned long texid = 0; + glm::vec2 texpos = {0, 0}; + glm::vec3 pos = {0, 0, 0}; + + vertex() { } + vertex(unsigned long texid, glm::vec2 texpos, glm::vec3 pos) : texid(texid), texpos(texpos), pos(pos) { } }; -unsigned int init(); +void vertex_attrib_pointers(); }; diff --git a/src/graphics/model.cpp b/src/graphics/model.cpp new file mode 100644 index 0000000..c8349dd --- /dev/null +++ b/src/graphics/model.cpp @@ -0,0 +1,122 @@ + +#include +#include + +#include +#include +#include + +#include + +#include "model.hpp" +#include "arrays.hpp" + +using namespace sim::graphics; + +model::model() +{ + +} + +model::~model() +{ + if(vbo) glDeleteBuffers(1, &vbo); + if(ebo) glDeleteBuffers(1, &ebo); + if(vao) glDeleteVertexArrays(1, &vao); +} + +void model::alloc() +{ + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); + glGenBuffers(1, &ebo); + + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + + arrays::vertex_attrib_pointers(); +} + +void model::load_vbo(const arrays::vertex* data, size_t size, int mode) +{ + glBufferData(GL_ARRAY_BUFFER, size * sizeof(data[0]), data, mode); +} + +void model::load_ebo(const unsigned int* data, size_t size, int mode) +{ + glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(data[0]), data, mode); + this->size = size; +} + +struct proc_state +{ + size_t at = 0; + std::vector vertices; + std::vector indices; +}; + +static void proc_mesh(proc_state& state, aiMesh* mesh, const aiScene* scene) +{ + for(unsigned int i = 0; i < mesh->mNumVertices; i++) + { + arrays::vertex vertex; + + vertex.pos = {mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z}; + + state.vertices.push_back(vertex); + } + + unsigned int at = state.at; + + for(unsigned int i = 0; i < mesh->mNumFaces; i++) + { + aiFace face = mesh->mFaces[i]; + unsigned int j; + + for(j = 0; j < face.mNumIndices; j++) + { + state.indices.push_back(face.mIndices[j] + state.at); + } + + at += j; + } + + state.at = at; +} + +static void proc_node(proc_state& state, aiNode* node, const aiScene* scene) +{ + for(size_t i = 0; i < node->mNumMeshes; i++) + { + aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; + proc_mesh(state, mesh, scene); + } + + for(size_t i = 0; i < node->mNumChildren; i++) + { + proc_node(state, node->mChildren[i], scene); + } +} + +void model::load(const char* path) +{ + proc_state state; + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs); + proc_node(state, scene->mRootNode, scene); + + load_vbo(&state.vertices[0], state.vertices.size(), GL_STATIC_DRAW); + load_ebo(&state.indices[0], state.indices.size(), GL_STATIC_DRAW); +} + +void model::bind() +{ + glBindVertexArray(vao); +} + +void model::render() +{ + glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0); +} + diff --git a/src/graphics/model.hpp b/src/graphics/model.hpp new file mode 100644 index 0000000..c181f3f --- /dev/null +++ b/src/graphics/model.hpp @@ -0,0 +1,25 @@ + +#pragma once + +#include "arrays.hpp" + +namespace sim::graphics +{ + +struct model +{ + unsigned int vao = 0, vbo = 0, ebo = 0, size = 0; + + model(); + ~model(); + + void bind(); + void alloc(); + void load(const char* path); + void load_vbo(const arrays::vertex* data, size_t size, int mode); + void load_ebo(const unsigned int* data, size_t size, int mode); + void render(); +}; + +}; + diff --git a/src/graphics/resize.cpp b/src/graphics/resize.cpp index 20dda38..238292a 100644 --- a/src/graphics/resize.cpp +++ b/src/graphics/resize.cpp @@ -17,6 +17,16 @@ static int win_restore_h; static int win_restore_x; static int win_restore_y; +glm::vec2 resize::get_size() +{ + return {win_w, win_h}; +} + +double resize::get_aspect() +{ + return win_w / win_h; +} + void resize::toggle_fullscreen() { GLFWwindow* win = window::get_window(); diff --git a/src/graphics/resize.hpp b/src/graphics/resize.hpp index 50e0eb1..ca060e8 100644 --- a/src/graphics/resize.hpp +++ b/src/graphics/resize.hpp @@ -1,11 +1,15 @@ #pragma once +#include + namespace sim::graphics::resize { void init(); void toggle_fullscreen(); +glm::vec2 get_size(); +double get_aspect(); }; diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 7784dc1..e6a6fce 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -17,14 +17,20 @@ layout (location = 0) in sampler2D aTex; layout (location = 1) in vec2 aTexPos; layout (location = 2) in vec3 aPos; +uniform mat4 model; +uniform mat4 projection; + out flat sampler2D tex; out vec2 texPos; +out float zVal; void main() { - gl_Position = vec4(aPos, 1.0); + vec4 pos = model * vec4(aPos, 1.0); + gl_Position = projection * pos; texPos = aTexPos; tex = aTex; + zVal = 8.0f / (pos.z * pos.z); } )"; @@ -35,6 +41,7 @@ static const char* FRAGMENT_SHADER = R"( in flat sampler2D tex; in vec2 texPos; +in float zVal; out vec4 FragColour; @@ -44,7 +51,7 @@ uniform mat4 tex_mat; void main() { vec4 texdata = do_tex ? texture2D(tex, texPos) : vec4(1); - FragColour = tex_mat * texdata; + FragColour = tex_mat * texdata * vec4(zVal, zVal, zVal, 1); } )"; @@ -53,6 +60,8 @@ static unsigned int prog_id; int shader::gl_tex_mat; int shader::gl_do_tex; +int shader::gl_model; +int shader::gl_projection; static int load_shader(const char** src, int type) { @@ -87,6 +96,8 @@ unsigned int shader::init_program() gl_tex_mat = glGetUniformLocation(prog_id, "tex_mat"); gl_do_tex = glGetUniformLocation(prog_id, "do_tex"); + gl_model = glGetUniformLocation(prog_id, "model"); + gl_projection = glGetUniformLocation(prog_id, "projection"); glUseProgram(prog_id); glDeleteShader(vsh_id); diff --git a/src/graphics/shader.hpp b/src/graphics/shader.hpp index ad15fe8..a68e99d 100644 --- a/src/graphics/shader.hpp +++ b/src/graphics/shader.hpp @@ -6,6 +6,8 @@ namespace sim::graphics::shader extern int gl_tex_mat; extern int gl_do_tex; +extern int gl_model; +extern int gl_projection; unsigned int init_program(); diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index 4032059..71c31e4 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -3,9 +3,12 @@ #include #include +#include // glm::translate, glm::rotate, glm::scale +#include // glm::perspective #include +#include "model.hpp" #include "arrays.hpp" #include "keyboard.hpp" #include "resize.hpp" @@ -17,6 +20,7 @@ using namespace sim::graphics; static GLFWwindow* win; static bool win_should_close = false; +static model Model; void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { @@ -56,29 +60,41 @@ void window::create() keyboard::init(); resize::init(); font::init(); - shader::init_program(); - arrays::init(); + + Model.alloc(); + Model.load("monkey.obj"); glViewport(0, 0, 800, 600); } void window::loop() { - glClearColor(0.2f, 0.3f, 0.5f, 1.0f); + glClearColor(0, 0, 0, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glm::mat4 m = { + glm::mat4 mat_colour = { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - glUniformMatrix4fv(shader::gl_tex_mat, 1, false, &m[0][0]); - glUniform1i(shader::gl_do_tex, 1); + glm::mat4 mat_model = glm::mat4(1.0f); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + mat_model = glm::translate(mat_model, glm::vec3(0.0f, 0.0f, -5.0f)); +// mat_model = glm::scale(mat_model, glm::vec3(1.0f) * 0.05f); + mat_model = glm::rotate(mat_model, float(M_PI * 0.125), glm::vec3(1, 1, 1)); + + glm::mat4 mat_projection = glm::perspective(float(M_PI * 0.25), (float)resize::get_aspect(), 0.1f, 100.f); + + glUniformMatrix4fv(shader::gl_tex_mat, 1, false, &mat_colour[0][0]); + glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]); + glUniformMatrix4fv(shader::gl_model, 1, false, &mat_model[0][0]); + glUniform1i(shader::gl_do_tex, 0); + + Model.bind(); + Model.render(); glfwSwapBuffers(win); glfwPollEvents();