Refactored SHPrimitiveGenerator and added Sphere generation support

This commit is contained in:
Kah Wei 2022-10-26 16:05:18 +08:00
parent 43ea33cabf
commit 632df80d06
2 changed files with 182 additions and 48 deletions

View File

@ -3,7 +3,7 @@
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Aug 30, 2022
\brief Contains definitions for all of the functions of the classes that deal
\brief Contains definitions for all of the functions of the classes that deal
with storage and management of vertex and index buffers of meshes.
Copyright (C) 2022 DigiPen Institute of Technology.
@ -18,50 +18,59 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Static Member Definitions */
/*-----------------------------------------------------------------------------------*/
SHMeshData SHPrimitiveGenerator::cubeMesh;
SHMeshData SHPrimitiveGenerator::sphereMesh;
/*-----------------------------------------------------------------------------------*/
/* Primitive Generation Functions */
/*-----------------------------------------------------------------------------------*/
SHMeshData SHPrimitiveGenerator::Cube() noexcept
{
SHMeshData mesh;
mesh.VertexPositions =
mesh.VertexPositions =
{
// front
SHVec3(-0.5f, -0.5f, 0.5f),
SHVec3( 0.5f, -0.5f, 0.5f),
SHVec3( 0.5f, 0.5f, 0.5f),
SHVec3(-0.5f, 0.5f, 0.5f),
SHVec3(-0.5f, -0.5f, 0.5f),
SHVec3(0.5f, -0.5f, 0.5f),
SHVec3(0.5f, 0.5f, 0.5f),
SHVec3(-0.5f, 0.5f, 0.5f),
// back
SHVec3(-0.5f, -0.5f, -0.5f),
SHVec3(-0.5f, 0.5f, -0.5f),
SHVec3( 0.5f, 0.5f, -0.5f),
SHVec3( 0.5f, -0.5f, -0.5f),
SHVec3(-0.5f, -0.5f, -0.5f),
SHVec3(-0.5f, 0.5f, -0.5f),
SHVec3(0.5f, 0.5f, -0.5f),
SHVec3(0.5f, -0.5f, -0.5f),
// top
// top
SHVec3(-0.5f, 0.5f, -0.5f),
SHVec3(-0.5f, 0.5f, 0.5f),
SHVec3( 0.5f, 0.5f, 0.5f),
SHVec3( 0.5f, 0.5f, -0.5f),
SHVec3(0.5f, 0.5f, 0.5f),
SHVec3(0.5f, 0.5f, -0.5f),
// bottom
SHVec3(-0.5f, -0.5f, -0.5f),
SHVec3( 0.5f, -0.5f, -0.5f),
SHVec3( 0.5f, -0.5f, 0.5f),
SHVec3(-0.5f, -0.5f, 0.5f),
SHVec3(-0.5f, -0.5f, -0.5f),
SHVec3(0.5f, -0.5f, -0.5f),
SHVec3(0.5f, -0.5f, 0.5f),
SHVec3(-0.5f, -0.5f, 0.5f),
// right
SHVec3(0.5f, -0.5f, -0.5f),
SHVec3(0.5f, 0.5f, -0.5f),
SHVec3(0.5f, 0.5f, 0.5f),
SHVec3(0.5f, -0.5f, 0.5f),
SHVec3(0.5f, -0.5f, -0.5f),
SHVec3(0.5f, 0.5f, -0.5f),
SHVec3(0.5f, 0.5f, 0.5f),
SHVec3(0.5f, -0.5f, 0.5f),
// left
SHVec3(-0.5f, -0.5f, -0.5f),
SHVec3(-0.5f, -0.5f, 0.5f),
SHVec3(-0.5f, 0.5f, 0.5f),
SHVec3(-0.5f, 0.5f, -0.5f)
SHVec3(-0.5f, -0.5f, -0.5f),
SHVec3(-0.5f, -0.5f, 0.5f),
SHVec3(-0.5f, 0.5f, 0.5f),
SHVec3(-0.5f, 0.5f, -0.5f)
};
mesh.VertexTexCoords =
mesh.VertexTexCoords =
{
SHVec2(0.0f, 1.0f),
SHVec2(1.0f, 1.0f),
@ -99,7 +108,7 @@ namespace SHADE
SHVec2(0.0f, 0.0f)
};
mesh.VertexTangents =
mesh.VertexTangents =
{
// front
SHVec3(1.0f, 0.0f, 0.0f),
@ -118,7 +127,7 @@ namespace SHADE
SHVec3(1.0f, 0.0f, 0.0f),
SHVec3(1.0f, 0.0f, 0.0f),
SHVec3(1.0f, 0.0f, 0.0f),
// bottom
SHVec3(1.0f, 0.0f, 0.0f),
SHVec3(1.0f, 0.0f, 0.0f),
@ -193,10 +202,93 @@ namespace SHADE
Handle<SHMesh> SHPrimitiveGenerator::Cube(SHMeshLibrary& meshLibrary) noexcept
{
static SHMeshData meshData = Cube();
if (cubeMesh.VertexPositions.empty())
cubeMesh = Cube();
return addMeshDataTo(cubeMesh, meshLibrary);
}
Handle<SHADE::SHMesh> SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept
{
if (cubeMesh.VertexPositions.empty())
cubeMesh = Cube();
return addMeshDataTo(cubeMesh, gfxSystem);
}
SHADE::SHMeshData SHPrimitiveGenerator::Sphere() noexcept
{
SHMeshData meshData;
const int LAT_SLICES = 8;
const int LONG_SLICES = 8;
float radius = 1;
for (int latNum = 0; latNum <= LAT_SLICES; ++latNum)
{
float theta = static_cast<float>(latNum * std::numbers::pi / LAT_SLICES);
float sinTheta = sin(theta);
float cosTheta = cos(theta);
for (int longNum = 0; longNum <= LONG_SLICES; ++longNum)
{
float phi = static_cast<float>(longNum * 2 * std::numbers::pi / LONG_SLICES);
float sinPhi = sin(phi);
float cosPhi = cos(phi);
const SHVec3 NORMAL = SHVec3(cosPhi * sinTheta, cosTheta, sinPhi * sinTheta);
meshData.VertexNormals.emplace_back(NORMAL);
meshData.VertexTangents.emplace_back(/* TODO */);
meshData.VertexTexCoords.emplace_back
(
1.0f - (longNum / static_cast<float>(LONG_SLICES)),
1.0f - (latNum / static_cast<float>(LAT_SLICES))
);
meshData.VertexPositions.emplace_back(radius * NORMAL.x, radius * NORMAL.y, radius * NORMAL.z);
}
}
for (int latNumber{}; latNumber < LAT_SLICES; latNumber++)
{
for (int longNumber{}; longNumber < LONG_SLICES; longNumber++)
{
const auto FIRST = (latNumber * (LONG_SLICES + 1)) + longNumber;
const auto SECOND = (FIRST + LONG_SLICES + 1);
meshData.Indices.emplace_back(FIRST);
meshData.Indices.emplace_back(SECOND);
meshData.Indices.emplace_back(FIRST + 1);
meshData.Indices.emplace_back(SECOND);
meshData.Indices.emplace_back(SECOND + 1);
meshData.Indices.emplace_back(FIRST + 1);
}
}
return meshData;
}
SHADE::Handle<SHADE::SHMesh> SHPrimitiveGenerator::Sphere(SHMeshLibrary& meshLibrary) noexcept
{
if (sphereMesh.VertexPositions.empty())
sphereMesh = Sphere();
return addMeshDataTo(sphereMesh, meshLibrary);
}
SHADE::Handle<SHADE::SHMesh> SHPrimitiveGenerator::Sphere(SHGraphicsSystem& gfxSystem) noexcept
{
if (sphereMesh.VertexPositions.empty())
sphereMesh = Sphere();
return addMeshDataTo(sphereMesh, gfxSystem);
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
SHADE::Handle<SHADE::SHMesh> SHPrimitiveGenerator::addMeshDataTo(const SHMeshData& meshData, SHMeshLibrary& meshLibrary) noexcept
{
return meshLibrary.AddMesh
(
static_cast<uint32_t>(meshData.VertexPositions.size()),
static_cast<uint32_t>(meshData.VertexPositions.size()),
meshData.VertexPositions.data(),
meshData.VertexTexCoords.data(),
meshData.VertexTangents.data(),
@ -206,17 +298,16 @@ namespace SHADE
);
}
Handle<SHADE::SHMesh> SHPrimitiveGenerator::Cube(SHGraphicsSystem& gfxSystem) noexcept
SHADE::Handle<SHADE::SHMesh> SHPrimitiveGenerator::addMeshDataTo(const SHMeshData& meshData, SHGraphicsSystem& gfxSystem) noexcept
{
static SHMeshData meshData = Cube();
return gfxSystem.AddMesh
(
static_cast<uint32_t>(meshData.VertexPositions.size()),
static_cast<uint32_t>(meshData.VertexPositions.size()),
meshData.VertexPositions.data(),
meshData.VertexTexCoords.data(),
meshData.VertexTangents.data(),
meshData.VertexNormals.data(),
static_cast<uint32_t>(meshData.Indices.size()),
static_cast<uint32_t>(meshData.Indices.size()),
meshData.Indices.data()
);
}

View File

@ -4,9 +4,9 @@
\par email: kahwei.tng\@digipen.edu
\date Sep 18, 2022
\brief Contains the static class definition of SHPrimitiveGenerator.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
@ -29,12 +29,12 @@ namespace SHADE
/*************************************************************************************/
/*!
\brief
Static class that contains functions for generating 3D primitives.
Static class that contains functions for generating 3D primitives.
*/
/*************************************************************************************/
class SH_API SHPrimitiveGenerator
{
public:
public:
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
@ -42,7 +42,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Primitive Generation Functions */
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
/***********************************************************************************/
/*!
\brief
@ -52,20 +52,20 @@ namespace SHADE
SHMeshData object containing vertex data for the cube.
*/
/***********************************************************************************/
[[nodiscard]] static SHMeshData Cube() noexcept;
[[nodiscard]] static SHMeshData Cube() noexcept;
/***********************************************************************************/
/*!
\brief
Produces a cube and constructs a SHMesh using the SHMeshLibrary provided.
\param meshLibrary
Reference to the SHMeshLibrary to procude and store a cube mesh in.
Reference to the SHMeshLibrary to produce and store a cube mesh in.
\return
SHMesh object that points to the generated cube mesh in the SHMeshLibrary.
*/
/***********************************************************************************/
[[nodiscard]] static Handle<SHMesh> Cube(SHMeshLibrary& meshLibrary) noexcept;
[[nodiscard]] static Handle<SHMesh> Cube(SHMeshLibrary& meshLibrary) noexcept;
/***********************************************************************************/
/*!
\brief
@ -78,12 +78,55 @@ namespace SHADE
SHMesh object that points to the generated cube mesh in the SHGraphicsSystem.
*/
/***********************************************************************************/
[[nodiscard]] static Handle<SHMesh> Cube(SHGraphicsSystem& gfxSystem) noexcept;
[[nodiscard]] static Handle<SHMesh> Cube(SHGraphicsSystem& gfxSystem) noexcept;
/***********************************************************************************/
/*!
\brief
Produces a sphere and stores the data in a SHMeshData object.
private:
\return
SHMeshData object containing vertex data for the sphere.
*/
/***********************************************************************************/
[[nodiscard]] static SHMeshData Sphere() noexcept;
/***********************************************************************************/
/*!
\brief
Produces a sphere and constructs a SHMesh using the SHMeshLibrary provided.
\param meshLibrary
Reference to the SHMeshLibrary to produce and store a sphere mesh in.
\return
SHMesh object that points to the generated sphere mesh in the SHMeshLibrary.
*/
/***********************************************************************************/
[[nodiscard]] static Handle<SHMesh> Sphere(SHMeshLibrary& meshLibrary) noexcept;
/***********************************************************************************/
/*!
\brief
Produces a sphere and constructs a SHMesh using the SHGraphicsSystem provided.
\param gfxSystem
Reference to the SHGraphicsSystem to produce and store a sphere mesh in.
\return
SHMesh object that points to the generated sphere mesh in the SHGraphicsSystem.
*/
/***********************************************************************************/
[[nodiscard]] static Handle<SHMesh> Sphere(SHGraphicsSystem& gfxSystem) noexcept;
private:
/*---------------------------------------------------------------------------------*/
/* Helper Functions */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] static SHMeshData genCubeData() noexcept;
static Handle<SHMesh> addMeshDataTo(const SHMeshData& meshData, SHMeshLibrary& meshLibrary) noexcept;
static Handle<SHMesh> addMeshDataTo(const SHMeshData& meshData, SHGraphicsSystem& gfxSystem) noexcept;
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
static SHMeshData cubeMesh;
static SHMeshData sphereMesh;
};
}