smk::RenderTarget Class Reference

#include <RenderTarget.hpp>

Inheritance diagram for smk::RenderTarget:

Description

A texture where things can be / a smk::Drawable can be drawn on.

Implemented by:

Definition at line 25 of file RenderTarget.hpp.

Public Member Functions

 RenderTarget ()
 Build an invalid RenderTarget. It can be replaced later by using the move operator. More...
 
 RenderTarget (RenderTarget &&other) noexcept
 Constructor from temporary. More...
 
 RenderTarget (const RenderTarget &rhs)=delete
 
RenderTargetoperator= (RenderTarget &&other) noexcept
 Move operator. More...
 
RenderTargetoperator= (const RenderTarget &rhs)=delete
 
void Clear (const glm::vec4 &color)
 Clear the surface with a single color. More...
 
void SetView (const View &view)
 Set the View to use. More...
 
void SetView (const glm::mat4 &mat)
 Set the View to use. More...
 
const Viewview () const
 Return the View currently assigned to this RenderTarget. More...
 
void SetShaderProgram (ShaderProgram &shader_program)
 Set the ShaderProgram to be used. More...
 
ShaderProgramshader_program_2d ()
 Return the default predefined 2D shader program. It is bound by default. More...
 
ShaderProgramshader_program_3d ()
 Return the default predefined 3D shader program. More...
 
virtual void Draw (const Drawable &drawable)
 Draw on the surface. More...
 
virtual void Draw (RenderState &state)
 Draw on the surface. More...
 
glm::vec2 dimensions () const
 the dimension (width, height) of the drawing area. More...
 
int width () const
 the width of the surface. More...
 
int height () const
 the height of the surface. More...
 

Static Public Member Functions

static void Bind (RenderTarget *target)
 

Protected Member Functions

void InitRenderTarget ()
 

Protected Attributes

int width_ = 0
 
int height_ = 0
 
glm::mat4 projection_matrix_ = glm::mat4(1)
 
smk::View view_
 
Shader vertex_shader_2d_
 
Shader fragment_shader_2d_
 
ShaderProgram shader_program_2d_
 
Shader vertex_shader_3d_
 
Shader fragment_shader_3d_
 
ShaderProgram shader_program_3d_
 
ShaderProgram shader_program_
 
GLuint frame_buffer_ = 0
 

Constructor & Destructor Documentation

◆ RenderTarget() [1/2]

smk::RenderTarget::RenderTarget ( )
default

Build an invalid RenderTarget. It can be replaced later by using the move operator.

◆ RenderTarget() [2/2]

smk::RenderTarget::RenderTarget ( RenderTarget &&  other)
noexcept

Constructor from temporary.

Definition at line 42 of file RenderTarget.cpp.

42 {
43 operator=(std::move(other));
44}
RenderTarget & operator=(RenderTarget &&other) noexcept
Move operator.

Member Function Documentation

◆ Bind()

void smk::RenderTarget::Bind ( RenderTarget target)
static

Definition at line 28 of file RenderTarget.cpp.

28 {
29 if (render_target == target) {
30 return;
31 }
32 render_target = target;
33 glBindFramebuffer(GL_FRAMEBUFFER, render_target->frame_buffer_);
34 glViewport(0, 0, render_target->width_, render_target->height_);
35}

◆ Clear()

void smk::RenderTarget::Clear ( const glm::vec4 &  color)

Clear the surface with a single color.

Parameters
colorAn opaque color to fill the surface.

Definition at line 65 of file RenderTarget.cpp.

65 {
66 Bind(this);
67 glClearColor(color.r, color.g, color.b, color.a); // NOLINT
68 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
69 glDisable(GL_DEPTH_TEST);
70 glDisable(GL_CULL_FACE);
71}

◆ dimensions()

glm::vec2 smk::RenderTarget::dimensions ( ) const

the dimension (width, height) of the drawing area.

Returns
the dimensions in (pixels, pixels) of the surface.

Definition at line 219 of file RenderTarget.cpp.

219 {
220 return glm::vec2(width_, height_);
221}

◆ Draw() [1/2]

void smk::RenderTarget::Draw ( const Drawable drawable)
virtual

Draw on the surface.

Parameters
drawableThe object to be drawn on the surface.
Examples
sprite_move.cpp.

Definition at line 162 of file RenderTarget.cpp.

162 {
163 Bind(this);
164 RenderState state;
165 state.shader_program = shader_program_;
166 state.view = glm::mat4(1.F);
167 state.color = smk::Color::White;
168 state.blend_mode = smk::BlendMode::Alpha;
169 drawable.Draw(*this, state);
170}
const glm::vec4 White
White.
Definition: Color.cpp:29
static const BlendMode Alpha
destination = source * source.a + destination * (1 - souce.a)
Definition: BlendMode.hpp:68

◆ Draw() [2/2]

void smk::RenderTarget::Draw ( RenderState state)
virtual

Draw on the surface.

Parameters
stateThe RenderState to be usd for drawing.

Definition at line 174 of file RenderTarget.cpp.

174 {
175 // Vertex Array
176 if (cached_render_state_.vertex_array != state.vertex_array) {
177 cached_render_state_.vertex_array = state.vertex_array;
178 state.vertex_array.Bind();
179 }
180
181 // Shader
182 if (cached_render_state_.shader_program != state.shader_program) {
183 cached_render_state_.shader_program = state.shader_program;
184 cached_render_state_.shader_program.Use();
185 }
186
187 // Color
188 if (cached_render_state_.color != state.color) {
189 cached_render_state_.color = state.color;
190 cached_render_state_.shader_program.SetUniform("color", state.color);
191 }
192
193 // View (not cached)
194 state.shader_program.SetUniform("projection", projection_matrix_);
195 state.shader_program.SetUniform("view", state.view);
196
197 // Texture
198 const auto& texture = state.texture.id() ? state.texture : WhiteTexture();
199 if (cached_render_state_.texture != texture || g_invalidate_textures) {
200 cached_render_state_.texture = texture;
201 texture.Bind();
202 g_invalidate_textures = false;
203 }
204
205 if (cached_render_state_.blend_mode != state.blend_mode) {
206 cached_render_state_.blend_mode = state.blend_mode;
207 glEnable(GL_BLEND);
208 glBlendEquationSeparate(state.blend_mode.equation_rgb,
209 state.blend_mode.equation_alpha);
210 glBlendFuncSeparate(state.blend_mode.src_rgb, state.blend_mode.dst_rgb,
211 state.blend_mode.src_alpha, state.blend_mode.dst_alpha);
212 }
213
214 glDrawArrays(GL_TRIANGLES, 0, GLsizei(state.vertex_array.size()));
215}

◆ height()

int smk::RenderTarget::height ( ) const

the height of the surface.

Returns
the height in pixels of the surface.

Definition at line 231 of file RenderTarget.cpp.

231 {
232 return height_;
233}

◆ InitRenderTarget()

void smk::RenderTarget::InitRenderTarget ( )
protected

Definition at line 235 of file RenderTarget.cpp.

235 {
236 View default_view;
237 default_view.SetCenter(float(width_) / 2.F, float(height_) / 2.F); // NOLINT
238 default_view.SetSize(float(width_), float(height_));
239 SetView(default_view);
240
241 vertex_shader_2d_ = Shader::FromString(R"(
242 layout(location = 0) in vec2 space_position;
243 layout(location = 1) in vec2 texture_position;
244
245 uniform mat4 projection;
246 uniform mat4 view;
247
248 out vec2 f_texture_position;
249
250 void main() {
251 f_texture_position = texture_position;
252 gl_Position = projection * view * vec4(space_position, 0.F, 1.F);
253 }
254 )",
255 GL_VERTEX_SHADER);
256
257 fragment_shader_2d_ = Shader::FromString(R"(
258 in vec2 f_texture_position;
259 uniform sampler2D texture_0;
260 uniform vec4 color;
261 out vec4 out_color;
262
263 void main() {
264 out_color = texture(texture_0, f_texture_position) * color;
265 }
266 )",
267 GL_FRAGMENT_SHADER);
268
269 shader_program_2d_.AddShader(vertex_shader_2d_);
270 shader_program_2d_.AddShader(fragment_shader_2d_);
271 shader_program_2d_.Link();
272
273 vertex_shader_3d_ = Shader::FromString(R"(
274 layout(location = 0) in vec3 space_position;
275 layout(location = 1) in vec3 normal;
276 layout(location = 2) in vec2 texture_position;
277
278 uniform mat4 projection;
279 uniform mat4 view;
280
281 out vec4 fPosition;
282 out vec2 fTexture;
283 out vec3 fNormal;
284
285 void main() {
286 fTexture = texture_position;
287 fPosition = view * vec4(space_position,1.F);
288 fNormal = vec3(view * vec4(normal,0.F));
289
290 gl_Position = projection * fPosition;
291 }
292 )",
293 GL_VERTEX_SHADER);
294
295 fragment_shader_3d_ = Shader::FromString(R"(
296 uniform sampler2D texture_0;
297 uniform vec4 color;
298
299 uniform vec4 light_position;
300 uniform float ambient;
301 uniform float diffuse;
302 uniform float specular;
303 uniform float specular_power;
304
305 in vec4 fPosition;
306 in vec2 fTexture;
307 in vec3 fNormal;
308
309 out vec4 out_color;
310
311 void main(void)
312 {
313 vec3 object_dir =-normalize(fPosition.xyz);
314 vec3 normal_dir = normalize(fNormal);
315 vec3 light_dir = normalize(light_position.xyz-fPosition.xyz);
316 vec3 reflect_dir = -reflect(object_dir,normal_dir);
317
318 float diffuse_strength = max(0.F, dot(normal_dir, light_dir));
319 float specular_strength = pow(max(0.F, dot(reflect_dir, light_dir)),
320 specular_power);
321
322 out_color = texture(texture_0, fTexture);
323 out_color.rgb *= ambient +
324 diffuse * diffuse_strength +
325 specular * specular_strength;
326 out_color *= color;
327 }
328 )",
329 GL_FRAGMENT_SHADER);
330
331 shader_program_3d_.AddShader(vertex_shader_3d_);
332 shader_program_3d_.AddShader(fragment_shader_3d_);
333 shader_program_3d_.Link();
334
335 shader_program_3d_.Use();
336 constexpr auto default_light_position = glm::vec4(0.F, 5.F, 0.F, 1.F);
337 constexpr auto default_ambient = 0.3F;
338 constexpr auto default_diffuse = 0.5F;
339 constexpr auto default_specular = 0.5F;
340 constexpr auto default_specular_power = 4.F;
341 shader_program_3d_.SetUniform("light_position", default_light_position);
342 shader_program_3d_.SetUniform("ambient", default_ambient);
343 shader_program_3d_.SetUniform("diffuse", default_diffuse);
344 shader_program_3d_.SetUniform("specular", default_specular);
345 shader_program_3d_.SetUniform("specular_power", default_specular_power);
346
347 SetShaderProgram(shader_program_2d_);
348}
void SetView(const View &view)
Set the View to use.
void SetShaderProgram(ShaderProgram &shader_program)
Set the ShaderProgram to be used.
void SetUniform(const std::string &name, float x, float y, float z)
Assign shader vec3 uniform.
Definition: Shader.cpp:377
void AddShader(const Shader &shader)
Add a Shader to the program list. This must called multiple time for each shader components before ca...
Definition: Shader.cpp:224
void Use() const
Bind the ShaderProgram. Future draw will use it. This unbind any previously bound ShaderProgram.
Definition: Shader.cpp:429
void Link() const
Add a Shader to the program list.
Definition: Shader.cpp:236
static Shader FromString(const std::string &content, GLenum type)
Load a shader from a std::string.
Definition: Shader.cpp:60

◆ operator=()

RenderTarget & smk::RenderTarget::operator= ( RenderTarget &&  other)
noexcept

Move operator.

Definition at line 47 of file RenderTarget.cpp.

47 {
48 std::swap(width_, other.width_);
49 std::swap(height_, other.height_);
50 std::swap(projection_matrix_, other.projection_matrix_);
51 std::swap(view_, other.view_);
52 std::swap(vertex_shader_2d_, other.vertex_shader_2d_);
53 std::swap(fragment_shader_2d_, other.fragment_shader_2d_);
54 std::swap(shader_program_2d_, other.shader_program_2d_);
55 std::swap(vertex_shader_3d_, other.vertex_shader_3d_);
56 std::swap(fragment_shader_3d_, other.fragment_shader_3d_);
57 std::swap(shader_program_3d_, other.shader_program_3d_);
58 std::swap(shader_program_, other.shader_program_);
59 std::swap(frame_buffer_, other.frame_buffer_);
60 return *this;
61}

◆ SetShaderProgram()

void smk::RenderTarget::SetShaderProgram ( ShaderProgram shader_program)

Set the ShaderProgram to be used.

Parameters
shader_programThe ShaderProgram to be used.

Example:

auto shader_vertex = Shader::FromString(R"(
layout(location = 0) in vec2 space_position;
layout(location = 1) in vec2 texture_position;
uniform mat4 projection;
uniform mat4 view;
out vec2 f_texture_position;
void main() {
f_texture_position = texture_position;
gl_Position = projection * view * vec4(space_position, 0.F, 1.F);
}
)", GL_VERTEX_SHADER);
fragment_shader_2d_ = Shader::FromString(R"(
in vec2 f_texture_position;
uniform sampler2D texture_0;
uniform vec4 color;
out vec4 out_color;
void main() {
vec4 inverted_color = vec4(1.F) - color;
out_color = texture(texture_0, f_texture_position) * inverted_color.
}
)";
auto shader_program = smk::ShaderProgram>();
shader_program.AddShader(vertex_shader_2d_);
shader_program.AddShader(fragment_shader_2d_);
shader_program.Link();
window.SetShaderProgram(shader_program);
A shader program is a set of shader (for instance vertex shader + pixel shader) defining the renderin...
Definition: Shader.hpp:114

{

Definition at line 140 of file RenderTarget.cpp.

140 {
141 shader_program_ = shader_program;
142 shader_program_.Use();
143 shader_program_.SetUniform("texture_0", 0);
144 shader_program_.SetUniform("color", glm::vec4(1.F, 1.F, 1.F, 1.F));
145 shader_program_.SetUniform("projection", glm::mat4(1.F));
146 shader_program_.SetUniform("view", glm::mat4(1.F));
147}

◆ SetView() [1/2]

void smk::RenderTarget::SetView ( const glm::mat4 &  mat)

Set the View to use.

Parameters
matThe matrix to transform from/to the OpenGL space [-1,1]^3 to the screen space.

Definition at line 90 of file RenderTarget.cpp.

90 {
91 projection_matrix_ = mat;
92}

◆ SetView() [2/2]

void smk::RenderTarget::SetView ( const View view)

Set the View to use.

Parameters
viewThe view to use.

Definition at line 75 of file RenderTarget.cpp.

75 {
76 view_ = view;
77 float z_x = +2.F / view.width_; // NOLINT [0, width] -> [-1,1]
78 float z_y = -2.F / view.height_; // NOLINT [0, height] -> [-1,1]
79 float t_x = -view.x_ * z_x;
80 float t_y = -view.y_ * z_y;
81 SetView(glm::mat4(z_x, 0.F, 0.F, 0.F, //
82 0.F, z_y, 0.F, 0.F, //
83 0.F, 0.F, 1.F, 0.F, //
84 t_x, t_y, 0.F, 1.F)); //
85}
const View & view() const
Return the View currently assigned to this RenderTarget.
float x_
<
Definition: View.hpp:27

◆ shader_program_2d()

ShaderProgram & smk::RenderTarget::shader_program_2d ( )

Return the default predefined 2D shader program. It is bound by default.

Definition at line 151 of file RenderTarget.cpp.

151 {
152 return shader_program_2d_;
153};

◆ shader_program_3d()

ShaderProgram & smk::RenderTarget::shader_program_3d ( )

Return the default predefined 3D shader program.

Definition at line 156 of file RenderTarget.cpp.

156 {
157 return shader_program_3d_;
158};

◆ view()

const View & smk::RenderTarget::view ( ) const

Return the View currently assigned to this RenderTarget.

Definition at line 95 of file RenderTarget.cpp.

95 {
96 return view_;
97}

◆ width()

int smk::RenderTarget::width ( ) const

the width of the surface.

Returns
the height of the surface.

Definition at line 225 of file RenderTarget.cpp.

225 {
226 return width_;
227}

Member Data Documentation

◆ fragment_shader_2d_

Shader smk::RenderTarget::fragment_shader_2d_
protected

Definition at line 71 of file RenderTarget.hpp.

◆ fragment_shader_3d_

Shader smk::RenderTarget::fragment_shader_3d_
protected

Definition at line 75 of file RenderTarget.hpp.

◆ frame_buffer_

GLuint smk::RenderTarget::frame_buffer_ = 0
protected

Definition at line 81 of file RenderTarget.hpp.

◆ height_

int smk::RenderTarget::height_ = 0
protected

Definition at line 63 of file RenderTarget.hpp.

◆ projection_matrix_

glm::mat4 smk::RenderTarget::projection_matrix_ = glm::mat4(1)
protected

Definition at line 66 of file RenderTarget.hpp.

◆ shader_program_

ShaderProgram smk::RenderTarget::shader_program_
protected

Definition at line 79 of file RenderTarget.hpp.

◆ shader_program_2d_

ShaderProgram smk::RenderTarget::shader_program_2d_
protected

Definition at line 72 of file RenderTarget.hpp.

◆ shader_program_3d_

ShaderProgram smk::RenderTarget::shader_program_3d_
protected

Definition at line 76 of file RenderTarget.hpp.

◆ vertex_shader_2d_

Shader smk::RenderTarget::vertex_shader_2d_
protected

Definition at line 70 of file RenderTarget.hpp.

◆ vertex_shader_3d_

Shader smk::RenderTarget::vertex_shader_3d_
protected

Definition at line 74 of file RenderTarget.hpp.

◆ view_

smk::View smk::RenderTarget::view_
protected

Definition at line 67 of file RenderTarget.hpp.

◆ width_

int smk::RenderTarget::width_ = 0
protected

Definition at line 62 of file RenderTarget.hpp.


The documentation for this class was generated from the following files: