From ac6e2f4979605b65b9b794a229a94872e2fe664c Mon Sep 17 00:00:00 2001 From: Christopher Bruns Date: Thu, 31 Dec 2015 11:57:21 -0500 Subject: [PATCH] Use gl_RenderState to manipulate OpenGL color mask for anaglyph modes, to avoid glGet... --- src/gl/renderer/gl_renderstate.cpp | 19 +++++++++++++++++++ src/gl/renderer/gl_renderstate.h | 25 +++++++++++++++++++++++++ src/gl/stereo3d/gl_anaglyph.h | 27 ++++++++++++++++++++------- src/gl/stereo3d/scoped_color_mask.h | 12 +++++++----- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/gl/renderer/gl_renderstate.cpp b/src/gl/renderer/gl_renderstate.cpp index 15b046349..3e5900e45 100644 --- a/src/gl/renderer/gl_renderstate.cpp +++ b/src/gl/renderer/gl_renderstate.cpp @@ -70,6 +70,8 @@ void FRenderState::Reset() { mTextureEnabled = true; mBrightmapEnabled = mFogEnabled = mGlowEnabled = false; + mColorMask[0] = mColorMask[1] = mColorMask[2] = mColorMask[3] = true; + currentColorMask[0] = currentColorMask[1] = currentColorMask[2] = currentColorMask[3] = true; mFogColor.d = -1; mTextureMode = -1; mLightIndex = -1; @@ -257,6 +259,8 @@ void FRenderState::Apply() } } + ApplyColorMask(); + if (mVertexBuffer != mCurrentVertexBuffer) { if (mVertexBuffer == NULL) glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -268,6 +272,21 @@ void FRenderState::Apply() +void FRenderState::ApplyColorMask() +{ + if ((mColorMask[0] != currentColorMask[0]) || + (mColorMask[1] != currentColorMask[1]) || + (mColorMask[2] != currentColorMask[2]) || + (mColorMask[3] != currentColorMask[3])) + { + glColorMask(mColorMask[0], mColorMask[1], mColorMask[2], mColorMask[3]); + currentColorMask[0] = mColorMask[0]; + currentColorMask[1] = mColorMask[1]; + currentColorMask[2] = mColorMask[2]; + currentColorMask[3] = mColorMask[3]; + } +} + void FRenderState::ApplyMatrices() { if (GLRenderer->mShaderManager != NULL) diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 74065fe4c..aa24072c2 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -47,6 +47,8 @@ class FRenderState bool mFogEnabled; bool mGlowEnabled; bool mBrightmapEnabled; + bool mColorMask[4]; + bool currentColorMask[4]; int mLightIndex; int mSpecialEffect; int mTextureMode; @@ -116,6 +118,7 @@ public: } void Apply(); + void ApplyColorMask(); void ApplyMatrices(); void ApplyLightIndex(int index); @@ -174,6 +177,28 @@ public: mDesaturation = 0; } + void GetColorMask(bool& r, bool &g, bool& b, bool& a) const + { + r = mColorMask[0]; + g = mColorMask[1]; + b = mColorMask[2]; + a = mColorMask[3]; + } + + void SetColorMask(bool r, bool g, bool b, bool a) + { + mColorMask[0] = r; + mColorMask[1] = g; + mColorMask[2] = b; + mColorMask[3] = a; + } + + void ResetColorMask() + { + for (int i = 0; i < 4; ++i) + mColorMask[i] = true; + } + void SetTextureMode(int mode) { mTextureMode = mode; diff --git a/src/gl/stereo3d/gl_anaglyph.h b/src/gl/stereo3d/gl_anaglyph.h index 6f86540fe..d60506dde 100644 --- a/src/gl/stereo3d/gl_anaglyph.h +++ b/src/gl/stereo3d/gl_anaglyph.h @@ -39,6 +39,7 @@ #include "gl_stereo3d.h" #include "gl_stereo_leftright.h" #include "gl/system/gl_system.h" +#include "gl/renderer/gl_renderstate.h" namespace s3d { @@ -50,9 +51,9 @@ public: ColorMask(bool r, bool g, bool b) : r(r), g(g), b(b) {} ColorMask inverse() const { return ColorMask(!r, !g, !b); } - GLboolean r; - GLboolean g; - GLboolean b; + bool r; + bool g; + bool b; }; @@ -60,8 +61,14 @@ class AnaglyphLeftPose : public LeftEyePose { public: AnaglyphLeftPose(const ColorMask& colorMask, float ipd) : LeftEyePose(ipd), colorMask(colorMask) {} - virtual void SetUp() const { glColorMask(colorMask.r, colorMask.g, colorMask.b, true); } - virtual void TearDown() const { glColorMask(1,1,1,1); } + virtual void SetUp() const { + gl_RenderState.SetColorMask(colorMask.r, colorMask.g, colorMask.b, true); + gl_RenderState.ApplyColorMask(); + } + virtual void TearDown() const { + gl_RenderState.ResetColorMask(); + gl_RenderState.ApplyColorMask(); + } private: ColorMask colorMask; }; @@ -70,8 +77,14 @@ class AnaglyphRightPose : public RightEyePose { public: AnaglyphRightPose(const ColorMask& colorMask, float ipd) : RightEyePose(ipd), colorMask(colorMask) {} - virtual void SetUp() const { glColorMask(colorMask.r, colorMask.g, colorMask.b, true); } - virtual void TearDown() const { glColorMask(1,1,1,1); } + virtual void SetUp() const { + gl_RenderState.SetColorMask(colorMask.r, colorMask.g, colorMask.b, true); + gl_RenderState.ApplyColorMask(); + } + virtual void TearDown() const { + gl_RenderState.ResetColorMask(); + gl_RenderState.ApplyColorMask(); + } private: ColorMask colorMask; }; diff --git a/src/gl/stereo3d/scoped_color_mask.h b/src/gl/stereo3d/scoped_color_mask.h index 262d34cf7..5c35752e5 100644 --- a/src/gl/stereo3d/scoped_color_mask.h +++ b/src/gl/stereo3d/scoped_color_mask.h @@ -44,16 +44,18 @@ class ScopedColorMask { public: - ScopedColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a) + ScopedColorMask(bool r, bool g, bool b, bool a) { - glGetBooleanv(GL_COLOR_WRITEMASK, saved); - glColorMask(r, g, b, a); + gl_RenderState.GetColorMask(saved[0], saved[1], saved[2], saved[3]); + gl_RenderState.SetColorMask(r, g, b, a); + gl_RenderState.ApplyColorMask(); } ~ScopedColorMask() { - glColorMask(saved[0], saved[1], saved[2], saved[3]); + gl_RenderState.SetColorMask(saved[0], saved[1], saved[2], saved[3]); + gl_RenderState.ApplyColorMask(); } private: - GLboolean saved[4]; + bool saved[4]; };