Started rendering

This commit is contained in:
Antoine Pilote
2023-08-13 01:07:18 -04:00
parent 17486e1666
commit 2f0c5794d2
8 changed files with 81 additions and 26 deletions

View File

@@ -33,22 +33,23 @@ void main()
vec4 totalPosition = vec4(0.0f);
for (int i = 0; i < MAX_BONES_INFLUENCE; i++)
{
if (BonesIDs[i] == -1)
if (BoneIDs[i] == -1)
{
continue;
}
if (BonesIDs[i] >= MAX_BONES)
if (BoneIDs[i] >= MAX_BONES)
{
totalPosition = vec4(VertexPosition, 1.0f);
break;
}
vec4 localPositon = u_FinalBonesMatrice[BoneIDs[i]] * vec4(VertexPosition, 1.0f);
vec4 localPosition = vec4(VertexPosition, 1.0f);
totalPosition += localPosition * Weights[i];
// vec3 localNormal = mat3(u_FinalBonesMatrice[BoneIDs[i]]) * Normal;
}
gl_Position = u_Projection * u_View * u_Model * vec4(totalPosition, 1.0f);
gl_Position = u_Projection * u_View * u_Model * vec4(VertexPosition, 1.0f);
}
#shader fragment

View File

@@ -50,6 +50,13 @@ namespace Nuake {
m_Stride += VertexBufferElement::GetSizeOfType(RendererEnum::UINT) * count;
}
template<>
void Push<int>(unsigned int count)
{
m_Elements.push_back({ RendererEnum::INT, count, false });
m_Stride += VertexBufferElement::GetSizeOfType(RendererEnum::INT) * count;
}
template<>
void Push<unsigned char>(unsigned int count)
{

View File

@@ -40,6 +40,11 @@ namespace Nuake
return m_Indices;
}
std::vector<Bone>& SkinnedMesh::GetBones()
{
return m_Bones;
}
Ref<Material> SkinnedMesh::GetMaterial() inline const
{
return m_Material;
@@ -77,7 +82,7 @@ namespace Nuake
{
m_VertexArray = CreateScope<VertexArray>();
m_VertexArray->Bind();
m_VertexBuffer = CreateScope<VertexBuffer>(m_Vertices.data(), m_Vertices.size() * sizeof(Vertex));
m_VertexBuffer = CreateScope<VertexBuffer>(m_Vertices.data(), m_Vertices.size() * sizeof(SkinnedVertex));
m_ElementBuffer = CreateScope<VertexBuffer>(m_Indices.data(), m_Indices.size() * sizeof(unsigned int), RendererEnum::ELEMENT_ARRAY_BUFFER);
VertexBufferLayout bufferLayout = VertexBufferLayout();
@@ -86,8 +91,8 @@ namespace Nuake
bufferLayout.Push<float>(3); // Normal
bufferLayout.Push<float>(3); // Tangent
bufferLayout.Push<float>(3); // Bitangent
bufferLayout.Push<unsigned int>(4); // BoneIds
bufferLayout.Push<float>(4); // Weights
bufferLayout.Push<int>(1); // BoneIds
bufferLayout.Push<float>(1); // Weights
m_VertexArray->AddBuffer(*m_VertexBuffer, bufferLayout);
m_VertexArray->Unbind();

View File

@@ -70,7 +70,14 @@ namespace Nuake {
void OGLRendererAPI::VertexAttribPointer(const unsigned int index, const int size, const RendererEnum type, bool normalized, int stride, const void* pointer)
{
GLenum glType = GetType(type);
glVertexAttribPointer(index, size, glType, normalized, stride, pointer);
if (glType == GL_INT)
{
glVertexAttribIPointer(index, size, glType, stride, pointer);
}
else
{
glVertexAttribPointer(index, size, glType, normalized, stride, pointer);
}
}
void OGLRendererAPI::DrawMultiElements(const RendererEnum mode, const int count, const RendererEnum type, const void* const* indices, unsigned int drawCount)

View File

@@ -360,6 +360,9 @@ namespace Nuake
gBufferSkinnedMeshShader->SetUniformMat4f("u_View", mView);
// Skinned Models
const uint32_t entityIdUniformLocation = gBufferSkinnedMeshShader->FindUniformLocation("u_EntityID");
const uint32_t modelMatrixUniformLocation = gBufferSkinnedMeshShader->FindUniformLocation("u_Model");
auto skinnedModelView = scene.m_Registry.view<TransformComponent, SkinnedModelComponent, VisibilityComponent>();
for (auto e : skinnedModelView)
{
@@ -369,13 +372,19 @@ namespace Nuake
{
for (auto& m : mesh.ModelResource->GetMeshes())
{
const uint32_t entityIdUniformLocation = gBufferSkinnedMeshShader->FindUniformLocation("u_EntityID");
const uint32_t modelMatrixUniformLocation = gBufferSkinnedMeshShader->FindUniformLocation("u_Model");
m->GetMaterial()->Bind(gBufferSkinnedMeshShader);
uint32_t boneId = 0;
for (auto& b : m->GetBones())
{
const std::string boneMatrixUniformName = "u_FinalBonesMatrice[" + std::to_string(boneId) + "]";
gBufferSkinnedMeshShader->SetUniformMat4f(boneMatrixUniformName, b.Offset);
boneId++;
}
gBufferSkinnedMeshShader->SetUniformMat4f(modelMatrixUniformLocation, transform.GetGlobalTransform());
gBufferSkinnedMeshShader->SetUniform1i(entityIdUniformLocation, (uint32_t)e + 1);
m->Draw(gBufferSkinnedMeshShader, false);
m->Draw(gBufferSkinnedMeshShader, true);
}
}
}

View File

@@ -12,7 +12,7 @@ namespace Nuake
Vector3 bitangent;
};
const uint32_t MAX_BONE_INFLUENCE = 4;
const uint32_t MAX_BONE_INFLUENCE = 1;
struct SkinnedVertex
{
Vector3 position;
@@ -22,7 +22,6 @@ namespace Nuake
Vector3 bitangent;
int boneIDs[MAX_BONE_INFLUENCE];
//weights from each bone
float weights[MAX_BONE_INFLUENCE];
};

View File

@@ -116,10 +116,10 @@ namespace Nuake
Ref<SkinnedMesh> ModelLoader::ProcessSkinnedMesh(aiMesh* node, const aiScene* scene)
{
auto vertices = ProcessSkinnedVertices(node);
auto indices = ProcessIndices(node);
auto material = ProcessMaterials(scene, node);
auto bones = std::vector<Bone>();
auto& vertices = ProcessSkinnedVertices(node);
auto& indices = ProcessIndices(node);
auto& material = ProcessMaterials(scene, node);
auto& bones = std::vector<Bone>();
if (node->HasBones())
{
for (uint32_t i = 0; i < node->mNumBones; i++)
@@ -128,15 +128,28 @@ namespace Nuake
const std::string& boneName = bone->mName.C_Str();
const auto& boneMatrix = bone->mOffsetMatrix;
for (uint32_t j = 0; j < bone->mNumWeights; j++)
Bone newBone;
newBone.Name = boneName;
newBone.Offset = ConvertMatrixToGLMFormat(bone->mOffsetMatrix);
const uint32_t numWeight = bone->mNumWeights;
newBone.VertexWeights.reserve(numWeight);
for (uint32_t j = 0; j < numWeight; j++)
{
aiVertexWeight vertexWeight = bone->mWeights[j];
const uint32_t vertexWeightVertexId = vertexWeight.mVertexId;
vertices[vertexWeightVertexId].boneIDs[j] = i;
vertices[vertexWeightVertexId].weights[j] = vertexWeight.mWeight;
BoneVertexWeight boneVertexWeight
{
vertexWeightVertexId,
vertexWeight.mWeight
};
const float weigth = vertexWeight.mWeight;
uint32_t vertexId = vertexWeight.mVertexId;
vertices[vertexId].boneIDs[j] = i;
newBone.VertexWeights.push_back(std::move(boneVertexWeight));
}
bones.push_back(std::move(newBone));
}
}
else
@@ -145,8 +158,6 @@ namespace Nuake
}
// Fill in the bones in the vertices
Ref<SkinnedMesh> mesh = CreateRef<SkinnedMesh>();
mesh->AddSurface(vertices, indices, bones);
mesh->SetMaterial(material);
@@ -245,7 +256,12 @@ namespace Nuake
for (uint32_t i = 0; i < mesh->mNumVertices; i++)
{
SkinnedVertex vertex;
for (int w = 0; w < MAX_BONE_INFLUENCE; w++)
{
vertex.boneIDs[w] = -1;
vertex.weights[w] = 0.0f;
}
Vector3 current;
// Position

View File

@@ -45,5 +45,16 @@ namespace Nuake
void ProcessSkinnedNode(aiNode* node, const aiScene* scene);
Ref<SkinnedMesh> ProcessSkinnedMesh(aiMesh* node, const aiScene* scene);
std::vector<SkinnedVertex> ProcessSkinnedVertices(aiMesh* mesh);
static inline Matrix4 ConvertMatrixToGLMFormat(const aiMatrix4x4& from)
{
Matrix4 to;
//the a,b,c,d in assimp is the row ; the 1,2,3,4 is the column
to[0][0] = from.a1; to[1][0] = from.a2; to[2][0] = from.a3; to[3][0] = from.a4;
to[0][1] = from.b1; to[1][1] = from.b2; to[2][1] = from.b3; to[3][1] = from.b4;
to[0][2] = from.c1; to[1][2] = from.c2; to[2][2] = from.c3; to[3][2] = from.c4;
to[0][3] = from.d1; to[1][3] = from.d2; to[2][3] = from.d3; to[3][3] = from.d4;
return to;
}
};
}