FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
renderer.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_RENDERER_H
16 #define FPLBASE_RENDERER_H
17 
18 #include "fplbase/config.h" // Must come first.
19 
20 #include "fplbase/environment.h"
21 #include "fplbase/material.h"
22 #include "fplbase/mesh.h"
23 #include "fplbase/render_state.h"
24 #include "fplbase/shader.h"
25 #include "fplbase/texture.h"
26 #include "fplbase/version.h"
27 #include "fplutil/mutex.h"
28 #include "mathfu/glsl_mappings.h"
29 
30 namespace fplbase {
31 
32 struct RendererBaseImpl;
33 struct RendererImpl;
34 class Renderer;
35 
36 /// @file
37 /// @addtogroup fplbase_renderer
38 /// @{
39 
40 /// @class RendererBase
41 /// @brief Manages the rendering system, handling the window and resources.
42 ///
43 /// The core of the rendering system. Deals with setting up and shutting down
44 /// the window + OpenGL context, and creating/using resources
45 /// such as shaders, textures, and geometry.
46 ///
47 /// This is a singleton that has shared ownership amongst all Renderer classes.
48 /// When the first Renderer class is created, this RendererBase class is also
49 /// created. When the last Renderer class is destroyed, this RendererBase
50 /// class is also destroyed.
51 ///
52 /// Users should instantiate one or more Renderer classes. Users should *not*
53 /// instantiate RendererBase.
54 ///
55 /// Users can call a subset of Renderer functionality via the RendererBase
56 /// singleton. For example,
57 /// @code{.cpp}
58 /// if (RendererBase::Get()->feature_level() >= kFeatureLevel30) {
59 /// // Go ahead and use VAOs (vertex array objects).
60 /// }
61 /// @endcode
62 /// But all of the RendererBase functionality is also in Renderer,
63 /// and using it may (potentially) be faster since each thread has its own
64 /// Renderer and there is only one shared RendererBase. So prefer using
65 /// Renderer over the RendererBase singleton, when you have a Renderer around.
66 class RendererBase {
67  public:
68  // Construction and destruction cannot be private since we use
69  // std::shared_ptr in Renderer (friend-ing is insufficient), but you should
70  // not create your own RendererBase.
71  // RendererBase should only be constructed internally.
72  RendererBase();
73  ~RendererBase();
74 
75  /// @brief Initializes the renderer by initializing the Environment object.
76  ///
77  /// both parameters are used on desktop platforms, but may be ignored on
78  /// mobile platforms or certain environment backends in favor of the native
79  /// resolution.
80  /// Whether this actually still needs to create an context depends on the
81  /// backend.
82  /// A descriptive error is in last_error() if it returns false.
83  ///
84  /// @param window_size The default size to initialize the window to.
85  /// @param window_title The title of the created window.
86  /// @param window_mode Determines whether the window will use the window size,
87  /// enter fullscreen mode, or enter fullscreen mode with the HW scaler
88  /// disabled.
89  /// @return Returns true on success, false if there was an error.
90  bool Initialize(const mathfu::vec2i &window_size, const char *window_title,
91  WindowMode window_mode = kWindowModeWindowedScaled);
92 
93  /// @brief Swaps frames. Call this once per frame inside your main loop.
94  ///
95  /// The two arguments are typically the result of the InputManager's
96  /// minimized() and Time() (seconds since the start of the program).
97  ///
98  /// @param minimized If the window is considered to be minimized.
99  /// @param time The seconds since the start of the program.
100  void AdvanceFrame(bool minimized, double time);
101 
102  /// @brief Cleans up the resources initialized by the renderer.
103  void ShutDown() { environment_.ShutDown(); }
104  /// @brief Sets the window size, for when window is not owned by the renderer.
105  ///
106  /// In the non-window-owning use case, call to update the window size whenever
107  /// it changes.
108  ///
109  /// @param window_size The size to set the window to.
110  void SetWindowSize(const mathfu::vec2i &window_size) {
111  environment_.SetWindowSize(window_size);
112  }
113 
114  /// @brief Create a shader object from two strings containing glsl code.
115  ///
116  /// Returns nullptr upon error, with a descriptive message in glsl_error().
117  /// Attribute names in the vertex shader should be aPosition, aNormal,
118  /// aTexCoord, aColor, aBoneIndices and aBoneWeights, to match whatever
119  /// attributes your vertex data has.
120  ///
121  /// @param vs_source The source code of the vertex shader.
122  /// @param ps_source The source code of the fragment shader.
123  Shader *CompileAndLinkShader(const char *vs_source, const char *ps_source);
124 
125  /// @brief Like CompileAndLinkShader, but pass in an old shader to replace.
126  ///
127  /// Use `placement new` to use the same memory for the new shader.
128  /// Returns nullptr upon error, with a descriptive message in glsl_error().
129  /// @note Only call this at the start of the frame.
130  ///
131  /// @param shader The old shader to replace with the recompiled shader.
132  /// @param vs_source The source code of the vertex shader.
133  /// @param ps_source The source code of the fragment shader.
134  Shader *RecompileShader(const char *vs_source, const char *ps_source,
135  Shader *shader);
136 
137  /// @brief Checks for multithreading API.
138  /// Returns true if the graphics API allows multi-threading
139  bool AllowMultiThreading();
140 
141  /// @brief Set bone transforms in vertex shader uniforms.
142  ///
143  /// Allows vertex shader to skin each vertex to the bone position.
144  ///
145  /// @param bone_transforms Array of transforms to pass to the shader.
146  /// @param num_bones The length of the provided array.
147  void SetAnimation(const mathfu::AffineTransform *bone_transforms,
148  int num_bones);
149 
150  /// @brief Contains the last error that occurred, if there is one.
151  ///
152  /// If any of the more complex loading operations (shaders, textures etc.)
153  /// fail, this sting will contain a more informative error message.
154  ///
155  /// @brief Returns the last error that occurred.
156  const std::string &last_error() const { return last_error_; }
157  void set_last_error(const std::string &last_error) {
158  last_error_ = last_error;
159  }
160 
161  /// @brief The device's current framebuffer size.
162  ///
163  /// May change from frame to frame due to window resizing or Android
164  /// navigation buttons turning on/off.
165  ///
166  /// @return Returns the current framebuffer size.
167  const mathfu::vec2i &window_size() const {
168  return environment_.window_size();
169  }
170  /// @overload mathfu::vec2i &window_size()
171  mathfu::vec2i &window_size() { return environment_.window_size(); }
172 
173  /// @brief Sets the window size
174  ///
175  /// Sets the window_size member variable to the given parameter
176  /// @param ws The 2D vector for the window size
177  void set_window_size(const mathfu::vec2i &ws) {
178  environment_.SetWindowSize(ws);
179  }
180 
181  /// @brief Get the size of the viewport.
182  ///
183  /// This may be larger than the framebuffer/window on Android if the hardware
184  /// scalar is enabled.
185  ///
186  /// @return Returns the current viewport size.
187  mathfu::vec2i GetViewportSize() { return environment_.GetViewportSize(); }
188 
189  Environment &environment() { return environment_; }
190 
191  /// @brief Time in seconds since program start.
192  ///
193  /// Used by animated shaders, updated once per frame only.
194  ///
195  /// @return Returns the time in seconds since program start.
196  double time() const { return time_; }
197 
198  /// @brief The supported OpenGL ES feature level.
199  /// @return Returns the supported OpenGL ES feature level.
200  FeatureLevel feature_level() const { return environment_.feature_level(); }
201 
202  /// @brief The blend that will be used for all draw calls.
203  /// @return Returns the blend mode that will be used.
204  BlendMode force_blend_mode() const { return force_blend_mode_; }
205  /// @brief Set to override the blend mode used for all draw calls.
206  ///
207  /// Overrides the blend that was set by calling SetBlendMode.
208  /// Set to kBlendModeCount to disable.
209  ///
210  /// @param bm The blend mode to be used.
211  void set_force_blend_mode(BlendMode bm) { force_blend_mode_ = bm; }
212 
213  /// @brief Set this force any shader that gets loaded to use this pixel shader
214  /// instead (for debugging purposes).
215  /// @param ps The pixel shader that will be used.
216  void set_override_pixel_shader(const std::string &ps) {
217  override_pixel_shader_ = ps;
218  }
219 
220  /// @brief Get the max number of uniforms components (i.e. individual floats,
221  /// so a mat4 needs 16 of them).
222  ///
223  /// The value is in individual floats, so a mat4 needs 16 of them. This
224  /// variable is also available in the shader as
225  /// GL_MAX_VERTEX_UNIFORM_COMPONENTS. From this, you can compute safe sizes
226  /// of uniform arrays etc.
227  ///
228  /// @return Returns the max number of uniform components.
229  int max_vertex_uniform_components() { return max_vertex_uniform_components_; }
230 
231  /// @brief Returns the version of the FPL Base Library.
232  const FplBaseVersion *GetFplBaseVersion() const { return version_; }
233 
234  /// @brief Returns if a texture format is supported by the hardware.
235  bool SupportsTextureFormat(TextureFormat texture_format) const;
236 
237  /// @brief Returns if a NPOT textures are supported by the hardware.
238  /// see: https://www.opengl.org/wiki/NPOT_Texture
239  bool SupportsTextureNpot() const;
240 
241  // For internal use only.
242  RendererBaseImpl* impl() { return impl_; }
243 
244  // Get current singleton instance.
245  static RendererBase *Get() {
246  assert(!the_base_weak_.expired());
247  return the_base_raw_;
248  }
249 
250  private:
251  friend class Renderer;
252 
253  ShaderHandle CompileShader(bool is_vertex_shader, ShaderHandle program,
254  const char *source);
255  Shader *CompileAndLinkShaderHelper(const char *vs_source,
256  const char *ps_source, Shader *shader);
257 
258  // Backend-specific create and destroy calls. These just call new and delete
259  // on the platform-specific impl structs.
260  static RendererBaseImpl *CreateRendererBaseImpl();
261  static void DestroyRendererBaseImpl(RendererBaseImpl *impl);
262 
263  // Platform-dependent data.
264  RendererBaseImpl* impl_;
265 
266  // Initialize rendering platform parameters like uniform limits,
267  // supported texture formats, etc.
268  bool InitializeRenderingState();
269 
270  double time_;
271 
272  std::string last_error_;
273 
274  Environment environment_;
275 
276  int64_t supports_texture_format_; // 1 bit for each enum in TextureFormat.
277 
278  bool supports_texture_npot_;
279 
280  Shader *force_shader_;
281  BlendMode force_blend_mode_;
282  std::string override_pixel_shader_;
283 
284  int max_vertex_uniform_components_;
285 
286  // Current version of the library.
287  const FplBaseVersion *version_;
288 
289  // Singleton instance.
290  // Ownership is shared amongst the Renderer classes.
291  // Singleton is created when the first Renderer is created, and destroyed
292  // when the last Renderer is destroyed.
293  static std::weak_ptr<RendererBase> the_base_weak_;
294 
295  // There is no way to get the raw pointer from the weak_ptr above, so store it
296  // unsafely here. This is necessary to support the Get() call.
297  static RendererBase* the_base_raw_;
298 
299  // Ensure creation and deletion of singleton is atomic.
300  static fplutil::Mutex the_base_mutex_;
301 };
302 
303 /// @class Renderer
304 /// @brief Renderer is the main API class for rendering commands.
305 ///
306 /// Graphics APIs that support multi-threading (e.g. Vulkan) can have multiple
307 /// Renderer classes, one for each thread. Non-multi-threaded APIs (e.g. OpenGL)
308 /// should avoid using two Renderer classes at the same time, though it's
309 /// valid for more than one to exist.
310 class Renderer {
311  public:
312  Renderer();
313  ~Renderer();
314 
315  /// @brief Shader uniform: model_view_projection
316  /// @return Returns the current model view projection being used.
317  const mathfu::mat4 &model_view_projection() const {
318  return model_view_projection_;
319  }
320  /// @brief Sets the shader uniform model_view_projection
321  /// @param mvp The model view projection to be passed to the shader.
322  void set_model_view_projection(const mathfu::mat4 &mvp) {
323  model_view_projection_ = mvp;
324  }
325 
326  /// @brief Shader uniform: model (object to world transform only)
327  /// @return Returns the current model transform being used.
328  const mathfu::mat4 &model() const { return model_; }
329  /// @brief Sets the shader uniform model transform.
330  /// @param model The model transform to be passed to the shader.
331  void set_model(const mathfu::mat4 &model) { model_ = model; }
332 
333  /// @brief Shader uniform: color
334  /// @return Returns the current color being used.
335  const mathfu::vec4 &color() const { return color_; }
336  /// @brief Sets the shader uniform color.
337  /// @param color The color to be passed to the shader.
338  void set_color(const mathfu::vec4 &color) { color_ = color; }
339 
340  /// @brief Shader uniform: light_pos
341  /// @return Returns the current light position being used.
342  const mathfu::vec3 &light_pos() const { return light_pos_; }
343  /// @brief Sets the shader uniform light position.
344  /// @param light_pos The light position to be passed to the shader.
345  void set_light_pos(const mathfu::vec3 &light_pos) { light_pos_ = light_pos; }
346 
347  /// @brief Shader uniform: camera_pos
348  /// @return Returns the current camera position being used.
349  const mathfu::vec3 &camera_pos() const { return camera_pos_; }
350  /// @brief Sets the shader uniform camera position.
351  /// @param camera_pos The camera position to be passed to the shader.
352  void set_camera_pos(const mathfu::vec3 &camera_pos) {
353  camera_pos_ = camera_pos;
354  }
355 
356  /// @brief Shader uniform: bone_transforms
357  /// @return Returns the current array of bone transforms being used.
358  const mathfu::AffineTransform *bone_transforms() const {
359  return bone_transforms_;
360  }
361  /// @brief The number of bones in the bone_transforms() array.
362  /// @return Returns the length of the bone_transforms() array.
363  int num_bones() const { return num_bones_; }
364  /// @brief Sets the shader uniform bone transforms.
365  /// @param bone_transforms The bone transforms to be passed to the shader.
366  /// @param num_bones The length of the bone_transforms array provided.
367  void SetBoneTransforms(const mathfu::AffineTransform *bone_transforms,
368  int num_bones) {
369  bone_transforms_ = bone_transforms;
370  num_bones_ = num_bones;
371  }
372 
373  /// @brief Clears the framebuffer.
374  ///
375  /// Call this after AdvanceFrame if desired.
376  ///
377  /// @param color The color to clear the buffer to.
378  void ClearFrameBuffer(const mathfu::vec4 &color);
379 
380  /// @brief Clears the depthbuffer. Leaves the colorbuffer untouched.
381  /// @param render_context Pointer to the render context
382  void ClearDepthBuffer();
383 
384  /// @brief Begin rendering commands.
385  /// This must be called before any rendering commands are done
386  void BeginRendering();
387 
388  /// @brief End rendering commands.
389  /// This is called after all of the rendering commands are done
390  void EndRendering();
391 
392  /// @brief Sets the blend mode used by the renderer.
393  ///
394  /// Set alpha test (cull pixels with alpha below amount) vs alpha blend
395  /// (blend with framebuffer pixel regardedless).
396  ///
397  /// @param blend_mode The type of blend mode, see materials.h for valid enum
398  /// values.
399  /// @param amount The value used with kBlendModeTest, defaults to 0.5f.
400  void SetBlendMode(BlendMode blend_mode, float amount);
401  /// @overload void SetBlendMode(BlendMode blend_mode)
402  void SetBlendMode(BlendMode blend_mode);
403 
404  /// @brief Sets the stencil mode. By default, the stencil test is off.
405  ///
406  /// @param mode The stencil mode to use.
407  /// @param ref The reference value to test against or write.
408  /// @param mask Value that is ANDed with both the ref value and the stored
409  /// stencil value when the test is done.
410  void SetStencilMode(StencilMode mode, int ref, StencilMask mask);
411 
412  /// @brief Sets the culling mode. By default, no culling happens.
413  ///
414  /// @param mode The type of culling mode to use.
415  void SetCulling(CullingMode mode);
416 
417  /// @brief Sets the viewport region.
418  ///
419  /// @param viewport The viewport region to set.
420  void SetViewport(const Viewport &viewport);
421 
422  /// @brief Set function used for the depth test.
423  ///
424  /// @param depth_func The depth function to use.
425  void SetDepthFunction(DepthFunction func);
426 
427  /// @brief Turn on a scissor region. Arguments are in screen pixels.
428  ///
429  /// @param pos The lower left corner of the scissor box.
430  /// @param size The width and height of the scissor box.s
431  void ScissorOn(const mathfu::vec2i &ops, const mathfu::vec2i &size);
432  /// @brief Turn off the scissor region.
433  void ScissorOff();
434 
435  // Forwarded methods from the RendererBase class
436 
437  /// @brief Initializes the renderer by initializing the Environment object.
438  bool Initialize(const mathfu::vec2i &window_size, const char *window_title,
439  const WindowMode window_mode = kWindowModeWindowedScaled) {
440  return base_->Initialize(window_size, window_title, window_mode);
441  }
442 
443  /// @brief Swaps frames. Call this once per frame inside your main loop.
444  void AdvanceFrame(bool minimized, double time);
445 
446  /// @brief Cleans up the resources initialized by the renderer.
447  void ShutDown() { base_->ShutDown(); }
448 
449  /// @brief Sets the window size, for when window is not owned by the renderer.
450  void SetWindowSize(const mathfu::vec2i &window_size) {
451  base_->SetWindowSize(window_size);
452  }
453 
454  /// @brief Create a shader object from two strings containing glsl code.
455  Shader *CompileAndLinkShader(const char *vs_source, const char *ps_source) {
456  return base_->CompileAndLinkShader(vs_source, ps_source);
457  }
458 
459  /// @brief Like CompileAndLinkShader, but pass in an old shader to replace.
460  Shader *RecompileShader(const char *vs_source, const char *ps_source,
461  Shader *shader) {
462  return base_->RecompileShader(vs_source, ps_source, shader);
463  }
464 
465  /// @brief Checks for multithreading API.
467  return (base_->AllowMultiThreading());
468  }
469 
470  /// @brief Set bone transforms in vertex shader uniforms.
471  void SetAnimation(const mathfu::AffineTransform *bone_transforms,
472  int num_bones) {
473  base_->SetAnimation(bone_transforms, num_bones);
474  }
475 
476  /// @brief Contains the last error that occurred, if there is one.
477  const std::string &last_error() const {
478  return base_->last_error();
479  }
480  void set_last_error(const std::string &last_error) {
481  base_->set_last_error(last_error);
482  }
483 
484  /// @brief The device's current framebuffer size.
485  const mathfu::vec2i &window_size() const {
486  return base_->window_size();
487  }
488  /// @overload mathfu::vec2i &window_size()
489  mathfu::vec2i &window_size() { return base_->window_size(); }
490 
491  /// @brief Sets the window size
492  void set_window_size(const mathfu::vec2i &ws) {
493  base_->set_window_size(ws);
494  }
495 
496  /// @brief Get the size of the viewport.
497 
498  mathfu::vec2i GetViewportSize() {
499  return base_->GetViewportSize();
500  }
501 
502  Environment &environment() { return base_->environment(); }
503 
504  /// @brief Time in seconds since program start.
505  double time() const { return base_->time(); }
506 
507  /// @brief The supported OpenGL ES feature level.
508  FeatureLevel feature_level() const {
509  return base_->feature_level();
510  }
511 
512  /// @brief The blend that will be used for all draw calls.
514  return base_->force_blend_mode();
515  }
516  /// @brief Set to override the blend mode used for all draw calls.
518  base_->set_force_blend_mode(bm);
519  }
520 
521  /// @brief Set this force any shader that gets loaded to use this pixel shader
522  void set_override_pixel_shader(const std::string &ps) {
523  base_->set_override_pixel_shader(ps);
524  }
525 
526  /// @brief Get the max number of uniforms components
528  return base_->max_vertex_uniform_components();
529  }
530 
531  /// @brief Returns the version of the FPL Base Library.
532  const FplBaseVersion *GetFplBaseVersion() const {
533  return base_->GetFplBaseVersion();
534  }
535 
536  /// @brief Returns if a texture format is supported by the hardware.
537  bool SupportsTextureFormat(TextureFormat texture_format) const {
538  return base_->SupportsTextureFormat(texture_format);
539  }
540 
541  /// @brief Returns if a NPOT textures are supported by the hardware.
542  bool SupportsTextureNpot() const {
543  return base_->SupportsTextureNpot();
544  }
545 
546  /// @brief Returns the current render state.
547  const RenderState &GetRenderState() const { return render_state_; }
548 
549  /// @brief Updates the cached render state with the given render state.
550  ///
551  /// This should be used to avoid mismatch between the expected render state
552  /// and the actual state of the graphics API. The situation tends to happen
553  /// when using the graphics API directly outside of the renderer.
554  /// (Note that no operations will be called in here, only the cached state
555  /// will be updated.)
556  void UpdateCachedRenderState(const RenderState &render_state);
557 
558  /// @brief Returns the active shader, or nullptr if no active shader.
559  const Shader *GetActiveShader() const { return shader_; }
560 
561  /// @brief Returns the active shader, or nullptr if no active shader.
562  Shader *GetActiveShader() { return shader_; }
563 
564  // For internal use only.
565  RendererImpl* impl() { return impl_; }
566 
567  MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
568 
569  private:
570  // Backend-specific create and destroy calls. These just call new and delete
571  // on the platform-specific impl structs.
572  static RendererImpl *CreateRendererImpl();
573  static void DestroyRendererImpl(RendererImpl *impl);
574 
575  // Platform-dependent data.
576  RendererImpl* impl_;
577 
578  // Shared pointer ensures RendererBase gets deleted once all Renderers are
579  // deleted. That is, ownership of RendererBase is shared amongst Renderers.
580  std::shared_ptr<fplbase::RendererBase> base_;
581 
582  // The mvp. Use the Ortho() and Perspective() methods in mathfu::Matrix
583  // to conveniently change the camera.
584  mathfu::mat4 model_view_projection_;
585  mathfu::mat4 model_;
586  mathfu::vec4 color_;
587  mathfu::vec3 light_pos_;
588  mathfu::vec3 camera_pos_;
589  const mathfu::AffineTransform *bone_transforms_;
590  int num_bones_;
591 
592  Shader *shader_;
593  RenderState render_state_;
594 
595  BlendMode blend_mode_;
596  float blend_amount_;
597  CullingMode cull_mode_;
598  DepthFunction depth_function_;
599  StencilMode stencil_mode_;
600  int stencil_ref_;
601  uint32_t stencil_mask_;
602 };
603 
604 /// @}
605 
606 } // namespace fplbase
607 
608 #endif // FPLBASE_RENDERER_H
Shader * GetActiveShader()
Returns the active shader, or nullptr if no active shader.
Definition: renderer.h:562
void SetCulling(CullingMode mode)
Sets the culling mode. By default, no culling happens.
const mathfu::vec2i & window_size() const
The device's current framebuffer size.
Definition: renderer.h:167
const std::string & last_error() const
Contains the last error that occurred, if there is one.
Definition: renderer.h:477
bool SupportsTextureFormat(TextureFormat texture_format) const
Returns if a texture format is supported by the hardware.
Definition: renderer.h:537
const mathfu::vec4 & color() const
Shader uniform: color.
Definition: renderer.h:335
void ScissorOff()
Turn off the scissor region.
int max_vertex_uniform_components()
Get the max number of uniforms components.
Definition: renderer.h:527
Shader * RecompileShader(const char *vs_source, const char *ps_source, Shader *shader)
Like CompileAndLinkShader, but pass in an old shader to replace.
Definition: renderer.h:460
double time() const
Time in seconds since program start.
Definition: renderer.h:505
BlendMode
Specifies the blending mode used by the blend function.
Definition: render_state.h:40
CullingMode
Specifies the which face is culled when rendering.
Definition: render_state.h:80
const mathfu::vec2i & window_size() const
The device's current framebuffer size.
Definition: renderer.h:485
Definition: detailed_render_state.h:142
bool AllowMultiThreading()
Checks for multithreading API.
Definition: renderer.h:466
StencilMode
Specifies stencil modes for the stencil test.
Definition: render_state.h:27
void set_override_pixel_shader(const std::string &ps)
Set this force any shader that gets loaded to use this pixel shader.
Definition: renderer.h:522
Shader * RecompileShader(const char *vs_source, const char *ps_source, Shader *shader)
Like CompileAndLinkShader, but pass in an old shader to replace.
const mathfu::vec3 & light_pos() const
Shader uniform: light_pos.
Definition: renderer.h:342
Represents a shader consisting of a vertex and pixel shader.
Definition: shader.h:42
void set_light_pos(const mathfu::vec3 &light_pos)
Sets the shader uniform light position.
Definition: renderer.h:345
void set_force_blend_mode(BlendMode bm)
Set to override the blend mode used for all draw calls.
Definition: renderer.h:517
void SetBoneTransforms(const mathfu::AffineTransform *bone_transforms, int num_bones)
Sets the shader uniform bone transforms.
Definition: renderer.h:367
uint32_t StencilMask
Specifies the type for the Stencil Mask.
Definition: render_state.h:24
bool SupportsTextureNpot() const
Returns if a NPOT textures are supported by the hardware. see: https://www.opengl.org/wiki/NPOT_Texture.
const FplBaseVersion * GetFplBaseVersion() const
Returns the version of the FPL Base Library.
Definition: renderer.h:232
mathfu::vec2i & window_size()
Definition: renderer.h:489
Manages the rendering system, handling the window and resources.
Definition: renderer.h:66
void set_color(const mathfu::vec4 &color)
Sets the shader uniform color.
Definition: renderer.h:338
void BeginRendering()
Begin rendering commands. This must be called before any rendering commands are done.
DepthFunction
Specifies the depth function used for rendering.
Definition: render_state.h:64
const mathfu::vec3 & camera_pos() const
Shader uniform: camera_pos.
Definition: renderer.h:349
void SetDepthFunction(DepthFunction func)
Set function used for the depth test.
FeatureLevel feature_level() const
The supported OpenGL ES feature level.
Definition: renderer.h:508
void set_window_size(const mathfu::vec2i &ws)
Sets the window size.
Definition: renderer.h:177
bool Initialize(const mathfu::vec2i &window_size, const char *window_title, const WindowMode window_mode=kWindowModeWindowedScaled)
Initializes the renderer by initializing the Environment object.
Definition: renderer.h:438
void EndRendering()
End rendering commands. This is called after all of the rendering commands are done.
void SetWindowSize(const mathfu::vec2i &window_size)
Sets the window size, for when window is not owned by the renderer.
Definition: renderer.h:110
TextureFormat
Definition: texture.h:36
Definition: environment.h:56
Shader * CompileAndLinkShader(const char *vs_source, const char *ps_source)
Create a shader object from two strings containing glsl code.
Definition: renderer.h:455
void SetWindowSize(const mathfu::vec2i &window_size)
Sets the window size, for when window is not owned by the renderer.
Definition: renderer.h:450
void set_model_view_projection(const mathfu::mat4 &mvp)
Sets the shader uniform model_view_projection.
Definition: renderer.h:322
mathfu::vec2i GetViewportSize()
Get the size of the viewport.
Definition: renderer.h:498
BlendMode force_blend_mode() const
The blend that will be used for all draw calls.
Definition: renderer.h:513
void UpdateCachedRenderState(const RenderState &render_state)
Updates the cached render state with the given render state.
bool AllowMultiThreading()
Checks for multithreading API. Returns true if the graphics API allows multi-threading.
void SetAnimation(const mathfu::AffineTransform *bone_transforms, int num_bones)
Set bone transforms in vertex shader uniforms.
int max_vertex_uniform_components()
Get the max number of uniforms components (i.e. individual floats, so a mat4 needs 16 of them)...
Definition: renderer.h:229
const FplBaseVersion * GetFplBaseVersion() const
Returns the version of the FPL Base Library.
Definition: renderer.h:532
mathfu::vec2i GetViewportSize()
Get the size of the viewport.
Definition: renderer.h:187
const mathfu::mat4 & model() const
Shader uniform: model (object to world transform only)
Definition: renderer.h:328
mathfu::vec2i & window_size()
Definition: renderer.h:171
double time() const
Time in seconds since program start.
Definition: renderer.h:196
const RenderState & GetRenderState() const
Returns the current render state.
Definition: renderer.h:547
void set_model(const mathfu::mat4 &model)
Sets the shader uniform model transform.
Definition: renderer.h:331
void set_window_size(const mathfu::vec2i &ws)
Sets the window size.
Definition: renderer.h:492
void AdvanceFrame(bool minimized, double time)
Swaps frames. Call this once per frame inside your main loop.
void ScissorOn(const mathfu::vec2i &ops, const mathfu::vec2i &size)
Turn on a scissor region. Arguments are in screen pixels.
void ClearDepthBuffer()
Clears the depthbuffer. Leaves the colorbuffer untouched.
FeatureLevel feature_level() const
The supported OpenGL ES feature level.
Definition: renderer.h:200
void set_override_pixel_shader(const std::string &ps)
Set this force any shader that gets loaded to use this pixel shader instead (for debugging purposes)...
Definition: renderer.h:216
const mathfu::mat4 & model_view_projection() const
Shader uniform: model_view_projection.
Definition: renderer.h:317
bool SupportsTextureNpot() const
Returns if a NPOT textures are supported by the hardware.
Definition: renderer.h:542
void SetBlendMode(BlendMode blend_mode, float amount)
Sets the blend mode used by the renderer.
void set_camera_pos(const mathfu::vec3 &camera_pos)
Sets the shader uniform camera position.
Definition: renderer.h:352
const Shader * GetActiveShader() const
Returns the active shader, or nullptr if no active shader.
Definition: renderer.h:559
mathfu::recti Viewport
Specifies the region of the surface to be used for rendering.
Definition: renderer_common.h:23
void set_force_blend_mode(BlendMode bm)
Set to override the blend mode used for all draw calls.
Definition: renderer.h:211
bool SupportsTextureFormat(TextureFormat texture_format) const
Returns if a texture format is supported by the hardware.
void ClearFrameBuffer(const mathfu::vec4 &color)
Clears the framebuffer.
void SetAnimation(const mathfu::AffineTransform *bone_transforms, int num_bones)
Set bone transforms in vertex shader uniforms.
Definition: renderer.h:471
void SetStencilMode(StencilMode mode, int ref, StencilMask mask)
Sets the stencil mode. By default, the stencil test is off.
bool Initialize(const mathfu::vec2i &window_size, const char *window_title, WindowMode window_mode=kWindowModeWindowedScaled)
Initializes the renderer by initializing the Environment object.
const std::string & last_error() const
Contains the last error that occurred, if there is one.
Definition: renderer.h:156
void ShutDown()
Cleans up the resources initialized by the renderer.
Definition: renderer.h:103
int num_bones() const
The number of bones in the bone_transforms() array.
Definition: renderer.h:363
void AdvanceFrame(bool minimized, double time)
Swaps frames. Call this once per frame inside your main loop.
Renderer is the main API class for rendering commands.
Definition: renderer.h:310
BlendMode force_blend_mode() const
The blend that will be used for all draw calls.
Definition: renderer.h:204
Shader * CompileAndLinkShader(const char *vs_source, const char *ps_source)
Create a shader object from two strings containing glsl code.
void ShutDown()
Cleans up the resources initialized by the renderer.
Definition: renderer.h:447
void SetViewport(const Viewport &viewport)
Sets the viewport region.
const mathfu::AffineTransform * bone_transforms() const
Shader uniform: bone_transforms.
Definition: renderer.h:358