Ver Fonte

Add support to Material to Unit

Daniele Bartolini há 12 anos atrás
pai
commit
e34a290b90
2 ficheiros alterados com 84 adições e 4 exclusões
  1. 73 4
      engine/world/Unit.cpp
  2. 11 0
      engine/world/Unit.h

+ 73 - 4
engine/world/Unit.cpp

@@ -35,6 +35,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PhysicsResource.h"
 #include "Device.h"
 #include "ResourceManager.h"
+#include "Sprite.h"
 
 namespace crown
 {
@@ -51,6 +52,7 @@ Unit::Unit(World& w, const UnitResource* ur, const Matrix4x4& pose)
 	, m_num_meshes(0)
 	, m_num_sprites(0)
 	, m_num_actors(0)
+	, m_num_materials(0)
 {
 	m_controller.component.id = INVALID_ID;
 	create_objects(pose);
@@ -93,6 +95,8 @@ void Unit::create_objects(const Matrix4x4& pose)
 	create_camera_objects();
 	create_renderable_objects();
 	create_physics_objects();
+
+	set_default_material();
 }
 
 //-----------------------------------------------------------------------------
@@ -133,6 +137,13 @@ void Unit::destroy_objects()
 		m_controller.component.id = INVALID_ID;
 	}
 
+	// Destroy materials
+	for (uint32_t i = 0; i < m_num_materials; i++)
+	{
+		m_world.render_world()->destroy_material(m_materials[i].component);
+	}
+	m_num_materials = 0;
+
 	// Destroy scene graph
 	m_scene_graph.destroy();
 }
@@ -151,16 +162,34 @@ void Unit::create_camera_objects()
 //-----------------------------------------------------------------------------
 void Unit::create_renderable_objects()
 {
+	// Create renderables
 	for (uint32_t i = 0; i < m_resource->num_renderables(); i++)
 	{
 		const UnitRenderable renderable = m_resource->get_renderable(i);
 
-		switch (renderable.type)
+		if (renderable.type == UnitRenderable::MESH)
 		{
-			case UnitRenderable::MESH: add_mesh(renderable.name, m_world.render_world()->create_mesh(renderable.resource, m_scene_graph, renderable.node)); break;
-			case UnitRenderable::SPRITE: add_sprite(renderable.name, m_world.render_world()->create_sprite(renderable.resource, m_scene_graph, renderable.node)); break;
-			default: CE_FATAL("Oops, bad renderable type"); break;
+			MeshResource* mr = (MeshResource*) device()->resource_manager()->data(renderable.resource);
+			MeshId mesh = m_world.render_world()->create_mesh(mr, m_scene_graph, renderable.node);
+			add_mesh(renderable.name, mesh);
 		}
+		else if (renderable.type == UnitRenderable::SPRITE)
+		{
+			SpriteResource* sr = (SpriteResource*) device()->resource_manager()->data(renderable.resource);
+			SpriteId sprite = m_world.render_world()->create_sprite(sr, m_scene_graph, renderable.node);
+			add_sprite(renderable.name, sprite);
+		}
+		else
+		{
+			CE_FATAL("Oops, bad renderable type");
+		}
+	}
+
+	// Create materials
+	if (m_resource->material_resource().id != 0)
+	{
+		MaterialResource* mr = (MaterialResource*) device()->resource_manager()->data(m_resource->material_resource());
+		add_material(hash::murmur2_32("default", string::strlen("default"), 0), m_world.render_world()->create_material(mr));
 	}
 }
 
@@ -186,6 +215,18 @@ void Unit::create_physics_objects()
 	}
 }
 
+//-----------------------------------------------------------------------------
+void Unit::set_default_material()
+{
+	if (m_num_materials == 0) return;
+
+	for (uint32_t i = 0; i < m_num_sprites; i++)
+	{
+		Sprite* s = m_world.render_world()->lookup_sprite(m_sprites[i].component);
+		s->set_material(m_materials[0].component);
+	}
+}
+
 //-----------------------------------------------------------------------------
 int32_t Unit::node(const char* name) const
 {
@@ -360,6 +401,14 @@ void Unit::add_actor(StringId32 name, ActorId actor)
 	add_component(name, actor, m_num_actors, m_actors);
 }
 
+//-----------------------------------------------------------------------------
+void Unit::add_material(StringId32 name, MaterialId material)
+{
+	CE_ASSERT(m_num_materials < MAX_MATERIAL_COMPONENTS, "Max material number reached");
+
+	add_component(name, material, m_num_materials, m_materials);
+}
+
 //-----------------------------------------------------------------------------
 void Unit::set_controller(StringId32 name, ControllerId controller)
 {
@@ -458,4 +507,24 @@ Controller* Unit::controller()
 	return NULL;
 }
 
+//-----------------------------------------------------------------------------
+Material* Unit::material(const char* name)
+{
+	MaterialId material = find_component(name, m_num_materials, m_materials);
+
+	CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%s'", name);
+
+	return m_world.render_world()->lookup_material(material);
+}
+
+//-----------------------------------------------------------------------------
+Material* Unit::material(uint32_t i)
+{
+	MaterialId material = find_component(i, m_num_materials, m_materials);
+
+	CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%d'", i);
+
+	return m_world.render_world()->lookup_material(material);
+}
+
 } // namespace crown

+ 11 - 0
engine/world/Unit.h

@@ -64,6 +64,7 @@ typedef Id SpriteId;
 typedef Id ActorId;
 typedef Id ControllerId;
 typedef	Id ComponentId;
+typedef Id MaterialId;
 
 class Camera;
 class Mesh;
@@ -74,11 +75,13 @@ class World;
 class SceneGraphManager;
 class PhysicsGraphManager;
 struct UnitResource;
+class Material;
 
 #define MAX_CAMERA_COMPONENTS 8
 #define MAX_MESH_COMPONENTS 8
 #define MAX_SPRITE_COMPONENTS 8
 #define MAX_ACTOR_COMPONENTS 8
+#define MAX_MATERIAL_COMPONENTS 8
 
 struct Unit
 {
@@ -121,6 +124,7 @@ struct Unit
 	void				add_sprite(StringId32 name, SpriteId sprite);
 	void				add_actor(StringId32 name, ActorId actor);
 	void				set_controller(StringId32 name, ControllerId controller);
+	void				add_material(StringId32 name, MaterialId material);
 
 	Camera*				camera(const char* name);
 	Camera*				camera(uint32_t i);
@@ -134,6 +138,9 @@ struct Unit
 	Actor*				actor(const char* name);
 	Actor*				actor(uint32_t i);
 
+	Material*			material(const char* name);
+	Material*			material(uint32_t i);
+
 	Controller*			controller();
 
 private:
@@ -143,6 +150,7 @@ private:
 	void				create_camera_objects();
 	void				create_renderable_objects();
 	void				create_physics_objects();
+	void				set_default_material();
 
 public:
 
@@ -163,6 +171,9 @@ public:
 	uint32_t			m_num_actors;
 	Component 			m_actors[MAX_ACTOR_COMPONENTS];
 
+	uint32_t			m_num_materials;
+	Component			m_materials[MAX_MATERIAL_COMPONENTS];
+
 	Component			m_controller;
 };