LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
Loading...
Searching...
No Matches
lg_scene_graph.h
1/*
2 * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2025
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 (64 - 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 VBO (= VBO/IBO/INFO) */
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 name[SCENENODE_NAME_MAX_LEN + 1]; /* User-defined - may be NOT unique */
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 /* Name */
75 char name[LG_MESH_NAME_MAX_LEN + 1]; /* OBJ/FBX basename if type == MESH */
76 /* Transl. */
77 float x_t;
78 float y_t;
79 float z_t;
80 /* Orientation as Euler ang (as 3 floats) - TO BE USED IN GAME LOOP UI */
81 float x_oe;
82 float y_oe;
83 float z_oe;
84 /* Scaling */
85 float x_s;
86 float y_s;
87 float z_s;
88 /* Orientation as a quat (as 4 doubles) - TO BE USED WHEN SAVING/LOADING */
89 double x_oq;
90 double y_oq;
91 double z_oq;
92 double w_oq;
93 };
94 struct {
95 int32_t id2;
96 int32_t parent_id2;
97 int32_t type2;
98 char name2[LG_MESH_NAME_MAX_LEN + 1];
99 float v[17];
100 };
102
103/* New nodes/nodes ids added to the scene should start at or above first_obj_node/SCENE_FIRST_OBJ_NODE_ID (id = 3) */
104enum {
105 SCENE_ROOT_NODE_ID = 0,
106 SCENE_GRID_NODE_ID,
107 SCENE_XYZ_ARROWS_NODE_ID,
108 SCENE_FIRST_OBJ_NODE_ID /* Actually first user obj - 1 */
109};
110
111typedef struct {
112 /* All serializable data */
113 int32_t id; /* Set to -1 on error */
114 LG_Light light1;
115 Rec2Df landscape_rec;
116 LG_Node_NTOS nodes_ntos[NODE_NTOS_MAX_NUM];
117 int32_t n_node_ntos;
118 char euler_ang_rot_order[4];
119 /* Not serializable data */
120 LG_Camera *cam1;
121 LG_SceneNode *root;
122 LG_SceneNode *grid;
123 LG_SceneNode *xyz_arrows;
124 LG_SceneNode *first_obj_node;
125} LG_Scene;
126
127LG_SceneNode *lg_scenenode_new(int, const char *, lg_scenenode_type);
128
130
132
134
135/* Any good ? */
136#define lg_scenenode_free2(s_node) {lg_scenenode_free(s_node); s_node = NULL;}
137
139
141
143
145
147
149
151
153
155
157
159
161
163
165
167
169
171
172const char *lg_scenenode_type_string(int);
173
174#endif /* LG_SCENE_GRAPH_H */
LG_SceneNode * lg_scenenode_find_by_name(LG_SceneNode *node, const char *name)
Definition lg_scene_graph.c:345
LG_SceneNode * lg_scenenode_new(int id, const char *name, lg_scenenode_type type)
Definition lg_scene_graph.c:29
void lg_traverse_scene_graph(LG_SceneNode *node, int(*func)(LG_SceneNode *))
Definition lg_scene_graph.c:255
void lg_scenenode_set_local_matrix(LG_SceneNode *node, vec3_t transl, LG_Quat rot, vec3_t scaling)
Definition lg_scene_graph.c:385
void lg_scenenode_update_local_matrix(LG_SceneNode *node, vec3_t transl, LG_Quat rot, vec3_t scaling)
Definition lg_scene_graph.c:406
LG_SceneNode * lg_scenenode_find_by_id(LG_SceneNode *node, int id)
Definition lg_scene_graph.c:328
void lg_scenenode_info(LG_SceneNode *node)
Definition lg_scene_graph.c:604
void lg_scenenode_attach_to_camera(LG_SceneNode *node, const LG_Camera *cam, vec3_t transl, vec3_t scaling)
Definition lg_scene_graph.c:483
void lg_traverse_scene_graph_2(LG_SceneNode *node, int(*func)(LG_SceneNode *, LG_Scene *), LG_Scene *scene)
Definition lg_scene_graph.c:289
int lg_scenenode_clone_worldm_l3dvb(LG_SceneNode *dest_node, LG_SceneNode *src_node)
Definition lg_scene_graph.c:518
LG_SceneNode * lg_scenenode_get_root(LG_SceneNode *node)
Definition lg_scene_graph.c:165
void lg_scenenode_update_world_matrix_tree(LG_SceneNode *node, mat4_t *parent_world_matrix)
Definition lg_scene_graph.c:424
void lg_scenenode_add_child(LG_SceneNode *node, LG_SceneNode *child)
Definition lg_scene_graph.c:89
void lg_traverse_printout_scene_graph(LG_SceneNode *node, zboolean extra_info)
Definition lg_scene_graph.c:593
void lg_scenenode_set_world_matrix(LG_SceneNode *node, vec3_t transl, LG_Quat rot, vec3_t scaling)
Definition lg_scene_graph.c:455
void lg_scenenode_remove_child(LG_SceneNode *node, LG_SceneNode *child)
Definition lg_scene_graph.c:114
void lg_scenenode_remove_and_free_all(LG_SceneNode *node)
Definition lg_scene_graph.c:212
void lg_scenenode_reset_local_matrix(LG_SceneNode *node)
Definition lg_scene_graph.c:362
int lg_scenenode_remove_and_free_leaf(LG_SceneNode *node)
Definition lg_scene_graph.c:185
int lg_scenenode_info2(LG_SceneNode *node)
Definition lg_scene_graph.c:671
const char * lg_scenenode_type_string(int type)
Definition lg_scene_graph.c:683
void lg_scenenode_free(LG_SceneNode *node)
Definition lg_scene_graph.c:150
Definition lg_camera.h:22
Definition lg_light.h:26
Definition lg_mesh.h:84
Definition lg_quaternions.h:18
Definition lg_scene_graph.h:43
Definition lg_scene_graph.h:111
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