diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 3c2fbc6a4..8bcdf0404 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -87,6 +87,9 @@ DisplayState Graphics::saveState() if (s.scissor) glGetIntegerv(GL_SCISSOR_BOX, s.scissorBox); + for (int i = 0; i < 4; i++) + s.colorMask[i] = colorMask[i]; + return s; } @@ -101,6 +104,7 @@ void Graphics::restoreState(const DisplayState &s) setScissor(s.scissorBox[0], s.scissorBox[1], s.scissorBox[2], s.scissorBox[3]); else setScissor(); + setColorMask(s.colorMask[0], s.colorMask[1], s.colorMask[2], s.colorMask[3]); } bool Graphics::setMode(int width, int height, WindowFlags *flags) @@ -135,6 +139,9 @@ bool Graphics::setMode(int width, int height, WindowFlags *flags) // "Normal" blending glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // Enable all color component writes. + setColorMask(true, true, true, true); + // Enable line/point smoothing. setLineStyle(LINE_SMOOTH); glEnable(GL_POINT_SMOOTH); @@ -338,12 +345,12 @@ void Graphics::useStencil(bool invert) { glStencilFunc(GL_EQUAL, (int)(!invert), 1); // invert ? 0 : 1 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + setColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]); } void Graphics::discardStencil() { - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + setColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]); glDisable(GL_STENCIL_TEST); } @@ -507,6 +514,21 @@ Font *Graphics::getFont() const return currentFont; } +void Graphics::setColorMask(bool r, bool g, bool b, bool a) +{ + colorMask[0] = r; + colorMask[1] = g; + colorMask[2] = b; + colorMask[3] = a; + + glColorMask((GLboolean) r, (GLboolean) g, (GLboolean) b, (GLboolean) a); +} + +const bool *Graphics::getColorMask() const +{ + return colorMask; +} + void Graphics::setBlendMode(Graphics::BlendMode mode) { const int gl_1_4 = GLEE_VERSION_1_4; diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 696fc525c..9d79f57a2 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -77,6 +77,9 @@ struct DisplayState bool scissor; GLint scissorBox[4]; + // Color mask. + bool colorMask[4]; + // Window info. std::string caption; bool mouseVisible; @@ -94,6 +97,7 @@ struct DisplayState pointSize = 1.0f; pointStyle = Graphics::POINT_SMOOTH; scissor = false; + colorMask[0] = colorMask[1] = colorMask[2] = colorMask[3] = true; caption = ""; mouseVisible = true; } @@ -299,6 +303,17 @@ public: **/ Font *getFont() const; + /** + * Sets the enabled color components when rendering. + **/ + void setColorMask(bool r, bool g, bool b, bool a); + + /** + * Gets the current color mask. + * Returns an array of 4 booleans representing the mask. + **/ + const bool *getColorMask() const; + /** * Sets the current blend mode. **/ @@ -495,6 +510,7 @@ private: float lineWidth; GLint matrixLimit; GLint userMatrices; + bool colorMask[4]; int getRenderWidth() const; int getRenderHeight() const; diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 90cfc7649..5334153ad 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -701,6 +701,28 @@ int w_getFont(lua_State *L) return 1; } +int w_setColorMask(lua_State *L) +{ + bool mask[4]; + for (int i = 0; i < 4; i++) + mask[i] = luax_toboolean(L, i + 1); + + // r, g, b, a + instance->setColorMask(mask[0], mask[1], mask[2], mask[3]); + + return 0; +} + +int w_getColorMask(lua_State *L) +{ + const bool *mask = instance->getColorMask(); + + for (int i = 0; i < 4; i++) + luax_pushboolean(L, mask[i]); + + return 4; +} + int w_setBlendMode(lua_State *L) { Graphics::BlendMode mode; @@ -1483,6 +1505,8 @@ static const luaL_Reg functions[] = { "setFont", w_setFont }, { "getFont", w_getFont }, + { "setColorMask", w_setColorMask }, + { "getColorMask", w_getColorMask }, { "setBlendMode", w_setBlendMode }, { "getBlendMode", w_getBlendMode }, { "setDefaultFilter", w_setDefaultFilter }, diff --git a/src/modules/graphics/opengl/wrap_Graphics.h b/src/modules/graphics/opengl/wrap_Graphics.h index 89f7c68bb..b67d0e2e5 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.h +++ b/src/modules/graphics/opengl/wrap_Graphics.h @@ -71,6 +71,8 @@ int w_setBackgroundColor(lua_State *L); int w_getBackgroundColor(lua_State *L); int w_setFont(lua_State *L); int w_getFont(lua_State *L); +int w_setColorMask(lua_State *L); +int w_getColorMask(lua_State *L); int w_setBlendMode(lua_State *L); int w_getBlendMode(lua_State *L); int w_setDefaultFilter(lua_State *L);