From cd303c7c6292391d21620666a91a71bb74b5ad82 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 30 Dec 2013 01:44:04 -0400 Subject: [PATCH] Added Mesh:setWireframe and Mesh:isWireframe. This should *only* be used for debugging: the wireframe lines behave differently from regular love.graphics lines, and the functionality may not work on OpenGL ES. --- src/modules/graphics/opengl/Mesh.cpp | 17 +++++++++++++++++ src/modules/graphics/opengl/Mesh.h | 11 +++++++++++ src/modules/graphics/opengl/wrap_Mesh.cpp | 16 ++++++++++++++++ src/modules/graphics/opengl/wrap_Mesh.h | 2 ++ 4 files changed, 46 insertions(+) diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index ff4fc934b..e0749a323 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -38,6 +38,7 @@ Mesh::Mesh(const std::vector &verts, Mesh::DrawMode mode) , draw_mode(mode) , image(nullptr) , colors_enabled(false) + , wireframe(false) { setVertices(verts); } @@ -212,6 +213,16 @@ bool Mesh::hasVertexColors() const return colors_enabled; } +void Mesh::setWireframe(bool enable) +{ + wireframe = enable; +} + +bool Mesh::isWireframe() const +{ + return wireframe; +} + void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const { const size_t pos_offset = offsetof(Vertex, x); @@ -250,6 +261,9 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), vbo->getPointer(color_offset)); } + if (wireframe) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + GLenum mode = getGLDrawMode(draw_mode); if (ibo && element_count > 0) @@ -268,6 +282,9 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo glDrawArrays(mode, 0, vertex_count); } + if (wireframe) + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); diff --git a/src/modules/graphics/opengl/Mesh.h b/src/modules/graphics/opengl/Mesh.h index 45d21ce53..35cc21cbd 100644 --- a/src/modules/graphics/opengl/Mesh.h +++ b/src/modules/graphics/opengl/Mesh.h @@ -138,6 +138,15 @@ public: void setVertexColors(bool enable); bool hasVertexColors() const; + /** + * Sets whether the Mesh will be drawn as wireframe lines instead of filled + * triangles (has no effect for DRAW_MODE_POINTS.) + * This should only be used as a debugging tool. The wireframe lines do not + * behave the same as regular love.graphics lines. + **/ + void setWireframe(bool enable); + bool isWireframe() const; + // Implements Drawable. void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const; @@ -163,6 +172,8 @@ private: // Whether the per-vertex colors are used when drawing. bool colors_enabled; + bool wireframe; + static StringMap::Entry drawModeEntries[]; static StringMap drawModes; diff --git a/src/modules/graphics/opengl/wrap_Mesh.cpp b/src/modules/graphics/opengl/wrap_Mesh.cpp index 624eecbf3..c4399eb24 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.cpp +++ b/src/modules/graphics/opengl/wrap_Mesh.cpp @@ -299,6 +299,20 @@ int w_Mesh_hasVertexColors(lua_State *L) return 1; } +int w_Mesh_setWireframe(lua_State *L) +{ + Mesh *t = luax_checkmesh(L, 1); + t->setWireframe(luax_toboolean(L, 2)); + return 0; +} + +int w_Mesh_isWireframe(lua_State *L) +{ + Mesh *t = luax_checkmesh(L, 1); + luax_pushboolean(L, t->isWireframe()); + return 1; +} + static const luaL_Reg functions[] = { { "setVertex", w_Mesh_setVertex }, @@ -314,6 +328,8 @@ static const luaL_Reg functions[] = { "getDrawMode", w_Mesh_getDrawMode }, { "setVertexColors", w_Mesh_setVertexColors }, { "hasVertexColors", w_Mesh_hasVertexColors }, + { "setWireframe", w_Mesh_setWireframe }, + { "isWireframe", w_Mesh_isWireframe }, { 0, 0 } }; diff --git a/src/modules/graphics/opengl/wrap_Mesh.h b/src/modules/graphics/opengl/wrap_Mesh.h index 1204bbcea..e3d52ebc8 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.h +++ b/src/modules/graphics/opengl/wrap_Mesh.h @@ -47,6 +47,8 @@ int w_Mesh_setDrawMode(lua_State *L); int w_Mesh_getDrawMode(lua_State *L); int w_Mesh_setVertexColors(lua_State *L); int w_Mesh_hasVertexColors(lua_State *L); +int w_Mesh_setWireframe(lua_State *L); +int w_Mesh_isWireframe(lua_State *L); extern "C" int luaopen_mesh(lua_State *L);