Browse Source

Mesh:getVertexMap now returns nil rather than an empty table, if no vertex map is set (resolves issue #1096.)

Alex Szpakowski 9 years ago
parent
commit
bb24c1b7a6

+ 17 - 4
src/modules/graphics/opengl/Mesh.cpp

@@ -64,6 +64,7 @@ Mesh::Mesh(const std::vector<AttribFormat> &vertexformat, const void *data, size
 	, vertexCount(0)
 	, vertexStride(0)
 	, ibo(nullptr)
+	, useIndexBuffer(false)
 	, elementCount(0)
 	, elementDataType(0)
 	, drawMode(drawmode)
@@ -90,6 +91,7 @@ Mesh::Mesh(const std::vector<AttribFormat> &vertexformat, int vertexcount, DrawM
 	, vertexCount((size_t) vertexcount)
 	, vertexStride(0)
 	, ibo(nullptr)
+	, useIndexBuffer(false)
 	, elementCount(0)
 	, elementDataType(getGLDataTypeFromMax(vertexcount))
 	, drawMode(drawmode)
@@ -414,6 +416,7 @@ void Mesh::setVertexMap(const std::vector<uint32> &map)
 	if (!ibo && size > 0)
 		ibo = new GLBuffer(size, nullptr, GL_ELEMENT_ARRAY_BUFFER, vbo->getUsage());
 
+	useIndexBuffer = true;
 	elementCount = map.size();
 
 	if (!ibo || elementCount == 0)
@@ -437,6 +440,11 @@ void Mesh::setVertexMap(const std::vector<uint32> &map)
 	elementDataType = datatype;
 }
 
+void Mesh::setVertexMap()
+{
+	useIndexBuffer = false;
+}
+
 /**
  * Copies index data from a mapped buffer to a vector.
  **/
@@ -448,14 +456,17 @@ static void copyFromIndexBuffer(void *buffer, size_t count, std::vector<uint32>
 		indices.push_back((uint32) elems[i]);
 }
 
-void Mesh::getVertexMap(std::vector<uint32> &map) const
+bool Mesh::getVertexMap(std::vector<uint32> &map) const
 {
-	if (!ibo || elementCount == 0)
-		return;
+	if (!useIndexBuffer)
+		return false;
 
 	map.clear();
 	map.reserve(elementCount);
 
+	if (!ibo || elementCount == 0)
+		return true;
+
 	GLBuffer::Bind ibobind(*ibo);
 
 	// We unmap the buffer in Mesh::draw, Mesh::setVertexMap, and Mesh::flush.
@@ -472,6 +483,8 @@ void Mesh::getVertexMap(std::vector<uint32> &map) const
 		copyFromIndexBuffer<uint32>(buffer, elementCount, map);
 		break;
 	}
+
+	return true;
 }
 
 size_t Mesh::getVertexMapCount() const
@@ -588,7 +601,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo
 
 	gl.prepareDraw();
 
-	if (ibo && elementCount > 0)
+	if (useIndexBuffer && ibo && elementCount > 0)
 	{
 		// Use the custom vertex map (index buffer) to draw the vertices.
 		GLBuffer::Bind ibo_bind(*ibo);

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

@@ -154,12 +154,13 @@ public:
 	 * {0, 1, 2, 3, 4, ...}
 	 **/
 	void setVertexMap(const std::vector<uint32> &map);
+	void setVertexMap();
 
 	/**
 	 * Fills the uint32 vector passed into the method with the previously set
 	 * vertex map (index buffer) values.
 	 **/
-	void getVertexMap(std::vector<uint32> &map) const;
+	bool getVertexMap(std::vector<uint32> &map) const;
 
 	/**
 	 * Gets the total number of elements in the vertex map array.
@@ -242,6 +243,7 @@ private:
 
 	// Element (vertex index) buffer, for the vertex map.
 	GLBuffer *ibo;
+	bool useIndexBuffer;
 	size_t elementCount;
 	GLenum elementDataType;
 

+ 15 - 1
src/modules/graphics/opengl/wrap_Mesh.cpp

@@ -331,6 +331,13 @@ int w_Mesh_setVertexMap(lua_State *L)
 {
 	Mesh *t = luax_checkmesh(L, 1);
 
+	if (lua_isnoneornil(L, 2))
+	{
+		// Disable the vertex map / index buffer.
+		luax_catchexcept(L, [&](){ t->setVertexMap(); });
+		return 0;
+	}
+
 	bool is_table = lua_istable(L, 2);
 	int nargs = is_table ? (int) luax_objlen(L, 2) : lua_gettop(L) - 1;
 
@@ -361,7 +368,14 @@ int w_Mesh_getVertexMap(lua_State *L)
 	Mesh *t = luax_checkmesh(L, 1);
 
 	std::vector<uint32> vertex_map;
-	luax_catchexcept(L, [&](){ t->getVertexMap(vertex_map); });
+	bool has_vertex_map = false;
+	luax_catchexcept(L, [&](){ has_vertex_map = t->getVertexMap(vertex_map); });
+
+	if (!has_vertex_map)
+	{
+		lua_pushnil(L);
+		return 1;
+	}
 
 	int element_count = (int) vertex_map.size();