LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_scene_graph.h
1/*
2 * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2026
3 * All rights reserved
4 */
5
6/*
7 * LG_NODE_NTOS AND LG_SCENE ARE USED MAINLY IN LG_SCENE.C
8 * (THIS MAY CREATE A BIT OF CONFUSION)
9 */
10
11#ifndef LG_SCENE_GRAPH_H
12#define LG_SCENE_GRAPH_H
13
14#define SCENENODE_NAME_MAX_LEN (512 - 1)
15#define SCENENODE_CHILD_MAX_NUM 256
16#define SCENE_GRAPH_MAX_DEPTH 32
17#define SCENEGRAPH_PRINTOUT_MAX_DEPTH 8
18
19#define NODE_NTOS_MAX_NUM 1024
20
21#define SCENE_SERIAL_DATA_SIZE offsetof(LG_Scene, cam1)
22
23typedef enum {
24 ROOT,
25 FIRST_OBJ,
26 MESH, /* Mesh = OBJ or BMESH */
27 LINES3D,
28 GRID, /* Means lines3d_vb is dynamic */
29 LANDSCAPE_ROOT,
30 QT_NODE,
31 SKYBOX,
32 LIGHT,
33 SHADER_ONLY,
34 UNDEFINED
35} lg_scenenode_type;
36
37typedef struct LG_SceneNode LG_SceneNode;
38
39/*
40 * NODE'S LOCAL_MATRIX = NODE'S TRANSFORMATION RELATIVE TO ITS PARENT
41 * NODE'S WORLD_MATRIX = NODE'S TRANSFORMATION RELATIVE TO THE ROOT NODE (WORLD)
42 */
44 int32_t id; /* User-defined - SHOULD BE UNIQUE */
45 char full_path[SCENENODE_NAME_MAX_LEN + 1]; /* User-defined - usually mesh full path */
46 lg_scenenode_type type;
47 LG_SceneNode *parent;
48 LG_SceneNode *child[SCENENODE_CHILD_MAX_NUM];
49 unsigned int n_child;
50 LG_Mesh *mesh;
51 Lines3D_VB lines3d_vb; /* Line set vertex buffer and num of vertices */
52 mat4_t local_matrix0; /* 'Private' member - how to 'hide' this stuff */
53 mat4_t world_matrix0; /* 'Private' member - how to 'hide' this stuff */
54 mat4_t *local_matrix; /* Adjust (TRS) the mesh/lines3d_vb in its local space */
55 mat4_t *world_matrix; /* Put (TRS) the mesh/lines3d_vb in world space */
56 LG_ShaderProg shader_prog;
57 LG_ShaderULocs shader_u_loc; /* Shader uniforms locations */
58 LG_VAO vao; /* Vertex Array Object */
59 LG_Texture *tex_1; /* For textures other than thoses from mesh mtl file */
60};
61
62/*
63 * === Scene node serializable data ===
64 * NTOS stands for (mesh file) Name and (scene node) TOS (Transl/Orientation/Scaling)
65 * (could/should be ITNTOS for Ids/Type/Name/Transl/Orientation/Scaling for more accuracy)
66 * Use float for LG_EulerAng
67 */
68typedef union {
69 struct {
70 /* Ids and Type */
71 int32_t id; /* User-defined - SHOULD BE UNIQUE -> always > 0, 0 = top node */
72 int32_t parent_id; /* Set to < 0 if node = top node */
73 int32_t type; /* Actually lg_scenenode_type (int), may be other that MESH */
74 char full_path[LG_MESH_NAME_MAX_LEN + 1]; /* Full path to OBJ/FBX/BMESH file if type == MESH */
75 /* Transl. */
76 float x_t;
77 float y_t;
78 float z_t;
79 /* Orientation as Euler ang (as 3 floats) - TO BE USED IN GAME LOOP UI */
80 float x_oe;
81 float y_oe;
82 float z_oe;
83 /* Scaling */
84 float x_s;
85 float y_s;
86 float z_s;
87 /* Orientation as a quat (as 4 doubles) - TO BE USED WHEN SAVING/LOADING */
88 double x_oq;
89 double y_oq;
90 double z_oq;
91 double w_oq;
92 };
93 struct {
94 int32_t id2;
95 int32_t parent_id2;
96 int32_t type2;
97 char full_path2[LG_MESH_NAME_MAX_LEN + 1];
98 float v[17];
99 };
101
102/* New nodes/nodes ids added to the scene should start at or above first_obj_node/SCENE_FIRST_OBJ_NODE_ID (id = 3) */
103enum {
104 SCENE_ROOT_NODE_ID = 0,
105 SCENE_GRID_NODE_ID,
106 SCENE_XYZ_ARROWS_NODE_ID,
107 SCENE_FIRST_OBJ_NODE_ID /* Actually first user obj - 1 */
108};
109
110typedef struct {
111 /* All serializable data */
112 int32_t id; /* Set to -1 on error */
113 LG_Light light1;
114 Rec2Df landscape_rec;
115 LG_Node_NTOS nodes_ntos[NODE_NTOS_MAX_NUM];
116 int32_t n_node_ntos;
117 char euler_ang_rot_order[4];
118 /* Not serializable data */
119 LG_Camera *cam1;
120 LG_SceneNode *root;
121 LG_SceneNode *grid;
122 LG_SceneNode *xyz_arrows;
123 LG_SceneNode *first_obj_node;
124} LG_Scene;
125
126LG_SceneNode *lg_scenenode_new(int, const char *, lg_scenenode_type);
127
129
131
133
135
136/* Any good ? */
137#define lg_scenenode_free2(s_node) {lg_scenenode_free(s_node); s_node = NULL;}
138
140
142
144
146
148
150
152
154
156
158
160
162
164
166
168
170
172
174
175const char *lg_scenenode_type_string(int);
176
177#endif /* LG_SCENE_GRAPH_H */
void lg_scenenode_remove_and_free_all_below(LG_SceneNode *node)
Definition lg_scene_graph.c:241
void lg_traverse_scene_graph(LG_SceneNode *node, int(*func)(LG_SceneNode *))
Definition lg_scene_graph.c:286
void lg_scenenode_free_v2(LG_SceneNode **node)
Definition lg_scene_graph.c:163
void lg_scenenode_set_local_matrix(LG_SceneNode *node, vec3_t transl, LG_Quat rot, vec3_t scaling)
Definition lg_scene_graph.c:416
void lg_scenenode_update_local_matrix(LG_SceneNode *node, vec3_t transl, LG_Quat rot, vec3_t scaling)
Definition lg_scene_graph.c:437
LG_SceneNode * lg_scenenode_find_by_id(LG_SceneNode *node, int id)
Definition lg_scene_graph.c:359
void lg_scenenode_info(LG_SceneNode *node)
Definition lg_scene_graph.c:635
void lg_scenenode_attach_to_camera(LG_SceneNode *node, const LG_Camera *cam, vec3_t transl, vec3_t scaling)
Definition lg_scene_graph.c:514
void lg_traverse_scene_graph_2(LG_SceneNode *node, int(*func)(LG_SceneNode *, LG_Scene *), LG_Scene *scene)
Definition lg_scene_graph.c:320
LG_SceneNode * lg_scenenode_find_by_name(LG_SceneNode *node, const char *full_path)
Definition lg_scene_graph.c:376
int lg_scenenode_clone_worldm_l3dvb(LG_SceneNode *dest_node, LG_SceneNode *src_node)
Definition lg_scene_graph.c:549
LG_SceneNode * lg_scenenode_get_root(LG_SceneNode *node)
Definition lg_scene_graph.c:179
void lg_scenenode_update_world_matrix_tree(LG_SceneNode *node, mat4_t *parent_world_matrix)
Definition lg_scene_graph.c:455
void lg_scenenode_add_child(LG_SceneNode *node, LG_SceneNode *child)
Definition lg_scene_graph.c:84
void lg_traverse_printout_scene_graph(LG_SceneNode *node, zboolean extra_info)
Definition lg_scene_graph.c:624
void lg_scenenode_set_world_matrix(LG_SceneNode *node, vec3_t transl, LG_Quat rot, vec3_t scaling)
Definition lg_scene_graph.c:486
LG_SceneNode * lg_scenenode_new(int id, const char *full_path, lg_scenenode_type type)
Definition lg_scene_graph.c:24
void lg_scenenode_remove_child(LG_SceneNode *node, LG_SceneNode *child)
Definition lg_scene_graph.c:109
void lg_scenenode_remove_and_free_all(LG_SceneNode *node)
Definition lg_scene_graph.c:226
void lg_scenenode_reset_local_matrix(LG_SceneNode *node)
Definition lg_scene_graph.c:393
int lg_scenenode_remove_and_free_leaf(LG_SceneNode *node)
Definition lg_scene_graph.c:199
int lg_scenenode_info2(LG_SceneNode *node)
Definition lg_scene_graph.c:702
const char * lg_scenenode_type_string(int type)
Definition lg_scene_graph.c:714
void lg_scenenode_free(LG_SceneNode *node)
Definition lg_scene_graph.c:145
Definition lg_camera.h:22
Definition lg_light.h:26
Definition lg_mesh.h:85
Definition lg_quaternions.h:18
Definition lg_scene_graph.h:43
Definition lg_scene_graph.h:110
Definition lg_shader_progs.h:11
Definition lg_shader_progs.h:16
Definition lg_textures.h:45
Definition lg_vbo.h:52
Definition lg_3d_primitives.h:53
Definition lg_gr_func.h:56
Definition math_3d.h:123
Definition lg_scene_graph.h:68
Definition math_3d.h:179