libsf3
Loading...
Searching...
No Matches
sf3_model.h
Go to the documentation of this file.
1#ifndef __SF3_MODEL__
2#define __SF3_MODEL__
3#include "sf3_core.h"
4
6#define SF3_FORMAT_ID_MODEL 0x05
7
28
42
46 uint32_t count;
48 uint32_t faces[];
49};
50
54 uint32_t count;
56 float vertices[];
57};
58
63 struct sf3_identifier identifier;
69 uint32_t material_size;
70 sf3_str16 textures[];
71};
72
78 return (const sf3_str16 *)SF3_SKIP_STRP(texture);
79}
80
82SF3_INLINE const struct sf3_faces *sf3_model_faces(const struct sf3_model *model){
83 return (const struct sf3_faces *)(((char*)model->textures)+model->material_size);
84}
85
87SF3_INLINE const struct sf3_vertices *sf3_model_vertices(const struct sf3_model *model){
88 const struct sf3_faces *faces = sf3_model_faces(model);
89 return (const struct sf3_vertices *)(faces->faces+faces->count);
90}
91
93SF3_INLINE uint8_t sf3_model_texture_count(const struct sf3_model *model){
94 uint8_t type = model->material_type;
95 return ((type >> 0) & 1) +
96 ((type >> 1) & 1) +
97 ((type >> 2) & 1) +
98 ((type >> 3) & 1) +
99 ((type >> 4) & 1) +
100 ((type >> 5) & 1) +
101 ((type >> 6) & 1) +
102 ((type >> 7) & 1);
103}
104
106SF3_INLINE uint8_t sf3_model_vertex_stride(const struct sf3_model *model){
107 uint8_t type = model->vertex_format;
108 return
109 ((type >> 0) & 1) * 3 +
110 ((type >> 1) & 1) * 2 +
111 ((type >> 2) & 1) * 3 +
112 ((type >> 3) & 1) * 3 +
113 ((type >> 4) & 1) * 3;
114}
115
122 switch(format){
123 case SF3_VERTEX_POSITION: return 3;
124 case SF3_VERTEX_UV: return 2;
125 case SF3_VERTEX_COLOR: return 3;
126 case SF3_VERTEX_NORMAL: return 3;
127 case SF3_VERTEX_TANGENT: return 3;
128 default: return 0;
129 }
130}
131
133SF3_INLINE uint32_t sf3_model_vertex_count(const struct sf3_model *model){
134 return sf3_model_vertices(model)->count / sf3_model_vertex_stride(model);
135}
136
138SF3_EXPORT enum sf3_material_type sf3_model_texture_material(const struct sf3_model *model, uint8_t index){
139 uint8_t type = model->material_type;
140 for(uint8_t i=0; i<8; ++i){
141 uint8_t bit = (1 << i);
142 if(index == 0)
143 return bit;
144 if(type & bit){
145 index--;
146 }
147 }
148 return 0xFF;
149}
150
156 switch(type){
157 case SF3_MATERIAL_ALBEDO: return "albedo";
158 case SF3_MATERIAL_NORMAL: return "normal";
159 case SF3_MATERIAL_METALLIC: return "metallic";
160 case SF3_MATERIAL_METALNESS: return "metalness";
161 case SF3_MATERIAL_ROUGHNESS: return "roughness";
162 case SF3_MATERIAL_OCCLUSION: return "occlusion";
163 case SF3_MATERIAL_SPECULAR: return "specular";
164 case SF3_MATERIAL_EMISSION: return "emission";
165 default: return "Unknown";
166 }
167}
168
170SF3_EXPORT size_t sf3_model_size(const struct sf3_model *model){
171 const struct sf3_vertices *verts = sf3_model_vertices(model);
172 const void *start = (const void *)model;
173 const void *end = (const void *)verts;
174 return (end-start) + sizeof(uint32_t) + verts->count * sizeof(float);
175}
176#endif
#define SF3_INLINE
Definition sf3_core.h:16
#define SF3_EXPORT
Definition sf3_core.h:13
#define SF3_PACK
Definition sf3_core.h:9
#define SF3_SKIP_STRP(STR)
Definition sf3_core.h:64
SF3_EXPORT enum sf3_material_type sf3_model_texture_material(const struct sf3_model *model, uint8_t index)
Returns the material usage type for the given texture index in the model.
Definition sf3_model.h:138
sf3_vertex_format
The vertex attributes that can be stored, as bit-field flags.
Definition sf3_model.h:30
@ SF3_VERTEX_NORMAL
The vertex normal vector as an X, Y, Z triplet.
Definition sf3_model.h:38
@ SF3_VERTEX_TANGENT
The vertex tangent vector as an X, Y, Z triplet.
Definition sf3_model.h:40
@ SF3_VERTEX_POSITION
The vertex position vector as an X, Y, Z triplet.
Definition sf3_model.h:32
@ SF3_VERTEX_UV
The texture coordinates as a U, V duplet.
Definition sf3_model.h:34
@ SF3_VERTEX_COLOR
The vertex colour as an R, G, B triplet.
Definition sf3_model.h:36
SF3_INLINE uint8_t sf3_model_vertex_attribute_count(enum sf3_vertex_format format)
Definition sf3_model.h:121
SF3_INLINE uint8_t sf3_model_texture_count(const struct sf3_model *model)
Returns the number of textures stored in the model.
Definition sf3_model.h:93
SF3_INLINE uint8_t sf3_model_vertex_stride(const struct sf3_model *model)
Returns the number of floats per vertex.
Definition sf3_model.h:106
SF3_EXPORT size_t sf3_model_size(const struct sf3_model *model)
Computes the size of the model file in bytes.
Definition sf3_model.h:170
SF3_EXPORT const char * sf3_model_material_type(enum sf3_material_type type)
Definition sf3_model.h:155
SF3_INLINE const struct sf3_faces * sf3_model_faces(const struct sf3_model *model)
Returns the faces array of the model.
Definition sf3_model.h:82
SF3_INLINE uint32_t sf3_model_vertex_count(const struct sf3_model *model)
Returns the number of vertices stored in the model.
Definition sf3_model.h:133
SF3_INLINE const struct sf3_vertices * sf3_model_vertices(const struct sf3_model *model)
Returns the vertices array of the model.
Definition sf3_model.h:87
sf3_material_type
The material types that can be stored, as bit-field flags.
Definition sf3_model.h:9
@ SF3_MATERIAL_OCCLUSION
The texture stores the occlusion value.
Definition sf3_model.h:22
@ SF3_MATERIAL_ALBEDO
The texture stores the albedo colour.
Definition sf3_model.h:11
@ SF3_MATERIAL_EMISSION
The texture stores the emission colour.
Definition sf3_model.h:26
@ SF3_MATERIAL_SPECULAR
The texture stores the specular reflectivity value.
Definition sf3_model.h:24
@ SF3_MATERIAL_METALLIC
Definition sf3_model.h:16
@ SF3_MATERIAL_NORMAL
The texture stores the normal vectors.
Definition sf3_model.h:13
@ SF3_MATERIAL_METALNESS
The texture stores the metalness value.
Definition sf3_model.h:18
@ SF3_MATERIAL_ROUGHNESS
The texture stores the roughness value.
Definition sf3_model.h:20
SF3_INLINE const sf3_str16 * sf3_model_next_texture(const struct sf3_str16 *texture)
Definition sf3_model.h:77
An array of faces.
Definition sf3_model.h:44
uint32_t count
The number of vertex indices stored.
Definition sf3_model.h:46
uint32_t faces[]
The vertex indices, three of which make up a face.
Definition sf3_model.h:48
The basic header structure of every SF3 file.
Definition sf3_core.h:70
sf3_str16 textures[]
Definition sf3_model.h:70
uint32_t material_size
The size of the textures array in octets.
Definition sf3_model.h:69
uint8_t material_type
The material type description.
Definition sf3_model.h:67
uint8_t vertex_format
The vertex attribute format description.
Definition sf3_model.h:65
An array of vertex data.
Definition sf3_model.h:52
uint32_t count
The number of floats stored.
Definition sf3_model.h:54