LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
lg_scene.c File Reference

Functions

LG_Scene lg_scene_new (int id, LG_Camera *cam1, LG_Light light1, Rec2Df landscape_rec, uint16_t grid_width)
 
void lg_scene_free (LG_Scene *scene)
 
LG_SceneNodelg_scenenode_new_and_set (int id, LG_SceneNode *parent_node, const char *mesh_file, LG_LoadMesh_Flags flags, vec3_t transl, LG_Quat orientation, vec3_t scaling)
 
LG_SceneNodelg_generate_scenenodes_from_scene_data (LG_Scene *scene, LG_SceneNode *top_node, LG_LoadMesh_Flags flags)
 
mat4_t lg_get_TRS_matrix_from_TOS_v (vec3_t transl, LG_EulerAng orientation, vec3_t scaling, const char *rot_order)
 
zboolean lg_set_TRS_matrix_from_node_NTOS (mat4_t *m_TRS, LG_Node_NTOS *tos)
 
void lg_node_NTOS_array_info (LG_Scene *scene)
 
void lg_node_NTOS_array_info_to_stream (LG_Scene *scene, FILE *s)
 
zboolean lg_scene_load_data_from_assets (LG_Scene *scene, const char *path)
 
zboolean lg_scene_load_data_from_file (LG_Scene *scene, const char *path)
 
zboolean lg_scene_save_data_to_file (LG_Scene *scene, const char *path)
 
zboolean lg_create_wr_scenes_dir ()
 

Detailed Description

 === Higher level scene/frame stuff ===

 The starting point for scenes

 SERIALIZING nodes hierarchy with LG_Node_NTOS[]

Function Documentation

◆ lg_scene_new()

LG_Scene lg_scene_new ( int  id,
LG_Camera cam1,
LG_Light  light1,
Rec2Df  landscape_rec,
uint16_t  grid_width 
)

Create and init a new scene

Can not contain more than NODE_NTOS_MAX_NUM mesh references

LG_Node_NTOS and LG_Scene are defined in lg_scene_graph.h

// === Scene node serializable data ===
// NTOS stands for (mesh file) Name and (scene node) TOS (Transl/Orientation/Scaling)
// Use float for LG_EulerAng
typedef union {
struct {
// Name
char name[LG_MESH_NAME_MAX_LEN + 1]; // OBJ/FBX basename
int32_t id; // User-defined - SHOULD BE UNIQUE -> always > 0, 0 = top node
int32_t parent_id; // Set to < 0 if node = top node
// Transl.
float x_t;
float y_t;
float z_t;
// Orientation as LG_EulerAng
float x_o;
float y_o;
float z_o;
// Scaling
float x_s;
float y_s;
float z_s;
// Rot. order
char rot_order[4];
};
struct {
char name2[LG_MESH_NAME_MAX_LEN + 1];
int32_t id2;
int32_t parent_id2;
float v[9];
char rot_order2[4];
};
enum {SCENE_ROOT_NODE_ID = 0, SCENE_GRID_NODE_ID, SCENE_XYZ_ARROWS_NODE_ID, SCENE_FIRST_OBJ_NODE_ID};
typedef struct {
// Serializable data
int32_t id; // Set to -1 on error
LG_Light light1;
Rec2Df landscape_rec;
LG_Node_NTOS nodes_ntos[NODE_NTOS_MAX_NUM];
int32_t n_node_ntos;
// Not serializable data
LG_Camera *cam1;
LG_SceneNode *root;
LG_SceneNode *grid;
LG_SceneNode *xyz_arrows;
LG_SceneNode *first_obj_node;

Generated nodes:

  • root -> parent of grid and xyz_arrows, id = 0, type = ROOT
  • grid -> with an initialized Lines3D_VB horizontal grid, id = 1, type = GRID
  • xyz_arrows -> with an initialized Lines3D_VB xyz_arrows, id = 2, type = LINES3D

New nodes ids added to the scene should start at or above SCENE_FIRST_OBJ_NODE_ID (id = 3)

Parameters
idScene's id, set to -1 on error
cam1A pointer to a LG_Camera
light1A LG_Light
landscape_recA Rec2Df, should be centered
grid_widthGrid width, ie number of units along one row or one column, should be even - if odd, we substract one
Returns
An initialized LG_Scene if OK, id set to -1 on error

◆ lg_scene_free()

void lg_scene_free ( LG_Scene scene)

Free scene root, grid, and xyz_arrows nodes

Parameters
sceneA LG_Scene

◆ lg_scenenode_new_and_set()

LG_SceneNode* lg_scenenode_new_and_set ( int  id,
LG_SceneNode parent_node,
const char *  mesh_file,
LG_LoadMesh_Flags  flags,
vec3_t  transl,
LG_Quat  orientation,
vec3_t  scaling 
)

Helper func to create and set a new LG_SceneNode instance

Create node, add node to scene graph, load node's mesh, and set node's local matrix

Helper macros for less confusion:

#define transl_v3(x, y, z) vec3(x, y, z)
#define scaling_v3(x, y, z) vec3(x, y, z)

Mesh must be freed when done with the scene

Parameters
idNode's id
parent_nodeA pointer to a scene graph node
mesh_fileRelative path to mesh file to load, inside the mesh folder
flagsApply to mesh: force_reload, invert_z, normalize_xyz, horiz_center, vert_center, vert_bottom
translTranslation/position vec3
orientationOrientation quat
scalingScaling vec3
Returns
A pointer to a LG_SceneNode if OK, NULL on error - must be freed afterwards

◆ lg_generate_scenenodes_from_scene_data()

LG_SceneNode* lg_generate_scenenodes_from_scene_data ( LG_Scene scene,
LG_SceneNode top_node,
LG_LoadMesh_Flags  flags 
)

Generate scene nodes (LG_SceneNode *) from a LG_Scene serializable data and mesh files by parsing scene's (LG_Node_NTOS) nodes_ntos[NODE_NTOS_MAX_NUM]

Will create and set scene nodes from an array of LG_Node_NTOS (deserialize nodes tree)

NTOS stands for (mesh file) Name and (scene node) TOS (Transl/Orientation/Scaling)

Generated scene nodes id should start at or above SCENE_FIRST_OBJ_NODE_ID (id = 3)

You can then add top_node to your scene root node

Example code:

LG_LoadMesh_Flags flags1 = lg_loadmesh_flags(FALSE, FALSE, NORMALIZE_XYZ, HORIZ_CENTER, FALSE, VERT_BOTTOM);
// Load scene1 serializable data from file
if (!lg_scene_load_data_from_file(&lev->scene, lg_app_wr_file("scenes/scene_1.scene"))) {
if (question_win("'scenes/scene_1.scene' not found\nCreate, save to file, and use default one") == YES) {
INFO_ERR("No scene file available - will create, save to file, and use default one\n")
// SCENE_FIRST_OBJ_NODE_ID = 3
LG_Node_NTOS node_ntos_set1[] = {
{{"obj/shark1.obj", 4, 3, 0, 40, 0, 0, 0, 0, 25, 25, 25, ROT_ORDER}},
{{"obj/pikachu2.obj", 5, 3, -50, 0, 50, 0, 0, 0, 25, 25, 25, ROT_ORDER}},
{{"obj/tree2.obj", 6, 3, 50, 0, 50, 0, 0, 0, 25, 25, 25, ROT_ORDER}},
{{"obj/windmill1.obj", 7, 3, -50, 0, -50, 0, 0, 0, 25, 25, 25, ROT_ORDER}},
{{"obj/cube3.obj", 8, 3, 50, 0, -50, 0, 0, 0, 12, 12, 12, ROT_ORDER}},
{{"obj/airplane.obj", 9, 6, 4, 1, -2, 0, 0, 0, 1.5, 1.5, 1.5, ROT_ORDER}}};
lev->scene.n_node_ntos = (int)(sizeof(node_ntos_set1) / sizeof(LG_Node_NTOS));
for (i = 0; i < lev->scene.n_node_ntos; i++) {
lev->scene.nodes_ntos[i] = node_ntos_set1[i];
}
lg_scene_save_data_to_file(&lev->scene, lg_app_wr_file("scenes/scene_1.scene"));
}
}
// Meshes - continue
lev->scene.first_obj_node = lg_scenenode_new(SCENE_FIRST_OBJ_NODE_ID, "first_obj_node", FIRST_OBJ);
if (lev->scene.first_obj_node == NULL) return;
lev->scene.first_obj_node = lg_generate_scenenodes_from_scene_data(&lev->scene, lev->scene.first_obj_node, lev->flags1);
if (lev->scene.first_obj_node == NULL) return;
lg_scenenode_add_child(lev->scene.root, lev->scene.first_obj_node);
Parameters
scenePointer to a LG_Scene
top_nodeA scene node
flagsMesh loading flags
Returns
top_node

◆ lg_get_TRS_matrix_from_TOS_v()

mat4_t lg_get_TRS_matrix_from_TOS_v ( vec3_t  transl,
LG_EulerAng  orientation,
vec3_t  scaling,
const char *  rot_order 
)

Create a TRS matrix (Transl/Rotation/Scaling) from TOS values (Transl/Orientation/Scaling)

Parameters
translTranslation vector
orientationOrientation LG_EulerAng
scalingScaling vector

◆ lg_set_TRS_matrix_from_node_NTOS()

zboolean lg_set_TRS_matrix_from_node_NTOS ( mat4_t m_TRS,
LG_Node_NTOS tos 
)

Set an existing TRS matrix (Transl/Rotation/Scaling) from a LG_Node_NTOS

Parameters
m_TRSPointer to a mat4_t
tosPointer to a LG_Node_NTOS
scalingScaling vector

◆ lg_node_NTOS_array_info()

void lg_node_NTOS_array_info ( LG_Scene scene)

Print out LG_Scene's nodes_ntos[] info

Parameters
sceneA pointer to a LG_Scene

◆ lg_node_NTOS_array_info_to_stream()

void lg_node_NTOS_array_info_to_stream ( LG_Scene scene,
FILE *  s 
)

Send LG_Scene's nodes_ntos[] info to stream (as text)

Parameters
sceneA pointer to a LG_Scene
sStream

◆ lg_scene_load_data_from_assets()

zboolean lg_scene_load_data_from_assets ( LG_Scene scene,
const char *  path 
)

Load LG_Scene serializable data from a file in the ASSETS/SCENES_DIR folder

Parameters
scenePointer to a LG_Scene
pathRelative path to scene file, inside env->assets_dir/scenes
Returns
A new LG_Scene, id set to -1 on error

◆ lg_scene_load_data_from_file()

zboolean lg_scene_load_data_from_file ( LG_Scene scene,
const char *  path 
)

Load LG_Scene serializable data from a file in the APP WRITABLE folder

Parameters
scenePointer to a LG_Scene
pathPath to scene file, inside env->app_wr_dir
Returns
TRUE if OK, FALSE otherwise

◆ lg_scene_save_data_to_file()

zboolean lg_scene_save_data_to_file ( LG_Scene scene,
const char *  path 
)

Save LG_Scene serializable data to a file in the APP WRITABLE folder

Parameters
scenePointer a to LG_Scene
pathPath to scene file, inside env->app_wr_dir
Returns
TRUE if OK, FALSE on error

◆ lg_create_wr_scenes_dir()

zboolean lg_create_wr_scenes_dir ( )

Create the scene folder (SCENES_DIR) in the APP WRITABLE folder

Returns
TRUE if OK, FALSE on error
LG_LoadMesh_Flags
Definition: lg_mesh.h:119
Rec2Df
Definition: lg_gr_func.h:56
lg_scenenode_new
LG_SceneNode * lg_scenenode_new(int id, const char *name, lg_scenenode_type type)
Definition: lg_scene_graph.c:31
LG_SceneNode
Definition: lg_scene_graph.h:40
LG_Scene
Definition: lg_scene_graph.h:102
LG_Light
Definition: lg_light.h:26
lg_scenenode_add_child
void lg_scenenode_add_child(LG_SceneNode *node, LG_SceneNode *child)
Definition: lg_scene_graph.c:91
LG_Camera
Definition: lg_camera.h:22
LG_Node_NTOS
Definition: lg_scene_graph.h:64
lg_loadmesh_flags
LG_LoadMesh_Flags lg_loadmesh_flags(zboolean force_reload, zboolean invert_z, zboolean normalize_xyz, zboolean horiz_center, zboolean vert_center, zboolean vert_bottom)
Definition: lg_mesh.c:532
lg_scene_save_data_to_file
zboolean lg_scene_save_data_to_file(LG_Scene *scene, const char *path)
Definition: lg_scene.c:507
lg_app_wr_file
const char * lg_app_wr_file(const char *file_name)
Definition: lg_dirs_stuff.c:190
lg_scene_load_data_from_file
zboolean lg_scene_load_data_from_file(LG_Scene *scene, const char *path)
Definition: lg_scene.c:478
lg_generate_scenenodes_from_scene_data
LG_SceneNode * lg_generate_scenenodes_from_scene_data(LG_Scene *scene, LG_SceneNode *top_node, LG_LoadMesh_Flags flags)
Definition: lg_scene.c:283