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:
2022-05-04 23:25:18 +02:00
parent eb0e7f7a51
commit 9165e30d0e
19 changed files with 322 additions and 112 deletions

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:
std::vector<glm::vec3> vertices;
std::vector<GLushort> elements;
std::vector<glm::vec2> uv;
};
class Mesh {
public:
std::vector<glm::vec3> vertices;
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);
}