소스 검색

Merge branch 'world' of https://github.com/taylor001/crown into world

mikymod 12 년 전
부모
커밋
6335dc944f

+ 3 - 0
engine/CMakeLists.txt

@@ -77,6 +77,7 @@ set (SRC
 	Unit.cpp
 	SceneGraph.cpp
 	SoundWorld.cpp
+	Mesh.cpp
 )
 
 set (HEADERS
@@ -88,6 +89,7 @@ set (HEADERS
 	Unit.h
 	SceneGraph.h
 	SoundWorld.h
+	Mesh.h
 )
 
 set (CORE_SRC
@@ -339,6 +341,7 @@ set (LUA_SRC
 	lua/LuaUnit.cpp
 	lua/LuaCamera.cpp
 	lua/LuaSound.cpp
+	lua/LuaMesh.cpp
 )
 
 set (LUA_HEADERS

+ 3 - 0
engine/Camera.cpp

@@ -39,6 +39,9 @@ void Camera::create(Unit& parent, int32_t node)
 {
 	m_parent = &parent;
 	m_node = node;
+	m_projection_type = ProjectionType::PERSPECTIVE;
+
+	update_projection_matrix();
 }
 
 //-----------------------------------------------------------------------

+ 2 - 2
engine/Camera.h

@@ -78,12 +78,12 @@ struct Camera
 	float			far_clip_distance() const;
 	void			set_far_clip_distance(float far);
 
-private:
+public:
 
 	void			update_projection_matrix();
 	void			update_frustum();
 
-private:
+public:
 
 	Unit*					m_parent;
 	int32_t					m_node;

+ 6 - 0
engine/Device.cpp

@@ -424,6 +424,12 @@ void Device::frame()
 	m_frame_count++;
 }
 
+//-----------------------------------------------------------------------------
+void Device::render_world(World& world, Camera& camera, float dt)
+{
+	world.update(camera, dt);
+}
+
 //-----------------------------------------------------------------------------
 World* Device::create_world()
 {

+ 4 - 0
engine/Device.h

@@ -52,6 +52,7 @@ class BundleCompiler;
 class ResourcePackage;
 class RPCServer;
 class World;
+class Camera;
 
 /// The Engine.
 /// It is the place where to look for accessing all of
@@ -106,6 +107,9 @@ public:
 	/// Updates all the subsystems
 	void					frame();
 
+	/// Updates the given @a world and renders it from the given @a camera.
+	void					render_world(World& world, Camera& camera, float dt);
+
 	World*					create_world();
 	void					destroy_world(World* world);
 

+ 101 - 0
engine/Mesh.cpp

@@ -0,0 +1,101 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Mesh.h"
+#include "MeshResource.h"
+#include "Vec3.h"
+#include "Mat4.h"
+#include "Quat.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+void Mesh::create(const MeshResource* mr)
+{
+	m_vbuffer = mr->m_vbuffer;
+	m_ibuffer = mr->m_ibuffer;
+}
+
+//-----------------------------------------------------------------------------
+Vec3 Mesh::local_position() const
+{
+	return m_local_pose.translation();
+}
+
+//-----------------------------------------------------------------------------
+Quat Mesh::local_rotation() const
+{
+	return Quat(Vec3(1, 0, 0), 0.0f);
+}
+
+//-----------------------------------------------------------------------------
+Mat4 Mesh::local_pose() const
+{
+	return m_local_pose;
+}
+
+//-----------------------------------------------------------------------------
+Vec3 Mesh::world_position() const
+{
+	return m_world_pose.translation();
+}
+
+//-----------------------------------------------------------------------------
+Quat Mesh::world_rotation() const
+{
+	return Quat(Vec3(1, 0, 0), 0.0f);
+}
+
+//-----------------------------------------------------------------------------
+Mat4 Mesh::world_pose() const
+{
+	return m_world_pose;
+}
+
+//-----------------------------------------------------------------------------
+void Mesh::set_local_position(const Vec3& pos)
+{
+	m_local_pose.set_translation(pos);
+}
+
+//-----------------------------------------------------------------------------
+void Mesh::set_local_rotation(const Quat& rot)
+{
+	Mat4& local_pose = m_local_pose;
+
+	Vec3 local_translation = local_pose.translation();
+	local_pose = rot.to_mat4();
+	local_pose.set_translation(local_translation);
+}
+
+//-----------------------------------------------------------------------------
+void Mesh::set_local_pose(const Mat4& pose)
+{
+	m_local_pose = pose;
+}
+
+} // namespace crown

+ 64 - 0
engine/Mesh.h

@@ -0,0 +1,64 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "RendererTypes.h"
+#include "Mat4.h"
+
+namespace crown
+{
+
+struct MeshResource;
+
+struct Mesh
+{
+	void			create(const MeshResource* mr);
+
+	Vec3			local_position() const;
+	Quat			local_rotation() const;
+	Mat4			local_pose() const;
+
+	Vec3			world_position() const;
+	Quat			world_rotation() const;
+	Mat4			world_pose() const;
+
+	void			set_local_position(const Vec3& pos);
+	void			set_local_rotation(const Quat& rot);
+	void			set_local_pose(const Mat4& pose);
+
+public:
+
+	int32_t m_node;
+
+	Mat4 m_local_pose;
+	Mat4 m_world_pose;
+
+	VertexBufferId m_vbuffer;
+	IndexBufferId m_ibuffer;
+};
+
+} // namespace crown

+ 3 - 1
engine/World.cpp

@@ -109,8 +109,10 @@ Camera* World::lookup_camera(CameraId camera)
 }
 
 //-----------------------------------------------------------------------------
-void World::update(float /*dt*/)
+void World::update(Camera& camera, float dt)
 {
+	(void)camera;
+	(void)dt;
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 1
engine/World.h

@@ -63,7 +63,7 @@ public:
 	Unit*				lookup_unit(UnitId unit);
 	Camera*				lookup_camera(CameraId camera);
 
-	void				update(float dt);
+	void				update(Camera& camera, float dt);
 
 	SoundWorld&			sound_world();
 

+ 14 - 0
engine/lua/LuaDevice.cpp

@@ -93,6 +93,19 @@ CE_EXPORT int device_destroy_world(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+CE_EXPORT int device_render_world(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = stack.get_world(1);
+	Camera* camera = stack.get_camera(2);
+	const float dt = stack.get_float(3);
+
+	device()->render_world(*world, *camera, dt);
+	return 0;
+}
+
 //-----------------------------------------------------------------------------
 CE_EXPORT int device_create_resource_package(lua_State* L)
 {
@@ -124,6 +137,7 @@ void load_device(LuaEnvironment& env)
 	env.load_module_function("Device", "stop",                     device_stop);
 	env.load_module_function("Device", "create_world",             device_create_world);
 	env.load_module_function("Device", "destroy_world",            device_destroy_world);
+	env.load_module_function("Device", "render_world",             device_render_world);
 	env.load_module_function("Device", "create_resource_package",  device_create_resource_package);
 	env.load_module_function("Device", "destroy_resource_package", device_destroy_resource_package);
 }

+ 1 - 0
engine/lua/LuaEnvironment.cpp

@@ -62,6 +62,7 @@ CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
 	load_camera(*env);
 	load_world(*env);
 	load_sound(*env);
+	load_mesh(*env);
 
 	return 1;
 }

+ 1 - 0
engine/lua/LuaEnvironment.h

@@ -112,6 +112,7 @@ void load_unit(LuaEnvironment& env);
 void load_camera(LuaEnvironment& env);
 void load_world(LuaEnvironment& env);
 void load_sound(LuaEnvironment& env);
+void load_mesh(LuaEnvironment& env);
 
 CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
 

+ 151 - 0
engine/lua/LuaMesh.cpp

@@ -0,0 +1,151 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Mesh.h"
+#include "Quat.h"
+#include "LuaStack.h"
+#include "LuaEnvironment.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_local_position(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+
+	stack.push_vec3(mesh->local_position());
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_local_rotation(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+
+	stack.push_quat(mesh->local_rotation());
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_local_pose(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+
+	stack.push_mat4(mesh->local_pose());
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_world_position(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+
+	stack.push_vec3(mesh->world_position());
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_world_rotation(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+
+	stack.push_quat(mesh->world_rotation());
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_world_pose(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+
+	stack.push_mat4(mesh->world_pose());
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_set_local_position(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+	const Vec3 pos = stack.get_vec3(2);
+
+	mesh->set_local_position(pos);
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_set_local_rotation(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+	const Quat rot = stack.get_quat(2);
+
+	mesh->set_local_rotation(rot);
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int mesh_set_local_pose(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Mesh* mesh = stack.get_mesh(1);
+	const Mat4 pose = stack.get_mat4(2);
+
+	mesh->set_local_pose(pose);
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+void load_mesh(LuaEnvironment& env)
+{
+	env.load_module_function("Mesh", "local_position",         mesh_local_position);
+	env.load_module_function("Mesh", "local_rotation",         mesh_local_rotation);
+	env.load_module_function("Mesh", "local_pose",             mesh_local_pose);
+	env.load_module_function("Mesh", "world_position",         mesh_world_position);
+	env.load_module_function("Mesh", "world_rotation",         mesh_world_rotation);
+	env.load_module_function("Mesh", "world_pose",             mesh_world_pose);
+	env.load_module_function("Mesh", "set_local_position",     mesh_set_local_position);
+	env.load_module_function("Mesh", "set_local_rotation",     mesh_set_local_rotation);
+	env.load_module_function("Mesh", "set_local_pose",         mesh_set_local_pose);
+}
+
+} // namespace crown

+ 28 - 0
engine/lua/LuaStack.h

@@ -38,6 +38,8 @@ class Mat4;
 class Quat;
 class Unit;
 class Camera;
+class World;
+class Mesh;
 
 class LuaStack
 {
@@ -147,6 +149,18 @@ public:
 		return lua_touserdata(m_state, index);	
 	}
 
+	//-----------------------------------------------------------------------------
+	void push_world(World* world)
+	{
+		lua_pushlightuserdata(m_state, world);
+	}
+
+	//-----------------------------------------------------------------------------
+	World* get_world(int32_t index)
+	{
+		return (World*) lua_touserdata(m_state, index);
+	}
+
 	//-----------------------------------------------------------------------------
 	void push_unit(Unit* unit)
 	{
@@ -159,16 +173,30 @@ public:
 		return (Unit*) lua_touserdata(m_state, index);
 	}
 
+	//-----------------------------------------------------------------------------
 	void push_camera(Camera* camera)
 	{
 		lua_pushlightuserdata(m_state, camera);
 	}
 
+	//-----------------------------------------------------------------------------
 	Camera* get_camera(int32_t index)
 	{
 		return (Camera*) lua_touserdata(m_state, index);
 	}
 
+	//-----------------------------------------------------------------------------
+	void push_mesh(Mesh* mesh)
+	{
+		lua_pushlightuserdata(m_state, mesh);
+	}
+
+	//-----------------------------------------------------------------------------
+	Mesh* get_mesh(int32_t index)
+	{
+		return (Mesh*) lua_touserdata(m_state, index);
+	}
+
 	Vec2& get_vec2(int32_t index);
 	Vec3& get_vec3(int32_t index);
 	Mat4& get_mat4(int32_t index);

+ 1 - 1
engine/resource/ResourceManager.cpp

@@ -82,7 +82,7 @@ void ResourceManager::unload(ResourceId name)
 }
 
 //-----------------------------------------------------------------------------
-void* ResourceManager::lookup(const char* type, const char* name)
+const void* ResourceManager::lookup(const char* type, const char* name) const
 {
 	ResourceId id = resource_id(type, name);
 	ResourceEntry* entry = find(id);

+ 1 - 1
engine/resource/ResourceManager.h

@@ -74,7 +74,7 @@ public:
 	void					unload(ResourceId name);
 
 	/// Returns the resource instance associated to the given @a type and @a name.
-	void*					lookup(const char* type, const char* name);
+	const void*				lookup(const char* type, const char* name) const;
 
 	/// Returns whether the manager has the @a name resource into
 	/// its list of resources.