RenderTarget.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_RENDER_TARGET_HPP
6#define SMK_RENDER_TARGET_HPP
7
8#include <glm/glm.hpp>
9#include <memory>
10#include <smk/RenderState.hpp>
11#include <smk/Shader.hpp>
12#include <smk/VertexArray.hpp>
13#include <smk/View.hpp>
14
15namespace smk {
16
17class Drawable;
18struct RenderState;
19
20/// A texture where things can be / a smk::Drawable can be drawn on.
21///
22/// Implemented by:
23/// * Window
24/// * Framebuffer.
26 public:
28 RenderTarget(RenderTarget&& other) noexcept;
29 RenderTarget(const RenderTarget& rhs) = delete;
30 RenderTarget& operator=(RenderTarget&& other) noexcept;
31 RenderTarget& operator=(const RenderTarget& rhs) = delete;
32
33 // 0. Clear the framebuffer
34 void Clear(const glm::vec4& color);
35
36 // 1. Set the view
37 void SetView(const View& view);
38 void SetView(const glm::mat4& mat);
39 const View& view() const;
40
41 // 2. Set a shader to render elements.
42 void SetShaderProgram(ShaderProgram& shader_program);
45
46 // 3. Draw some stuff.
47 virtual void Draw(const Drawable& drawable);
48 virtual void Draw(RenderState& state);
49
50 // Surface dimensions:
51 glm::vec2 dimensions() const;
52 int width() const;
53 int height() const;
54
55 // Bind the OpenGL RenderFrame. This function is useless, because it is called
56 // automatically for you. Use this only when you use direct OpenGL call.
57 static void Bind(RenderTarget* target);
58
59 protected:
60 void InitRenderTarget();
61
62 int width_ = 0;
63 int height_ = 0;
64
65 // View:
66 glm::mat4 projection_matrix_ = glm::mat4(1);
67 smk::View view_;
68
69 // Shaders:
70 Shader vertex_shader_2d_;
71 Shader fragment_shader_2d_;
72 ShaderProgram shader_program_2d_;
73
74 Shader vertex_shader_3d_;
75 Shader fragment_shader_3d_;
76 ShaderProgram shader_program_3d_;
77
78 // Current shader program.
79 ShaderProgram shader_program_;
80
81 GLuint frame_buffer_ = 0;
82};
83
84} // namespace smk
85
86#endif /* end of include guard: SMK_RENDER_TARGET_HPP */
Interface for class that can be draw to a smk::RenderTarget.
Definition: Drawable.hpp:14
RenderTarget & operator=(RenderTarget &&other) noexcept
Move operator.
void SetView(const View &view)
Set the View to use.
void SetShaderProgram(ShaderProgram &shader_program)
Set the ShaderProgram to be used.
ShaderProgram & shader_program_2d()
Return the default predefined 2D shader program. It is bound by default.
const View & view() const
Return the View currently assigned to this RenderTarget.
int height() const
the height of the surface.
virtual void Draw(const Drawable &drawable)
Draw on the surface.
ShaderProgram & shader_program_3d()
Return the default predefined 3D shader program.
RenderTarget()
Build an invalid RenderTarget. It can be replaced later by using the move operator.
glm::vec2 dimensions() const
the dimension (width, height) of the drawing area.
void Clear(const glm::vec4 &color)
Clear the surface with a single color.
int width() const
the width of the surface.
A shader program is a set of shader (for instance vertex shader + pixel shader) defining the renderin...
Definition: Shader.hpp:114
A Shader is a little program that rest on the GPU. They are run on a specific section of the graphic ...
Definition: Shader.hpp:66
Contain all the data needed to draw.
Definition: RenderState.hpp:17