Browse Source

Added Mesh:getVertices (resolves issue #771)

Alex Szpakowski 11 years ago
parent
commit
206a1abd81

+ 11 - 0
src/modules/graphics/opengl/Mesh.cpp

@@ -76,6 +76,17 @@ void Mesh::setVertices(const std::vector<Vertex> &verts)
 	memcpy(vbo_mapper.get(), &verts[0], size);
 	memcpy(vbo_mapper.get(), &verts[0], size);
 }
 }
 
 
+const Vertex *Mesh::getVertices() const
+{
+	if (vbo)
+	{
+		VertexBuffer::Bind vbo_bind(*vbo);
+		return (Vertex *) vbo->map();
+	}
+
+	return nullptr;
+}
+
 void Mesh::setVertex(size_t index, const Vertex &v)
 void Mesh::setVertex(size_t index, const Vertex &v)
 {
 {
 	if (index >= vertex_count)
 	if (index >= vertex_count)

+ 5 - 0
src/modules/graphics/opengl/Mesh.h

@@ -71,6 +71,11 @@ public:
 	 **/
 	 **/
 	void setVertices(const std::vector<Vertex> &verts);
 	void setVertices(const std::vector<Vertex> &verts);
 
 
+	/**
+	 * Gets all of the vertices in the Mesh as an array.
+	 **/
+	const Vertex *getVertices() const;
+
 	/**
 	/**
 	 * Sets an individual vertex in the Mesh.
 	 * Sets an individual vertex in the Mesh.
 	 * @param index The index into the list of vertices to use.
 	 * @param index The index into the list of vertices to use.

+ 47 - 0
src/modules/graphics/opengl/wrap_Mesh.cpp

@@ -133,6 +133,52 @@ int w_Mesh_setVertices(lua_State *L)
 	return 0;
 	return 0;
 }
 }
 
 
+int w_Mesh_getVertices(lua_State *L)
+{
+	Mesh *t = luax_checkmesh(L, 1);
+
+	const Vertex *vertices = t->getVertices();
+
+	size_t count = t->getVertexCount();
+	lua_createtable(L, count, 0);
+
+	for (size_t i = 0; i < count; i++)
+	{
+		// Create vertex table.
+		lua_createtable(L, 8, 0);
+
+		lua_pushnumber(L, vertices[i].x);
+		lua_rawseti(L, -2, 1);
+
+		lua_pushnumber(L, vertices[i].y);
+		lua_rawseti(L, -2, 2);
+
+		lua_pushnumber(L, vertices[i].s);
+		lua_rawseti(L, -2, 3);
+
+		lua_pushnumber(L, vertices[i].t);
+		lua_rawseti(L, -2, 4);
+
+		lua_pushnumber(L, vertices[i].r);
+		lua_rawseti(L, -2, 5);
+
+		lua_pushnumber(L, vertices[i].g);
+		lua_rawseti(L, -2, 6);
+
+		lua_pushnumber(L, vertices[i].b);
+		lua_rawseti(L, -2, 7);
+
+		lua_pushnumber(L, vertices[i].a);
+		lua_rawseti(L, -2, 8);
+
+		// Insert vertex table into vertices table.
+		lua_rawseti(L, -2, i + 1);
+	}
+
+	// Return vertices table.
+	return 1;
+}
+
 int w_Mesh_getVertexCount(lua_State *L)
 int w_Mesh_getVertexCount(lua_State *L)
 {
 {
 	Mesh *t = luax_checkmesh(L, 1);
 	Mesh *t = luax_checkmesh(L, 1);
@@ -258,6 +304,7 @@ static const luaL_Reg functions[] =
 	{ "setVertex", w_Mesh_setVertex },
 	{ "setVertex", w_Mesh_setVertex },
 	{ "getVertex", w_Mesh_getVertex },
 	{ "getVertex", w_Mesh_getVertex },
 	{ "setVertices", w_Mesh_setVertices },
 	{ "setVertices", w_Mesh_setVertices },
+	{ "getVertices", w_Mesh_getVertices },
 	{ "getVertexCount", w_Mesh_getVertexCount },
 	{ "getVertexCount", w_Mesh_getVertexCount },
 	{ "setVertexMap", w_Mesh_setVertexMap },
 	{ "setVertexMap", w_Mesh_setVertexMap },
 	{ "getVertexMap", w_Mesh_getVertexMap },
 	{ "getVertexMap", w_Mesh_getVertexMap },

+ 1 - 0
src/modules/graphics/opengl/wrap_Mesh.h

@@ -37,6 +37,7 @@ Mesh *luax_checkmesh(lua_State *L, int idx);
 int w_Mesh_setVertex(lua_State *L);
 int w_Mesh_setVertex(lua_State *L);
 int w_Mesh_getVertex(lua_State *L);
 int w_Mesh_getVertex(lua_State *L);
 int w_Mesh_setVertices(lua_State *L);
 int w_Mesh_setVertices(lua_State *L);
+int w_Mesh_getVertices(lua_State *L);
 int w_Mesh_getVertexCount(lua_State *L);
 int w_Mesh_getVertexCount(lua_State *L);
 int w_Mesh_setVertexMap(lua_State *L);
 int w_Mesh_setVertexMap(lua_State *L);
 int w_Mesh_getVertexMap(lua_State *L);
 int w_Mesh_getVertexMap(lua_State *L);