tinypbrt 1.1.1
pbrt-v4 scene file parser for C11 with a header-only C++20 wrapper
Loading...
Searching...
No Matches
examples/materials/main.c
#include <stdio.h>
#include <string.h>
#include <example_utils.h>
static void print_roughness(const tpbrt_textures_list_t textures, const tpbrt_material_roughness_params_t* const r) {
printf(" uroughness: ");
example_print_texture_handle(stdout, textures, &r->u_roughness);
printf("\n vroughness: ");
example_print_texture_handle(stdout, textures, &r->v_roughness);
printf("\n remaproughness: %s\n", r->remap_roughness ? "true" : "false");
}
static void print_material(const tpbrt_scene_t* const scene, const tpbrt_material_t* const material) {
const tpbrt_textures_list_t textures = scene->textures;
printf(" %s", EXAMPLE_NAME(example_material_names, material->type));
if (material->name.size > 0u) { printf(" \"" EXAMPLE_STR_FMT "\"", EXAMPLE_STR_ARG(material->name)); }
printf("\n");
switch (material->type) {
printf(" reflectance: ");
example_print_texture_handle(stdout, textures, &material->as.diffuse.reflectance);
printf("\n");
break;
printf(" reflectance: ");
example_print_texture_handle(stdout, textures, &material->as.coated_diffuse.reflectance);
printf("\n thickness: %g\n", material->as.coated_diffuse.thickness);
print_roughness(textures, &material->as.coated_diffuse.roughness_params);
break;
const tpbrt_material_conductor_params_t* const c = &material->as.conductor;
printf(" eta: ");
example_print_texture_handle(stdout, textures, &c->as.conductor.eta);
printf("\n k: ");
example_print_texture_handle(stdout, textures, &c->as.conductor.k);
}
else {
printf(" reflectance: ");
example_print_texture_handle(stdout, textures, &c->as.reflectance);
}
printf("\n");
print_roughness(textures, &c->roughness_params);
break;
}
printf(" eta: ");
example_print_texture_handle(stdout, textures, &material->as.dielectric.eta);
printf("\n");
print_roughness(textures, &material->as.dielectric.roughness_params);
break;
printf(" reflectance: ");
example_print_texture_handle(stdout, textures, &material->as.diffuse_transmission.reflectance);
printf("\n transmittance: ");
example_print_texture_handle(stdout, textures, &material->as.diffuse_transmission.transmittance);
printf("\n");
break;
for (int i = 0; i < 2; ++i) {
const tpbrt_material_t* inner = TPBRT_NULL;
if (tpbrt_get_material_by_handle(&scene->materials, material->as.mix.materials[i], &inner) ==
printf(" materials[%d]: \"" EXAMPLE_STR_FMT "\"\n", i, EXAMPLE_STR_ARG(inner->name));
}
}
break;
default: printf(" (parameters of this type are not printed by the example)\n"); break;
}
}
int main(const int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "usage: %s scene.pbrt [material_name]\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;
}
for (size_t i = 0; i < scene.shapes.count; ++i) {
const tpbrt_shape_t* const shape = &scene.shapes.shapes[i];
printf("shape %zu: %s\n", i, EXAMPLE_NAME(example_shape_names, shape->type));
const tpbrt_material_t* material = TPBRT_NULL;
if (tpbrt_get_material_by_handle(&scene.materials, shape->material, &material) == TPBRT_ERROR_NONE) {
print_material(&scene, material);
}
else { printf(" no material (pbrt-v4 uses \"diffuse\")\n"); }
const tpbrt_area_light_t* area_light = TPBRT_NULL;
if (tpbrt_get_area_light_by_handle(&scene.lights.area_lights, shape->area_light, &area_light) ==
printf(" emits: ");
example_print_spectrum(stdout, &area_light->as.diffuse.l);
printf(" scale=%g\n", area_light->scale);
}
}
const char* const wanted = argc > 2 ? argv[2] : "gold";
name.data = wanted;
name.size = strlen(wanted);
const tpbrt_material_t* found = TPBRT_NULL;
if (tpbrt_get_material_by_name(&scene.materials, name, &found) == TPBRT_ERROR_NONE) {
printf("\nmaterial \"%s\" found by name:\n", wanted);
print_material(&scene, found);
}
else { printf("\nmaterial \"%s\" not found\n", wanted); }
printf("\ntextures:\n");
for (size_t i = 0; i < scene.textures.count; ++i) {
const tpbrt_texture_t* const texture = &scene.textures.textures[i];
printf(" \"" EXAMPLE_STR_FMT "\": %s %s\n", EXAMPLE_STR_ARG(texture->name),
texture->type == TPBRT_TEXTURE_TYPE_FLOAT ? "float" : "spectrum",
EXAMPLE_NAME(example_texture_class_names, texture->tex_class));
printf(" tex1: ");
example_print_texture_handle(stdout, scene.textures, &texture->as.checkerboard.tex1);
printf("\n tex2: ");
example_print_texture_handle(stdout, scene.textures, &texture->as.checkerboard.tex2);
printf("\n uscale=%g vscale=%g\n", texture->mapping_as.uv.u_scale, texture->mapping_as.uv.v_scale);
}
}
return 0;
}
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_area_light_by_handle(const tpbrt_area_lights_list_t *area_lights_list, tpbrt_area_light_handle_t handle, const tpbrt_area_light_t **area_light)
Returns pointer to area light to which handle is provided.
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_error_t tpbrt_get_material_by_name(const tpbrt_materials_list_t *materials, tpbrt_string_t name, const tpbrt_material_t **material)
Returns pointer to material with given name.
@ TPBRT_MATERIAL_TYPE_DIFFUSE
Diffuse.
Definition material.h:36
@ TPBRT_MATERIAL_TYPE_COATED_DIFFUSE
Coated diffuse.
Definition material.h:28
@ TPBRT_MATERIAL_TYPE_MIX
Mix.
Definition material.h:46
@ TPBRT_MATERIAL_TYPE_DIELECTRIC
Dielectric.
Definition material.h:34
@ TPBRT_MATERIAL_TYPE_DIFFUSE_TRANSMISSION
Diffuse transmission.
Definition material.h:38
@ TPBRT_MATERIAL_TYPE_CONDUCTOR
Conductor.
Definition material.h:32
@ TPBRT_MATERIAL_CONDUCTOR_VALUE_TYPE_ETA_K
Conductor material value defined as 'eta' and 'k' values.
Definition material.h:142
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_TEXTURE_TYPE_FLOAT
Float texture.
Definition texture.h:58
@ TPBRT_TEXTURE_CLASS_CHECKER_BOARD
Checker board texture.
Definition texture.h:73
#define TPBRT_NULL
NULL value.
Definition fwd.h:19
tpbrt_spectrum_t l
Spectral distribution of the light's emitted radiance.
Definition light.h:176
PBRT area light representation.
Definition light.h:185
union tpbrt_area_light_t::@8 as
Value dependent on type value.
tpbrt_area_light_diffuse_params_t diffuse
Diffuse area light parameters.
Definition light.h:196
tpbrt_float_t scale
Scale factor that modulates the amount of light that the light source emits into the scene.
Definition light.h:189
tpbrt_area_lights_list_t area_lights
List of area lights.
Definition light.h:242
tpbrt_float_t thickness
Thickness of the medium between the two layers.
Definition material.h:133
tpbrt_texture_handle_t reflectance
Reflectance of the base diffuse layer.
Definition material.h:129
tpbrt_material_roughness_params_t roughness_params
Roughness parameters.
Definition material.h:131
Conductor material parameters.
Definition material.h:193
union tpbrt_material_conductor_params_t::@12 as
Value dependent on value_type value.
tpbrt_material_roughness_params_t roughness_params
Roughness parameters.
Definition material.h:212
tpbrt_texture_handle_t reflectance
Average reflectance of the conductor. Plausible values of "eta" and "k" are computed based on the ref...
Definition material.h:208
tpbrt_material_conductor_value_type_t value_type
Conductor's value type.
Definition material.h:214
tpbrt_texture_handle_t k
The conductor's wavelength-dependent absorption coefficient.
Definition material.h:204
struct tpbrt_material_conductor_params_t::@12::@13 conductor
Holds conductor 'eta' and 'k' values.
tpbrt_texture_handle_t eta
The wavelength-dependent index of refraction of the conductor.
Definition material.h:202
tpbrt_texture_handle_t eta
Index of refraction of the medium inside the surface or Wavelength-dependent index of refraction of t...
Definition material.h:225
tpbrt_material_roughness_params_t roughness_params
Roughness parameters.
Definition material.h:227
tpbrt_texture_handle_t reflectance
The reflectivity of the surface. Must be between 0 and 1.
Definition material.h:238
tpbrt_texture_handle_t transmittance
The transmissivity of the surface.
Definition material.h:251
tpbrt_texture_handle_t reflectance
The reflectivity of the surface.
Definition material.h:249
tpbrt_material_handle_t materials[2]
Handles of two materials to mix between.
Definition material.h:298
Material roughness parameters.
Definition material.h:100
tpbrt_texture_handle_t u_roughness
For surfaces with anisotropic roughness distributions, microfacet roughness in the u direction.
Definition material.h:104
tpbrt_texture_handle_t v_roughness
For surfaces with anisotropic roughness distributions, microfacet roughness in the v direction.
Definition material.h:106
tpbrt_bool_t remap_roughness
If true, roughness values are expected to be between 0 and 1, and are remapped to microfacet distribu...
Definition material.h:108
PBRT material representation.
Definition material.h:376
tpbrt_material_type_t type
Material value type.
Definition material.h:409
tpbrt_material_dielectric_params_t dielectric
Dielectric material parameters.
Definition material.h:391
tpbrt_material_mix_params_t mix
Mix material parameters.
Definition material.h:401
tpbrt_material_conductor_params_t conductor
Conductor material parameters.
Definition material.h:389
union tpbrt_material_t::@16 as
Value dependent on type value.
tpbrt_material_diffuse_transmission_params_t diffuse_transmission
Diffuse transmission material parameters.
Definition material.h:395
tpbrt_string_t name
Material name (if it is not named material name is empty).
Definition material.h:380
tpbrt_material_coated_diffuse_params_t coated_diffuse
Coated diffuse material parameters.
Definition material.h:385
tpbrt_material_diffuse_params_t diffuse
Diffuse material parameters.
Definition material.h:393
PBRT scene representation.
Definition scene.h:38
tpbrt_shapes_list_t shapes
List with all scene shapes.
Definition scene.h:68
tpbrt_lights_list_t lights
List with all scene lights.
Definition scene.h:66
tpbrt_materials_list_t materials
List with all scene materials.
Definition scene.h:62
tpbrt_textures_list_t textures
List with all scene textures.
Definition scene.h:60
PBRT shape representation.
Definition shape.h:218
tpbrt_area_light_handle_t area_light
Area light handle.
Definition shape.h:226
tpbrt_material_handle_t material
Material handle.
Definition shape.h:224
tpbrt_shape_type_t type
Shape value type.
Definition shape.h:232
tpbrt_size_t count
Number of shapes in list.
Definition shape.h:265
tpbrt_shape_t * shapes
Shapes list data.
Definition shape.h:263
Simple string representation (not always terminated with '\0', the size determines the length of the ...
Definition common.h:20
tpbrt_size_t size
String size.
Definition common.h:24
const tpbrt_char_t * data
String characters.
Definition common.h:22
tpbrt_texture_handle_t tex1
The texture to use for even checks.
Definition texture.h:121
tpbrt_texture_handle_t tex2
The texture to use for odd checks.
Definition texture.h:123
tpbrt_float_t u_scale
Scaling factor to be applied to the u texture coordinates.
Definition texture.h:332
tpbrt_float_t v_scale
Scaling factor to be applied to the v texture coordinates.
Definition texture.h:334
PBRT texture representation.
Definition texture.h:376
tpbrt_string_t name
Texture name.
Definition texture.h:386
tpbrt_texture_type_t type
Texture type.
Definition texture.h:380
union tpbrt_texture_t::@26 as
Value dependent on tex_class value.
tpbrt_texture_mapping_uv_params_t uv
UV texture mapping parameters.
Definition texture.h:391
union tpbrt_texture_t::@25 mapping_as
Value dependent on mapping value.
tpbrt_texture_checkerboard_params_t checkerboard
Checkerboard texture parameters.
Definition texture.h:405
tpbrt_texture_class_t tex_class
Texture class.
Definition texture.h:382
List of textures.
Definition texture.h:435
tpbrt_texture_t * textures
Textures list data.
Definition texture.h:437
tpbrt_size_t count
Number of textures in list.
Definition texture.h:439