tinypbrt 1.1.1
pbrt-v4 scene file parser for C11 with a header-only C++20 wrapper
Loading...
Searching...
No Matches
examples/export_obj/main.c
#include <math.h>
#include <stdio.h>
#include <example_utils.h>
typedef struct exporter_t {
FILE* out;
const tpbrt_scene_t* scene;
size_t vertex_count;
size_t triangle_count;
size_t object_count;
} exporter_t;
static void emit_vertex(const exporter_t* const e, const tpbrt_mat4_t* const m, const tpbrt_vec3_t p) {
const tpbrt_vec3_t w = example_transform_point(m, p);
fprintf(e->out, "v %g %g %g\n", w.x, w.y, w.z);
}
static void emit_triangle(exporter_t* const e, const size_t base, const size_t a, const size_t b, const size_t c) {
fprintf(e->out, "f %zu %zu %zu\n", base + a + 1u, base + b + 1u, base + c + 1u);
++e->triangle_count;
}
static void emit_material(const exporter_t* const e, const tpbrt_shape_t* const shape) {
const tpbrt_material_t* material = TPBRT_NULL;
if (tpbrt_get_material_by_handle(&e->scene->materials, shape->material, &material) != TPBRT_ERROR_NONE) { return; }
if (material->name.size > 0u) { fprintf(e->out, "usemtl " EXAMPLE_STR_FMT "\n", EXAMPLE_STR_ARG(material->name)); }
else { fprintf(e->out, "usemtl %s_%zu\n", EXAMPLE_NAME(example_material_names, material->type), (size_t)material->idx); }
}
static void export_sphere(exporter_t* const e, const tpbrt_mat4_t* const m, const tpbrt_shape_sphere_params_t* const s) {
enum { RINGS = 12, SEGMENTS = 24 };
const float pi = 3.14159265358979f;
const size_t base = e->vertex_count;
for (int r = 0; r <= RINGS; ++r) {
const float theta = pi * (float)r / (float)RINGS;
for (int seg = 0; seg <= SEGMENTS; ++seg) {
const float phi = 2.0f * pi * (float)seg / (float)SEGMENTS;
p.x = s->radius * sinf(theta) * cosf(phi);
p.y = s->radius * sinf(theta) * sinf(phi);
p.z = s->radius * cosf(theta);
emit_vertex(e, m, p);
}
}
e->vertex_count += (size_t)(RINGS + 1) * (SEGMENTS + 1);
for (size_t r = 0; r < RINGS; ++r) {
for (size_t seg = 0; seg < SEGMENTS; ++seg) {
const size_t i0 = (r * (SEGMENTS + 1)) + seg;
const size_t i1 = i0 + SEGMENTS + 1;
emit_triangle(e, base, i0, i1, i0 + 1u);
emit_triangle(e, base, i0 + 1u, i1, i1 + 1u);
}
}
}
static void export_cylinder(exporter_t* const e, const tpbrt_mat4_t* const m, const tpbrt_shape_cylinder_params_t* const c) {
enum { SEGMENTS = 24 };
const float phi_max = c->phi_max * 3.14159265358979f / 180.0f;
const size_t base = e->vertex_count;
for (int seg = 0; seg <= SEGMENTS; ++seg) {
const float phi = phi_max * (float)seg / (float)SEGMENTS;
p.x = c->radius * cosf(phi);
p.y = c->radius * sinf(phi);
p.z = c->z_min;
emit_vertex(e, m, p);
p.z = c->z_max;
emit_vertex(e, m, p);
}
e->vertex_count += 2u * (SEGMENTS + 1);
for (size_t seg = 0; seg < SEGMENTS; ++seg) {
emit_triangle(e, base, 2u * seg, (2u * seg) + 2u, (2u * seg) + 1u);
emit_triangle(e, base, (2u * seg) + 1u, (2u * seg) + 2u, (2u * seg) + 3u);
}
}
static void export_disk(exporter_t* const e, const tpbrt_mat4_t* const m, const tpbrt_shape_disk_params_t* const d) {
enum { SEGMENTS = 24 };
const float phi_max = d->phi_max * 3.14159265358979f / 180.0f;
const size_t base = e->vertex_count;
for (int seg = 0; seg <= SEGMENTS; ++seg) {
const float phi = phi_max * (float)seg / (float)SEGMENTS;
p.z = d->height;
p.x = d->inner_radius * cosf(phi);
p.y = d->inner_radius * sinf(phi);
emit_vertex(e, m, p);
p.x = d->radius * cosf(phi);
p.y = d->radius * sinf(phi);
emit_vertex(e, m, p);
}
e->vertex_count += 2u * (SEGMENTS + 1);
for (size_t seg = 0; seg < SEGMENTS; ++seg) {
emit_triangle(e, base, 2u * seg, (2u * seg) + 1u, (2u * seg) + 3u);
emit_triangle(e, base, 2u * seg, (2u * seg) + 3u, (2u * seg) + 2u);
}
}
static void export_shape(exporter_t* const e, const tpbrt_shape_t* const shape, const tpbrt_mat4_t* const object_to_world) {
fprintf(e->out, "o %s_%zu\n", EXAMPLE_NAME(example_shape_names, shape->type), e->object_count++);
emit_material(e, shape);
const size_t base = e->vertex_count;
switch (shape->type) {
for (size_t i = 0; i < mesh->p.count; ++i) { emit_vertex(e, object_to_world, mesh->p.data[i]); }
e->vertex_count += mesh->p.count;
for (size_t i = 0; i + 2u < mesh->indices.count; i += 3u) {
emit_triangle(e, base, mesh->indices.data[i], mesh->indices.data[i + 1u], mesh->indices.data[i + 2u]);
}
break;
}
for (size_t i = 0; i < mesh->p.count; ++i) { emit_vertex(e, object_to_world, mesh->p.data[i]); }
e->vertex_count += mesh->p.count;
for (size_t i = 0; i + 3u < mesh->indices.count; i += 4u) {
const tpbrt_uint_t* const q = &mesh->indices.data[i];
emit_triangle(e, base, q[0], q[1], q[3]);
emit_triangle(e, base, q[0], q[3], q[2]);
}
break;
}
case TPBRT_SHAPE_TYPE_SPHERE: export_sphere(e, object_to_world, &shape->as.sphere); break;
case TPBRT_SHAPE_TYPE_CYLINDER: export_cylinder(e, object_to_world, &shape->as.cylinder); break;
case TPBRT_SHAPE_TYPE_DISK: export_disk(e, object_to_world, &shape->as.disk); break;
fprintf(e->out, "# plymesh \"" EXAMPLE_STR_FMT "\" is not loaded by tinypbrt\n",
EXAMPLE_STR_ARG(shape->as.ply_mesh.file_name));
break;
default: fprintf(e->out, "# %s is not exported\n", EXAMPLE_NAME(example_shape_names, shape->type)); break;
}
}
int main(const int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "usage: %s scene.pbrt [out.obj]\n", argv[0]);
return 1;
}
const tpbrt_error_t err = tpbrt_scene_load_from_file(argv[1], &scene);
if (err != TPBRT_ERROR_NONE) {
example_print_error(argv[1], err);
return 1;
}
FILE* const out = argc > 2 ? example_fopen(argv[2], "w") : stdout;
if (out == TPBRT_NULL) {
fprintf(stderr, "cannot open %s\n", argv[2]);
return 1;
}
exporter_t e = { .out = out, .scene = &scene, .vertex_count = 0u, .triangle_count = 0u, .object_count = 0u };
fprintf(out, "# exported by tinypbrt-example-export_obj from %s\n", argv[1]);
for (size_t i = 0; i < scene.shapes.count; ++i) {
const tpbrt_shape_t* const shape = &scene.shapes.shapes[i];
const tpbrt_mat4_t* object_to_world = TPBRT_NULL;
if (tpbrt_get_transform(&scene.transforms, shape->transform.start_transform, &object_to_world) ==
export_shape(&e, shape, object_to_world);
}
}
for (size_t i = 0; i < scene.instances.count; ++i) {
const tpbrt_object_instance_t* const instance = &scene.instances.instances[i];
const tpbrt_object_t* object = TPBRT_NULL;
const tpbrt_mat4_t* instance_to_world = TPBRT_NULL;
if (tpbrt_get_object_by_handle(&scene.objects, instance->object, &object) != TPBRT_ERROR_NONE ||
tpbrt_get_transform(&scene.transforms, instance->transform.start_transform, &instance_to_world) !=
continue;
}
fprintf(out, "g instance_%zu_" EXAMPLE_STR_FMT "\n", i, EXAMPLE_STR_ARG(object->name));
for (size_t c = 0; c < object->children.count; ++c) {
const tpbrt_shape_t* const shape = &object->children.shapes[c];
const tpbrt_mat4_t* shape_to_object = TPBRT_NULL;
if (tpbrt_get_transform(&scene.transforms, shape->transform.start_transform, &shape_to_object) !=
continue;
}
const tpbrt_mat4_t shape_to_world = example_mat4_mul(instance_to_world, shape_to_object);
export_shape(&e, shape, &shape_to_world);
}
}
fprintf(stderr, "exported %zu vertices, %zu triangles\n", e.vertex_count, e.triangle_count);
if (out != stdout) { fclose(out); }
return 0;
}
@ FILE
Spectrum defined as file which contains wavelengths.
tpbrt_error_t
Error type (used as result type where success = TPBRT_ERROR_NONE = 0).
Definition error.h:20
@ TPBRT_ERROR_NONE
No error -> Success.
Definition error.h:22
tpbrt_error_t tpbrt_get_material_by_handle(const tpbrt_materials_list_t *materials, tpbrt_material_handle_t handle, const tpbrt_material_t **material)
Returns pointer to material to which handle is provided.
tpbrt_size_t size_t
Size type.
Definition fwd.hpp:107
uint32_t tpbrt_uint_t
Unsigned int type.
Definition fwd.h:78
tpbrt_error_t tpbrt_get_object_by_handle(const tpbrt_objects_list_t *objects, tpbrt_object_handle_t handle, const tpbrt_object_t **object)
Returns pointer to object to which handle is provided.
void tpbrt_scene_free(tpbrt_scene_t *scene)
Free all scene resources.
tpbrt_error_t tpbrt_scene_load_from_file(const tpbrt_char_t *filepath, tpbrt_scene_t *out_scene)
Loads scene from given file.
@ TPBRT_SHAPE_TYPE_BILINEAR_MESH
Bilinear mesh.
Definition shape.h:28
@ TPBRT_SHAPE_TYPE_SPHERE
Sphere.
Definition shape.h:36
@ TPBRT_SHAPE_TYPE_PLY_MESH
Ply file mesh.
Definition shape.h:40
@ TPBRT_SHAPE_TYPE_CYLINDER
Cylinder.
Definition shape.h:32
@ TPBRT_SHAPE_TYPE_TRIANGLE_MESH
Triangle mesh.
Definition shape.h:38
@ TPBRT_SHAPE_TYPE_DISK
Disk.
Definition shape.h:34
#define TPBRT_NULL
NULL value.
Definition fwd.h:19
tpbrt_error_t tpbrt_get_transform(const tpbrt_transforms_list_t *transforms, tpbrt_transform_handle_t handle, const tpbrt_mat4_t **transform)
Returns pointer to transform to which handle is provided.
4 by 4 row-major matrix.
Definition math.h:181
PBRT material representation.
Definition material.h:376
tpbrt_material_type_t type
Material value type.
Definition material.h:409
tpbrt_string_t name
Material name (if it is not named material name is empty).
Definition material.h:380
tpbrt_material_idx_t idx
Material index.
Definition material.h:378
PBRT object instance representation.
Definition object_instance.h:23
tpbrt_object_handle_t object
Handle to object.
Definition object_instance.h:25
tpbrt_transform_animated_t transform
Animated transform.
Definition object_instance.h:27
PBRT object representation.
Definition object.h:24
tpbrt_string_t name
Object name.
Definition object.h:26
tpbrt_object_instance_t * instances
Objects instances.
Definition object_instance.h:36
tpbrt_size_t count
Number of objects instances in list.
Definition object_instance.h:38
PBRT scene representation.
Definition scene.h:38
tpbrt_shapes_list_t shapes
List with all scene shapes.
Definition scene.h:68
tpbrt_transforms_list_t transforms
List with all unique transforms.
Definition scene.h:74
tpbrt_objects_list_t objects
List with all scene objects.
Definition scene.h:70
tpbrt_objects_instances_list_t instances
List with all scene objects instances.
Definition scene.h:72
Bilinear mesh parameters.
Definition shape.h:90
tpbrt_uint_array_t indices
The array of integer offsets into the per-vertex data arrays (P, and any of N, S, or uv that are pres...
Definition shape.h:92
tpbrt_point3_array_t p
The vertex positions of the bilinear mesh.
Definition shape.h:94
Cylinder shape parameters.
Definition shape.h:130
tpbrt_float_t z_min
The height of the cylinder's bottom along the z axis.
Definition shape.h:134
tpbrt_float_t radius
The cylinder's radius.
Definition shape.h:132
tpbrt_float_t phi_max
The maximum extent of the cylinder in phi (in spherical coordinates).
Definition shape.h:138
tpbrt_float_t z_max
The height of the cylinder's top along the z axis.
Definition shape.h:136
Disk shape parameters.
Definition shape.h:145
tpbrt_float_t inner_radius
The inner radius of the disk (if nonzero, the disk is an annulus).
Definition shape.h:151
tpbrt_float_t phi_max
The maximum extent of the disk in phi (in spherical coordinates).
Definition shape.h:153
tpbrt_float_t height
The position of the disk along the z axis.
Definition shape.h:147
tpbrt_float_t radius
The outer radius of the disk.
Definition shape.h:149
tpbrt_string_t file_name
File from which the PLY-format mesh is loaded. If the file name's extension is ".gz",...
Definition shape.h:194
Sphere shape parameters.
Definition shape.h:160
tpbrt_float_t radius
The sphere's radius.
Definition shape.h:162
PBRT shape representation.
Definition shape.h:218
tpbrt_shape_cylinder_params_t cylinder
Cylinder shape parameters.
Definition shape.h:243
tpbrt_transform_animated_t transform
Animated transform.
Definition shape.h:222
tpbrt_shape_disk_params_t disk
Disk shape parameters.
Definition shape.h:245
tpbrt_shape_ply_mesh_params_t ply_mesh
Ply mesh file parameters.
Definition shape.h:251
tpbrt_material_handle_t material
Material handle.
Definition shape.h:224
tpbrt_shape_type_t type
Shape value type.
Definition shape.h:232
tpbrt_shape_triangle_mesh_params_t triangle_mesh
Triangle mesh parameters.
Definition shape.h:249
tpbrt_shape_sphere_params_t sphere
Sphere shape parameters.
Definition shape.h:247
tpbrt_shape_bilinear_mesh_params_t bilinear_mesh
Bilinear mesh parameters.
Definition shape.h:239
union tpbrt_shape_t::@23 as
Value dependent on type value.
Triangle mesh parameters.
Definition shape.h:175
tpbrt_point3_array_t p
The vertex positions of the triangle mesh.
Definition shape.h:179
tpbrt_uint_array_t indices
The array of integer offsets into the per-vertex data arrays (P, and any of N, S, or uv that are pres...
Definition shape.h:177
tpbrt_size_t count
Number of shapes in list.
Definition shape.h:265
tpbrt_shape_t * shapes
Shapes list data.
Definition shape.h:263
tpbrt_size_t size
String size.
Definition common.h:24
tpbrt_transform_handle_t start_transform
Handle to starting transform.
Definition transform.h:34
tpbrt_size_t count
Array items count.
Definition math.h:57
tpbrt_uint_t * data
Array data.
Definition math.h:55
tpbrt_size_t count
Array items count.
Definition math.h:174
tpbrt_vec3_t * data
Array data.
Definition math.h:172
3 component vector.
Definition math.h:98