scg3  0.6
RenderState.h
Go to the documentation of this file.
1 
13 /*
14  * Copyright 2014-2019 Volker Ahlers
15  *
16  * Licensed under the Apache License, Version 2.0 (the "License");
17  * you may not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #ifndef RENDERSTATE_H_
30 #define RENDERSTATE_H_
31 
32 #include <cassert>
33 #include <stack>
34 #include "scg_glew.h"
35 #include "scg_glm.h"
36 #include "scg_internals.h"
37 
38 namespace scg {
39 
40 
47 class MatrixStack {
48 
49 public:
50 
52  stack_.push(glm::mat4(1.0f));
53  }
54 
55  const glm::mat4& getMatrix() const {
56  assert(!stack_.empty());
57  return stack_.top();
58  }
59 
60  void setMatrix(const glm::mat4& matrix) {
61  assert(!stack_.empty());
62  stack_.top() = matrix;
63  }
64 
65  void setIdentity() {
66  assert(!stack_.empty());
67  stack_.top() = glm::mat4(1.0f);
68  }
69 
70  void pushMatrix() {
71  assert(!stack_.empty());
72  stack_.push(stack_.top());
73  }
74 
75  void pushMatrix(const glm::mat4& matrix) {
76  stack_.push(matrix);
77  }
78 
79  void popMatrix() {
80  assert(!stack_.empty());
81  stack_.pop();
82  }
83 
84  void multMatrix(const glm::mat4& matrix) {
85  assert(!stack_.empty());
86  stack_.top() *= matrix;
87  }
88 
89 protected:
90 
91  std::stack<glm::mat4> stack_;
92 
93 };
94 
95 
107 class RenderState {
108 
109 public:
110 
114  RenderState();
115 
119  virtual ~RenderState();
120 
124  void init();
125 
129  int getNLights() const;
130 
134  GLuint getLightUBO() const;
135 
139  ColorCore* getColor();
140 
144  void setColor(ColorCore* core);
145 
150 
154  void setShader(ShaderCore* core);
155 
159  const glm::mat4& getViewTransform() const {
160  return viewTransform_;
161  }
162 
166  void setViewTransform(const glm::mat4& viewTransform) {
167  viewTransform_ = viewTransform;
168  }
169 
173  const glm::mat4& getProjection() const {
174  return projection_;
175  }
176 
180  void setProjection(const glm::mat4& projection) {
181  projection_ = projection;
182  }
183 
187  const glm::mat4& getMVPMatrix() const {
189  }
190 
194  const glm::mat4& getModelMatrix() const {
195  return tempMatrix_ = glm::inverse(viewTransform_) * modelViewStack.getMatrix();
196  }
197 
198 
204  void setLighting(bool isLightingEnabled);
205 
206 
210  bool isLightingEnabled() const;
211 
212 
216  void addLight();
217 
221  void removeLight();
222 
226  void setGlobalAmbientLight(const glm::vec4& globalAmbientLight);
227 
233 
238  void passToShader();
239 
240 public:
241 
242  // public member variables are used for efficiency
247 
248 protected:
249 
252  glm::mat4 projection_;
253  glm::mat4 viewTransform_;
254  mutable glm::mat4 tempMatrix_;
256  GLint nLights_;
257  GLuint lightUBO_;
259 
260 };
261 
262 
263 } /* namespace scg */
264 
265 #endif /* RENDERSTATE_H_ */
glm::mat4 projection_
Definition: RenderState.h:252
void setLighting(bool isLightingEnabled)
A core to set a shader program to be applied to subsequent nodes.
Definition: ShaderCore.h:69
virtual ~RenderState()
std::stack< glm::mat4 > stack_
Definition: RenderState.h:91
MatrixStack modelViewStack
Definition: RenderState.h:243
ColorCore * colorCore_
Definition: RenderState.h:250
const glm::mat4 & getMVPMatrix() const
Definition: RenderState.h:187
Local header file for GLM OpenGL Mathematics library.
void setMatrix(const glm::mat4 &matrix)
Definition: RenderState.h:60
const glm::mat4 & getModelMatrix() const
Definition: RenderState.h:194
void multMatrix(const glm::mat4 &matrix)
Definition: RenderState.h:84
MatrixStack textureStack
Definition: RenderState.h:245
MatrixStack projectionStack
Definition: RenderState.h:244
void setViewTransform(const glm::mat4 &viewTransform)
Definition: RenderState.h:166
void setColor(ColorCore *core)
const glm::mat4 & getMatrix() const
Definition: RenderState.h:55
ColorCore * getColor()
const glm::mat4 & getProjection() const
Definition: RenderState.h:173
void setGlobalAmbientLight(const glm::vec4 &globalAmbientLight)
A core to set a vertex color or a color transformation to be applied to subsequent geometry.
Definition: ColorCore.h:40
Local header file for GLEW library.
void setIdentity()
Definition: RenderState.h:65
ShaderCore * getShader()
glm::vec4 globalAmbientLight_
Definition: RenderState.h:258
int getNLights() const
bool isLightingEnabled() const
const glm::mat4 & getViewTransform() const
Definition: RenderState.h:159
The central render state that collects information about the current shader, transformations,...
Definition: RenderState.h:107
glm::mat4 tempMatrix_
Definition: RenderState.h:254
GLuint getLightUBO() const
Definition: Animation.h:28
glm::mat4 viewTransform_
Definition: RenderState.h:253
Internal definitions required by most classes.
void setShader(ShaderCore *core)
MatrixStack colorStack
Definition: RenderState.h:246
Matrix stack to store model-view, projection, texture, and color matrices, used by RenderState.
Definition: RenderState.h:47
void pushMatrix(const glm::mat4 &matrix)
Definition: RenderState.h:75
void setProjection(const glm::mat4 &projection)
Definition: RenderState.h:180
void applyProjectionViewTransform()
ShaderCore * shaderCore_
Definition: RenderState.h:251