GameKit  0.0.1a
C++ gamedev tools
RenderTarget.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: RenderTarget.cpp
5  *
6  * Description:
7  *
8  * Created: 14/06/2018 19:55:28
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/gl/GLCheck.hpp"
15 #include "gk/gl/IDrawable.hpp"
16 #include "gk/gl/RenderTarget.hpp"
17 #include "gk/gl/Shader.hpp"
18 #include "gk/gl/Texture.hpp"
19 #include "gk/gl/Vertex.hpp"
20 #include "gk/gl/VertexBuffer.hpp"
21 
22 namespace gk {
23 
24 const RenderStates RenderStates::Default{};
25 
26 void RenderTarget::draw(const IDrawable &drawable, const RenderStates &states) {
27  drawable.draw(*this, states);
28 }
29 
30 void RenderTarget::draw(const VertexBuffer &vertexBuffer, GLenum mode, std::size_t firstVertex, std::size_t vertexCount, const RenderStates &states) {
31  VertexBuffer::bind(&vertexBuffer);
32  beginDrawing(states);
33  glCheck(glDrawArrays(mode, firstVertex, vertexCount));
34  endDrawing(states);
35 }
36 
37 void RenderTarget::drawElements(const VertexBuffer &vertexBuffer, GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, const RenderStates &states) {
38  VertexBuffer::bind(&vertexBuffer);
39  beginDrawing(states);
40  glCheck(glDrawElements(mode, count, type, indices));
41  endDrawing(states);
42 }
43 
45  if (!states.shader) return;
46 
47  Shader::bind(states.shader);
48 
49  if (!m_view) {
50  states.shader->setUniform("u_projectionMatrix", states.projectionMatrix);
51  states.shader->setUniform("u_viewMatrix", states.viewMatrix);
52  }
53  else if (m_viewChanged)
54  applyCurrentView(states);
55 
56  states.shader->setUniform("u_modelMatrix", states.transform);
57 
59  states.shader->enableVertexAttribArray("coord3d");
60  glCheck(glVertexAttribPointer(states.shader->attrib("coord3d"), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d))));
61  }
62 
64  states.shader->enableVertexAttribArray("normal");
65  glCheck(glVertexAttribPointer(states.shader->attrib("normal"), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal))));
66  }
67 
69  states.shader->enableVertexAttribArray("texCoord");
70  glCheck(glVertexAttribPointer(states.shader->attrib("texCoord"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord))));
71  }
72 
74  states.shader->enableVertexAttribArray("color");
75  glCheck(glVertexAttribPointer(states.shader->attrib("color"), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color))));
76  }
77 
79  states.shader->enableVertexAttribArray("lightValue");
80  glCheck(glVertexAttribPointer(states.shader->attrib("lightValue"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue))));
81  }
82 
84  states.shader->enableVertexAttribArray("blockType");
85  glCheck(glVertexAttribPointer(states.shader->attrib("blockType"), 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, blockType))));
86  }
87 
89  states.shader->enableVertexAttribArray("ambientOcclusion");
90  glCheck(glVertexAttribPointer(states.shader->attrib("ambientOcclusion"), 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion))));
91  }
92 
93  if (states.texture)
94  Texture::bind(states.texture);
95 }
96 
98  if (!states.shader) return;
99 
100  Texture::bind(nullptr);
101 
103  states.shader->disableVertexAttribArray("ambientOcclusion");
105  states.shader->disableVertexAttribArray("blockType");
107  states.shader->disableVertexAttribArray("lightValue");
109  states.shader->disableVertexAttribArray("color");
111  states.shader->disableVertexAttribArray("texCoord");
113  states.shader->disableVertexAttribArray("normal");
115  states.shader->disableVertexAttribArray("coord3d");
116 
117  VertexBuffer::bind(nullptr);
118 
119  Shader::bind(nullptr);
120 }
121 
123  float width = static_cast<float>(getSize().x);
124  float height = static_cast<float>(getSize().y);
125  const FloatRect& viewport = view.getViewport();
126 
127  return IntRect(static_cast<int>(0.5f + width * viewport.x),
128  static_cast<int>(0.5f + height * viewport.y),
129  static_cast<int>(width * viewport.width),
130  static_cast<int>(height * viewport.height));
131 }
132 
134  IntRect viewport = getViewport(*m_view);
135  if (viewport != m_previousViewport) {
136  int top = getSize().y - (viewport.y + viewport.height);
137  glViewport(viewport.x, top, viewport.width, viewport.height);
138  m_previousViewport = viewport;
139  }
140 
141  states.shader->setUniform("u_projectionMatrix", m_view->getTransform());
142  states.shader->setUniform("u_viewMatrix", m_view->getViewTransform());
143 
144  m_viewChanged = false;
145 }
146 
147 } // namespace gk
148 
void enableVertexAttribArray(const std::string &name) const
Definition: Shader.cpp:154
const Shader * shader
Transform projectionMatrix
void beginDrawing(const RenderStates &states)
const FloatRect & getViewport() const
Definition: View.hpp:53
void disableVertexAttribArray(const std::string &name) const
Definition: Shader.cpp:158
2D view that defines what is shown on screen
Definition: View.hpp:29
void applyCurrentView(const RenderStates &states)
const Texture * texture
IntRect getViewport(const View &view) const
void endDrawing(const RenderStates &states)
static void bind(const Texture *texture)
Bind a texture for rendering.
Definition: Texture.cpp:94
virtual Vector2u getSize() const =0
T y
Top coordinate of the rectangle.
Definition: Rect.hpp:216
void draw(const IDrawable &drawable, const RenderStates &states=RenderStates::Default)
virtual void draw(RenderTarget &target, RenderStates states) const =0
Draw the object to a render target.
Transform transform
static void bind(const Shader *shader)
Definition: Shader.cpp:178
const View * m_view
static void bind(const VertexBuffer *vertexBuffer)
static const RenderStates Default
virtual const Transform & getTransform() const
Definition: View.cpp:75
IntRect m_previousViewport
Rect< int > IntRect
Definition: Rect.hpp:222
GLint attrib(const std::string &name) const
Definition: Shader.cpp:132
Abstract base class for objects that can be drawn to a render target.
Definition: IDrawable.hpp:25
T x
Left coordinate of the rectangle.
Definition: Rect.hpp:215
virtual const Transform & getViewTransform() const
Definition: View.cpp:85
#define glCheck(expr)
Definition: GLCheck.hpp:18
T width
Width of the rectangle.
Definition: Rect.hpp:217
void setUniform(const std::string &name, int n) const
Definition: Shader.cpp:162
Transform viewMatrix
void drawElements(const VertexBuffer &vertexBuffer, GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, const RenderStates &states=RenderStates::Default)
T height
Height of the rectangle.
Definition: Rect.hpp:218