initial commit
This commit is contained in:
parent
81af403965
commit
573e1a9fa3
220 changed files with 150440 additions and 0 deletions
172
samples/mesh_loader.cpp
Normal file
172
samples/mesh_loader.cpp
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "mesh_loader.h"
|
||||
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#define TINYOBJLOADER_USE_MAPBOX_EARCUT
|
||||
#include "tiny_obj_loader.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// Load a wavefront obj file using tiny obj loader and earcut for triangulation.
|
||||
void LoadTempMesh( const char* path, TempMesh* tempMesh, float scale, bool zUp )
|
||||
{
|
||||
std::string inputFile = path;
|
||||
tinyobj::ObjReaderConfig readerConfig;
|
||||
readerConfig.triangulate = true;
|
||||
readerConfig.mtl_search_path = "./"; // Path to material files
|
||||
|
||||
tinyobj::ObjReader reader;
|
||||
|
||||
if ( !reader.ParseFromFile( inputFile, readerConfig ) )
|
||||
{
|
||||
if ( !reader.Error().empty() )
|
||||
{
|
||||
std::cerr << "TinyObjReader: " << reader.Error();
|
||||
}
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
if ( !reader.Warning().empty() )
|
||||
{
|
||||
std::cout << "TinyObjReader: " << reader.Warning();
|
||||
}
|
||||
|
||||
const tinyobj::attrib_t& attrib = reader.GetAttrib();
|
||||
const std::vector<tinyobj::shape_t>& shapes = reader.GetShapes();
|
||||
// const std::vector<tinyobj::material_t>& objMaterials = reader.GetMaterials();
|
||||
|
||||
tempMesh->vertices.clear();
|
||||
tempMesh->indices.clear();
|
||||
tempMesh->materialIndices.clear();
|
||||
|
||||
int vertexCount = int( attrib.vertices.size() / 3 );
|
||||
for ( int i = 0; i < vertexCount; i++ )
|
||||
{
|
||||
float x = scale * attrib.vertices[3 * i + 0];
|
||||
float y = scale * attrib.vertices[3 * i + 1];
|
||||
float z = scale * attrib.vertices[3 * i + 2];
|
||||
b3Vec3 vertex = zUp ? b3Vec3{ y, z, x } : b3Vec3{ x, y, z };
|
||||
tempMesh->vertices.push_back( vertex );
|
||||
}
|
||||
|
||||
// Loop over shapes
|
||||
int shapeCount = (int)shapes.size();
|
||||
int materialIndex = 0;
|
||||
for ( int shapeIndex = 0; shapeIndex < shapeCount; ++shapeIndex )
|
||||
{
|
||||
int faceCount = (int)shapes[shapeIndex].mesh.num_face_vertices.size();
|
||||
int baseIndex = 0;
|
||||
for ( int faceIndex = 0; faceIndex < faceCount; ++faceIndex )
|
||||
{
|
||||
int faceVertexCount = (int)shapes[shapeIndex].mesh.num_face_vertices[faceIndex];
|
||||
|
||||
// todo only triangles for now
|
||||
if ( faceVertexCount != 3 )
|
||||
{
|
||||
baseIndex += faceVertexCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
for ( int vertexIndex = 0; vertexIndex < faceVertexCount; ++vertexIndex )
|
||||
{
|
||||
tinyobj::index_t idx = shapes[shapeIndex].mesh.indices[baseIndex + vertexIndex];
|
||||
tempMesh->indices.push_back( idx.vertex_index );
|
||||
}
|
||||
|
||||
tempMesh->materialIndices.push_back( materialIndex );
|
||||
materialIndex = ( materialIndex + 1 ) % 3;
|
||||
|
||||
baseIndex += faceVertexCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b3MeshData* CreateMeshData( const char* path, float scale, bool zUp, bool useMedianSplit, bool identifyConvexEdges, bool weldVertices )
|
||||
{
|
||||
std::string inputFile = path;
|
||||
tinyobj::ObjReaderConfig readerConfig;
|
||||
readerConfig.triangulate = true;
|
||||
readerConfig.mtl_search_path = "./"; // Path to material files
|
||||
|
||||
tinyobj::ObjReader reader;
|
||||
|
||||
if ( !reader.ParseFromFile( inputFile, readerConfig ) )
|
||||
{
|
||||
if ( !reader.Error().empty() )
|
||||
{
|
||||
std::cerr << "TinyObjReader: " << reader.Error();
|
||||
}
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
if ( !reader.Warning().empty() )
|
||||
{
|
||||
std::cout << "TinyObjReader: " << reader.Warning();
|
||||
}
|
||||
|
||||
const tinyobj::attrib_t& attrib = reader.GetAttrib();
|
||||
const std::vector<tinyobj::shape_t>& shapes = reader.GetShapes();
|
||||
// const std::vector<tinyobj::material_t>& objMaterials = reader.GetMaterials();
|
||||
|
||||
TempMesh mesh;
|
||||
|
||||
int vertexCount = int( attrib.vertices.size() / 3 );
|
||||
for ( int i = 0; i < vertexCount; i++ )
|
||||
{
|
||||
float x = scale * attrib.vertices[3 * i + 0];
|
||||
float y = scale * attrib.vertices[3 * i + 1];
|
||||
float z = scale * attrib.vertices[3 * i + 2];
|
||||
b3Vec3 vertex = zUp ? b3Vec3{ y, z, x } : b3Vec3{ x, y, z };
|
||||
mesh.vertices.push_back( vertex );
|
||||
}
|
||||
|
||||
// Loop over shapes
|
||||
int shapeCount = (int)shapes.size();
|
||||
int materialIndex = 0;
|
||||
for ( int shapeIndex = 0; shapeIndex < shapeCount; ++shapeIndex )
|
||||
{
|
||||
int faceCount = (int)shapes[shapeIndex].mesh.num_face_vertices.size();
|
||||
int baseIndex = 0;
|
||||
for ( int faceIndex = 0; faceIndex < faceCount; ++faceIndex )
|
||||
{
|
||||
int faceVertexCount = (int)shapes[shapeIndex].mesh.num_face_vertices[faceIndex];
|
||||
|
||||
// todo only triangles for now
|
||||
if ( faceVertexCount != 3 )
|
||||
{
|
||||
baseIndex += faceVertexCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
for ( int vertexIndex = 0; vertexIndex < faceVertexCount; ++vertexIndex )
|
||||
{
|
||||
tinyobj::index_t idx = shapes[shapeIndex].mesh.indices[baseIndex + vertexIndex];
|
||||
mesh.indices.push_back( idx.vertex_index );
|
||||
}
|
||||
|
||||
mesh.materialIndices.push_back( materialIndex );
|
||||
materialIndex = ( materialIndex + 1 ) % 3;
|
||||
|
||||
baseIndex += faceVertexCount;
|
||||
}
|
||||
}
|
||||
|
||||
b3MeshDef def = {};
|
||||
def.vertices = mesh.vertices.data();
|
||||
def.vertexCount = (int)mesh.vertices.size();
|
||||
def.indices = mesh.indices.data();
|
||||
def.triangleCount = (int)mesh.indices.size() / 3;
|
||||
def.materialIndices = mesh.materialIndices.data();
|
||||
def.useMedianSplit = useMedianSplit;
|
||||
def.identifyEdges = identifyConvexEdges;
|
||||
def.weldVertices = weldVertices;
|
||||
def.weldTolerance = 0.002f;
|
||||
|
||||
b3MeshData* meshData = b3CreateMesh( &def, nullptr, 0 );
|
||||
return meshData;
|
||||
}
|
||||
Loading…
Reference in a new issue