-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from ArnavMehta3000/main
Added Model loading + RHICompat rule
- Loading branch information
Showing
24 changed files
with
190,073 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "Mesh.h" | ||
#define TINYOBJLOADER_IMPLEMENTATION | ||
#include <tiny_obj_loader.h> | ||
|
||
namespace CGL::Graphics | ||
{ | ||
CGL_DEFINE_LOG_CATEGORY(Mesh); | ||
|
||
bool Mesh::LoadOBJ(const std::string& file) | ||
{ | ||
namespace obj = tinyobj; | ||
|
||
obj::ObjReader reader; | ||
|
||
if (!reader.ParseFromFile(file)) | ||
{ | ||
if (!reader.Error().empty()) | ||
{ | ||
CGL_LOG(Mesh, Error, "Failed to parse OBJ file: {}", reader.Error()); | ||
} | ||
return false; | ||
} | ||
|
||
if (!reader.Warning().empty()) | ||
{ | ||
CGL_LOG(Mesh, Warn, "Failed to parse OBJ file: {}", reader.Warning()); | ||
} | ||
|
||
auto& attrib = reader.GetAttrib(); | ||
auto& shapes = reader.GetShapes(); | ||
|
||
// Clear any existing SubMeshes | ||
SubMeshes.clear(); | ||
|
||
// Each shape corresponds to a mesh | ||
for (const auto& shape : shapes) | ||
{ | ||
SubMesh subMesh; | ||
|
||
// Loop over faces(polygon) | ||
size_t index_offset = 0; | ||
for (size_t f = 0; f < shape.mesh.num_face_vertices.size(); f++) | ||
{ | ||
size_t fv = shape.mesh.num_face_vertices[f]; | ||
|
||
// Loop over vertices in the face | ||
for (size_t v = 0; v < fv; v++) | ||
{ | ||
// Access to vertex index | ||
tinyobj::index_t idx = shape.mesh.indices[index_offset + v]; | ||
|
||
// Vertex coordinates | ||
tinyobj::real_t vx = attrib.vertices[3 * idx.vertex_index + 0]; | ||
tinyobj::real_t vy = attrib.vertices[3 * idx.vertex_index + 1]; | ||
tinyobj::real_t vz = attrib.vertices[3 * idx.vertex_index + 2]; | ||
subMesh.m_vertices.push_back(SM::Vector3(vx, vy, vz)); | ||
|
||
// Normals (if present) | ||
if (idx.normal_index >= 0) | ||
{ | ||
tinyobj::real_t nx = attrib.normals[3 * idx.normal_index + 0]; | ||
tinyobj::real_t ny = attrib.normals[3 * idx.normal_index + 1]; | ||
tinyobj::real_t nz = attrib.normals[3 * idx.normal_index + 2]; | ||
subMesh.m_normals.push_back(SM::Vector3(nx, ny, nz)); | ||
} | ||
|
||
// Texture coordinates (if present) | ||
if (idx.texcoord_index >= 0) | ||
{ | ||
tinyobj::real_t tx = attrib.texcoords[2 * idx.texcoord_index + 0]; | ||
tinyobj::real_t ty = attrib.texcoords[2 * idx.texcoord_index + 1]; | ||
subMesh.m_uvs.push_back(SM::Vector2(tx, ty)); | ||
} | ||
|
||
// Save index (used in drawing the elements) | ||
subMesh.m_indices.push_back(static_cast<u32>(index_offset + v)); | ||
} | ||
index_offset += fv; | ||
} | ||
|
||
// Add the processed submesh to the mesh | ||
SubMeshes.push_back(subMesh); | ||
} | ||
|
||
CGL_LOG(Mesh, Info, "Successfully loaded OBJ file: {}", file); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
#include <Core/Common.h> | ||
#include <Core/Logging/Log.h> | ||
#include <Core/Math/Math.h> | ||
#include <Core/Graphics/Buffer.h> | ||
|
||
namespace CGL::Graphics | ||
{ | ||
CGL_DECLARE_LOG_CATEGORY(Mesh); | ||
|
||
struct MeshData | ||
{ | ||
VertexBuffer VertexBuffer; | ||
IndexBuffer IndexBuffer; | ||
}; | ||
|
||
|
||
class SubMesh | ||
{ | ||
friend class Mesh; | ||
public: | ||
SubMesh() = default; | ||
~SubMesh() = default; | ||
|
||
inline const std::vector<SM::Vector3>& GetVertices() const { return m_vertices; } | ||
inline const std::vector<SM::Vector3>& GetNormals() const { return m_normals; } | ||
inline const std::vector<SM::Vector2>& GetUVs() const { return m_uvs; } | ||
inline const std::vector<u32>& GetIndices() const { return m_indices; } | ||
inline MeshData& GetMeshData() { return m_meshData; } | ||
|
||
private: | ||
MeshData m_meshData; | ||
std::vector<u32> m_indices; | ||
std::vector<SM::Vector3> m_vertices; | ||
std::vector<SM::Vector3> m_normals; | ||
std::vector<SM::Vector2> m_uvs; | ||
}; | ||
|
||
|
||
class Mesh | ||
{ | ||
public: | ||
Mesh() = default; | ||
~Mesh() = default; | ||
|
||
bool LoadOBJ(const std::string& file); | ||
|
||
public: | ||
std::vector<SubMesh> SubMeshes; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
struct PS_INPUT | ||
{ | ||
float4 Position : SV_Position; | ||
float3 Normal : NORMAL; | ||
float3 Color : COLOR0; | ||
}; | ||
|
||
struct PS_OUTPUT | ||
{ | ||
float4 Color : SV_Target0; | ||
}; | ||
|
||
PS_OUTPUT main(PS_INPUT input) | ||
{ | ||
PS_OUTPUT output = (PS_OUTPUT)0; | ||
output.Color = float4(input.Normal, 1.0); | ||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
cbuffer FrameData : register(b0) | ||
{ | ||
matrix WorldMatrix; | ||
matrix ViewMatrix; | ||
matrix ProjMatrix; | ||
}; | ||
|
||
struct VS_INPUT | ||
{ | ||
float4 Position : POSITION; | ||
float3 Normal : NORMAL; | ||
float3 Color : COLOR0; | ||
}; | ||
|
||
struct VS_OUTPUT | ||
{ | ||
float4 Position : SV_Position; | ||
float3 Normal : NORMAL; | ||
float3 Color : COLOR0; | ||
}; | ||
|
||
VS_OUTPUT main(VS_INPUT input) | ||
{ | ||
VS_OUTPUT output = (VS_OUTPUT)0; | ||
|
||
output.Position = input.Position; | ||
output.Position = mul(output.Position, WorldMatrix); | ||
output.Position = mul(output.Position, ViewMatrix); | ||
output.Position = mul(output.Position, ProjMatrix); | ||
|
||
output.Normal = input.Normal; | ||
output.Color = input.Color; | ||
|
||
return output; | ||
} |
Oops, something went wrong.