From: Mikko Rasa Date: Mon, 9 Sep 2013 16:00:40 +0000 (+0300) Subject: Add a flag to RenderPass to render the back faces of a mesh X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e7556941c41b1a0fc84fe7986c98ee79eccbf564;p=libs%2Fgl.git Add a flag to RenderPass to render the back faces of a mesh This is useful for transparency effects. --- diff --git a/source/renderer.cpp b/source/renderer.cpp index 3a44dcd3..31d9621e 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -112,6 +112,11 @@ void Renderer::set_winding_test(const WindingTest *w) state->winding_test = w; } +void Renderer::set_reverse_winding(bool r) +{ + state->reverse_winding = r; +} + void Renderer::push_state() { state_stack.push_back(state_stack.back()); @@ -220,7 +225,12 @@ void Renderer::apply_state() Program::unbind(); if(state->winding_test) - state->winding_test->bind(); + { + if(state->reverse_winding) + state->winding_test->get_reverse().bind(); + else + state->winding_test->bind(); + } else WindingTest::unbind(); diff --git a/source/renderer.h b/source/renderer.h index 00f97004..1254b7ad 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -125,6 +125,7 @@ public: void set_vertex_array(const VertexArray *); void set_element_buffer(const Buffer *); void set_winding_test(const WindingTest *); + void set_reverse_winding(bool); /** Saves the current state so it can be restored later. */ void push_state(); diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 9ee79d4b..158e9fc8 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -20,7 +20,8 @@ RenderPass::RenderPass(): shprog(0), shdata(0), material(0), - texturing(0) + texturing(0), + back_faces(false) { } RenderPass::RenderPass(const RenderPass &other): @@ -28,7 +29,8 @@ RenderPass::RenderPass(const RenderPass &other): shdata(other.shdata ? new ProgramData(*other.shdata) : 0), material(other.material), texturing(other.texturing ? new Texturing(*other.texturing) : 0), - tex_names(other.tex_names) + tex_names(other.tex_names), + back_faces(other.back_faces) { } RenderPass::~RenderPass() @@ -70,6 +72,7 @@ void RenderPass::apply(Renderer &renderer) const renderer.set_texturing(texturing); renderer.set_material(material.get()); renderer.set_shader_program(shprog, shdata.get()); + renderer.set_reverse_winding(back_faces); } @@ -90,6 +93,7 @@ void RenderPass::Loader::init() add("shader", &RenderPass::shprog); add("material", &Loader::material_inline); add("material", &Loader::material); + add("back_faces",&RenderPass::back_faces); add("texunit", &Loader::texunit); add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); diff --git a/source/renderpass.h b/source/renderpass.h index e891802e..934a213e 100644 --- a/source/renderpass.h +++ b/source/renderpass.h @@ -63,6 +63,7 @@ private: RefPtr material; Texturing *texturing; std::map tex_names; + bool back_faces; RenderPass &operator=(const RenderPass &); public: @@ -78,6 +79,8 @@ public: void set_texture(unsigned, const Texture *); const Texturing *get_texturing() const { return texturing; } int get_texture_index(const std::string &) const; + void set_back_faces(bool); + bool get_back_faces() const { return back_faces; } void apply(Renderer &) const; }; diff --git a/source/windingtest.cpp b/source/windingtest.cpp index de9e0a25..acbc20fc 100644 --- a/source/windingtest.cpp +++ b/source/windingtest.cpp @@ -44,13 +44,23 @@ void WindingTest::unbind() glDisable(GL_CULL_FACE); } -WindingTest &WindingTest::clockwise() +const WindingTest &WindingTest::get_reverse() const +{ + if(!test) + return *this; + else if(winding==CLOCKWISE) + return counterclockwise(); + else + return clockwise(); +} + +const WindingTest &WindingTest::clockwise() { static WindingTest test(CLOCKWISE); return test; } -WindingTest &WindingTest::counterclockwise() +const WindingTest &WindingTest::counterclockwise() { static WindingTest test(COUNTERCLOCKWISE); return test; diff --git a/source/windingtest.h b/source/windingtest.h index 0869d565..d8a4c421 100644 --- a/source/windingtest.h +++ b/source/windingtest.h @@ -34,8 +34,10 @@ public: static void unbind(); - static WindingTest &clockwise(); - static WindingTest &counterclockwise(); + const WindingTest &get_reverse() const; + + static const WindingTest &clockwise(); + static const WindingTest &counterclockwise(); }; } // namespace GL