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

Functions

LG_SceneNodelg_scenenode_new (int id, const char *name, lg_scenenode_type type)
 
void lg_scenenode_add_child (LG_SceneNode *node, LG_SceneNode *child)
 
void lg_scenenode_remove_child (LG_SceneNode *node, LG_SceneNode *child)
 
void lg_scenenode_free (LG_SceneNode *node)
 
LG_SceneNodelg_scenenode_get_root (LG_SceneNode *node)
 
int lg_scenenode_remove_and_free_leaf (LG_SceneNode *node)
 
void lg_scenenode_remove_and_free_all (LG_SceneNode *node)
 
void lg_traverse_scene_graph (LG_SceneNode *node, int(*func)(LG_SceneNode *))
 
void lg_traverse_scene_graph_2 (LG_SceneNode *node, int(*func)(LG_SceneNode *, LG_Scene *), LG_Scene *scene)
 
LG_SceneNodelg_scenenode_find_by_id (LG_SceneNode *node, int id)
 
LG_SceneNodelg_scenenode_find_by_name (LG_SceneNode *node, const char *name)
 
void lg_scenenode_reset_local_matrix (LG_SceneNode *node)
 
void lg_scenenode_set_local_matrix (LG_SceneNode *node, vec3_t scaling, vec3_t transl, LG_Quat rot)
 
void lg_scenenode_update_local_matrix (LG_SceneNode *node, vec3_t scaling, vec3_t transl, LG_Quat rot)
 
void lg_scenenode_update_world_matrix_tree (LG_SceneNode *node, mat4_t *parent_world_matrix)
 
void lg_scenenode_set_world_matrix (LG_SceneNode *node, vec3_t scaling, vec3_t transl, LG_Quat rot)
 
void lg_scenenode_attach_to_camera (LG_SceneNode *node, const LG_Camera *cam, vec3_t transl)
 
int lg_scenenode_clone_worldm_l3dvb (LG_SceneNode *dest_node, LG_SceneNode *src_node)
 
void lg_traverse_printout_scene_graph (LG_SceneNode *node, zboolean extra_info)
 
void lg_scenenode_info (LG_SceneNode *node)
 
int lg_scenenode_info2 (LG_SceneNode *node)
 
const char * lg_scenenode_type_string (int type)
 

Detailed Description

    === Scene graph implementation ===

Function Documentation

◆ lg_scenenode_new()

LG_SceneNode* lg_scenenode_new ( int  id,
const char *  name,
lg_scenenode_type  type 
)

Create a new LG_SceneNode instance

local_matrix and world_matrix are initialized as id matrix

Parameters
idNode's id
nameNode's name
typeOne of ROOT, MESH, LINES3D, GRID, LANDSCAPE, SKYBOX, LIGHT, SHADER_ONLY, UNDEFINED
sceneA pointer to a LG_Scene
Returns
A pointer to a LG_SceneNode if OK, NULL on error - must be freed afterwards

◆ lg_scenenode_add_child()

void lg_scenenode_add_child ( LG_SceneNode node,
LG_SceneNode child 
)

Add child node

Parameters
nodeA pointer to a LG_SceneNode
childA pointer to a LG_SceneNode

◆ lg_scenenode_remove_child()

void lg_scenenode_remove_child ( LG_SceneNode node,
LG_SceneNode child 
)

Find and remove child by id (but doesn't free)

Parameters
nodeA pointer to a LG_SceneNode
childA pointer to a LG_SceneNode

◆ lg_scenenode_free()

void lg_scenenode_free ( LG_SceneNode node)

Free a node and all allocated resources

You can also use lg_scenenode_free2() experimental macro, defined in lg_scenegraph.h

Parameters
nodeA pointer to a LG_SceneNode

◆ lg_scenenode_get_root()

LG_SceneNode* lg_scenenode_get_root ( LG_SceneNode node)

Get the root node

Parameters
nodeA pointer to a LG_SceneNode
Returns
The root LG_SceneNode if OK, NULL on error

◆ lg_scenenode_remove_and_free_leaf()

int lg_scenenode_remove_and_free_leaf ( LG_SceneNode node)

Remove and free a leaf node

Parameters
nodeA pointer to a LG_SceneNode
Returns
LG_OK if OK, LG_ERROR if node is NULL, or node is not a leaf, or node is the root node

◆ lg_scenenode_remove_and_free_all()

void lg_scenenode_remove_and_free_all ( LG_SceneNode node)

Remove and free all nodes but root

Parameters
nodeA pointer to a LG_SceneNode

◆ lg_traverse_scene_graph()

void lg_traverse_scene_graph ( LG_SceneNode node,
int(*)(LG_SceneNode *)  func 
)

Go from top to bottom, starting at node (may be different from the top node) - unsuitable (don't) to remove all nodes

Call (*func)(node) for each node

Parameters
nodeA pointer to a LG_SceneNode
funcFunc pointer

◆ lg_traverse_scene_graph_2()

void lg_traverse_scene_graph_2 ( LG_SceneNode node,
int(*)(LG_SceneNode *, LG_Scene *)  func,
LG_Scene scene 
)

Same as above but with different func signature and extra scene arg

◆ lg_scenenode_find_by_id()

LG_SceneNode* lg_scenenode_find_by_id ( LG_SceneNode node,
int  id 
)

Find (first ?) occurence of node by id (traversing the graph top to bottom from provided node)

Parameters
nodeNode
idNode's id
Returns
First found node or NULL

◆ lg_scenenode_find_by_name()

LG_SceneNode* lg_scenenode_find_by_name ( LG_SceneNode node,
const char *  name 
)

Find (first ?) occurence of node by name (traversing the graph top to bottom from provided node)

Parameters
nodeNode
nameNode's name
Returns
First found node or NULL

◆ lg_scenenode_reset_local_matrix()

void lg_scenenode_reset_local_matrix ( LG_SceneNode node)

=== Scene graph matrix ops ===

Reset node's local_matrix to id matrix

Parameters
nodeA pointer to a LG_SceneNode

◆ lg_scenenode_set_local_matrix()

void lg_scenenode_set_local_matrix ( LG_SceneNode node,
vec3_t  scaling,
vec3_t  transl,
LG_Quat  rot 
)

Set node's local matrix

Order is always scaling / transl. / rot.

Parameters
nodeA pointer to a LG_SceneNode
scalingScaling vec3_t vector
translTranslation vec3_t vector
rotRotation LG_Quat quaternion

◆ lg_scenenode_update_local_matrix()

void lg_scenenode_update_local_matrix ( LG_SceneNode node,
vec3_t  scaling,
vec3_t  transl,
LG_Quat  rot 
)

Update node's local matrix

Order is always scaling / transl. / rot.

Parameters
nodeA pointer to a LG_SceneNode
scalingScaling vec3_t vector
translTranslation vec3_t vector
rotRotation LG_Quat quaternion

◆ lg_scenenode_update_world_matrix_tree()

void lg_scenenode_update_world_matrix_tree ( LG_SceneNode node,
mat4_t parent_world_matrix 
)

Update (compute from local_matrix) world matrix of node (if transf matrix != NULL) AND children, recursively

Parameters
nodeA pointer to a LG_SceneNode
parent_world_matrixA pointer to a mat4_t transformation matrix

◆ lg_scenenode_set_world_matrix()

void lg_scenenode_set_world_matrix ( LG_SceneNode node,
vec3_t  scaling,
vec3_t  transl,
LG_Quat  rot 
)

Set world matrix of node (only this one) from local_matrix and 3 transfomation vectors

Order is always scaling / transl. / rot.

For special case uses, like attaching a node to a cam

Parameters
nodeA pointer to a LG_SceneNode
scalingScaling vec3_t vector
translTranslation vec3_t vector
rotRotation LG_Quat quaternion

◆ lg_scenenode_attach_to_camera()

void lg_scenenode_attach_to_camera ( LG_SceneNode node,
const LG_Camera cam,
vec3_t  transl 
)

Attach node to camera

Modify node's world_matrix to locate it relatively to the camera

Easier to use than lg_scenenode_set_world_matrix()

 lg_scenenode_set_world_matrix(node, vec3(1.0, 1.0, 1.0), cam->position, cam->orientation);
 // vs
 lg_scenenode_attach_to_camera(node, cam, {0.0, 1.0, -1.0});
Parameters
nodeA pointer to a LG_SceneNode
camA pointer to a LG_Camera
translTranslation relatively to the cam position

◆ lg_scenenode_clone_worldm_l3dvb()

int lg_scenenode_clone_worldm_l3dvb ( LG_SceneNode dest_node,
LG_SceneNode src_node 
)

'Clone' node's draw_stuff

  • Copy drawing stuff from src_node to dest_node, more precisely:
src_node -> dest_node
world_matrix world_matrix
mesh->b_cuboid_l3d_vb lines3d_vb
  • Only if src_node->type == MESH and dest_node->type == LINES3D

This line:

src_node->mesh->b_cuboid_l3d_vb -> dest_node->lines3d_vb

makes this func convenient to draw a mesh bounding cuboid

Parameters
dest_nodePointer to dest node
src_nodePointer to src node
Returns
LG_OK if OK, LG_ERROR otherwise

◆ lg_traverse_printout_scene_graph()

void lg_traverse_printout_scene_graph ( LG_SceneNode node,
zboolean  extra_info 
)

Go from top to bottom

Print out tree structure (quite nicely) -> node->name (node->id)

Parameters
nodeA pointer to a LG_SceneNode
extra_infoIf TRUE, print out extra info (node->id so far)

◆ lg_scenenode_info()

void lg_scenenode_info ( LG_SceneNode node)

Print out LG_SceneNode info

Parameters
nodeA pointer to a LG_SceneNode

◆ lg_scenenode_info2()

int lg_scenenode_info2 ( LG_SceneNode node)

Print out LG_SceneNode info

Same as above func but returns (int)0, so suitable to use with lg_traverse_scene_graph()

Parameters
nodeA pointer to a LG_SceneNode

◆ lg_scenenode_type_string()

const char* lg_scenenode_type_string ( int  type)

Return string for scenenode type (enum)

Parameters
typeScenenode type as an int (lg_scenenode_type enum)
Returns
Scenenode type as a string