Commits

Alex Szpakowski committed c3dcedf

Fixed Mesh:setVertexMap when the mesh has more than 2^16-1 vertices (still needs more work to make it use smaller data types when available), added Mesh:setVertices

  • Participants
  • Parent commits 7ed2836
  • Branches Mesh

Comments (0)

Files changed (5)

File src/modules/graphics/opengl/Mesh.cpp

 {
 
 Mesh::Mesh(const std::vector<Vertex> &verts, Mesh::DrawMode mode)
-	: vbo(0)
+	: vbo(nullptr)
 	, vertex_count(0)
-	, ibo(0)
+	, ibo(nullptr)
 	, element_count(0)
 	, draw_mode(mode)
-	, image(0)
+	, image(nullptr)
 	, colors_enabled(false)
 {
 	setVertices(verts);
 	if (vbo && size > vbo->getSize())
 	{
 		delete vbo;
-		vbo = 0;
+		vbo = nullptr;
 	}
 
 	if (!vbo)
 	VertexBuffer::Bind vbo_bind(*vbo);
 
 	// We unmap the vertex buffer in Mesh::draw. This lets us coalesce the
-	// buffer transfer calls.
+	// buffer transfer calls into just one.
 	Vertex *vertices = (Vertex *) vbo->map();
 	vertices[index] = v;
 }
 	return vertex_count;
 }
 
-void Mesh::setVertexMap(const std::vector<uint16> &map)
+void Mesh::setVertexMap(const std::vector<uint32> &map)
 {
 	for (size_t i = 0; i < map.size(); i++)
 	{
 			throw love::Exception("Invalid vertex map value: %d", map[i]);
 	}
 
-	size_t size = sizeof(uint16) * map.size();
+	size_t size = sizeof(uint32) * map.size();
 
 	if (ibo && size > ibo->getSize())
 	{
 		delete ibo;
-		ibo = 0;
+		ibo = nullptr;
 	}
 
 	if (!ibo)
 	}
 }
 
-const uint16 *Mesh::getVertexMap() const
+const uint32 *Mesh::getVertexMap() const
 {
 	if (ibo && element_count > 0)
 	{
 		VertexBuffer::Bind ibo_bind(*ibo);
 
 		// We unmap the buffer in Mesh::draw and Mesh::setVertexMap.
-		return (uint16 *) ibo->map();
+		return (uint32 *) ibo->map();
 	}
 
 	return 0;
 	if (image)
 		image->release();
 
-	image = 0;
+	image = nullptr;
 }
 
 Image *Mesh::getImage() const
 		ibo->unmap();
 
 		// Use the custom vertex map to draw the vertices.
-		glDrawElements(mode, element_count, GL_UNSIGNED_SHORT, ibo->getPointer(0));
+		glDrawElements(mode, element_count, GL_UNSIGNED_INT, ibo->getPointer(0));
 	}
 	else
 	{

File src/modules/graphics/opengl/Mesh.h

 	 * A 0-element vector is equivalent to the default vertex map:
 	 * {0, 1, 2, 3, 4, ...}
 	 **/
-	void setVertexMap(const std::vector<uint16> &map);
+	void setVertexMap(const std::vector<uint32> &map);
 
 	/**
 	 * Gets a pointer to the vertex map array. The pointer is only valid until
 	 * the next function call in the graphics module.
 	 * May return null if the vertex map is empty.
 	 **/
-	const uint16 *getVertexMap() const;
+	const uint32 *getVertexMap() const;
 
 	/**
 	 * Gets the total number of elements in the vertex map array.

File src/modules/graphics/opengl/wrap_Graphics.cpp

 		lua_rawgeti(L, 1, i);
 
 		if (lua_type(L, -1) != LUA_TTABLE)
-			return luax_typerror(L, 2, "table of tables");
+			return luax_typerror(L, 1, "table of tables");
 
 		for (int j = 1; j <= 8; j++)
 			lua_rawgeti(L, -j, j);

File src/modules/graphics/opengl/wrap_Mesh.cpp

 	return 8;
 }
 
+int w_Mesh_setVertices(lua_State *L)
+{
+	Mesh *t = luax_checkmesh(L, 1);
+
+	size_t vertex_count = lua_objlen(L, 2);
+	std::vector<Vertex> vertices;
+	vertices.reserve(vertex_count);
+
+	// Get the vertices from the table.
+	for (size_t i = 1; i <= vertex_count; i++)
+	{
+		lua_rawgeti(L, 2, i);
+
+		if (lua_type(L, -1) != LUA_TTABLE)
+			return luax_typerror(L, 2, "table of tables");
+
+		for (int j = 1; j <= 8; j++)
+			lua_rawgeti(L, -j, j);
+
+		Vertex v;
+
+		v.x = (float) luaL_checknumber(L, -8);
+		v.y = (float) luaL_checknumber(L, -7);
+
+		v.s = (float) luaL_checknumber(L, -6);
+		v.t = (float) luaL_checknumber(L, -5);
+
+		v.r = (unsigned char) luaL_optinteger(L, -4, 255);
+		v.g = (unsigned char) luaL_optinteger(L, -3, 255);
+		v.b = (unsigned char) luaL_optinteger(L, -2, 255);
+		v.a = (unsigned char) luaL_optinteger(L, -1, 255);
+
+		lua_pop(L, 9);
+		vertices.push_back(v);
+	}
+
+	EXCEPT_GUARD(t->setVertices(vertices);)
+	return 0;
+}
+
 int w_Mesh_getVertexCount(lua_State *L)
 {
 	Mesh *t = luax_checkmesh(L, 1);
 	bool is_table = lua_istable(L, 2);
 	int nargs = is_table ? lua_objlen(L, 2) : lua_gettop(L) - 1;
 
-	std::vector<uint16> vertexmap;
+	std::vector<uint32> vertexmap;
 	vertexmap.reserve(nargs);
 
 	for (int i = 0; i < nargs; i++)
 		if (is_table)
 		{
 			lua_rawgeti(L, 2, i + 1);
-			vertexmap.push_back(uint16(luaL_checkinteger(L, -1) - 1));
+			vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1));
 			lua_pop(L, 1);
 		}
 		else
-			vertexmap.push_back(uint16(luaL_checkinteger(L, i + 2) - 1));
+			vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1));
 	}
 
 	EXCEPT_GUARD(t->setVertexMap(vertexmap);)
 {
 	Mesh *t = luax_checkmesh(L, 1);
 
-	const uint16 *vertex_map = t->getVertexMap();
+	const uint32 *vertex_map = t->getVertexMap();
 	size_t elements = t->getVertexMapCount();
 
 	lua_createtable(L, elements, 0);
 {
 	{ "setVertex", w_Mesh_setVertex },
 	{ "getVertex", w_Mesh_getVertex },
+	{ "setVertices", w_Mesh_setVertices },
 	{ "getVertexCount", w_Mesh_getVertexCount },
 	{ "setVertexMap", w_Mesh_setVertexMap },
 	{ "getVertexMap", w_Mesh_getVertexMap },

File src/modules/graphics/opengl/wrap_Mesh.h

 
 int w_Mesh_setVertex(lua_State *L);
 int w_Mesh_getVertex(lua_State *L);
+int w_Mesh_setVertices(lua_State *L);
 int w_Mesh_getVertexCount(lua_State *L);
 int w_Mesh_setVertexMap(lua_State *L);
 int w_Mesh_getVertexMap(lua_State *L);