205 lines
5.7 KiB
C
205 lines
5.7 KiB
C
|
#pragma once
|
||
|
|
||
|
#include <vector>
|
||
|
|
||
|
#include "DrawMode.h"
|
||
|
|
||
|
#include "Exception.h"
|
||
|
|
||
|
template <typename VertexType, typename IndexType>
|
||
|
class MeshFactory
|
||
|
{
|
||
|
public:
|
||
|
typedef Mesh<VertexType, IndexType> MeshType;
|
||
|
|
||
|
protected:
|
||
|
MeshFactory() {} // Prevent Instantiation
|
||
|
|
||
|
public:
|
||
|
typedef unsigned int SizeType;
|
||
|
|
||
|
virtual ~MeshFactory()
|
||
|
{
|
||
|
for (SizeType i = 0; i < m_meshes.size(); ++i)
|
||
|
{
|
||
|
delete[] m_meshes[i]->vertices;
|
||
|
delete[] m_meshes[i]->indices;
|
||
|
delete m_meshes[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static MeshType* gen(const DrawMode& draw_mode, const VertexType& a)
|
||
|
{
|
||
|
MeshType* mesh;
|
||
|
switch (draw_mode)
|
||
|
{
|
||
|
case DrawMode::DRAW_POINTS:
|
||
|
mesh = create_mesh(1, 1);
|
||
|
mesh->vertices[0] = a;
|
||
|
mesh->indices[0] = 0;
|
||
|
return mesh;
|
||
|
case DrawMode::DRAW_LINE_STRIP:
|
||
|
case DrawMode::DRAW_LINE_LOOP:
|
||
|
case DrawMode::DRAW_LINES:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP:
|
||
|
case DrawMode::DRAW_TRIANGLE_FAN:
|
||
|
case DrawMode::DRAW_TRIANGLES:
|
||
|
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_LINES_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
|
||
|
case DrawMode::DRAW_PATCHES:
|
||
|
default:
|
||
|
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static MeshType* gen(const DrawMode& draw_mode, const VertexType& a, const VertexType& b)
|
||
|
{
|
||
|
MeshType* mesh;
|
||
|
switch (draw_mode)
|
||
|
{
|
||
|
case DrawMode::DRAW_POINTS:
|
||
|
case DrawMode::DRAW_LINE_STRIP:
|
||
|
case DrawMode::DRAW_LINE_LOOP:
|
||
|
case DrawMode::DRAW_LINES:
|
||
|
mesh = create_mesh(2, 2);
|
||
|
mesh->vertices[0] = a;
|
||
|
mesh->vertices[1] = b;
|
||
|
mesh->indices[0] = 0;
|
||
|
mesh->indices[1] = 1;
|
||
|
return mesh;
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP:
|
||
|
case DrawMode::DRAW_TRIANGLE_FAN:
|
||
|
case DrawMode::DRAW_TRIANGLES:
|
||
|
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_LINES_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
|
||
|
case DrawMode::DRAW_PATCHES:
|
||
|
default:
|
||
|
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static MeshType* gen(const DrawMode& draw_mode, const VertexType& a, const VertexType& b, const VertexType& c)
|
||
|
{
|
||
|
MeshType* mesh;
|
||
|
switch (draw_mode)
|
||
|
{
|
||
|
case DrawMode::DRAW_POINTS:
|
||
|
case DrawMode::DRAW_LINE_STRIP:
|
||
|
case DrawMode::DRAW_LINE_LOOP:
|
||
|
case DrawMode::DRAW_LINES:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP:
|
||
|
case DrawMode::DRAW_TRIANGLE_FAN:
|
||
|
case DrawMode::DRAW_TRIANGLES:
|
||
|
mesh = create_mesh(3, 3);
|
||
|
mesh->vertices[0] = a;
|
||
|
mesh->vertices[1] = b;
|
||
|
mesh->vertices[2] = c;
|
||
|
mesh->indices[0] = 0;
|
||
|
mesh->indices[1] = 1;
|
||
|
mesh->indices[2] = 2;
|
||
|
return mesh;
|
||
|
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_LINES_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
|
||
|
case DrawMode::DRAW_PATCHES:
|
||
|
default:
|
||
|
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static MeshType* gen(const DrawMode& draw_mode, const VertexType& a, const VertexType& b, const VertexType& c, const VertexType& d)
|
||
|
{
|
||
|
MeshType* mesh;
|
||
|
switch (draw_mode)
|
||
|
{
|
||
|
case DrawMode::DRAW_POINTS:
|
||
|
case DrawMode::DRAW_LINE_STRIP:
|
||
|
case DrawMode::DRAW_LINE_LOOP:
|
||
|
case DrawMode::DRAW_LINES:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP:
|
||
|
case DrawMode::DRAW_TRIANGLE_FAN:
|
||
|
mesh = create_mesh(4, 4);
|
||
|
mesh->vertices[0] = a;
|
||
|
mesh->vertices[1] = b;
|
||
|
mesh->vertices[2] = c;
|
||
|
mesh->vertices[3] = d;
|
||
|
mesh->indices[0] = 0;
|
||
|
mesh->indices[1] = 1;
|
||
|
mesh->indices[2] = 2;
|
||
|
mesh->indices[3] = 3;
|
||
|
return mesh;
|
||
|
case DrawMode::DRAW_TRIANGLES:
|
||
|
mesh = create_mesh(4, 6);
|
||
|
mesh->vertices[0] = a;
|
||
|
mesh->vertices[1] = b;
|
||
|
mesh->vertices[2] = c;
|
||
|
mesh->vertices[3] = d;
|
||
|
mesh->indices[0] = 0;
|
||
|
mesh->indices[1] = 1;
|
||
|
mesh->indices[2] = 2;
|
||
|
mesh->indices[3] = 2;
|
||
|
mesh->indices[4] = 1;
|
||
|
mesh->indices[5] = 3;
|
||
|
return mesh;
|
||
|
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_LINES_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
|
||
|
case DrawMode::DRAW_PATCHES:
|
||
|
default:
|
||
|
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static MeshType* gen(const DrawMode& draw_mode, const std::vector<VertexType>& vertices, const std::vector<IndexType>& indices)
|
||
|
{
|
||
|
MeshType* mesh;
|
||
|
switch (draw_mode)
|
||
|
{
|
||
|
case DrawMode::DRAW_POINTS:
|
||
|
case DrawMode::DRAW_LINE_STRIP:
|
||
|
case DrawMode::DRAW_LINE_LOOP:
|
||
|
case DrawMode::DRAW_LINES:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP:
|
||
|
case DrawMode::DRAW_TRIANGLE_FAN:
|
||
|
case DrawMode::DRAW_TRIANGLES:
|
||
|
int n = vertices.size();
|
||
|
mesh = create_mesh(n, n);
|
||
|
for (SizeType i = 0; i < vertices.size(); ++i)
|
||
|
{
|
||
|
mesh->vertices[i] = vertices[i];
|
||
|
mesh->indices[i] = i;
|
||
|
}
|
||
|
return mesh;
|
||
|
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_LINES_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
|
||
|
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
|
||
|
case DrawMode::DRAW_PATCHES:
|
||
|
default:
|
||
|
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
static MeshType* create_mesh(unsigned int vertex_count, unsigned int index_count)
|
||
|
{
|
||
|
MeshType* mesh = new MeshType();
|
||
|
mesh->vertices = new VertexType[vertex_count];
|
||
|
mesh->vertex_count = vertex_count;
|
||
|
mesh->indices = new IndexType[vertex_count];
|
||
|
mesh->index_count = index_count;
|
||
|
m_meshes.push_back(mesh);
|
||
|
return mesh;
|
||
|
}
|
||
|
private:
|
||
|
static std::vector<MeshType*> m_meshes;
|
||
|
};
|
||
|
|
||
|
template <typename VertexType, typename IndexType>
|
||
|
std::vector<Mesh<VertexType, IndexType>*> MeshFactory<VertexType, IndexType>::m_meshes = std::vector<Mesh<VertexType, IndexType>*>();
|