From 1b213612a545187f11487d18ca405bc9432e8ddc Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 18 Jun 2013 22:58:54 +0000 Subject: [PATCH] gpu: handle depth test, alpha blending and face culling in Renderer. --- src/gpu/rendercontext.cpp | 36 ++++++++++--- src/gpu/renderer.cpp | 101 +++++++++++++++++++++++++++++++++--- src/lol/gpu/rendercontext.h | 4 +- src/lol/gpu/renderer.h | 10 +++- src/ticker.cpp | 4 -- src/video.cpp | 75 +++----------------------- src/video.h | 5 -- 7 files changed, 141 insertions(+), 94 deletions(-) diff --git a/src/gpu/rendercontext.cpp b/src/gpu/rendercontext.cpp index 2e3bf93e..197b5dc4 100644 --- a/src/gpu/rendercontext.cpp +++ b/src/gpu/rendercontext.cpp @@ -58,7 +58,7 @@ class RenderContextData private: Scene *m_scene; - TrackedState m_blend; + TrackedState m_alpha_blend, m_depth_test, m_face_culling; }; /* @@ -73,18 +73,40 @@ RenderContext::RenderContext() RenderContext::~RenderContext() { - if (m_data->m_blend.HasChanged()) - g_renderer->SetBlendState(m_data->m_blend.GetValue()); + if (m_data->m_alpha_blend.HasChanged()) + g_renderer->SetAlphaBlend(m_data->m_alpha_blend.GetValue()); + + if (m_data->m_depth_test.HasChanged()) + g_renderer->SetDepthTest(m_data->m_depth_test.GetValue()); + + if (m_data->m_face_culling.HasChanged()) + g_renderer->SetFaceCulling(m_data->m_face_culling.GetValue()); delete m_data; } -void RenderContext::SetBlendState(bool set) +void RenderContext::SetAlphaBlend(bool set) +{ + if (!m_data->m_alpha_blend.HasChanged()) + m_data->m_alpha_blend.TrackValue(g_renderer->GetAlphaBlend()); + + g_renderer->SetAlphaBlend(set); +} + +void RenderContext::SetDepthTest(bool set) +{ + if (!m_data->m_depth_test.HasChanged()) + m_data->m_depth_test.TrackValue(g_renderer->GetDepthTest()); + + g_renderer->SetDepthTest(set); +} + +void RenderContext::SetFaceCulling(bool set) { - if (!m_data->m_blend.HasChanged()) - m_data->m_blend.TrackValue(g_renderer->GetBlendState()); + if (!m_data->m_face_culling.HasChanged()) + m_data->m_face_culling.TrackValue(g_renderer->GetFaceCulling()); - g_renderer->SetBlendState(set); + g_renderer->SetFaceCulling(set); } } /* namespace lol */ diff --git a/src/gpu/renderer.cpp b/src/gpu/renderer.cpp index 3364aee5..d4c1cdff 100644 --- a/src/gpu/renderer.cpp +++ b/src/gpu/renderer.cpp @@ -14,7 +14,22 @@ #include +#if defined _XBOX +# include +# undef near /* Fuck Microsoft */ +# undef far /* Fuck Microsoft again */ +#elif defined _WIN32 +# if defined USE_D3D9 +# include +# endif +# define WIN32_LEAN_AND_MEAN +# include +# undef near /* Fuck Microsoft */ +# undef far /* Fuck Microsoft again */ +#endif + #include "core.h" +#include "lolgl.h" namespace lol { @@ -32,7 +47,7 @@ class RendererData friend class Renderer; private: - bool m_blend; + bool m_blend, m_depth_test, m_face_culling; }; /* @@ -43,6 +58,13 @@ Renderer::Renderer() : m_data(new RendererData()) { m_data->m_blend = false; + SetAlphaBlend(true); + + m_data->m_depth_test = false; + SetDepthTest(true); + + m_data->m_face_culling = false; + SetFaceCulling(true); } Renderer::~Renderer() @@ -50,25 +72,88 @@ Renderer::~Renderer() delete m_data; } -void Renderer::SetBlendState(bool set) +/* + * Alpha blending + */ + +void Renderer::SetAlphaBlend(bool set) { if (m_data->m_blend == set) return; m_data->m_blend = set; + +#if defined USE_D3D9 || defined _XBOX +# define STR0(x) #x +# define STR(x) STR0(x) +# pragma message(__FILE__ "(" STR(__LINE__) "): warning: Renderer::SetAlphaBlend() not implemented") +#else if (set) - { + glEnable(GL_BLEND); + else + glDisable(GL_BLEND); +#endif +} + +bool Renderer::GetAlphaBlend() const +{ + return m_data->m_blend; +} - } +/* + * Depth test + */ + +void Renderer::SetDepthTest(bool set) +{ + if (m_data->m_depth_test == set) + return; + + m_data->m_depth_test = set; + +#if defined USE_D3D9 || defined _XBOX +# define STR0(x) #x +# define STR(x) STR0(x) +# pragma message(__FILE__ "(" STR(__LINE__) "): warning: Renderer::SetDepthTest() not implemented") +#else + if (set) + glEnable(GL_DEPTH_TEST); else - { + glDisable(GL_DEPTH_TEST); +#endif +} - } +bool Renderer::GetDepthTest() const +{ + return m_data->m_depth_test; } -bool Renderer::GetBlendState() const +/* + * Face culling + */ + +void Renderer::SetFaceCulling(bool set) { - return m_data->m_blend; + if (m_data->m_face_culling == set) + return; + + m_data->m_face_culling = set; + +#if defined USE_D3D9 || defined _XBOX +# define STR0(x) #x +# define STR(x) STR0(x) +# pragma message(__FILE__ "(" STR(__LINE__) "): warning: Renderer::SetFaceCulling() not implemented") +#else + if (set) + glEnable(GL_CULL_FACE); + else + glDisable(GL_CULL_FACE); +#endif +} + +bool Renderer::GetFaceCulling() const +{ + return m_data->m_face_culling; } } /* namespace lol */ diff --git a/src/lol/gpu/rendercontext.h b/src/lol/gpu/rendercontext.h index e60a9159..3377b74b 100644 --- a/src/lol/gpu/rendercontext.h +++ b/src/lol/gpu/rendercontext.h @@ -27,7 +27,9 @@ public: RenderContext(); ~RenderContext(); - void SetBlendState(bool set); + void SetAlphaBlend(bool set); + void SetDepthTest(bool set); + void SetFaceCulling(bool set); private: RenderContextData *m_data; diff --git a/src/lol/gpu/renderer.h b/src/lol/gpu/renderer.h index 31956399..32da1870 100644 --- a/src/lol/gpu/renderer.h +++ b/src/lol/gpu/renderer.h @@ -27,8 +27,14 @@ public: Renderer(); ~Renderer(); - void SetBlendState(bool set); - bool GetBlendState() const; + void SetAlphaBlend(bool set); + bool GetAlphaBlend() const; + + void SetDepthTest(bool set); + bool GetDepthTest() const; + + void SetFaceCulling(bool set); + bool GetFaceCulling() const; private: RendererData *m_data; diff --git a/src/ticker.cpp b/src/ticker.cpp index 01d7c2b7..86410be6 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -389,11 +389,7 @@ void TickerData::DrawThreadTick() Scene::GetDefault()->Reset(); Video::Clear(ClearMask::All); break; - case Entity::DRAWGROUP_HUD: - Video::SetDepth(false); - break; default: - Video::SetDepth(true); break; } diff --git a/src/video.cpp b/src/video.cpp index e1cf17b7..f1a5ab08 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -52,8 +52,6 @@ private: static mat4 proj_matrix; static ivec2 saved_viewport; static DebugRenderMode render_mode; - static bool face_culling; - static bool alpha_blend; #if defined USE_D3D9 || defined _XBOX # if defined USE_D3D9 static IDirect3D9 *d3d_ctx; @@ -70,8 +68,6 @@ private: mat4 VideoData::proj_matrix; ivec2 VideoData::saved_viewport(0, 0); DebugRenderMode VideoData::render_mode = DebugRenderMode::Default; -bool VideoData::face_culling = true; -bool VideoData::alpha_blend = true; #if defined USE_D3D9 || defined _XBOX # if defined USE_D3D9 @@ -91,8 +87,6 @@ float VideoData::clear_depth; void Video::Setup(ivec2 size) { - g_renderer = new Renderer(); - #if defined USE_D3D9 || defined _XBOX VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION); if (!VideoData::d3d_ctx) @@ -158,10 +152,11 @@ void Video::Setup(ivec2 size) # endif #endif + g_renderer = new Renderer(); + /* Initialise reasonable scene default properties */ SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f)); SetClearDepth(1.f); - SetAlphaBlend(true); SetDebugRenderMode(DebugRenderMode::Default); } @@ -238,60 +233,6 @@ void Video::SetFov(float theta) } } -void Video::SetDepth(bool set) -{ -#if defined USE_D3D9 || defined _XBOX -# define STR0(x) #x -# define STR(x) STR0(x) -# pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented") -#else - if (set) - glEnable(GL_DEPTH_TEST); - else - glDisable(GL_DEPTH_TEST); -#endif -} - -void Video::SetFaceCulling(bool set) -{ -#if defined USE_D3D9 || defined _XBOX -# define STR0(x) #x -# define STR(x) STR0(x) -# pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetFaceCulling() not implemented") -#else - VideoData::face_culling = set; - if (set) - glEnable(GL_CULL_FACE); - else - glDisable(GL_CULL_FACE); -#endif -} - -bool Video::HasFaceCulling() -{ - return VideoData::face_culling; -} - -void Video::SetAlphaBlend(bool set) -{ -#if defined USE_D3D9 || defined _XBOX -# define STR0(x) #x -# define STR(x) STR0(x) -# pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetAlphaBlend() not implemented") -#else - VideoData::alpha_blend = set; - if (set) - glEnable(GL_BLEND); - else - glDisable(GL_BLEND); -#endif -} - -bool Video::HasAlphaBlend() -{ - return VideoData::alpha_blend; -} - void Video::SetClearColor(vec4 color) { #if defined USE_D3D9 || defined _XBOX @@ -328,10 +269,10 @@ void Video::SetDebugRenderMode(DebugRenderMode d) #elif defined HAVE_GLES_2X glEnable(GL_CULL_FACE); #else - if (VideoData::render_mode == d && glIsEnabled(GL_CULL_FACE) == GL_TRUE) - SetFaceCulling(false); - else - SetFaceCulling(true); +// if (VideoData::render_mode == d && glIsEnabled(GL_CULL_FACE) == GL_TRUE) +// SetFaceCulling(false); +// else +// SetFaceCulling(true); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif break; @@ -342,14 +283,14 @@ void Video::SetDebugRenderMode(DebugRenderMode d) { #if defined USE_D3D9 || defined _XBOX #else - SetFaceCulling(!VideoData::face_culling); +// SetFaceCulling(!VideoData::face_culling); #endif } else { #if defined USE_D3D9 || defined _XBOX #else - SetFaceCulling(false); +// SetFaceCulling(false); #endif } #if defined USE_D3D9 || defined _XBOX diff --git a/src/video.h b/src/video.h index 7e28020c..eba9c2d2 100644 --- a/src/video.h +++ b/src/video.h @@ -71,11 +71,6 @@ public: static void SetCustomSize(ivec2 size); static void RestoreSize(); static void SetFov(float theta); - static void SetDepth(bool set); - static void SetFaceCulling(bool set); - static bool HasFaceCulling(); - static void SetAlphaBlend(bool set); - static bool HasAlphaBlend(); static void SetClearColor(vec4 color); static void SetClearDepth(float f); static void SetDebugRenderMode(DebugRenderMode d);