|
|
@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "IdTable.h"
|
|
|
#include "World.h"
|
|
|
#include "Allocator.h"
|
|
|
+#include "Log.h"
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
@@ -35,115 +36,189 @@ namespace crown
|
|
|
typedef Id CameraId;
|
|
|
|
|
|
Unit::Unit()
|
|
|
- : m_creator(NULL)
|
|
|
- , m_resource(NULL)
|
|
|
- , m_scene_graph(NULL)
|
|
|
- , m_component(NULL)
|
|
|
+ : m_world(NULL)
|
|
|
+ , m_num_cameras(0)
|
|
|
+ , m_num_meshes(0)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::create(World& creator, SceneGraph& graph, ComponentList& components, UnitId id, const Vector3& pos, const Quaternion& rot)
|
|
|
+void Unit::create(World& world, UnitId id, const Vector3& pos, const Quaternion& rot)
|
|
|
{
|
|
|
- m_root_node = graph.create_node(-1, pos, rot);
|
|
|
-
|
|
|
- m_scene_graph = &graph;
|
|
|
- m_component = &components;
|
|
|
- m_creator = &creator;
|
|
|
+ m_root_node = m_scene_graph.create_node(-1, pos, rot);
|
|
|
+ m_world = &world;
|
|
|
m_id = id;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void Unit::destroy()
|
|
|
{
|
|
|
+}
|
|
|
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+Vector3 Unit::local_position(int32_t node) const
|
|
|
+{
|
|
|
+ return m_scene_graph.local_position(node);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::load(UnitResource* ur)
|
|
|
+Quaternion Unit::local_rotation(int32_t node) const
|
|
|
{
|
|
|
- m_resource = ur;
|
|
|
+ return m_scene_graph.local_rotation(node);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::unload()
|
|
|
+Matrix4x4 Unit::local_pose(int32_t node) const
|
|
|
{
|
|
|
+ return m_scene_graph.local_pose(node);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::reload(UnitResource* new_ur)
|
|
|
+Vector3 Unit::world_position(int32_t node) const
|
|
|
{
|
|
|
- (void)new_ur;
|
|
|
+ return m_scene_graph.world_position(node);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Vector3 Unit::local_position(int32_t node) const
|
|
|
+Quaternion Unit::world_rotation(int32_t node) const
|
|
|
{
|
|
|
- return m_scene_graph->local_position(node);
|
|
|
+ return m_scene_graph.world_rotation(node);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Quaternion Unit::local_rotation(int32_t node) const
|
|
|
+Matrix4x4 Unit::world_pose(int32_t node) const
|
|
|
{
|
|
|
- return m_scene_graph->local_rotation(node);
|
|
|
+ return m_scene_graph.world_pose(node);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Matrix4x4 Unit::local_pose(int32_t node) const
|
|
|
+void Unit::set_local_position(const Vector3& pos, int32_t node)
|
|
|
{
|
|
|
- return m_scene_graph->local_pose(node);
|
|
|
+ m_scene_graph.set_local_position(node, pos);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Vector3 Unit::world_position(int32_t node) const
|
|
|
+void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
|
|
|
{
|
|
|
- return m_scene_graph->world_position(node);
|
|
|
+ m_scene_graph.set_local_rotation(node, rot);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Quaternion Unit::world_rotation(int32_t node) const
|
|
|
+void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
|
|
|
{
|
|
|
- return m_scene_graph->world_rotation(node);
|
|
|
+ m_scene_graph.set_local_pose(node, pose);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Matrix4x4 Unit::world_pose(int32_t node) const
|
|
|
+void Unit::link_node(int32_t child, int32_t parent)
|
|
|
{
|
|
|
- return m_scene_graph->world_pose(node);
|
|
|
+ m_scene_graph.link(child, parent);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::set_local_position(const Vector3& pos, int32_t node)
|
|
|
+void Unit::unlink_node(int32_t child)
|
|
|
{
|
|
|
- m_scene_graph->set_local_position(node, pos);
|
|
|
+ m_scene_graph.unlink(child);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
|
|
|
+void Unit::add_component(const char* name, Id component, uint32_t& size, Component* array)
|
|
|
{
|
|
|
- m_scene_graph->set_local_rotation(node, rot);
|
|
|
+ Component comp;
|
|
|
+ comp.name = hash::murmur2_32(name, string::strlen(name), 0);
|
|
|
+ comp.component = component;
|
|
|
+
|
|
|
+ array[size] = comp;
|
|
|
+ size++;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
|
|
|
+Id Unit::find_component(const char* name, uint32_t size, Component* array)
|
|
|
+{
|
|
|
+ uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
|
|
|
+
|
|
|
+ Id comp;
|
|
|
+ comp.id = INVALID_ID;
|
|
|
+
|
|
|
+ for (uint32_t i = 0; i < size; i++)
|
|
|
+ {
|
|
|
+ if (name_hash == array[i].name)
|
|
|
+ {
|
|
|
+ comp = array[i].component;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return comp;
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
|
|
|
+{
|
|
|
+ Id comp;
|
|
|
+ comp.id = INVALID_ID;
|
|
|
+
|
|
|
+ if (index < size)
|
|
|
+ {
|
|
|
+ comp = array[index].component;
|
|
|
+ }
|
|
|
+
|
|
|
+ return comp;
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+void Unit::add_camera(const char* name, CameraId camera)
|
|
|
{
|
|
|
- m_scene_graph->set_local_pose(node, pose);
|
|
|
+ CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
|
|
|
+
|
|
|
+ add_component(name, camera, m_num_cameras, m_cameras);
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+void Unit::add_mesh(const char* name, MeshId mesh)
|
|
|
+{
|
|
|
+ CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
|
|
|
+
|
|
|
+ add_component(name, mesh, m_num_meshes, m_meshes);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
Camera* Unit::camera(const char* name)
|
|
|
{
|
|
|
- Component* c = m_component->get_component(name);
|
|
|
+ CameraId cam = find_component(name, m_num_cameras, m_cameras);
|
|
|
|
|
|
- return m_creator->lookup_camera(c->component);
|
|
|
+ CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
|
|
|
+
|
|
|
+ return m_world->lookup_camera(cam);
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+Camera* Unit::camera(uint32_t i)
|
|
|
+{
|
|
|
+ CameraId cam = find_component(i, m_num_cameras, m_cameras);
|
|
|
+
|
|
|
+ CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
|
|
|
+
|
|
|
+ return m_world->lookup_camera(cam);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
Mesh* Unit::mesh(const char* name)
|
|
|
{
|
|
|
- Component* c = m_component->get_component(name);
|
|
|
+ MeshId mesh = find_component(name, m_num_meshes, m_meshes);
|
|
|
+
|
|
|
+ CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
|
|
|
+
|
|
|
+ return m_world->lookup_mesh(mesh);
|
|
|
+}
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+Mesh* Unit::mesh(uint32_t i)
|
|
|
+{
|
|
|
+ MeshId mesh = find_component(i, m_num_meshes, m_meshes);
|
|
|
+
|
|
|
+ CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
|
|
|
|
|
|
- return m_creator->lookup_mesh(c->component);
|
|
|
+ return m_world->lookup_mesh(mesh);
|
|
|
}
|
|
|
|
|
|
} // namespace crown
|