scene.h (2889B)
1 #ifndef SCENE_H 2 #define SCENE_H 3 4 #include "common.h" /* for IN */ 5 #include "vec3.h" /* for Vec3 */ 6 7 #include <stdint.h> /* for uint32_t */ 8 #include <stdlib.h> /* for free */ 9 10 typedef struct { 11 /* Number of vertices */ 12 uint32_t num_vertices; 13 /* Vertices coordinates. Size [num_vertices] */ 14 Vec3 *vs; 15 /* Number of triangles */ 16 uint32_t num_triangles; 17 /* Triangles indices. Size [num_triangles * 3] */ 18 uint32_t *is; 19 /* Index of the material in g_hrt_materials array defined in scene.h */ 20 uint32_t material_index; 21 /* Global cartesian velocity vector */ 22 Vec3 velocity; 23 /* Normals of the triangles. Size [num_triangles] */ 24 /* NOTE: This field is not stored in the HRT file. 25 The loader does not load, allocate or calculate it. */ 26 Vec3 *ns; 27 } Mesh; 28 29 typedef struct { 30 uint32_t num_meshes; 31 Mesh *meshes; 32 } Scene; 33 34 typedef struct { 35 /* Number of characters in the name */ 36 uint32_t name_sz; 37 /* Name of the material. Not null-terminated. */ 38 const char *name; 39 /* Relative permitivity and conductivity properties. 40 * Refer to ITU-R P.2040-3 Table 3. 41 */ 42 float a, b, c, d; 43 /* Scattering coefficient. 44 * Ratio between specular and diffuse reflection power. 45 * Must be in [0, 1]. 46 */ 47 float s; 48 /* Scattering pattern distribution ratios. 49 * Each must be in [0, 1]. 50 * The sum of all ratios must be 1. 51 * s1: around specular (Directive Model) 52 * s2: around surface normal (Lambertian Model) 53 * s3: around incident direction (Backward Lobe Model) 54 */ 55 float s1, s2, s3; 56 /* Directive model parameters. 57 * s1_alpha: lobe width integer parameter (TODO ref eq). 58 * Must be > 0. 59 */ 60 uint8_t s1_alpha; 61 /* Backward lobe model parameters. 62 * s3_alpha: lobe width integer parameter (TODO ref eq). 63 * Must be > 0. 64 */ 65 uint8_t s3_alpha; 66 } Material; 67 68 /** Free the memory allocated for a mesh fields. 69 * 70 * \param mesh pointer to the mesh to free 71 */ 72 static inline void free_mesh(Mesh* mesh) { 73 free(mesh->vs); 74 free(mesh->is); 75 free(mesh->ns); 76 } 77 /** Deep free the scene. Frees all the meshes. 78 * 79 * \param scene pointer to the scene to free 80 */ 81 static inline void free_scene(Scene* scene) { 82 for (uint32_t i = 0; i < scene->num_meshes; i++) { 83 free_mesh(&scene->meshes[i]); 84 } 85 free(scene->meshes); 86 } 87 88 /** Save a scene to a HRT file. (See README for details) 89 * 90 * NOTE: This function does not save the normals of the triangles (Mesh.ns field). 91 * 92 * \param scene pointer to the scene to save 93 * \param filepath path to the HRT file. Will be overwritten if exists. 94 */ 95 void scene_save(IN Scene* scene, IN const char* filepath); 96 97 /** Load a mesh from a HRT file. 98 * 99 * NOTE: This function does not load, allocate or calculate 100 * the normals of the triangles (Mesh.ns field). 101 * 102 * \param filepath path to the HRT file 103 * \return the loaded scene 104 */ 105 Scene scene_load(IN const char *filepath); 106 107 #endif /* SCENE_H */