FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
render_target.h
Go to the documentation of this file.
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLBASE_RENDER_TARGET_H
16 #define FPLBASE_RENDER_TARGET_H
17 
18 #include "fplbase/config.h" // Must come first.
19 
20 #include "mathfu/glsl_mappings.h"
21 #include "fplbase/material.h"
22 #include "fplbase/mesh.h"
23 #include "fplbase/renderer.h"
24 #include "fplbase/shader.h"
25 
26 #ifdef __ANDROID__
28 #endif
29 
30 namespace fplbase {
31 
32 enum RenderTargetFormat {
33  kRenderTargetFormatUByte,
34  kRenderTargetFormatCount,
35 };
36 
37 /// @file
38 /// @addtogroup fplbase_render_target
39 /// @{
40 
41 /// @class RenderTarget
42 /// @brief Abstracts a surface that can be rendered to.
43 ///
44 /// Calling SetAsRenderTarget() will cause all subsequent draw calls to be
45 /// drawn onto the RenderTarget instead of to the screen buffer.
46 class RenderTarget {
47  public:
48  RenderTarget() : initialized_(false) {}
49  /// @brief Initialize a render target of the provided dimensions.
50  ///
51  /// Defaults the format to GL_UNSIGNED_BYTE, using a depth buffer.
52  ///
53  /// @param dimensions The dimensions of the render target.
54  void Initialize(const mathfu::vec2i& dimensions);
55  /// @brief Initialize a render target of the provided dimensions.
56  ///
57  /// @param dimensions The dimensions of the render target.
58  /// @param format The OpenGL format of the generated texture.
59  /// @param useDepthBuffer Create a depth buffer used by the render target.
60  void Initialize(const mathfu::vec2i& dimensions, RenderTargetFormat format,
61  bool useDepthBuffer);
62 
63  /// @brief Deletes the associated opengl resources associated with the
64  /// RenderTarget.
65  void Delete();
66 
67  /// @brief Sets the RenderTarget as the active render target.
68  ///
69  /// All subsequent openGL draw calls will render to this RenderTarget
70  /// instead of wherever they were going before.
71  // TODO(shanee): deprecate, remove and implement Renderer::SetRenderTarget.
72  void SetAsRenderTarget() const;
73 
74  /// @brief Binds the texture associated with this rendertarget as the active
75  /// texture.
76  ///
77  /// Primarily useful when rendering the RenderTarget's texture as part of a
78  /// mesh. Throws an assert if the RenderTarget does not have a texture.
79  ///
80  /// @param texture_number The index of the texture to make active.
81  void BindAsTexture(int texture_number) const;
82 
83  /// @brief Checks if this rendertarget refer to an off-screen texture.
84  ///
85  /// This is important because rendertargets that aren't texture-based will
86  /// assert if you try to bind them as texture or access their textureId.
87  ///
88  /// @return Returns true if this rendertarget refers to an off-screen texture,
89  /// and false if it refers to the screen itself.
90  inline bool IsTexture() const { return ValidBufferHandle(framebuffer_id_); }
91 
92  /// @brief Gets the TextureId associated with the RenderTarget, assuming that
93  /// it is texture-based.
94  ///
95  /// Throws an assert if you try to call GetTextureId on a RenderTarget that
96  /// doesn't have a texture backing it, such as the screen's display buffer.
97  ///
98  /// @return Returns the TextureId associated with the RenderTarget.
99  inline TextureHandle GetTextureId() const {
100  assert(IsTexture());
101  return rendered_texture_id_;
102  }
103 
104  /// @brief Checks if the RenderTarget has been initialized.
105  ///
106  /// Returns true if this RenderTarget has been initialized and is ready to
107  /// use. Returns false if has not yet been initialized, failed initalization,
108  /// or has been deleted. Trying to use an uninitialized RenderTarget will
109  /// generally cause errors or asserts.
110  ///
111  /// @brief Returns true if it has been initialized, false otherwise.
112  bool initialized() const { return initialized_; }
113 
114  /// @brief Gets the RenderTarget that corresponds to the screen.
115  ///
116  /// @param renderer The renderer object to use.
117  /// @return Returns the RenderTarget that corresponds to the screen.
118  static RenderTarget ScreenRenderTarget(Renderer& renderer);
119 
120  private:
121  mathfu::vec2i dimensions_;
122  BufferHandle framebuffer_id_;
123  TextureHandle rendered_texture_id_;
124  BufferHandle depth_buffer_id_;
125  bool initialized_;
126 };
127 
128 /// @}
129 } // namespace fplbase
130 
131 #endif // FPLBASE_RENDER_TARGET_H
bool IsTexture() const
Checks if this rendertarget refer to an off-screen texture.
Definition: render_target.h:90
void Initialize(const mathfu::vec2i &dimensions)
Initialize a render target of the provided dimensions.
void Delete()
Deletes the associated opengl resources associated with the RenderTarget.
void BindAsTexture(int texture_number) const
Binds the texture associated with this rendertarget as the active texture.
bool initialized() const
Checks if the RenderTarget has been initialized.
Definition: render_target.h:112
static RenderTarget ScreenRenderTarget(Renderer &renderer)
Gets the RenderTarget that corresponds to the screen.
TextureHandle GetTextureId() const
Gets the TextureId associated with the RenderTarget, assuming that it is texture-based.
Definition: render_target.h:99
void SetAsRenderTarget() const
Sets the RenderTarget as the active render target.
Abstracts a surface that can be rendered to.
Definition: render_target.h:46
Renderer is the main API class for rendering commands.
Definition: renderer.h:310
Definition: handles.h:24