Skip to content

Commit

Permalink
Mesh Workflow kinda working, I am thinking it is now a transform issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jun 21, 2022
1 parent 2749a8f commit 450308a
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 132 deletions.
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/Renderer/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace Arcane
glm::vec3 GetViewDir() { return -glm::transpose(m_View)[2]; }
glm::vec3 GetRightVector() { return glm::transpose(m_View)[0]; }

glm::vec3 GetPosition() { return m_ViewData.CameraPosition; }

glm::vec2 GetSize() { return m_Size; }
void SetSize(uint32_t width, uint32_t height) { m_Size = {width, height}; }

Expand Down
129 changes: 63 additions & 66 deletions Arcane/src/Arcane/Renderer/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,84 @@ namespace Arcane

Mesh::Mesh(std::string filepath)
{
Assimp::Importer importer;

const aiScene* scene = importer.ReadFile(
"C:\\Projects\\Arcane-Engine\\EnchantingTable\\src\\Assets\\Models\\Cube.fbx", s_MeshImportFlags
Assimp::Importer import;
const aiScene* scene = import.ReadFile(
"C:\\Projects\\Arcane-Engine\\EnchantingTable\\src\\Assets\\Models\\Backpack.fbx",
s_MeshImportFlags
);

if (!scene) {
std::cout << importer.GetErrorString() << std::endl;
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
return;
}
else {
ProcessNode(scene->mRootNode, scene);

m_VertexBuffer = VertexBuffer::Create(m_Vertices.data(), sizeof(MeshVertex) * m_Vertices.size());
m_IndexBuffer = IndexBuffer::Create(m_Indices.data(), m_Indices.size());
m_VertexBuffer->AddIndexBuffer(m_IndexBuffer);
}
ProcessNode(scene->mRootNode, scene);
}

void Mesh::ProcessNode(aiNode* node, const aiScene* scene)
{
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
// process all the node's meshes (if any)
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
ProcessMesh(mesh, scene);
m_SubMeshes.push_back(ProcessMesh(mesh, scene));
}

for (unsigned int i = 0; i < node->mNumChildren; i++) {
// then do the same for each of its children
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
ProcessNode(node->mChildren[i], scene);
}
}


void Mesh::ProcessMesh(aiMesh* mesh, const aiScene* scene)
{
// Process Mesh into Submeshes
for (int i = 0; i < mesh->mNumVertices; i++) {
MeshVertex newVertex;

newVertex.vertex = {
mesh->mVertices[i].x,
mesh->mVertices[i].y,
mesh->mVertices[i].z
};

newVertex.normal = {
mesh->mNormals[i].x,
mesh->mNormals[i].y,
mesh->mNormals[i].z
};

if (mesh->mTextureCoords[0])
{
newVertex.texture = {
mesh->mTextureCoords[0][i].x,
mesh->mTextureCoords[0][i].y
};
}
else {
newVertex.texture = {0.0f, 0.0f};
}

m_Vertices.push_back(newVertex);
}

for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];

for (unsigned j = 0; j < face.mNumIndices; j++) {
m_Indices.push_back(face.mIndices[j]);
}
}

}

Material* Mesh::GetMaterial()
{
return m_Material;
}

void Mesh::SetMaterial(Material* material)
SubMesh* Mesh::ProcessMesh(aiMesh* mesh, const aiScene* scene)
{
m_Material = material;
std::vector<MeshVertex> vertices;
std::vector<uint32_t> indices;

// walk through each of the mesh's vertices
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
MeshVertex vertex;
glm::vec3 vector;
// positions
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.position = vector;

// normals
if (mesh->HasNormals())
{
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.normal = vector;
}

// texture coordinates
if (mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
{
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.texture = vec;

}
else
vertex.texture = glm::vec2(0.0f, 0.0f);

vertices.push_back(vertex);
}

for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}

return new SubMesh(vertices, indices);
}

}
23 changes: 3 additions & 20 deletions Arcane/src/Arcane/Renderer/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,19 @@

#include "Material.h"
#include "SubMesh.h"
#include "Buffer.h"

namespace Arcane
{
struct MeshVertex
{
glm::vec3 vertex;
glm::vec3 normal;
glm::vec2 texture;
};

class Mesh
{
public:
Mesh(std::string filepath);

Material* GetMaterial();
void SetMaterial(Material* material);

VertexBuffer* GetVertexBuffer() { return m_VertexBuffer; }
std::vector<SubMesh*> GetSubMeshes() { return m_SubMeshes; }
private:
void ProcessNode(aiNode* node, const aiScene* scene);
void ProcessMesh(aiMesh* mesh, const aiScene* scene);
SubMesh* ProcessMesh(aiMesh* mesh, const aiScene* scene);
private:
std::vector<MeshVertex> m_Vertices;
std::vector<uint32_t> m_Indices;

VertexBuffer* m_VertexBuffer;
IndexBuffer* m_IndexBuffer;

Material* m_Material;
std::vector<SubMesh*> m_SubMeshes;
};
}
10 changes: 9 additions & 1 deletion Arcane/src/Arcane/Renderer/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Arcane
struct CameraData {
glm::mat4 proj;
glm::mat4 view;
glm::vec3 cameraPosition;
};

struct Model {
Expand Down Expand Up @@ -264,6 +265,8 @@ namespace Arcane
{
for (int i = 0; i < s_Data.Meshes.size(); i++)
{
Mesh* currentMesh = s_Data.Meshes[i];

// Create Transform Component
TransformComponent& currentMeshComponent = s_Data.MeshTransforms[i];

Expand All @@ -274,7 +277,11 @@ namespace Arcane
// Write to uniform buffer
s_Data.ObjectUniformBuffer->WriteData((void*)&currentTransform, sizeof(Model));

Renderer::RenderMesh(s_Data.Meshes[i]->GetVertexBuffer(), s_Data.GeometryPipeline, {s_Data.GlobalDescriptorSet, s_Data.ObjectDescriptorSet});
for (int j = 0; j < currentMesh->GetSubMeshes().size(); j++) {
SubMesh* currentSubMesh = currentMesh->GetSubMeshes()[j];
Renderer::RenderMesh(currentSubMesh->GetVertexBuffer(), s_Data.GeometryPipeline, {s_Data.GlobalDescriptorSet, s_Data.ObjectDescriptorSet});
}

}
Renderer::RenderQuad(s_Data.GridVertexBuffer, s_Data.GridPipleine, {s_Data.GlobalDescriptorSet});
}
Expand All @@ -291,6 +298,7 @@ namespace Arcane
currentFrameCameraData.proj[1][1] *= -1;

currentFrameCameraData.view = s_Data.SceneCamera->GetView();
currentFrameCameraData.cameraPosition = s_Data.SceneCamera->GetPosition();
s_Data.GlobalUniformBuffer->WriteData((void*)&currentFrameCameraData, sizeof(CameraData));

GeometryPass();
Expand Down
3 changes: 0 additions & 3 deletions Arcane/src/Arcane/Renderer/SceneRenderer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <glm/glm.hpp>


#include "Arcane/ECS/Component.h"
#include "Framebuffer.h"
#include "Shader.h"
Expand All @@ -22,7 +20,6 @@ namespace Arcane
public:
SceneRenderer();


Framebuffer* GetFinalRenderFramebuffer();

void RenderScene();
Expand Down
39 changes: 7 additions & 32 deletions Arcane/src/Arcane/Renderer/SubMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,15 @@

namespace Arcane
{
SubMesh::SubMesh(aiMesh* submesh)
SubMesh::SubMesh(std::vector<MeshVertex> vertices, std::vector<uint32_t> indices)
{
// Get Vertices
for (int i = 0; i < submesh->mNumVertices; i++) {
SubMeshVertex newVertex;

// Get Position
newVertex.position = {
submesh->mVertices[i].x,
submesh->mVertices[i].y,
submesh->mVertices[i].z
};
// Create Vertex Buffer
m_VertexBuffer = VertexBuffer::Create(vertices.data(), vertices.size() * sizeof(MeshVertex));

// Get Normal
newVertex.normal = {
submesh->mNormals[i].x,
submesh->mNormals[i].y,
submesh->mNormals[i].z
};
// Create Index Buffer
m_IndexBuffer = IndexBuffer::Create(indices.data(), indices.size());

newVertex.texture = {
submesh->mTextureCoords[0][i].x,
submesh->mTextureCoords[0][i].y
};

m_Vertices.push_back(newVertex);
}

for (int i = 0; i < submesh->mNumFaces; i++) {
aiFace face = submesh->mFaces[i];

for (int j = 0; j < face.mNumIndices; j++) {
m_Indices.push_back(face.mIndices[j]);
}
}
// Add Index Buffer to Vertex Buffer
m_VertexBuffer->AddIndexBuffer(m_IndexBuffer);
}
}
18 changes: 9 additions & 9 deletions Arcane/src/Arcane/Renderer/SubMesh.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#pragma once

#include <glm/glm.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>

#include "Buffer.h"

namespace Arcane
{
struct SubMeshVertex
struct MeshVertex
{
glm::vec3 position;
glm::vec3 normal;
Expand All @@ -18,10 +16,12 @@ namespace Arcane
class SubMesh
{
public:
SubMesh(aiMesh* submesh);

SubMesh(std::vector<MeshVertex> vertices, std::vector<uint32_t> indices);

VertexBuffer* GetVertexBuffer() { return m_VertexBuffer; }
IndexBuffer* GetIndexBuffer() { return m_IndexBuffer; }
private:
std::vector<SubMeshVertex> m_Vertices;
std::vector<uint32_t> m_Indices;
VertexBuffer* m_VertexBuffer;
IndexBuffer* m_IndexBuffer;
};
}
Binary file added EnchantingTable/src/Assets/Models/Wheel.fbx
Binary file not shown.
1 change: 1 addition & 0 deletions EnchantingTable/src/Assets/Shaders/Grid.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ layout (location = 0) in vec3 aPos;
layout (set = 0, binding = 0) uniform MVP {
mat4 proj;
mat4 view;
vec3 cameraPosition;
} mvp;

layout (location = 0) out vec3 nearPoint;
Expand Down
Binary file modified EnchantingTable/src/Assets/Shaders/GridVert.spv
Binary file not shown.
10 changes: 9 additions & 1 deletion EnchantingTable/src/Assets/Shaders/Mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

layout(location = 0) in vec3 fragNormal;
layout(location = 1) in vec3 fragPos;
layout(location = 2) in vec3 fragCameraPos;

layout(location = 0) out vec4 Color;

Expand All @@ -20,8 +21,15 @@ void main() {
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;

// Specular Lighting
float specularStrength = 0.5f;
vec3 viewDir = normalize(fragCameraPos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;

// Calculate Result
vec3 result = (ambient + diffuse) * vec3(1.0f, 0.0f, 0.0f);
vec3 result = (ambient + diffuse + specular) * vec3(1.0f, 0.0f, 0.0f);

// Final Color
Color = vec4(result, 1.0f);
Expand Down
2 changes: 2 additions & 0 deletions EnchantingTable/src/Assets/Shaders/Mesh.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ layout (location = 2) in vec2 aTexCoord;

layout (location = 0) out vec3 fragNormal;
layout (location = 1) out vec3 fragPos;
layout (location = 2) out vec3 fragCameraPos;

// Global Frame data
layout (set = 0, binding = 0) uniform Camera {
mat4 proj;
mat4 view;
vec3 cameraPosition;
} camera;

// Per object data
Expand Down
Binary file modified EnchantingTable/src/Assets/Shaders/MeshFrag.spv
Binary file not shown.
Binary file modified EnchantingTable/src/Assets/Shaders/MeshVert.spv
Binary file not shown.

0 comments on commit 450308a

Please sign in to comment.