12 #ifndef MESHFACTORY_H_6014714286 13 #define MESHFACTORY_H_6014714286 22 #include <unordered_set> 55 template<
typename VertexType >
59 template<
typename VectorType >
60 static VertexType vertex(
const VectorType& );
104 template<
typename VertexType >
105 template<
typename VectorType >
116 template<
typename VertexType >
119 return createBox( size.x(), size.y(), size.z() );
123 template<
typename VertexType >
138 const std::size_t verticesCount = 6 * 4;
139 const std::size_t indicesCount = 6 * 2 * 3;
142 typedef typename MeshInstance::Vertex Vertex;
143 typedef typename MeshInstance:: Index Index;
144 Vertex vertices[ verticesCount ];
145 Index indices[ indicesCount ];
152 for(
unsigned int faceIndex = 0; faceIndex < 6; ++faceIndex )
154 const math::Matrix4f transform = baseTransform * transforms[ faceIndex ];
155 vertices[ ++lastVertex ] = vertex( transform *
math::Vector4f( -1, -1, 1, 1 ) );
156 vertices[ ++lastVertex ] = vertex( transform *
math::Vector4f( +1, -1, 1, 1 ) );
157 vertices[ ++lastVertex ] = vertex( transform *
math::Vector4f( +1, +1, 1, 1 ) );
158 vertices[ ++lastVertex ] = vertex( transform *
math::Vector4f( -1, +1, 1, 1 ) );
160 indices[ ++lastIndex ] = lastVertex - 3;
161 indices[ ++lastIndex ] = lastVertex - 2;
162 indices[ ++lastIndex ] = lastVertex;
164 indices[ ++lastIndex ] = lastVertex;
165 indices[ ++lastIndex ] = lastVertex - 2;
166 indices[ ++lastIndex ] = lastVertex - 1;
169 return MeshInstance::create
171 , vertices, verticesCount
172 , indices, indicesCount );
176 template<
typename VertexType >
180 const unsigned int verticesPerEdge = 2 + degree;
181 const unsigned int verticesPerSide = verticesPerEdge * verticesPerEdge;
182 const unsigned int facesPerSide = ( verticesPerEdge - 1 ) * ( verticesPerEdge - 1 );
183 const unsigned int indicesPerSide = 6 * facesPerSide;
195 const std::size_t verticesCount = 6 * verticesPerSide;
196 const std::size_t indicesCount = 6 * indicesPerSide;
200 typedef typename MeshInstance::Vertex Vertex;
201 typedef typename MeshInstance:: Index Index;
202 Vertex vertices[ verticesCount ];
203 Index indices[ indicesCount ];
210 for(
unsigned int sideIndex = 0; sideIndex < 6; ++sideIndex )
212 const math::Matrix4f transform = baseTransform * transforms[ sideIndex ];
214 for(
unsigned int y = 0; y < verticesPerEdge; ++y )
215 for(
unsigned int x = 0; x < verticesPerEdge; ++x )
217 const float fx = 2 * x / float( verticesPerEdge - 1 );
218 const float fy = 2 * y / float( verticesPerEdge - 1 );
219 const auto position =
math::Vector3f( -1 + fx, -1 + fy, 1 ).normalized();
220 vertices[ ++lastVertex ] = vertex( transform *
math::Vector4f( position.x(), position.y(), position.z(), 1 ) );
223 for(
unsigned int y = 0; y < verticesPerEdge - 1; ++y )
224 for(
unsigned int x = 0; x < verticesPerEdge - 1; ++x )
226 const uint16_t ul = x + y * verticesPerEdge;
227 const uint16_t ur = x + 1 + y * verticesPerEdge;
228 const uint16_t ll = x + ( y + 1 ) * verticesPerEdge;
229 const uint16_t lr = x + 1 + ( y + 1 ) * verticesPerEdge;
231 indices[ ++lastIndex ] = verticesPerSide * sideIndex + ul;
232 indices[ ++lastIndex ] = verticesPerSide * sideIndex + lr;
233 indices[ ++lastIndex ] = verticesPerSide * sideIndex + ll;
235 indices[ ++lastIndex ] = verticesPerSide * sideIndex + ul;
236 indices[ ++lastIndex ] = verticesPerSide * sideIndex + ur;
237 indices[ ++lastIndex ] = verticesPerSide * sideIndex + lr;
241 return MeshInstance::create
243 , vertices, verticesCount
244 , indices, indicesCount );
248 template<
typename VertexType >
252 typedef typename MeshInstance::Vertex Vertex;
253 typedef typename MeshInstance:: Index Index;
261 template<
typename VertexType >
264 return createFromSTL( std::fstream(path, std::ios::in | std::ios::binary) );
267 template<
typename VertexType >
270 auto origExceptMask = stlStream.exceptions();
271 stlStream.exceptions(std::istream::failbit | std::istream::badbit | std::istream::eofbit);
273 uint32_t amountTriangles;
276 stlStream.seekg(stlStream.beg + 80);
279 stlStream.read(reinterpret_cast<char*>(&amountTriangles), 4);
282 typedef typename MeshInstance::Vertex Vertex;
283 typedef typename MeshInstance::Index Index;
284 typedef typename std::pair< Vertex, std::size_t > VertexIndexPair;
286 std::size_t indicesCount = amountTriangles * 3;
287 std::unique_ptr<Index[]> indices (
new Index [indicesCount]);
288 std::unique_ptr<Vertex[]> vertices(
new Vertex [indicesCount]);
290 std::function<std::size_t(const VertexIndexPair&)> vertexHash =
291 [](
const VertexIndexPair& vertAndIndex) -> std::size_t
295 std::size_t hashVal = std::hash<decltype(vertAndIndex.first.x)>()(vertAndIndex.first.x);
296 hashVal ^= std::hash<decltype(vertAndIndex.first.y)>()(vertAndIndex.first.y) + 0x9e3779b9 + (hashVal << 6) + (hashVal >> 2);
297 hashVal ^= std::hash<decltype(vertAndIndex.first.z)> ()(vertAndIndex.first.z) + 0x9e3779b9 + (hashVal << 6) + (hashVal >> 2);
301 std::function<std::size_t(const VertexIndexPair&, const VertexIndexPair&)> vertexComp =
302 [](
const VertexIndexPair& vertAndIndex1,
const VertexIndexPair& vertAndIndex2) ->
bool 305 return ((vertAndIndex1.first.x) == (vertAndIndex2.first.x)) && ((vertAndIndex1.first.y) == (vertAndIndex2.first.y)) && ((vertAndIndex1.first.z) == (vertAndIndex2.first.z));
308 std::unordered_set< VertexIndexPair, decltype(vertexHash), decltype(vertexComp) > vertsWithArrIndices(amountTriangles * 3, vertexHash, vertexComp);
310 std::size_t indicesItCount = 0;
311 std::size_t verticesItCount = 0;
314 for (
unsigned int i = 0; i < amountTriangles; i++)
317 stlStream.seekg(12, std::ios_base::cur);
320 for (
unsigned int j = 0; j < 3; j++)
325 stlStream.read(reinterpret_cast<char*>(&x), 4);
326 stlStream.read(reinterpret_cast<char*>(&y), 4);
327 stlStream.read(reinterpret_cast<char*>(&z), 4);
334 auto emplRes = vertsWithArrIndices.emplace(std::piecewise_construct, std::forward_as_tuple(std::move(vert)), std::forward_as_tuple(verticesItCount));
338 vertices[verticesItCount++] = (emplRes.first)->first;
341 indices[indicesItCount++] = (emplRes.first)->second;
345 stlStream.seekg(2, std::ios_base::cur);
348 stlStream.exceptions(origExceptMask);
350 return MeshInstance::create
352 , vertices.get(), verticesItCount
353 , indices.get(), indicesCount);
362 #endif // MESHFACTORY_H_6014714286 Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
Matrix4f scaling4f(float x, float y, float z)
Creates scaling matrix for homogeneous coordinates.
static const unsigned int PRIMITIVE_TYPE_POINTS
Draws points. Indicates that each index makes up a single point.
-
static ManagedMesh< VertexType, uint8_t > & createBox(float width, float height, float depth)
Creates box with width, height and depth. The box is centered in .
+
static ManagedMesh< VertexType, uint8_t > & createBox(float width, float height, float depth)
Creates box with width, height and depth. The box is centered in .
Eigen::Matrix< float, 4, 1 > Vector4f
Defines vector.
Defines Carna::base::VertexAttributes.
static const unsigned int PRIMITIVE_TYPE_TRIANGLES
Draws triangles. Indicates that the indices make up the th triangle with .
@@ -90,9 +90,11 @@
Defines Carna::base::VertexBase.
Implements MeshBase class for particular VertexType and IndexType.
-
static ManagedMesh< VertexType, uint8_t > & createPoint()
Creates mesh that consists of a single point.
+
static ManagedMesh< VertexType, uint8_t > & createPoint()
Creates mesh that consists of a single point.
+
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Defines Carna::base::VertexBuffer.
-
static ManagedMesh< VertexType, uint32_t > & createFromSTL(const std::string &path)
Creates mesh from an STL file.
+
static ManagedMesh< VertexType, uint16_t > & createBall(float radius, unsigned int degree)
Creates sphere with radius and a vertices number determined by degree. The ball is centered in ...
+
static ManagedMesh< VertexType, uint32_t > & createFromSTL(const std::string &path)
Creates mesh from an STL file.