things are rendering

This commit is contained in:
Jay Robson 2024-01-21 22:33:55 +11:00
parent 4f82475251
commit 3217b3a1a5
10 changed files with 209 additions and 54 deletions

View File

@ -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)

View File

@ -4,6 +4,7 @@
#include <assimp/Importer.hpp>
#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;
}

View File

@ -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();
};

122
src/graphics/model.cpp Normal file
View File

@ -0,0 +1,122 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>
#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<arrays::vertex> vertices;
std::vector<unsigned int> 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);
}

25
src/graphics/model.hpp Normal file
View File

@ -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();
};
};

View File

@ -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();

View File

@ -1,11 +1,15 @@
#pragma once
#include <glm/vec2.hpp>
namespace sim::graphics::resize
{
void init();
void toggle_fullscreen();
glm::vec2 get_size();
double get_aspect();
};

View File

@ -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);

View File

@ -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();

View File

@ -3,9 +3,12 @@
#include <GLFW/glfw3.h>
#include <glm/matrix.hpp>
#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
#include <iostream>
#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();