Browse Source

Added an optional 'startvertex' argument to Mesh:setVertices, which specifies a (1-based) offset within the Mesh's vertices to replace.

Alex Szpakowski 9 years ago
parent
commit
ebf6ca822c

+ 2 - 3
src/modules/graphics/opengl/Mesh.cpp

@@ -360,11 +360,10 @@ void *Mesh::mapVertexData()
 	return vbo->map();
 	return vbo->map();
 }
 }
 
 
-void Mesh::unmapVertexData()
+void Mesh::unmapVertexData(size_t modifiedoffset, size_t modifiedsize)
 {
 {
-	// Assume the whole buffer was modified.
 	GLBuffer::Bind bind(*vbo);
 	GLBuffer::Bind bind(*vbo);
-	vbo->setMappedRangeModified(0, vbo->getSize());
+	vbo->setMappedRangeModified(modifiedoffset, modifiedsize);
 	vbo->unmap();
 	vbo->unmap();
 }
 }
 
 

+ 1 - 1
src/modules/graphics/opengl/Mesh.h

@@ -141,7 +141,7 @@ public:
 	void attachAttribute(const std::string &name, Mesh *mesh);
 	void attachAttribute(const std::string &name, Mesh *mesh);
 
 
 	void *mapVertexData();
 	void *mapVertexData();
-	void unmapVertexData();
+	void unmapVertexData(size_t modifiedoffset = 0, size_t modifiedsize = -1);
 
 
 	/**
 	/**
 	 * Flushes all modified data to the GPU.
 	 * Flushes all modified data to the GPU.

+ 12 - 4
src/modules/graphics/opengl/wrap_Mesh.cpp

@@ -109,10 +109,15 @@ int w_Mesh_setVertices(lua_State *L)
 {
 {
 	Mesh *t = luax_checkmesh(L, 1);
 	Mesh *t = luax_checkmesh(L, 1);
 	luaL_checktype(L, 2, LUA_TTABLE);
 	luaL_checktype(L, 2, LUA_TTABLE);
+	size_t vertoffset = (size_t) luaL_optnumber(L, 3, 1) - 1;
+
+	if (vertoffset >= t->getVertexCount())
+		return luaL_error(L, "Invalid vertex start index (must be between 1 and %d)", (int) t->getVertexCount());
 
 
 	size_t nvertices = luax_objlen(L, 2);
 	size_t nvertices = luax_objlen(L, 2);
-	if (nvertices != t->getVertexCount())
-		return luaL_error(L, "Invalid number of vertices (expected %d, got %d)", (int) t->getVertexCount(), (int) nvertices);
+
+	if (vertoffset + nvertices > t->getVertexCount())
+		return luaL_error(L, "Too many vertices (expected at most %d, got %d)", (int) t->getVertexCount() - (int) vertoffset, (int) nvertices);
 
 
 	const std::vector<Mesh::AttribFormat> &vertexformat = t->getVertexFormat();
 	const std::vector<Mesh::AttribFormat> &vertexformat = t->getVertexFormat();
 
 
@@ -120,7 +125,10 @@ int w_Mesh_setVertices(lua_State *L)
 	for (const Mesh::AttribFormat &format : vertexformat)
 	for (const Mesh::AttribFormat &format : vertexformat)
 		ncomponents += format.components;
 		ncomponents += format.components;
 
 
-	char *data = (char *) t->mapVertexData();
+	size_t stride = t->getVertexStride();
+	size_t byteoffset = vertoffset * stride;
+
+	char *data = (char *) t->mapVertexData() + byteoffset;
 
 
 	for (size_t i = 0; i < nvertices; i++)
 	for (size_t i = 0; i < nvertices; i++)
 	{
 	{
@@ -145,7 +153,7 @@ int w_Mesh_setVertices(lua_State *L)
 		lua_pop(L, ncomponents + 1);
 		lua_pop(L, ncomponents + 1);
 	}
 	}
 
 
-	t->unmapVertexData();
+	t->unmapVertexData(byteoffset, nvertices * stride);
 	return 0;
 	return 0;
 }
 }