VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Renderer.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef RENDERER_H
18 #define RENDERER_H
19 
20 #include "MeshInstance.h"
21 #include "RendererCommon.h"
22 #include "RenderNode.h"
23 #include "Shader.h"
24 #include "Texture.h"
25 
50 class Renderer {
51 public:
52  Renderer();
53  ~Renderer();
54 
60  void begin();
67  void end();
71  const Matrix4& getProjectionMatrix() const { return mProjectionMatrix; }
76  void setProjectionMatrix(const Matrix4& matrix);
77 
97  void drawSprite(const TexturePtr& texture, const Vector2& pos, float width, float height,
98  const Vector2& srcPos, float srcWidth, float srcHeight, float rotation, float opacity);
109  void drawMesh(const MeshInstancePtr& meshInstance, const ShaderPtr& shader);
116  void flush();
117 
126  void selectTexture(const TexturePtr& texture, int stage);
134  void selectMesh(const MeshInstancePtr& meshInstance);
142  void selectShader(const ShaderPtr& shader);
147  void clearTexture(int stage);
151  void clearTextures();
155  void clearMesh();
159  void clearShader();
160 
164  DebugRenderer* getDebugRenderer() const { return mDebugRenderer.get(); }
165 
175  static std::string addCompatibilityPrefixToShaderCode(const char* code);
176 
182  void attachAsCurrent();
186  void detachAsCurrent();
190  static void detachCurrent();
195  static Renderer* getCurrent();
196 private:
197  void init();
198 
199  std::vector<TexturePtr> mCurrentTextures;
200  MeshInstancePtr mCurrentMeshInstance;
201  ShaderPtr mCurrentShader;
202 
203  bool mInitialized = false;
204  Matrix4 mProjectionMatrix = {
205  1.0f, 0.0f, 0.0f, 0.0f,
206  0.0f, 1.0f, 0.0f, 0.0f,
207  0.0f, 0.0f, 1.0f, 0.0f,
208  0.0f, 0.0f, 0.0f, 1.0f };
209  MeshInstancePtr mSquareMesh;
210 
211  std::unique_ptr<DebugRenderer> mDebugRenderer;
212 
213  // TODO: This could be made thread local. C++11 thread_local is not yet supported on many
214  // platforms.
215  static Renderer* sCurrent;
216  static const char* SHADER_COMPATIBILITY_PREFIX;
217 };
218 
219 #endif // RENDERER_H
Header declaring and including types common to renderer classes such as Vector2. Also includes GL hea...
A class with helper methods for rendering primitives like lines, for debug visualization.
Definition: DebugRenderer.h:36
Class managing GL state switches and draw calls.
Definition: Renderer.h:50
const Matrix4 & getProjectionMatrix() const
Returns the projection matrix that will be used when rendering sprites.
Definition: Renderer.h:71
void selectShader(const ShaderPtr &shader)
Selects a Shader as current.
void selectTexture(const TexturePtr &texture, int stage)
Selects and binds a Texture to a texture stage.
float Matrix4[16]
4x4 float matrix, whose layout is compatible with GL.
Definition: RendererCommon.h:46
static std::string addCompatibilityPrefixToShaderCode(const char *code)
Prefixes the given shader code with code that normalizes shaders across some platforms.
DebugRenderer * getDebugRenderer() const
Returns the DebugRenderer managed by this Renderer.
Definition: Renderer.h:164
void flush()
Flush GL draw calls and state, and deselect objects.
void end()
Signals that rendering this frame should end.
void clearMesh()
Unbinds and clears the current MeshInstance.
b2Vec2 Vector2
2D float vector, whose layout is compatible with GL.
Definition: RendererCommon.h:50
void drawMesh(const MeshInstancePtr &meshInstance, const ShaderPtr &shader)
Draw a MeshInstance with a given Shader.
void attachAsCurrent()
Sets this Renderer as current, allowing it to be retrieved by getCurrent().
void selectMesh(const MeshInstancePtr &meshInstance)
Selects and binds a MeshInstance.
std::shared_ptr< Texture > TexturePtr
Shared pointer typedef for Texture.
Definition: PointerDeclarations.h:173
void begin()
Signal the Renderer that the frame has begun.
std::shared_ptr< Shader > ShaderPtr
Shared pointer typedef for Shader.
Definition: PointerDeclarations.h:143
void drawSprite(const TexturePtr &texture, const Vector2 &pos, float width, float height, const Vector2 &srcPos, float srcWidth, float srcHeight, float rotation, float opacity)
Draws a rectangular quadrilateral, textured with a Texture.
static Renderer * getCurrent()
Returns the Renderer that was set as current through a call to attachAsCurrent(). ...
void clearTextures()
Clears all texture stages.
std::shared_ptr< MeshInstance > MeshInstancePtr
Shared pointer typedef for MeshInstance.
Definition: PointerDeclarations.h:113
void clearTexture(int stage)
Unbinds and clears the Texture at the given stage.
void clearShader()
Unbinds and clears the current Shader.
void detachAsCurrent()
Unsets this Renderer as current.
void setProjectionMatrix(const Matrix4 &matrix)
Sets the projection matrix that will be used when rendering sprites.
static void detachCurrent()
Unsets the current Renderer, if any.