Procházet zdrojové kódy

Add Mesh, in this case I use a different coupling scheme: cache the world and local poses inside Mesh

Daniele Bartolini před 12 roky
rodič
revize
56edac1079

+ 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

+ 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

+ 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

+ 15 - 0
engine/lua/LuaStack.h

@@ -39,6 +39,7 @@ class Quat;
 class Unit;
 class Camera;
 class World;
+class Mesh;
 
 class LuaStack
 {
@@ -172,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);