Browse Source

world: add RenderWorld.mesh_set_geometry()

Part-of: #181
Daniele Bartolini 1 year ago
parent
commit
0701ceb4e7
4 changed files with 32 additions and 0 deletions
  1. 1 0
      docs/changelog.rst
  2. 5 0
      src/lua/lua_api.cpp
  3. 19 0
      src/world/render_world.cpp
  4. 7 0
      src/world/render_world.h

+ 1 - 0
docs/changelog.rst

@@ -12,6 +12,7 @@ Changelog
 * Added the ability to set a new (smoothed) timestep policy.
 * Added the ability to set a new (smoothed) timestep policy.
 * Improved ``graph`` command with the ability to add multiple fields, customize colors and limit the number of samples shown.
 * Improved ``graph`` command with the ability to add multiple fields, customize colors and limit the number of samples shown.
 * Lua API: Added ``Device.set_timestep_policy()`` and ``Device.set_timestep_smoothing()``.
 * Lua API: Added ``Device.set_timestep_policy()`` and ``Device.set_timestep_smoothing()``.
+* Lua API: Added ``RenderWorld.mesh_set_geometry()``.
 * Windows: fixed xinput.dll not found on some systems.
 * Windows: fixed xinput.dll not found on some systems.
 * Windows: fixed console output and absolute paths when launched under MinGW.
 * Windows: fixed console output and absolute paths when launched under MinGW.
 
 

+ 5 - 0
src/lua/lua_api.cpp

@@ -1866,6 +1866,11 @@ void load_api(LuaEnvironment &env)
 			stack.push_float(t);
 			stack.push_float(t);
 			return 1;
 			return 1;
 		});
 		});
+	env.add_module_function("RenderWorld", "mesh_set_geometry", [](lua_State *L) {
+			LuaStack stack(L);
+			stack.get_render_world(1)->mesh_set_geometry(stack.get_mesh_instance(2), stack.get_resource_name(3), stack.get_string_id_32(4));
+			return 0;
+		});
 	env.add_module_function("RenderWorld", "mesh_material", [](lua_State *L) {
 	env.add_module_function("RenderWorld", "mesh_material", [](lua_State *L) {
 			LuaStack stack(L);
 			LuaStack stack(L);
 			Material *material = stack.get_render_world(1)->mesh_material(stack.get_mesh_instance(2));
 			Material *material = stack.get_render_world(1)->mesh_material(stack.get_mesh_instance(2));

+ 19 - 0
src/world/render_world.cpp

@@ -123,6 +123,13 @@ MeshInstance RenderWorld::mesh_instance(UnitId unit)
 	return _mesh_manager.mesh(unit);
 	return _mesh_manager.mesh(unit);
 }
 }
 
 
+void RenderWorld::mesh_set_geometry(MeshInstance mesh, StringId64 mesh_resource, StringId32 geometry)
+{
+	CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
+	const MeshResource *mr = (MeshResource *)_resource_manager->get(RESOURCE_TYPE_MESH, mesh_resource);
+	_mesh_manager.set_geometry(mesh, mr, geometry);
+}
+
 Material *RenderWorld::mesh_material(MeshInstance mesh)
 Material *RenderWorld::mesh_material(MeshInstance mesh)
 {
 {
 	CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
 	CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
@@ -608,6 +615,18 @@ bool RenderWorld::MeshManager::has(UnitId unit)
 	return is_valid(mesh(unit));
 	return is_valid(mesh(unit));
 }
 }
 
 
+void RenderWorld::MeshManager::set_geometry(MeshInstance mesh, const MeshResource *mr, StringId32 geometry)
+{
+	const MeshGeometry *mg = mr->geometry(geometry);
+	CE_ENSURE(mg != NULL);
+
+	_data.resource[mesh.i] = mr;
+	_data.geometry[mesh.i] = mg;
+	_data.mesh[mesh.i].vbh = mg->vertex_buffer;
+	_data.mesh[mesh.i].ibh = mg->index_buffer;
+	_data.obb[mesh.i]      = mg->obb;
+}
+
 void RenderWorld::MeshManager::set_visible(MeshInstance inst, bool visible)
 void RenderWorld::MeshManager::set_visible(MeshInstance inst, bool visible)
 {
 {
 	if (visible) {
 	if (visible) {

+ 7 - 0
src/world/render_world.h

@@ -35,6 +35,10 @@ struct RenderWorld
 	/// Returns the ID of the mesh owned by the *unit*.
 	/// Returns the ID of the mesh owned by the *unit*.
 	MeshInstance mesh_instance(UnitId unit);
 	MeshInstance mesh_instance(UnitId unit);
 
 
+	/// Sets the @a geometry of the @a mesh. @a geometry must be a valid geometry name inside @a
+	/// mesh_resource.
+	void mesh_set_geometry(MeshInstance mesh, StringId64 mesh_resource, StringId32 geometry);
+
 	/// Returns the material of the @a mesh.
 	/// Returns the material of the @a mesh.
 	Material *mesh_material(MeshInstance mesh);
 	Material *mesh_material(MeshInstance mesh);
 
 
@@ -209,6 +213,9 @@ struct RenderWorld
 		///
 		///
 		bool has(UnitId unit);
 		bool has(UnitId unit);
 
 
+		///
+		void set_geometry(MeshInstance mesh, const MeshResource *mr, StringId32 geometry);
+
 		///
 		///
 		void set_visible(MeshInstance mesh, bool visible);
 		void set_visible(MeshInstance mesh, bool visible);