Jo Engine 2026.11.1
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
model.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, Johannes Fetz (johannesfetz@gmail.com)
4** All rights reserved.
5**
6** Redistribution and use in source and binary forms, with or without
7** modification, are permitted provided that the following conditions are met:
8** * Redistributions of source code must retain the above copyright
9** notice, this list of conditions and the following disclaimer.
10** * Redistributions in binary form must reproduce the above copyright
11** notice, this list of conditions and the following disclaimer in the
12** documentation and/or other materials provided with the distribution.
13** * Neither the name of the Johannes Fetz nor the
14** names of its contributors may be used to endorse or promote products
15** derived from this software without specific prior written permission.
16**
17** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
77
78#ifndef __JO_MODEL_H__
79# define __JO_MODEL_H__
80
81#ifdef JO_COMPILE_WITH_3D_SUPPORT
82
84# define JO_3D_MODEL_VERSION (1)
85
86/*
87** Material flags (jo_3d_material.flags, low 16 bits: the engine, high 16 bits: free for the program, see jo_model.py)
88*/
90# define JO_3D_MATERIAL_DUAL_PLANE (1 << 0)
92# define JO_3D_MATERIAL_SORT_MIN (1 << 1)
94# define JO_3D_MATERIAL_SORT_MAX (1 << 2)
97# define JO_3D_MATERIAL_SORT_PREVIOUS (JO_3D_MATERIAL_SORT_MIN | JO_3D_MATERIAL_SORT_MAX)
99# define JO_3D_MATERIAL_GOURAUD (1 << 3)
101# define JO_3D_MATERIAL_HALF_TRANSPARENT (1 << 4)
103# define JO_3D_MATERIAL_SCREEN_DOORS (1 << 5)
105# define JO_3D_MATERIAL_LIGHT (1 << 6)
107# define JO_3D_MATERIAL_FLIP_H (1 << 7)
109# define JO_3D_MATERIAL_FLIP_V (1 << 8)
111# define JO_3D_MATERIAL_END_CODES (1 << 9)
113# define JO_3D_MATERIAL_MIPMAPS (1 << 10)
115# define JO_3D_MATERIAL_SUBDIVISION (1 << 11)
118# define JO_3D_MATERIAL_SUBDIVISION_NEAR (1 << 12)
120# define JO_3D_MATERIAL_USER(N) (1 << (16 + (N)))
121
123typedef struct
124{
125 char name[16]; // MTL newmtl (truncated, ended by a zero)
126 unsigned char r; // Color (MTL Kd): the polygons without texture
127 unsigned char g;
128 unsigned char b;
129 unsigned char shine; // MTL Ns (0 to 255, for the program)
130 short texture; // Index in the textures of the model, -1: no texture
131 unsigned short subdivision_error; // MTL jo_subdivision_error (pixels, see
132 // jo_3d_set_texture_subdivision_error()), 0: the default
133 unsigned int flags; // JO_3D_MATERIAL_xxx
134 unsigned int user; // MTL jo_value (for the program)
136
138typedef struct
139{
140 char name[16];
141 unsigned short first_point;
142 unsigned short point_count; // The points used only by the group (from first_point)
143 unsigned short first_polygon;
144 unsigned short polygon_count;
146
148# define JO_3D_MAX_BONES (32)
150# define JO_3D_BONE_NO_GROUP (0xFFFF)
152# define JO_3D_ANIMATION_LOOP (1 << 0)
154# define JO_3D_ANIMATION_TICKS_PER_SECOND (60)
155
157typedef struct
158{
159 char name[16];
160 unsigned short group; // Group moved by the bone (JO_3D_BONE_NO_GROUP: none)
161 short parent; // Index of the parent bone, -1: root
162 jo_pos3D_fixed pivot; // Joint (model space): the bone turns around it
163} jo_3d_bone;
164
166typedef struct
167{
168 char name[16];
169 unsigned short key_count;
170 unsigned short key_ticks; // Time between 2 keys (1/60 s)
171 unsigned short flags; // JO_3D_ANIMATION_xxx
172 unsigned short duplicate; // 0 in the file. Set at the loading: 1 when an animation before
173 // has the same name (see jo_3d_model_is_animation())
174 unsigned int offset; // Keys: offset from the count of the "ANIM" chunk
176
178typedef struct
179{
180 jo_pos3D_fixed root; // Offset of the whole model
181 jo_angle angles[JO_3D_MAX_BONES][3]; // Around X, Y, Z (0x10000 = 360 degrees)
182} jo_3d_pose;
183
191typedef void (*jo_3d_bone_callback)(const int bone, void * const param, const jo_fixed (* const matrix)[3]);
192
194typedef struct
195{
196 jo_3d_mesh *mesh; // Drawn with jo_3d_mesh_draw()
197 jo_pos3D_fixed center; // Bounding sphere (see jo_3d_is_sphere_visible())
199 unsigned int material_count;
201 unsigned short *polygon_materials; // Material of each polygon
202 unsigned int texture_count;
203 int *textures; // Sprite ids
204 unsigned int normal_count; // 0: no normals
206 unsigned short *corner_normals; // 4 per polygon: index in normals (JO_NULL: none)
207 jo_pos3D_fixed *vertex_colors; // r, g, b per point, 1.0 = JO_FIXED_1 (JO_NULL: none)
208 unsigned short gouraud_tables; // First gouraud table (polygon k: + k), 0: none
209 unsigned int group_count;
211 unsigned int bone_count; // 0: no skeleton
213 signed char *bone_children; // First child of each bone, then the next sibling of
214 // each bone (-1: none)
215 unsigned int animation_count;
217 const unsigned char *animation_data; // The "ANIM" chunk from its count
218 void *file; // Data of the file (jo_3d_load_model(): the points, the
219 // polygons, etc are in it), JO_NULL: copies
220 bool file_not_owned; // The data of the file belong to the caller
221 // (jo_3d_create_model_in_place()): not freed with the model
222 bool single_block; // The model, its mesh, the attributes and the texture ids
223 // in one memory block (jo_3d_create_model_in_place())
225
233jo_3d_model *jo_3d_create_model_from_memory(const unsigned char * const data, const unsigned int size);
234
243jo_3d_model *jo_3d_create_model_in_place(const unsigned char * const data, const unsigned int size);
244
245#ifdef JO_COMPILE_WITH_FS_SUPPORT
246
255jo_3d_model *jo_3d_load_model(const char * const filename);
256
262jo_3d_model *__jo_3d_load_model_for_lights(const char * const filename);
263
270int jo_3d_model_texture(const char * const filename);
271
277int jo_3d_model_loaded_texture(const char * const name);
278
279#endif
280
284void jo_3d_free_model(jo_3d_model * const model);
285
292void jo_3d_model_free_textures_from(const int sprite_id);
293
299const jo_3d_model_group *jo_3d_model_find_group(const jo_3d_model * const model, const char * const name);
300
306int jo_3d_model_find_bone(const jo_3d_model * const model, const char * const name);
307
313int jo_3d_model_find_animation(const jo_3d_model * const model, const char * const name);
314
322bool jo_3d_model_is_animation(const jo_3d_model * const model, const int animation, const char * const name);
323
329int jo_3d_model_animation_duration(const jo_3d_model * const model, const int animation);
330
337void jo_3d_model_get_pose(const jo_3d_model * const model, const int animation, int time,
338 jo_3d_pose * const pose);
339
346void jo_3d_pose_blend(const jo_3d_model * const model, jo_3d_pose * const pose,
347 const jo_3d_pose * const other, const int weight);
348
355void jo_3d_model_draw_pose(const jo_3d_model * const model, const jo_3d_pose * const pose,
356 const jo_3d_bone_callback callback, void * const param);
357
362void jo_3d_model_set_hidden_bones(const unsigned int mask);
363
368static __jo_force_inline void jo_3d_model_draw(const jo_3d_model * const model)
369{
370 if (model->bone_count > 0)
372 else
373 jo_3d_mesh_draw(model->mesh);
374}
375
376#endif /* !JO_COMPILE_WITH_3D_SUPPORT */
377
378#endif /* !__JO_MODEL_H__ */
379
380/*
381** END OF FILE
382*/
static __jo_force_inline void jo_3d_mesh_draw(jo_3d_mesh *const mesh)
Draw a mesh.
Definition 3d.h:995
Mesh structure.
Definition 3d.h:216
unsigned int normal_count
Definition model.h:204
jo_3d_model_group * groups
Definition model.h:210
short parent
Definition model.h:161
int jo_3d_model_loaded_texture(const char *const name)
Get the sprite id of a texture already loaded by the models (embedded in a model, or loaded by jo_3d_...
char name[16]
Definition model.h:168
void(*) jo_3d_bone_callback(const int bone, void *const param, const jo_fixed(*const matrix)[3])
Called by jo_3d_model_draw_pose() after each bone is drawn, with the matrix of the bone (the program ...
Definition model.h:191
void jo_3d_model_free_textures_from(const int sprite_id)
Free the sprites added since a sprite (the textures of the models loaded since, their tiles and small...
jo_pos3D_fixed pivot
Definition model.h:162
jo_3d_mesh * mesh
Definition model.h:196
unsigned short subdivision_error
Definition model.h:131
jo_pos3D_fixed center
Definition model.h:197
char name[16]
Definition model.h:140
unsigned int texture_count
Definition model.h:202
jo_pos3D_fixed * normals
Definition model.h:205
unsigned char g
Definition model.h:127
bool jo_3d_model_is_animation(const jo_3d_model *const model, const int animation, const char *const name)
Check if an animation is the one jo_3d_model_find_animation() finds for a name (faster: the name of t...
bool single_block
Definition model.h:222
unsigned short * polygon_materials
Definition model.h:201
void jo_3d_free_model(jo_3d_model *const model)
Free a model (the textures stay loaded).
unsigned int bone_count
Definition model.h:211
unsigned short gouraud_tables
Definition model.h:208
char name[16]
Definition model.h:125
unsigned int group_count
Definition model.h:209
void jo_3d_model_set_hidden_bones(const unsigned int mask)
Bones not drawn by the next jo_3d_model_draw_pose() (a hat shot off the head of a character: its bone...
jo_3d_bone * bones
Definition model.h:212
unsigned short key_ticks
Definition model.h:170
int jo_3d_model_find_bone(const jo_3d_model *const model, const char *const name)
Find a bone of a model.
unsigned short first_polygon
Definition model.h:143
jo_angle angles[(32)][3]
Definition model.h:181
unsigned int animation_count
Definition model.h:215
jo_3d_animation * animations
Definition model.h:216
jo_3d_model * __jo_3d_load_model_for_lights(const char *const filename)
Load a model with a gouraud table per polygon (Internal usage: the cells, lit by the point lights,...
unsigned short group
Definition model.h:160
jo_pos3D_fixed root
Definition model.h:180
void jo_3d_model_draw_pose(const jo_3d_model *const model, const jo_3d_pose *const pose, const jo_3d_bone_callback callback, void *const param)
Draw a model with a skeleton with the current matrix.
unsigned int offset
Definition model.h:174
jo_fixed radius
Definition model.h:198
signed char * bone_children
Definition model.h:213
jo_3d_model * jo_3d_create_model_from_memory(const unsigned char *const data, const unsigned int size)
Create a model from the content of a JOM file.
jo_3d_model * jo_3d_create_model_in_place(const unsigned char *const data, const unsigned int size)
Create a model that uses the content of a JOM file in place (like jo_3d_load_model(): the points,...
void * file
Definition model.h:218
static __jo_force_inline void jo_3d_model_draw(const jo_3d_model *const model)
Draw a model with the current matrix.
Definition model.h:368
char name[16]
Definition model.h:159
unsigned short first_point
Definition model.h:141
const unsigned char * animation_data
Definition model.h:217
unsigned int flags
Definition model.h:133
void jo_3d_pose_blend(const jo_3d_model *const model, jo_3d_pose *const pose, const jo_3d_pose *const other, const int weight)
Blend 2 poses (a smooth change of animation).
#define JO_3D_MAX_BONES
Bones of a model at most (jo_3d_pose).
Definition model.h:148
jo_3d_material * materials
Definition model.h:200
unsigned short duplicate
Definition model.h:172
int jo_3d_model_texture(const char *const filename)
Get the sprite id of a texture of the models (loaded the first time: a texture shared by several mode...
unsigned char r
Definition model.h:126
jo_3d_model * jo_3d_load_model(const char *const filename)
Load a model (JOM file) from the current directory of the CD.
int jo_3d_model_find_animation(const jo_3d_model *const model, const char *const name)
Find an animation of a model.
unsigned char b
Definition model.h:128
unsigned short polygon_count
Definition model.h:144
bool file_not_owned
Definition model.h:220
int * textures
Definition model.h:203
const jo_3d_model_group * jo_3d_model_find_group(const jo_3d_model *const model, const char *const name)
Find a group of a model.
unsigned short * corner_normals
Definition model.h:206
short texture
Definition model.h:130
unsigned short key_count
Definition model.h:169
int jo_3d_model_animation_duration(const jo_3d_model *const model, const int animation)
Duration of an animation.
unsigned int material_count
Definition model.h:199
void jo_3d_model_get_pose(const jo_3d_model *const model, const int animation, int time, jo_3d_pose *const pose)
Pose of a skeleton at a time of an animation (the keys are interpolated).
unsigned char shine
Definition model.h:129
unsigned short point_count
Definition model.h:142
jo_pos3D_fixed * vertex_colors
Definition model.h:207
unsigned int user
Definition model.h:134
unsigned short flags
Definition model.h:171
Animation of a skeleton (the same layout as the file: 28 bytes).
Definition model.h:167
Bone of a rigid skeleton (the same layout as the file: 32 bytes).
Definition model.h:158
Material of a model (the same layout as the file: 32 bytes).
Definition model.h:124
3D model loaded from a JOM file
Definition model.h:195
Group of a model: OBJ o or g (the same layout as the file: 24 bytes).
Definition model.h:139
Pose of a skeleton: the angles of the bones (see jo_3d_model_get_pose() and jo_3d_model_draw_pose()).
Definition model.h:179
#define JO_NULL
NULL implementation.
Definition tools.h:239
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
short jo_angle
Angle.
Definition types.h:54
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:48
3D position using fixed numbers
Definition types.h:92