Incorrectly loading a model, Adding a VertexArray abstraction

* Using import library assimp to incorrectly load a cube.obj
* Using a temporary Renderable class as a placeholder for all data needed
to render the mesh.
* Vertex Array abstraction added
This commit is contained in:
Nigel Barink 2022-05-04 23:25:18 +02:00
parent eb0e7f7a51
commit 9165e30d0e
19 changed files with 322 additions and 112 deletions

2
.gitignore vendored
View File

@ -16,3 +16,5 @@ x64/
*.gltf
!sponza.gltf
imgui.ini

3
.gitmodules vendored
View File

@ -16,3 +16,6 @@
[submodule "ImGui"]
path = libs/ImGui
url = https://github.com/ocornut/imgui.git
[submodule "assimp"]
path = libs/assimp
url = https://github.com/assimp/assimp.git

View File

@ -1,5 +1,6 @@
#include "include/AssetManager/ModelImporter.h"
void ModelImporter::ImportFBX(std::string path)
{
spdlog::warn("ImportFBX not implemented!");
@ -25,7 +26,10 @@ void ModelImporter::Import(std::string path)
spdlog::warn("Import not implemented!");
}
void ModelImporter::Test() {
std::vector<BarinkEngine::Mesh> ModelImporter::Test() {
/*
spdlog::info("====== Tiny GLTF ======");
tinygltf::Model loadedModel;
tinygltf::TinyGLTF loader;
std::string error;
@ -44,4 +48,68 @@ void ModelImporter::Test() {
spdlog::info("Meshes in model: {}", loadedModel.meshes.size());
spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size());
*/
spdlog::info("======= Assimp ======");
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
return processNode(currentNode, scene);
}
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) {
std::vector<BarinkEngine::Mesh> meshes;
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
auto m2 = processNode(node->mChildren[i], scene);
for(auto m : m2) {
meshes.push_back(m);
}
}
return meshes;
}
BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
std::vector<glm::vec3> vertices ;
std::vector<unsigned int> indices;
// Process vertices
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertices.push_back(vector);
}
// Process Indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
if (face.mNumIndices < 3)
continue;
for (unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
BarinkEngine::Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
}

View File

@ -58,7 +58,7 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha
}
char* Shader::readFile (const char* filePath){
spdlog::info("try Open file {}", filePath);
std::ifstream file ;
file.open(filePath);

View File

@ -0,0 +1,25 @@
#include <MyGraphicsEngine/VertexArray.h>
#include <glad/glad.h>
void VertexArray::Create(){
glGenVertexArrays(1, &id);
}
void VertexArray::Bind(){
glBindVertexArray(id);
}
void VertexArray::Unbind(){
glBindVertexArray(0);
}
void VertexArray::Delete() {
glDeleteVertexArrays(1, &id);
}
void VertexArray::AttachAttribute(unsigned int index , int size, int stride ){
glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);
}

View File

@ -4,7 +4,10 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define TINYGLTF_NO_EXTERNAL_IMAGE
#include <tiny_gltf.h>
#include <MyGraphicsEngine/Mesh.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <spdlog/spdlog.h>
#include <string>
@ -14,9 +17,11 @@ private:
void ImportBlend(std::string path);
void ImportGLTF(std::string path);
void ImportOBJ(std::string path);
static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene);
static std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene);
public:
void Import(std::string path);
static void Test();
static std::vector<BarinkEngine::Mesh> Test();
};

View File

@ -0,0 +1,12 @@
#pragma once
#include <string>
class EditorWindow {
protected:
std::string WindowTitle;
public:
virtual void Show() = 0;
};

View File

@ -1,10 +1,12 @@
#pragma once
#include <vector>
#include <glm/glm.hpp>
namespace BarinkEngine{
class Mesh {
public:
class Mesh {
public:
std::vector<glm::vec3> vertices;
std::vector<GLushort> elements;
std::vector<unsigned int > elements;
std::vector<glm::vec2> uv;
};
};
}

View File

@ -0,0 +1,18 @@
#pragma once
class VertexArray{
private:
unsigned int id;
public:
void Create();
void Bind();
void Unbind();
void Delete();
void AttachAttribute(unsigned int index, int size, int stride);
};

View File

@ -3,5 +3,5 @@
out vec4 FragColor;
void main(){
FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f);
FragColor = vec4(0.5f, 0.5f, 0.0f , 1.0f);
}

View File

@ -0,0 +1,12 @@
# Blender 3.1.2 MTL File: 'None'
# www.blender.org
newmtl Material
Ns 360.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

View File

@ -0,0 +1,40 @@
# Blender 3.1.2
# www.blender.org
mtllib Cube.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vn -0.0000 1.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -1.0000 -0.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vt 0.625000 0.500000
vt 0.375000 0.500000
vt 0.625000 0.750000
vt 0.375000 0.750000
vt 0.875000 0.500000
vt 0.625000 0.250000
vt 0.125000 0.500000
vt 0.375000 0.250000
vt 0.875000 0.750000
vt 0.625000 1.000000
vt 0.625000 0.000000
vt 0.375000 1.000000
vt 0.375000 0.000000
vt 0.125000 0.750000
s 0
usemtl Material
f 1/1/1 5/5/1 7/9/1 3/3/1
f 4/4/2 3/3/2 7/10/2 8/12/2
f 8/13/3 7/11/3 5/6/3 6/8/3
f 6/7/4 2/2/4 4/4/4 8/14/4
f 2/2/5 1/1/5 3/3/5 4/4/5
f 6/8/6 5/6/6 1/1/6 2/2/6

View File

@ -0,0 +1,50 @@
#include "Renderable.h"
Renderable Renderable::Load()
{
return Renderable();
}
Renderable::Renderable()
{
meshes = ModelImporter::Test();
transform.Scale = glm::vec3(1.0f);
transform.Rotation = glm::vec3(0.0f, 90.0f, 0.0f);
transform.Position = glm::vec3(0.0f, 0.0f, -10.0f);
VAO.Create();
VAO.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(glm::vec3), false);
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true);
VAO.AttachAttribute(0, 3, 0);
vertexBuffer.Unbind(false);
VAO.Unbind();
}
Renderable::~Renderable()
{
}
void Renderable::Draw()
{
VAO.Bind();
elementBuffer.Bind(true);
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_SHORT, NULL);
VAO.Unbind();
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <vector>
#include <spdlog/spdlog.h>
#include <MyGraphicsEngine/Mesh.h>
#include <MyGraphicsEngine/Transform.h>
#include <include/AssetManager/ModelImporter.h>
#include <include/MyGraphicsEngine/Buffer.h>
#include <include/MyGraphicsEngine/VertexArray.h>
class Renderable {
private:
std::vector<BarinkEngine::Mesh> meshes;
Renderable();
public:
Buffer vertexBuffer;
Buffer elementBuffer;
VertexArray VAO;
Transform transform;
~Renderable();
static Renderable Load();
void Draw();
};

View File

@ -1,59 +1,36 @@
#include <string>
#include <MyGraphicsEngine/Shader.h>
#include <MyGraphicsEngine/Window.h>
#include <MyGraphicsEngine/Camera.h>
#include <MyGraphicsEngine/Mesh.h>
#include <MyGraphicsEngine/Transform.h>
#include <MyGraphicsEngine/Buffer.h>
#include <string>
#include <glm/glm.hpp>
#include "imgui.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "Renderable.h"
#include <filesystem>
/*
* extern "C"
extern "C"
{
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
}
#include <include/AssetManager/ModelImporter.h>
/*
#include <gorilla/gau.h>
#include <gorilla/ga.h>
*/
int main(int argc, char* argv[]) {
spdlog::info("Working directory: {}", argv[0]);
//ModelImporter::Test();
Transform t;
t.Scale.x = 1.0f;
t.Scale.y = 1.0f;
t.Rotation.y = 90.0f;
Mesh mesh;
mesh.vertices = {
glm::vec3( 0.5f, 0.5f, 0.0f), // top, right
glm::vec3( 0.5f, -0.5f, 0.0f), // bottom right
glm::vec3(-0.5f, -0.5f, 0.0f), // bottom left
glm::vec3(-0.5f, 0.5f, 0.0f) // top left
};
mesh.elements = {
0,1,3,
1,2,3
};
Camera cam(glm::vec3(2.0f, 0.0f, 0.0f), glm::vec3(0.0f,0.0f,0.0f), 90.0f);
char cwd[256];
getcwd(cwd, 256);
spdlog::info("Working directory: {}", cwd);
BarinkWindow GameWindow(800, 600);
@ -67,44 +44,21 @@ int main(int argc, char* argv[]) {
ImGui_ImplGlfw_InitForOpenGL(GameWindow.windowptr(), true);
ImGui_ImplOpenGL3_Init("#version 440");
Camera cam(glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
Renderable Cube = Renderable::Load();
spdlog::info("==== Load Shader(s) ====");
std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
Shader shader (vertexShaderSource, fragmentShaderSource);
/*
* lua_State* L = luaL_newstate();
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaL_dostring(L, "print('BarinkEngine')");
luaL_dofile(L,"./script.lua");
*/
spdlog::info("==== Run script ====");
luaL_dofile(L,"build/SandboxApplication/Debug/script.lua");
spdlog::info("Vertices: {}, {} bytes", mesh.vertices.size(), sizeof(glm::vec3));
spdlog::info("Elements: {}, {} bytes", mesh.elements.size(), sizeof(GLushort));
Buffer VertexBuffer;
Buffer ElementBuffer;
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
VertexBuffer.createBuffer();
VertexBuffer.Bind(false);
VertexBuffer.setBufferData(&mesh.vertices[0], mesh.vertices.size() * sizeof(glm::vec3),false);
ElementBuffer.createBuffer();
ElementBuffer.Bind(true);
ElementBuffer.setBufferData(&mesh.elements[0], mesh.elements.size() * sizeof(GLushort), true);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
VertexBuffer.Unbind(false);
glBindVertexArray(0);
/*
* gau_Manager* mgr;
@ -124,15 +78,13 @@ int main(int argc, char* argv[]) {
while (!GameWindow.WindowShouldClose()) {
glm::mat4 tran = glm::translate(glm::mat4(), t.Position);
glm::mat4 scale = glm::scale(glm::mat4(), t.Scale);
glm::mat4 tran = glm::translate(glm::mat4(), Cube.transform.Position);
glm::mat4 scale = glm::scale(glm::mat4(), Cube.transform.Scale);
glm::mat4 rot =
glm::rotate(glm::mat4(), glm::radians(t.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(t.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(t.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::rotate(glm::mat4(), glm::radians(Cube.transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(Cube.transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(Cube.transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 model = tran * rot * scale;
@ -148,23 +100,23 @@ int main(int argc, char* argv[]) {
shader.setUniformMat4("P", projection);
shader.setUniformMat4("M", model);
shader.setUniformMat4("V", cam.GetViewMatrix());
glBindVertexArray(VAO);
ElementBuffer.Bind(true);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
Cube.Draw();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Transform Test Tool");
ImGui::Begin("Test");
ImGui::SliderFloat("Scale Y:", (float*)&t.Scale.y, 1, 4);
ImGui::SliderFloat("Scale X:", (float*)&t.Scale.x, 1, 4);
ImGui::SliderFloat("Scale Y:", (float*)&Cube.transform.Scale.y, 1, 4);
ImGui::SliderFloat("Scale X:", (float*)&Cube.transform.Scale.x, 1, 4);
ImGui::SliderFloat("Position X:", (float*)&t.Position.z, -5, 5);
ImGui::SliderFloat("Camera Zoom:", &cam.Zoom, 40, 190);
ImGui::SliderFloat("Position X:", (float*)&Cube.transform.Position.x, -9, -60);
ImGui::SliderFloat("Rotate Y:", (float*)&Cube.transform.Rotation.y, 0, 180);
ImGui::SliderFloat("Rotate Z:", (float*)&Cube.transform.Rotation.z, 0, 180);
ImGui::End();
@ -182,8 +134,8 @@ int main(int argc, char* argv[]) {
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glDeleteVertexArrays(1, &VAO);
ElementBuffer.Delete();
Cube.VAO.Delete();
Cube.elementBuffer.Delete();
}

View File

@ -9,7 +9,7 @@
<input type="checkbox" checked></input> Basic Triangle rendering \
<input type="checkbox" checked></input> Basic IMGui \
<input type="checkbox"></input> Basic Textures \
<input type="checkbox"></input> Link GLEW \
<input type="checkbox" checked></input> Link GLEW or GLAD \
<input type="checkbox" checked></input> Work on basic logging \
<input type="checkbox"></input> Input handling \
<input type="checkbox"></input> More shader work \

View File

@ -1,10 +0,0 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0
[Window][Transform Test Tool]
Pos=443,154
Size=396,490
Collapsed=0

1
libs/assimp Submodule

@ -0,0 +1 @@
Subproject commit 19f2a624a9d69aa8d1680685766b76bd8983ffe8

View File

@ -12,6 +12,7 @@ workspace "BarinkEngine"
buildmessage "Building BarinkEngineSandbox ..."
includedirs {
"./libs/assimp/include",
"./libs/glad/include",
"./MyGraphicsEngine",
"./MyGraphicsEngine/include",
@ -20,7 +21,9 @@ workspace "BarinkEngine"
"./libs/GorillaAudio/include",
"./libs/lua/include",
"./libs/glfw/include",
"./libs/ImGui"
"./libs/ImGui",
"./libs/lua/include"
}
libdirs{
@ -31,7 +34,7 @@ workspace "BarinkEngine"
}
links{
"liblua",
"lua54",
"spdlog",
"glfw3",
"MyGraphicsEngine"
@ -62,6 +65,7 @@ workspace "BarinkEngine"
buildmessage "Building MyGraphicsEngine ..."
includedirs {
"./libs/assimp/include",
"./libs/glad/include",
"./libs/glfw/include",
"./libs/tinygltf",
@ -74,11 +78,13 @@ workspace "BarinkEngine"
libdirs{
"./libs/spdlog/build/Release",
"./libs/assimp/lib/Debug",
"./libs/glfw/build/src/Debug"
}
links {
"spdlog",
"assimp-vc143-mtd",
"glfw3"
}