source: trunk/src/gpu/rendercontext.cpp @ 2761

Last change on this file since 2761 was 2761, checked in by sam, 9 years ago

gpu: implement face culling mode in render contexts.

File size: 3.8 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16
17#include "core.h"
18
19namespace lol
20{
21
22template<typename T> class TrackedState
23{
24public:
25    inline TrackedState()
26      : m_changed(false)
27    {}
28
29    inline void TrackValue(T const &value)
30    {
31        m_value = value;
32        m_changed = true;
33    }
34
35    inline bool HasChanged()
36    {
37        return m_changed;
38    }
39
40    inline T GetValue()
41    {
42        return m_value;
43    }
44
45private:
46    T m_value;
47    bool m_changed;
48};
49
50class RenderContextData
51{
52    friend class RenderContext;
53
54private:
55    Scene *m_scene;
56
57    TrackedState<vec4> m_clear_color;
58    TrackedState<float> m_clear_depth;
59    TrackedState<bool> m_alpha_blend;
60    TrackedState<BlendFactor> m_blend_src;
61    TrackedState<BlendFactor> m_blend_dst;
62    TrackedState<bool> m_alpha_test;
63    TrackedState<bool> m_depth_test;
64    TrackedState<CullMode> m_face_culling;
65};
66
67/*
68 * Public RenderContext class
69 */
70
71RenderContext::RenderContext()
72  : m_data(new RenderContextData())
73{
74    m_data->m_scene = Scene::GetDefault();
75}
76
77RenderContext::~RenderContext()
78{
79    if (m_data->m_clear_color.HasChanged())
80        g_renderer->SetClearColor(m_data->m_clear_color.GetValue());
81
82    if (m_data->m_clear_depth.HasChanged())
83        g_renderer->SetClearDepth(m_data->m_clear_depth.GetValue());
84
85    if (m_data->m_alpha_blend.HasChanged())
86        g_renderer->SetAlphaBlend(m_data->m_alpha_blend.GetValue());
87
88    if (m_data->m_blend_src.HasChanged())
89        g_renderer->SetBlendFunc(m_data->m_blend_src.GetValue(),
90                                 m_data->m_blend_dst.GetValue());
91
92    if (m_data->m_alpha_test.HasChanged())
93        g_renderer->SetAlphaTest(m_data->m_alpha_test.GetValue());
94
95    if (m_data->m_depth_test.HasChanged())
96        g_renderer->SetDepthTest(m_data->m_depth_test.GetValue());
97
98    if (m_data->m_face_culling.HasChanged())
99        g_renderer->SetFaceCulling(m_data->m_face_culling.GetValue());
100
101    delete m_data;
102}
103
104void RenderContext::SetClearColor(vec4 color)
105{
106    if (!m_data->m_clear_color.HasChanged())
107        m_data->m_clear_color.TrackValue(g_renderer->GetClearColor());
108
109    g_renderer->SetClearColor(color);
110}
111
112void RenderContext::SetClearDepth(float depth)
113{
114    if (!m_data->m_clear_depth.HasChanged())
115        m_data->m_clear_depth.TrackValue(g_renderer->GetClearDepth());
116
117    g_renderer->SetClearDepth(depth);
118}
119
120void RenderContext::SetAlphaBlend(bool set)
121{
122    if (!m_data->m_alpha_blend.HasChanged())
123        m_data->m_alpha_blend.TrackValue(g_renderer->GetAlphaBlend());
124
125    g_renderer->SetAlphaBlend(set);
126}
127
128void RenderContext::SetBlendFunc(BlendFactor src, BlendFactor dst)
129{
130    if (!m_data->m_blend_src.HasChanged())
131        m_data->m_blend_src.TrackValue(g_renderer->GetBlendFuncSrc());
132    if (!m_data->m_blend_dst.HasChanged())
133        m_data->m_blend_dst.TrackValue(g_renderer->GetBlendFuncDst());
134
135    g_renderer->SetBlendFunc(src, dst);
136}
137
138void RenderContext::SetAlphaTest(bool set)
139{
140    if (!m_data->m_alpha_test.HasChanged())
141        m_data->m_alpha_test.TrackValue(g_renderer->GetAlphaTest());
142
143    g_renderer->SetAlphaTest(set);
144}
145
146void RenderContext::SetDepthTest(bool set)
147{
148    if (!m_data->m_depth_test.HasChanged())
149        m_data->m_depth_test.TrackValue(g_renderer->GetDepthTest());
150
151    g_renderer->SetDepthTest(set);
152}
153
154void RenderContext::SetFaceCulling(CullMode mode)
155{
156    if (!m_data->m_face_culling.HasChanged())
157        m_data->m_face_culling.TrackValue(g_renderer->GetFaceCulling());
158
159    g_renderer->SetFaceCulling(mode);
160}
161
162} /* namespace lol */
163
Note: See TracBrowser for help on using the repository browser.