loader_aw

Load OBJ/MTL file formats
git clone git://git.meso-star.fr/loader_aw.git
Log | Files | Refs | README | LICENSE

aw.h (6954B)


      1 /* Copyright (C) 2014-2017, 2020-2023 Vincent Forest (vaplv@free.fr)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU Lesser General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU Lesser General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef AW_H
     17 #define AW_H
     18 
     19 #include <rsys/rsys.h>
     20 #include <rsys/str.h>
     21 
     22 #ifdef AW_SHARED_BUILD
     23   #define AW_API extern EXPORT_SYM
     24 #elif defined(AW_STATIC_BUILD)
     25   #define AW_API extern LOCAL_SYM
     26 #else
     27   #define AW_API extern IMPORT_SYM
     28 #endif
     29 
     30 #ifndef NDEBUG
     31   #define AW(Func) ASSERT(aw_##Func == RES_OK)
     32 #else
     33   #define AW(Func) aw_##Func
     34 #endif
     35 
     36 #define AW_ID_NONE ((size_t)(-1))
     37 
     38 enum aw_color_space {
     39   AW_COLOR_RGB,
     40   AW_COLOR_XYZ
     41 };
     42 
     43 enum aw_map_flag {
     44   AW_MAP_BLEND_U = BIT(0),
     45   AW_MAP_BLEND_V = BIT(1),
     46   AW_MAP_COLOR_CORRECTION = BIT(2),
     47   AW_MAP_CLAMP = BIT(3)
     48 };
     49 
     50 enum aw_map_channel {
     51   AW_MAP_CHANNEL_RED,
     52   AW_MAP_CHANNEL_GREEN,
     53   AW_MAP_CHANNEL_BLUE,
     54   AW_MAP_CHANNEL_MATTE,
     55   AW_MAP_CHANNEL_LUMINANCE,
     56   AW_MAP_CHANNEL_DEPTH
     57 };
     58 
     59 struct aw_obj_desc {
     60   size_t faces_count;
     61   size_t positions_count;
     62   size_t normals_count;
     63   size_t texcoords_count;
     64   size_t groups_count;
     65   size_t smooth_groups_count;
     66   size_t usemtls_count;
     67   size_t mtllibs_count;
     68 };
     69 
     70 struct aw_obj_face {
     71   size_t vertex_id; /* Index of the first face vertex */
     72   size_t vertices_count;
     73   size_t group_id; /* Index of the face group */
     74   size_t smooth_group_id; /* Index of the face smooth group */
     75   size_t mtl_id; /* Index of the face material */
     76 };
     77 
     78 struct aw_obj_named_group {
     79   const char* name;
     80   size_t face_id;
     81   size_t faces_count;
     82 };
     83 
     84 struct aw_obj_smooth_group {
     85   size_t face_id; /* Index of the first smooth group face */
     86   size_t faces_count;
     87   char is_smoothed;
     88 };
     89 
     90 struct aw_obj_vertex {
     91   size_t position_id;
     92   size_t texcoord_id;
     93   size_t normal_id;
     94 };
     95 
     96 struct aw_obj_vertex_data {
     97   double position[4];
     98   double normal[3];
     99   double texcoord[3];
    100 };
    101 
    102 struct aw_color {
    103   double value[3];
    104   enum aw_color_space color_space;
    105 };
    106 
    107 struct aw_map {
    108   const char* filename; /* NULL <=> Not defined */
    109   int options_mask;
    110   double image_bias; /* Scalar to add to the image pixels */
    111   double image_scale; /* Scalar to multiply to the image pixels */
    112   double texcoord_bias[3];
    113   double texcoord_scale[3];
    114   double texcoord_turbulence[3];
    115   size_t resolution; /* image size = resolution x resolution */
    116   enum aw_map_channel scalar; /* Channel used to create a scalar texture */
    117   double bump_multiplier; /* Only available on bump maps */
    118 };
    119 
    120 struct aw_material {
    121   const char* name;
    122   struct aw_color ambient;
    123   struct aw_color diffuse;
    124   struct aw_color specular;
    125   struct aw_color transmission;
    126   double specular_exponent;
    127   double refraction_index;
    128   size_t illumination_model; /* In [0, 10] */
    129   struct aw_map ambient_map;
    130   struct aw_map diffuse_map;
    131   struct aw_map specular_map;
    132   struct aw_map specular_exponent_map; /* Scalar texture */
    133   struct aw_map bump_map; /* Scalar texture with valid bump multiplier */
    134 };
    135 
    136 struct aw_obj;
    137 struct aw_mtl;
    138 struct logger;
    139 struct mem_allocator;
    140 
    141 BEGIN_DECLS
    142 
    143 /*******************************************************************************
    144  * Obj functions
    145  ******************************************************************************/
    146 AW_API res_T
    147 aw_obj_create
    148   (struct logger* logger, /* NULL <=> use default logger*/
    149    struct mem_allocator* allocator, /* NULL <=> use default allocator */
    150    const int verbose, /* Verbosity level */
    151    struct aw_obj** obj);
    152 
    153 AW_API res_T
    154 aw_obj_ref_get
    155   (struct aw_obj* obj);
    156 
    157 AW_API res_T
    158 aw_obj_ref_put
    159   (struct aw_obj* obj);
    160 
    161 AW_API res_T
    162 aw_obj_load
    163   (struct aw_obj* obj,
    164    const char* filename);
    165 
    166 AW_API res_T
    167 aw_obj_load_stream
    168   (struct aw_obj* obj,
    169    FILE* stream,
    170    const char* stream_name); /* May be NULL <=> default name, i.e. "stream" */
    171 
    172 AW_API res_T
    173 aw_obj_clear
    174   (struct aw_obj* obj);
    175 
    176 /* Clear and release the internal memory */
    177 AW_API res_T
    178 aw_obj_purge
    179   (struct aw_obj* obj);
    180 
    181 AW_API res_T
    182 aw_obj_get_desc
    183   (const struct aw_obj* obj,
    184    struct aw_obj_desc* desc);
    185 
    186 AW_API res_T
    187 aw_obj_get_face
    188   (const struct aw_obj* obj,
    189    const size_t face_id,
    190    struct aw_obj_face* face);
    191 
    192 AW_API res_T
    193 aw_obj_get_group
    194   (const struct aw_obj* obj,
    195    const size_t group_id,
    196    struct aw_obj_named_group* group);
    197 
    198 AW_API res_T
    199 aw_obj_get_smooth_group
    200   (const struct aw_obj* obj,
    201    const size_t smooth_group_id,
    202    struct aw_obj_smooth_group* smooth_group);
    203 
    204 AW_API res_T
    205 aw_obj_get_mtl
    206   (const struct aw_obj* obj,
    207    const size_t mtl_id,
    208    struct aw_obj_named_group* mtl);
    209 
    210 AW_API res_T
    211 aw_obj_get_mtllib
    212   (const struct aw_obj* obj,
    213    const size_t mtllib_id,
    214    const char** mtllib);
    215 
    216 AW_API res_T
    217 aw_obj_get_vertex
    218   (const struct aw_obj* obj,
    219    const size_t vertex_id,
    220    struct aw_obj_vertex* vertex);
    221 
    222 AW_API res_T
    223 aw_obj_get_vertex_data
    224   (const struct aw_obj* obj,
    225    const struct aw_obj_vertex* vertex,
    226    struct aw_obj_vertex_data* vertex_data);
    227 
    228 AW_API res_T
    229 aw_obj_get_positions
    230   (const struct aw_obj* obj,
    231    const double** positions); /* List of double 4 */
    232 
    233 AW_API res_T
    234 aw_obj_get_texcoords
    235   (const struct aw_obj* obj,
    236    const double** texcoords); /* List of double 3 */
    237 
    238 AW_API res_T
    239 aw_obj_get_normals
    240   (const struct aw_obj* obj,
    241    const double** normals); /* List of double 3 */
    242 
    243 /*******************************************************************************
    244  * Mtl functions
    245  ******************************************************************************/
    246 AW_API res_T
    247 aw_mtl_create
    248   (struct logger* logger, /* NULL <=> use default logger */
    249    struct mem_allocator* allocator, /* NULL <=> use default allocator */
    250    const int verbose, /* Verbosity level */
    251    struct aw_mtl** mtl);
    252 
    253 AW_API res_T
    254 aw_mtl_ref_get
    255   (struct aw_mtl* mtl);
    256 
    257 AW_API res_T
    258 aw_mtl_ref_put
    259   (struct aw_mtl* mtl);
    260 
    261 AW_API res_T
    262 aw_mtl_load
    263   (struct aw_mtl* mtl,
    264    const char* filename);
    265 
    266 AW_API res_T
    267 aw_mtl_load_stream
    268   (struct aw_mtl* mtl,
    269    FILE* stream,
    270    const char* stream_name); /* May be NULL <=> default name, i.e. "stream" */
    271 
    272 AW_API res_T
    273 aw_mtl_clear
    274   (struct aw_mtl* mtl);
    275 
    276 /* Clear and release the internal memory */
    277 AW_API res_T
    278 aw_mtl_purge
    279   (struct aw_mtl* mtl);
    280 
    281 AW_API res_T
    282 aw_mtl_get_materials_count
    283   (struct aw_mtl* mtl,
    284    size_t* materials_count);
    285 
    286 /* The filled material must be released by the aw_material_release function */
    287 AW_API res_T
    288 aw_mtl_get_material
    289   (struct aw_mtl* mtl,
    290    const size_t imaterial,
    291    struct aw_material* material);
    292 
    293 END_DECLS
    294 
    295 #endif /* AW_H */
    296