From 4ef4a82959146569cfb362176075e7e8e142fb24 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sun, 24 Apr 2022 15:44:49 +0100
Subject: [PATCH] clean up more types in procedural

---
 fggl/data/procedural.cpp    | 79 ++++++++++++++++++-------------------
 include/fggl/data/model.hpp |  2 +-
 2 files changed, 39 insertions(+), 42 deletions(-)

diff --git a/fggl/data/procedural.cpp b/fggl/data/procedural.cpp
index 9917560..ec408f2 100644
--- a/fggl/data/procedural.cpp
+++ b/fggl/data/procedural.cpp
@@ -19,7 +19,7 @@ static glm::vec3 calcSurfaceNormal(glm::vec3 vert1, glm::vec3 vert2, glm::vec3 v
 	return glm::normalize( glm::cross( edge1, edge2 ) );
 }
 
-static void computeNormalsDirect( fggl::data::Mesh& mesh, const int* colIdx, int nPoints) {
+static void computeNormalsDirect( fggl::data::Mesh& mesh, const fggl::data::Mesh::IndexType * colIdx, int nPoints) {
 
 	// we're assuming all the normals are zero...
 	for ( int i=0; i<nPoints; i++ ) {
@@ -41,12 +41,12 @@ static void computeNormalsDirect( fggl::data::Mesh& mesh, const int* colIdx, int
 }
 
 static void compute_normals(fggl::data::Mesh& mesh,
-							const std::vector<int>& idxList, // source index
-							const std::vector<unsigned int>& idxMapping // source-to-mesh lookup
+							const std::vector<Mesh::IndexType>& idxList, // source index
+							const std::vector<Mesh::IndexType>& idxMapping // source-to-mesh lookup
 							) {
 
 	// clear the normals, so the summation below works correctly
-	for (unsigned int vertexIndex : idxMapping) {
+	for (auto vertexIndex : idxMapping) {
 		auto& vertex = mesh.vertex( vertexIndex );
 		vertex.normal = ILLEGAL_NORMAL;
 	}
@@ -76,9 +76,9 @@ static void compute_normals(fggl::data::Mesh& mesh,
 }
 
 static void populateMesh(fggl::data::Mesh& mesh, const fggl::math::mat4 transform,
-						 const int nIdx, const fggl::math::vec3* pos, const int* idx ) {
+						 const int nIdx, const fggl::math::vec3* pos, const Mesh::IndexType* idx ) {
 
-	int* colIdx = new int[nIdx];
+	auto* colIdx = new fggl::data::Mesh::IndexType[nIdx];
 
 	// generate mesh
 	for (int i=0; i<nIdx; i++) {
@@ -95,7 +95,7 @@ static void populateMesh(fggl::data::Mesh& mesh, const fggl::math::mat4 transfor
 static void populateMesh(fggl::data::Mesh& mesh,
 						 const fggl::math::mat4 transform,
 						 const std::vector<fggl::math::vec3>& posList,
-						 const std::vector<int>& idxList) {
+						 const std::vector<fggl::data::Mesh::IndexType>& idxList) {
 
 	// tmp store the resulting mesh indexes (incase the mesh has multiple primatives)
 	std::vector<unsigned int> colIdx;
@@ -117,7 +117,7 @@ static void populateMesh(fggl::data::Mesh& mesh,
 
 namespace fggl::data {
 
-	static void quads2Tris(std::vector<int>& indexList, int stacks, int slices) {
+	static void quads2Tris(std::vector<Mesh::IndexType>& indexList, int stacks, int slices) {
 		const auto HORZ_SIZE = slices + 1;
 		for (int vertical = 0; vertical < stacks; vertical++) {
 			for (int horz = 0; horz < slices; horz++) {
@@ -159,7 +159,7 @@ namespace fggl::data {
 		}
 
 		// combine the vertices into triangles
-		std::vector<int> indexList;
+		std::vector<Mesh::IndexType> indexList;
 		quads2Tris(indexList, stacks, slices);
 
 		populateMesh(mesh, offset, positions, indexList);
@@ -244,36 +244,33 @@ fggl::data::Mesh fggl::data::make_quad_xz() {
 
 
 fggl::data::Mesh fggl::data::make_cube(fggl::data::Mesh& mesh, const fggl::math::mat4& transform) {
-
 	// done as two loops, top loop is 0,1,2,3, bottom loop is 4,5,6,7
-	constexpr fggl::math::vec3 pos[] {
-		{-0.5,  0.5, -0.5}, // 0 TOP LOOP
-		{ 0.5,  0.5, -0.5}, // 1
-		{ 0.5,  0.5,  0.5}, // 2
-		{-0.5,  0.5,  0.5}, // 3
-		{-0.5, -0.5, -0.5}, // 4 BOTTOM LOOP
-		{ 0.5, -0.5, -0.5}, // 5
-		{ 0.5, -0.5,  0.5}, // 6
-		{-0.5, -0.5,  0.5}  // 7
-	};
-
-	constexpr int idx[] {
-		0, 3, 1, // top
-		3, 2, 1,
-		0, 1, 4, // side 0 - 1
-		5, 4, 1,
-		1, 2, 5, // side 1 - 2
-		2, 6, 5,
-		3, 7, 2, // side 2 - 3
-		2, 7, 6,
-		0, 4, 3, // side 3 - 0
-		4, 7, 3,
-		4, 5, 7, // bottom
-		7, 5, 6,
-	};
-
-	int nIdx = sizeof(idx) / sizeof(int);
-	populateMesh(mesh, transform, nIdx, pos, idx);
+	constexpr std::array<fggl::math::vec3, 8> pos {{
+													   {-0.5, 0.5, -0.5}, // 0 TOP LOOP
+													   {0.5, 0.5, -0.5}, // 1
+													   {0.5, 0.5, 0.5}, // 2
+													   {-0.5, 0.5, 0.5}, // 3
+													   {-0.5, -0.5, -0.5}, // 4 BOTTOM LOOP
+													   {0.5, -0.5, -0.5}, // 5
+													   {0.5, -0.5, 0.5}, // 6
+													   {-0.5, -0.5, 0.5}  // 7
+												   }};
+
+	constexpr std::array<Mesh::IndexType, 36> idx {{
+														 0, 3, 1, // top
+														 3, 2, 1,
+														 0, 1, 4, // side 0 - 1
+														 5, 4, 1,
+														 1, 2, 5, // side 1 - 2
+														 2, 6, 5,
+														 3, 7, 2, // side 2 - 3
+														 2, 7, 6,
+														 0, 4, 3, // side 3 - 0
+														 4, 7, 3,
+														 4, 5, 7, // bottom
+														 7, 5, 6,
+													 }};
+	populateMesh(mesh, transform, idx.size(), pos.data(), idx.data());
 	return mesh;
 }
 
@@ -292,7 +289,7 @@ fggl::data::Mesh fggl::data::make_slope(fggl::data::Mesh& mesh, const fggl::math
 		{-0.5, -0.5,  0.5}  // 7
 	};
 
-	constexpr int idx[] {
+	constexpr Mesh::IndexType idx[] {
 		0, 7, 1, // ramp
 		7, 6, 1,
 		0, 1, 4, // side 0 - 1
@@ -324,7 +321,7 @@ fggl::data::Mesh fggl::data::make_ditch(fggl::data::Mesh& mesh, const fggl::math
 		{-0.5, -0.5,  0.5}  // 7
 	};
 
-	constexpr int idx[] {
+	constexpr Mesh::IndexType idx[] {
 		0, 3, 1, // top
 		3, 6, 1,
 		0, 1, 4, // side 0 - 1
@@ -356,7 +353,7 @@ fggl::data::Mesh fggl::data::make_point(fggl::data::Mesh& mesh, const fggl::math
 		{-0.5, -0.5,  0.5}  // 7
 	};
 
-	constexpr int idx[] {
+	constexpr Mesh::IndexType idx[] {
 		0, 7, 5, // top
 		7, 6, 5,
 		0, 5, 4, // side 0 - 1
diff --git a/include/fggl/data/model.hpp b/include/fggl/data/model.hpp
index 2a21df4..3134a2d 100644
--- a/include/fggl/data/model.hpp
+++ b/include/fggl/data/model.hpp
@@ -66,10 +66,10 @@ namespace fggl::data {
 	}
 
 	class Mesh {
+		public:
 			using IndexType = unsigned int;
 			constexpr static const IndexType INVALID_IDX = IndexType(UINT_MAX);
 
-		public:
 			Mesh();
 			~Mesh() = default;
 
-- 
GitLab