Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2474e27ab | ||
|
|
6c6390f034 | ||
|
|
4d6649cc6d | ||
|
|
d543b1447f | ||
|
|
d6eeb14216 | ||
|
|
aa670fe91e | ||
|
|
ebdbd8a231 |
@@ -209,6 +209,9 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
||||
printf("# of materials = %d\n", (int)materials.size());
|
||||
printf("# of shapes = %d\n", (int)shapes.size());
|
||||
|
||||
// Append `default` material
|
||||
materials.push_back(tinyobj::material_t());
|
||||
|
||||
// Load diffuse textures
|
||||
{
|
||||
for (size_t m = 0; m < materials.size(); m++) {
|
||||
@@ -248,11 +251,6 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
||||
|
||||
{
|
||||
for (size_t s = 0; s < shapes.size(); s++) {
|
||||
size_t current_material_id = 0;
|
||||
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
|
||||
// Base case
|
||||
current_material_id = shapes[s].mesh.material_ids[s];
|
||||
}
|
||||
DrawObject o;
|
||||
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
|
||||
for (size_t f = 0; f < shapes[s].mesh.indices.size() / 3; f++) {
|
||||
@@ -260,12 +258,16 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
||||
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
|
||||
tinyobj::index_t idx2 = shapes[s].mesh.indices[3 * f + 2];
|
||||
|
||||
current_material_id = shapes[s].mesh.material_ids[f];
|
||||
int current_material_id = shapes[s].mesh.material_ids[f];
|
||||
|
||||
if (current_material_id >= materials.size()) {
|
||||
std::cerr << "Invalid material index: " << current_material_id << std::endl;
|
||||
if ((current_material_id < 0) || (current_material_id >= static_cast<int>(materials.size()))) {
|
||||
// Invaid material ID. Use default material.
|
||||
current_material_id = materials.size() - 1; // Default material is added to the last item in `materials`.
|
||||
}
|
||||
|
||||
//if (current_material_id >= materials.size()) {
|
||||
// std::cerr << "Invalid material index: " << current_material_id << std::endl;
|
||||
//}
|
||||
//
|
||||
float diffuse[3];
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
diffuse[i] = materials[current_material_id].diffuse[i];
|
||||
@@ -364,7 +366,15 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
||||
|
||||
o.vb = 0;
|
||||
o.numTriangles = 0;
|
||||
o.material_id = current_material_id;
|
||||
|
||||
// OpenGL viewer does not support texturing with per-face material.
|
||||
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
|
||||
// Base case
|
||||
o.material_id = shapes[s].mesh.material_ids[s];
|
||||
} else {
|
||||
o.material_id = materials.size() - 1; // = ID for default material.
|
||||
}
|
||||
|
||||
if (vb.size() > 0) {
|
||||
glGenBuffers(1, &o.vb);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
|
||||
|
||||
74
examples/voxelize/main.cc
Normal file
74
examples/voxelize/main.cc
Normal file
@@ -0,0 +1,74 @@
|
||||
#define VOXELIZER_IMPLEMENTATION
|
||||
#include "voxelizer.h"
|
||||
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#include "../../tiny_obj_loader.h"
|
||||
|
||||
bool Voxelize(const char* filename, float voxelsizex, float voxelsizey, float voxelsizez, float precision)
|
||||
{
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
|
||||
|
||||
if (!err.empty()) {
|
||||
printf("err: %s\n", err.c_str());
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
printf("failed to load : %s\n", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (shapes.size() == 0) {
|
||||
printf("err: # of shapes are zero.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only use first shape.
|
||||
{
|
||||
vx_mesh_t* mesh;
|
||||
vx_mesh_t* result;
|
||||
|
||||
mesh = vx_mesh_alloc(attrib.vertices.size(), shapes[0].mesh.indices.size());
|
||||
|
||||
for (size_t f = 0; f < shapes[0].mesh.indices.size(); f++) {
|
||||
mesh->indices[f] = shapes[0].mesh.indices[f].vertex_index;
|
||||
}
|
||||
|
||||
for (size_t v = 0; v < attrib.vertices.size() / 3; v++) {
|
||||
mesh->vertices[v].x = attrib.vertices[3*v+0];
|
||||
mesh->vertices[v].y = attrib.vertices[3*v+1];
|
||||
mesh->vertices[v].z = attrib.vertices[3*v+2];
|
||||
}
|
||||
|
||||
result = vx_voxelize(mesh, voxelsizex, voxelsizey, voxelsizez, precision);
|
||||
|
||||
printf("Number of vertices: %ld\n", result->nvertices);
|
||||
printf("Number of indices: %ld\n", result->nindices);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(
|
||||
int argc,
|
||||
char** argv)
|
||||
{
|
||||
if (argc < 4) {
|
||||
printf("Usage: voxelize input.obj voxelsizex voxelsizey voxelsizez precision\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
const char* filename = argv[1];
|
||||
float voxelsizex = atof(argv[2]);
|
||||
float voxelsizey = atof(argv[3]);
|
||||
float voxelsizez = atof(argv[4]);
|
||||
float prec = atof(argv[5]);
|
||||
bool ret = Voxelize(filename, voxelsizex, voxelsizey, voxelsizez, prec);
|
||||
|
||||
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -236,13 +236,13 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
|
||||
printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str());
|
||||
printf(" material.disp = %s\n", materials[i].displacement_texname.c_str());
|
||||
printf(" <<PBR>>\n");
|
||||
printf(" material.Pr = %f\n", materials[i].roughness);
|
||||
printf(" material.Pm = %f\n", materials[i].metallic);
|
||||
printf(" material.Ps = %f\n", materials[i].sheen);
|
||||
printf(" material.Pc = %f\n", materials[i].clearcoat_thickness);
|
||||
printf(" material.Pcr = %f\n", materials[i].clearcoat_thickness);
|
||||
printf(" material.aniso = %f\n", materials[i].anisotropy);
|
||||
printf(" material.anisor = %f\n", materials[i].anisotropy_rotation);
|
||||
printf(" material.Pr = %f\n", static_cast<const double>(materials[i].roughness));
|
||||
printf(" material.Pm = %f\n", static_cast<const double>(materials[i].metallic));
|
||||
printf(" material.Ps = %f\n", static_cast<const double>(materials[i].sheen));
|
||||
printf(" material.Pc = %f\n", static_cast<const double>(materials[i].clearcoat_thickness));
|
||||
printf(" material.Pcr = %f\n", static_cast<const double>(materials[i].clearcoat_thickness));
|
||||
printf(" material.aniso = %f\n", static_cast<const double>(materials[i].anisotropy));
|
||||
printf(" material.anisor = %f\n", static_cast<const double>(materials[i].anisotropy_rotation));
|
||||
printf(" material.map_Ke = %s\n", materials[i].emissive_texname.c_str());
|
||||
printf(" material.map_Pr = %s\n", materials[i].roughness_texname.c_str());
|
||||
printf(" material.map_Pm = %s\n", materials[i].metallic_texname.c_str());
|
||||
|
||||
@@ -23,6 +23,7 @@ THE SOFTWARE.
|
||||
*/
|
||||
|
||||
//
|
||||
// version 1.0.2 : Improve parsing speed by about a factor of 2 for large files(#105)
|
||||
// version 1.0.1 : Fixes a shape is lost if obj ends with a 'usemtl'(#104)
|
||||
// version 1.0.0 : Change data structure. Change license from BSD to MIT.
|
||||
//
|
||||
@@ -75,6 +76,7 @@ typedef struct {
|
||||
float clearcoat_roughness; // [0, 1] default 0
|
||||
float anisotropy; // aniso. [0, 1] default 0
|
||||
float anisotropy_rotation; // anisor. [0, 1] default 0
|
||||
float pad0;
|
||||
std::string roughness_texname; // map_Pr
|
||||
std::string metallic_texname; // map_Pm
|
||||
std::string sheen_texname; // map_Ps
|
||||
@@ -418,8 +420,21 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
|
||||
read = 1;
|
||||
end_not_reached = (curr != s_end);
|
||||
while (end_not_reached && IS_DIGIT(*curr)) {
|
||||
static const double pow_lut[] = {
|
||||
1.0,
|
||||
0.1,
|
||||
0.01,
|
||||
0.001,
|
||||
0.0001,
|
||||
0.00001,
|
||||
0.000001,
|
||||
0.0000001,
|
||||
};
|
||||
const int lut_entries = sizeof pow_lut / sizeof pow_lut[0];
|
||||
|
||||
// NOTE: Don't use powf here, it will absolutely murder precision.
|
||||
mantissa += static_cast<int>(*curr - 0x30) * pow(10.0, -read);
|
||||
mantissa += static_cast<int>(*curr - 0x30) *
|
||||
(read < lut_entries ? pow_lut[read] : pow(10.0, -read));
|
||||
read++;
|
||||
curr++;
|
||||
end_not_reached = (curr != s_end);
|
||||
@@ -459,8 +474,8 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
|
||||
}
|
||||
|
||||
assemble:
|
||||
*result =
|
||||
(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
|
||||
*result = (sign == '+' ? 1 : -1) *
|
||||
(exponent ? ldexp(mantissa * pow(5.0, exponent), exponent) : mantissa);
|
||||
return true;
|
||||
fail:
|
||||
return false;
|
||||
@@ -692,9 +707,8 @@ void LoadMtl(std::map<std::string, int> *material_map,
|
||||
material_t material;
|
||||
InitMaterial(&material);
|
||||
|
||||
std::string linebuf;
|
||||
while (inStream->peek() != -1) {
|
||||
std::string linebuf;
|
||||
|
||||
safeGetline(*inStream, linebuf);
|
||||
|
||||
// Trim trailing whitespace.
|
||||
@@ -1028,6 +1042,7 @@ bool MaterialStreamReader::operator()(const std::string &matId,
|
||||
std::vector<material_t> *materials,
|
||||
std::map<std::string, int> *matMap,
|
||||
std::string *err) {
|
||||
(void)matId;
|
||||
LoadMtl(matMap, materials, &m_inStream);
|
||||
if (!m_inStream) {
|
||||
std::stringstream ss;
|
||||
@@ -1090,8 +1105,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
|
||||
shape_t shape;
|
||||
|
||||
std::string linebuf;
|
||||
while (inStream->peek() != -1) {
|
||||
std::string linebuf;
|
||||
safeGetline(*inStream, linebuf);
|
||||
|
||||
// Trim newline '\r\n' or '\n'
|
||||
|
||||
Reference in New Issue
Block a user