diff --git a/src/python/magnum/gl.cpp b/src/python/magnum/gl.cpp index f38eeb3..dfd7d81 100644 --- a/src/python/magnum/gl.cpp +++ b/src/python/magnum/gl.cpp @@ -944,6 +944,11 @@ void gl(py::module_& m) { #endif ; + py::enum_{m, "MeshIndexType", "Mesh index type"} + .value("UNSIGNED_BYTE", GL::MeshIndexType::UnsignedByte) + .value("UNSIGNED_SHORT", GL::MeshIndexType::UnsignedShort) + .value("UNSIGNED_INT", GL::MeshIndexType::UnsignedInt); + /* Class definition above AbstractShaderProgram, since that needs it for the draw() signature */ mesh.def(py::init(), "Constructor", py::arg("primitive") = GL::MeshPrimitive::Triangles) @@ -965,6 +970,14 @@ void gl(py::module_& m) { .def_property("count", &GL::Mesh::count, [](GL::Mesh& self, UnsignedInt count) { self.setCount(count); }, "Vertex/index count") + .def_property_readonly("is_indexed", &GL::Mesh::isIndexed, "Whether the mesh is indexed") + .def_property_readonly("index_type", [](GL::Mesh& self) { + if(!self.isIndexed()) { + PyErr_SetString(PyExc_RuntimeError, "the mesh is not indexed"); + throw py::error_already_set{}; + } + return self.indexType(); + }, "Index type") /* Using lambdas to avoid method chaining getting into signatures */ @@ -975,7 +988,22 @@ void gl(py::module_& m) { the mesh */ pyObjectHolderFor(self).buffers.emplace_back(pyObjectFromInstance(buffer)); }, "Add vertex buffer", py::arg("buffer"), py::arg("offset"), py::arg("stride"), py::arg("attribute")) - /** @todo more */ + /** @todo add_vertex_buffer_instanced */ + + .def("set_index_buffer", [](GL::Mesh& self, GL::Buffer& buffer, GLintptr offset, GL::MeshIndexType type) { + self.setIndexBuffer(buffer, offset, type); + + /* Keep a reference to the buffer to avoid it being deleted before + the mesh */ + pyObjectHolderFor(self).buffers.emplace_back(pyObjectFromInstance(buffer)); + }, "Set index buffer", py::arg("buffer"), py::arg("offset"), py::arg("type")) + .def("set_index_buffer", [](GL::Mesh& self, GL::Buffer& buffer, GLintptr offset, MeshIndexType type) { + self.setIndexBuffer(buffer, offset, type); + + /* Keep a reference to the buffer to avoid it being deleted before + the mesh */ + pyObjectHolderFor(self).buffers.emplace_back(pyObjectFromInstance(buffer)); + }, "Set index buffer", py::arg("buffer"), py::arg("offset"), py::arg("type")) .def_property_readonly("buffers", [](GL::Mesh& self) { return pyObjectHolderFor(self).buffers; diff --git a/src/python/magnum/test/test_gl_gl.py b/src/python/magnum/test/test_gl_gl.py index b324ac6..9c9e5f8 100644 --- a/src/python/magnum/test/test_gl_gl.py +++ b/src/python/magnum/test/test_gl_gl.py @@ -278,7 +278,7 @@ class Mesh(GLTestCase): a.count = 15 self.assertEqual(a.count, 15) - def test_add_buffer(self): + def test_add_vertex_buffer(self): buffer = gl.Buffer() buffer_refcount = sys.getrefcount(buffer) @@ -293,6 +293,27 @@ class Mesh(GLTestCase): del mesh self.assertEqual(sys.getrefcount(buffer), buffer_refcount) + def test_set_index_buffer(self): + buffer = gl.Buffer() + buffer_refcount = sys.getrefcount(buffer) + + # Adding a buffer to the mesh should increase its ref count + mesh = gl.Mesh() + mesh.set_index_buffer(buffer, 0, gl.MeshIndexType.UNSIGNED_INT) + self.assertEqual(len(mesh.buffers), 1) + self.assertIs(mesh.buffers[0], buffer) + self.assertEqual(sys.getrefcount(buffer), buffer_refcount + 1) + + # Trying with the generic type as well + mesh.set_index_buffer(buffer, 0, MeshIndexType.UNSIGNED_SHORT) + self.assertEqual(len(mesh.buffers), 2) + self.assertIs(mesh.buffers[1], buffer) + self.assertEqual(sys.getrefcount(buffer), buffer_refcount + 2) + + # Deleting the mesh should decrease it again + del mesh + self.assertEqual(sys.getrefcount(buffer), buffer_refcount) + class Renderbuffer(GLTestCase): def test_init(self): renderbuffer = gl.Renderbuffer()