From d0970658bfab1b3c5f9f0181d5dcb643057c9d22 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 24 Apr 2013 12:04:24 -0700 Subject: [PATCH] Exposed Geometry:setVertexColors and Geometry:hasVertexColors, to enable/disable per-vertex colors. Vertex colors are automatically enabled if newGeometry or setVertex is called with a color other than 255,255,255,255 --- src/modules/graphics/opengl/wrap_Geometry.cpp | 26 +++++++++++++++---- src/modules/graphics/opengl/wrap_Geometry.h | 4 ++- src/modules/graphics/opengl/wrap_Graphics.cpp | 6 ++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/modules/graphics/opengl/wrap_Geometry.cpp b/src/modules/graphics/opengl/wrap_Geometry.cpp index 1504daef9..cfe6f74ee 100644 --- a/src/modules/graphics/opengl/wrap_Geometry.cpp +++ b/src/modules/graphics/opengl/wrap_Geometry.cpp @@ -37,14 +37,14 @@ Geometry *luax_checkgeometry(lua_State *L, int idx) // different name than in Geometry.cpp to make the triangulation transparent int w_Geometry_getVertexCount(lua_State *L) { - Geometry *geom = luax_checktype(L, 1, "Geometry", GRAPHICS_GEOMETRY_T); + Geometry *geom = luax_checkgeometry(L, 1); lua_pushinteger(L, geom->getNumVertices()); return 1; } int w_Geometry_getVertex(lua_State *L) { - Geometry *geom = luax_checktype(L, 1, "Geometry", GRAPHICS_GEOMETRY_T); + Geometry *geom = luax_checkgeometry(L, 1); size_t i = size_t(luaL_checkint(L, 2)); try { @@ -68,7 +68,7 @@ int w_Geometry_getVertex(lua_State *L) int w_Geometry_setVertex(lua_State *L) { - Geometry *geom = luax_checktype(L, 1, "Geometry", GRAPHICS_GEOMETRY_T); + Geometry *geom = luax_checkgeometry(L, 1); size_t i = size_t(luaL_checkint(L, 2)); vertex v; @@ -90,7 +90,7 @@ int w_Geometry_setVertex(lua_State *L) return luaL_error(L, e.what()); } - if (lua_gettop(L) > 6) + if (v.r != 255 || v.g != 255 || v.b != 255 || v.a != 255) geom->setVertexColors(true); return 0; @@ -98,17 +98,33 @@ int w_Geometry_setVertex(lua_State *L) int w_Geometry_flip(lua_State *L) { - Geometry *geom = luax_checktype(L, 1, "Geometry", GRAPHICS_GEOMETRY_T); + Geometry *geom = luax_checkgeometry(L, 1); geom->flip(luax_toboolean(L, 2), luax_toboolean(L, 3)); return 0; } +int w_Geometry_setVertexColors(lua_State *L) +{ + Geometry *geom = luax_checkgeometry(L, 1); + geom->setVertexColors(luax_toboolean(L, 2)); + return 0; +} + +int w_Geometry_hasVertexColors(lua_State *L) +{ + Geometry *geom = luax_checkgeometry(L, 1); + luax_pushboolean(L, geom->hasVertexColors()); + return 1; +} + static const luaL_Reg w_Geometry_functions[] = { { "getVertexCount", w_Geometry_getVertexCount }, { "getVertex", w_Geometry_getVertex }, { "setVertex", w_Geometry_setVertex }, { "flip", w_Geometry_flip }, + { "setVertexColors", w_Geometry_setVertexColors }, + { "hasVertexColors", w_Geometry_hasVertexColors }, { 0, 0 } }; diff --git a/src/modules/graphics/opengl/wrap_Geometry.h b/src/modules/graphics/opengl/wrap_Geometry.h index 6a51ab2c6..c5415d37a 100644 --- a/src/modules/graphics/opengl/wrap_Geometry.h +++ b/src/modules/graphics/opengl/wrap_Geometry.h @@ -33,10 +33,12 @@ namespace opengl { Geometry *luax_checkgeometry(lua_State *L, int idx); -int w_Geometry_flip(lua_State *L); int w_Geometry_getVertexCount(lua_State *L); int w_Geometry_getVertex(lua_State *L); int w_Geometry_setVertex(lua_State *L); +int w_Geometry_flip(lua_State *L); +int w_Geometry_setVertexColors(lua_State *L); +int w_Geometry_hasVertexColors(lua_State *L); extern "C" int luaopen_geometry(lua_State *L); } // opengl diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index b9c7d4ef9..630a2decf 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -391,9 +391,6 @@ int w_newGeometry(lua_State *L) if (!lua_istable(L, -1)) return luaL_typerror(L, 1, "table of tables"); - if (lua_objlen(L, -1) > 4) - hasvertexcolors = true; - for (int j = 1; j <= 8; j++) lua_rawgeti(L, -j, j); @@ -410,6 +407,9 @@ int w_newGeometry(lua_State *L) lua_pop(L, 9); + if (v.r != 255 || v.g != 255 || v.b != 255 || v.a != 255) + hasvertexcolors = true; + vertices.push_back(v); }