CDrawUtil: Make use of std::array

While we're at it, we can make several arrays constexpr.
This commit is contained in:
Lioncash 2020-06-16 18:04:07 -04:00
parent 43596280c5
commit 4d34589816
4 changed files with 38 additions and 73 deletions

View File

@ -18,7 +18,7 @@ void CIndexBuffer::AddIndex(uint16 index)
mIndices.push_back(index); mIndices.push_back(index);
} }
void CIndexBuffer::AddIndices(uint16 *indices, size_t count) void CIndexBuffer::AddIndices(const uint16 *indices, size_t count)
{ {
Reserve(count); Reserve(count);
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)

View File

@ -17,7 +17,7 @@ public:
explicit CIndexBuffer(GLenum type); explicit CIndexBuffer(GLenum type);
~CIndexBuffer(); ~CIndexBuffer();
void AddIndex(uint16 index); void AddIndex(uint16 index);
void AddIndices(uint16 *indices, size_t count); void AddIndices(const uint16 *indices, size_t count);
void Reserve(size_t size); void Reserve(size_t size);
void Clear(); void Clear();
void Buffer(); void Buffer();

View File

@ -3,42 +3,6 @@
#include "Core/GameProject/CResourceStore.h" #include "Core/GameProject/CResourceStore.h"
#include <Common/Log.h> #include <Common/Log.h>
#include <Common/Math/CTransform4f.h> #include <Common/Math/CTransform4f.h>
#include <iostream>
// ************ MEMBER INITIALIZATION ************
std::optional<CVertexBuffer> CDrawUtil::mGridVertices;
CIndexBuffer CDrawUtil::mGridIndices;
std::optional<CDynamicVertexBuffer> CDrawUtil::mSquareVertices;
CIndexBuffer CDrawUtil::mSquareIndices;
std::optional<CDynamicVertexBuffer> CDrawUtil::mLineVertices;
CIndexBuffer CDrawUtil::mLineIndices;
TResPtr<CModel> CDrawUtil::mpCubeModel;
std::optional<CVertexBuffer> CDrawUtil::mWireCubeVertices;
CIndexBuffer CDrawUtil::mWireCubeIndices;
TResPtr<CModel> CDrawUtil::mpSphereModel;
TResPtr<CModel> CDrawUtil::mpDoubleSidedSphereModel;
TResPtr<CModel> CDrawUtil::mpWireSphereModel;
CShader *CDrawUtil::mpColorShader;
CShader *CDrawUtil::mpColorShaderLighting;
CShader *CDrawUtil::mpBillboardShader;
CShader *CDrawUtil::mpLightBillboardShader;
CShader *CDrawUtil::mpTextureShader;
CShader *CDrawUtil::mpCollisionShader;
CShader *CDrawUtil::mpTextShader;
TResPtr<CTexture> CDrawUtil::mpCheckerTexture;
TResPtr<CTexture> CDrawUtil::mpLightTextures[4];
TResPtr<CTexture> CDrawUtil::mpLightMasks[4];
bool CDrawUtil::mDrawUtilInitialized = false;
// ************ PUBLIC ************ // ************ PUBLIC ************
void CDrawUtil::DrawGrid(CColor LineColor, CColor BoldLineColor) void CDrawUtil::DrawGrid(CColor LineColor, CColor BoldLineColor)
@ -366,13 +330,13 @@ void CDrawUtil::LoadCheckerboardTexture(uint32 GLTextureUnit)
CTexture* CDrawUtil::GetLightTexture(ELightType Type) CTexture* CDrawUtil::GetLightTexture(ELightType Type)
{ {
Init(); Init();
return mpLightTextures[(int) Type]; return mpLightTextures[static_cast<size_t>(Type)];
} }
CTexture* CDrawUtil::GetLightMask(ELightType Type) CTexture* CDrawUtil::GetLightMask(ELightType Type)
{ {
Init(); Init();
return mpLightMasks[(int) Type]; return mpLightMasks[static_cast<size_t>(Type)];
} }
CModel* CDrawUtil::GetCubeModel() CModel* CDrawUtil::GetCubeModel()
@ -453,37 +417,37 @@ void CDrawUtil::InitSquare()
EVertexAttribute::Tex4 | EVertexAttribute::Tex4 |
EVertexAttribute::Tex5 | EVertexAttribute::Tex5 |
EVertexAttribute::Tex6 | EVertexAttribute::Tex6 |
EVertexAttribute::Tex7 ); EVertexAttribute::Tex7);
mSquareVertices->SetVertexCount(4); mSquareVertices->SetVertexCount(4);
CVector3f SquareVertices[] = { static constexpr std::array SquareVertices{
CVector3f(-1.f, 1.f, 0.f), CVector3f(-1.f, 1.f, 0.f),
CVector3f( 1.f, 1.f, 0.f), CVector3f( 1.f, 1.f, 0.f),
CVector3f( 1.f, -1.f, 0.f), CVector3f( 1.f, -1.f, 0.f),
CVector3f(-1.f, -1.f, 0.f) CVector3f(-1.f, -1.f, 0.f)
}; };
CVector3f SquareNormals[] = { static constexpr std::array SquareNormals{
CVector3f(0.f, 0.f, 1.f), CVector3f(0.f, 0.f, 1.f),
CVector3f(0.f, 0.f, 1.f), CVector3f(0.f, 0.f, 1.f),
CVector3f(0.f, 0.f, 1.f), CVector3f(0.f, 0.f, 1.f),
CVector3f(0.f, 0.f, 1.f) CVector3f(0.f, 0.f, 1.f)
}; };
CVector2f SquareTexCoords[] = { static constexpr std::array SquareTexCoords{
CVector2f(0.f, 1.f), CVector2f(0.f, 1.f),
CVector2f(1.f, 1.f), CVector2f(1.f, 1.f),
CVector2f(1.f, 0.f), CVector2f(1.f, 0.f),
CVector2f(0.f, 0.f) CVector2f(0.f, 0.f)
}; };
mSquareVertices->BufferAttrib(EVertexAttribute::Position, SquareVertices); mSquareVertices->BufferAttrib(EVertexAttribute::Position, SquareVertices.data());
mSquareVertices->BufferAttrib(EVertexAttribute::Normal, SquareNormals); mSquareVertices->BufferAttrib(EVertexAttribute::Normal, SquareNormals.data());
for (uint32 iTex = 0; iTex < 8; iTex++) for (uint32 iTex = 0; iTex < 8; iTex++)
{ {
EVertexAttribute Attrib = (EVertexAttribute) (EVertexAttribute::Tex0 << iTex); const auto Attrib = static_cast<EVertexAttribute>(EVertexAttribute::Tex0 << iTex);
mSquareVertices->BufferAttrib(Attrib, SquareTexCoords); mSquareVertices->BufferAttrib(Attrib, SquareTexCoords.data());
} }
mSquareIndices.Reserve(4); mSquareIndices.Reserve(4);
@ -528,7 +492,7 @@ void CDrawUtil::InitWireCube()
mWireCubeVertices->AddVertex(CVector3f( 0.5f, 0.5f, 0.5f)); mWireCubeVertices->AddVertex(CVector3f( 0.5f, 0.5f, 0.5f));
mWireCubeVertices->AddVertex(CVector3f(-0.5f, 0.5f, 0.5f)); mWireCubeVertices->AddVertex(CVector3f(-0.5f, 0.5f, 0.5f));
uint16 Indices[] = { static constexpr std::array<uint16, 24> Indices{
0, 1, 0, 1,
1, 2, 1, 2,
2, 3, 2, 3,
@ -542,7 +506,7 @@ void CDrawUtil::InitWireCube()
2, 6, 2, 6,
3, 5 3, 5
}; };
mWireCubeIndices.AddIndices(Indices, sizeof(Indices) / sizeof(uint16)); mWireCubeIndices.AddIndices(Indices.data(), Indices.size());
mWireCubeIndices.SetPrimitiveType(GL_LINES); mWireCubeIndices.SetPrimitiveType(GL_LINES);
} }

View File

@ -7,6 +7,7 @@
#include "Core/Resource/Model/CModel.h" #include "Core/Resource/Model/CModel.h"
#include "Core/Resource/CLight.h" #include "Core/Resource/CLight.h"
#include <array>
#include <optional> #include <optional>
/** /**
@ -19,48 +20,48 @@
class CDrawUtil class CDrawUtil
{ {
// 7x7 Grid // 7x7 Grid
static std::optional<CVertexBuffer> mGridVertices; static inline std::optional<CVertexBuffer> mGridVertices;
static CIndexBuffer mGridIndices; static inline CIndexBuffer mGridIndices;
// Square // Square
static std::optional<CDynamicVertexBuffer> mSquareVertices; static inline std::optional<CDynamicVertexBuffer> mSquareVertices;
static CIndexBuffer mSquareIndices; static inline CIndexBuffer mSquareIndices;
// Line // Line
static std::optional<CDynamicVertexBuffer> mLineVertices; static inline std::optional<CDynamicVertexBuffer> mLineVertices;
static CIndexBuffer mLineIndices; static inline CIndexBuffer mLineIndices;
// Cube // Cube
static TResPtr<CModel> mpCubeModel; static inline TResPtr<CModel> mpCubeModel;
// Wire Cube // Wire Cube
static std::optional<CVertexBuffer> mWireCubeVertices; static inline std::optional<CVertexBuffer> mWireCubeVertices;
static CIndexBuffer mWireCubeIndices; static inline CIndexBuffer mWireCubeIndices;
// Sphere // Sphere
static TResPtr<CModel> mpSphereModel; static inline TResPtr<CModel> mpSphereModel;
static TResPtr<CModel> mpDoubleSidedSphereModel; static inline TResPtr<CModel> mpDoubleSidedSphereModel;
// Wire Sphere // Wire Sphere
static TResPtr<CModel> mpWireSphereModel; static inline TResPtr<CModel> mpWireSphereModel;
// Shaders // Shaders
static CShader *mpColorShader; static inline CShader *mpColorShader = nullptr;
static CShader *mpColorShaderLighting; static inline CShader *mpColorShaderLighting = nullptr;
static CShader *mpBillboardShader; static inline CShader *mpBillboardShader = nullptr;
static CShader *mpLightBillboardShader; static inline CShader *mpLightBillboardShader = nullptr;
static CShader *mpTextureShader; static inline CShader *mpTextureShader = nullptr;
static CShader *mpCollisionShader; static inline CShader *mpCollisionShader = nullptr;
static CShader *mpTextShader; static inline CShader *mpTextShader = nullptr;
// Textures // Textures
static TResPtr<CTexture> mpCheckerTexture; static inline TResPtr<CTexture> mpCheckerTexture;
static TResPtr<CTexture> mpLightTextures[4]; static inline std::array<TResPtr<CTexture>, 4> mpLightTextures;
static TResPtr<CTexture> mpLightMasks[4]; static inline std::array<TResPtr<CTexture>, 4> mpLightMasks;
// Have all the above members been initialized? // Have all the above members been initialized?
static bool mDrawUtilInitialized; static inline bool mDrawUtilInitialized = false;
public: public:
static void DrawGrid(CColor LineColor, CColor BoldLineColor); static void DrawGrid(CColor LineColor, CColor BoldLineColor);