hx3d  1
2D/3D Simple Game Framework
ordered_batch.cpp
1 /*
2  Ordered Batch.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/ordered_batch.hpp"
22 
23 #include "hx3d/graphics/cameras/camera.hpp"
24 
25 #include "hx3d/core/core.hpp"
26 
27 #include "hx3d/graphics/shader.hpp"
28 #include "hx3d/graphics/texture.hpp"
29 #include "hx3d/graphics/font.hpp"
30 
31 #include "hx3d/utils/log.hpp"
32 #include "hx3d/utils/assets.hpp"
33 
34 namespace hx3d {
35 namespace graphics {
36 
37 OrderedBatch::OrderedBatch(): BaseBatch() {}
38 
40  if (_shader == nullptr) {
41  Log.Error("Attempt to draw without shader.");
42  return;
43  }
44 
45  Shader::use(_shader);
46  _shader->setUniformMatrix4f("u_projection", _camera->projection);
47  _shader->setUniformMatrix4f("u_view", _camera->view);
48 }
49 
51 
55  for (auto& pair: _meshes) {
56  auto& model = pair.first;
57  Mesh* mesh = pair.second;
58 
59  _shader->setUniformMatrix4f("u_model", model);
60  mesh->draw(_shader);
61  }
62 
66  for (auto& pair: _funcTexts) {
67  gui::Text& text = pair.first;
68  auto& func = pair.second;
69 
70  Texture::use(text.getFont(), text.getCharacterSize());
71 
72  float oldX = text.transform.position.x;
73  if (text.isCenterAligned())
74  text.transform.position.x -= text.getLength() / 2;
75 
76  glm::mat4 model = text.transform.compute();
77  text.transform.position.x = oldX;
78 
79  auto& shader = text.getFont()->getShader();
80  Shader::use(shader);
81  shader->setUniformMatrix4f("u_model", model);
82  shader->setUniformMatrix4f("u_view", _camera->view);
83  shader->setUniformMatrix4f("u_projection", _camera->projection);
84  text.functionDraw(shader, func);
85 
86  Shader::use(_shader);
87  _shader->setUniformMatrix4f("u_view", _camera->view);
88  _shader->setUniformMatrix4f("u_projection", _camera->projection);
89 
91  }
92 
96  for (gui::Text& text: _texts) {
97  Texture::use(text.getFont(), text.getCharacterSize());
98 
99  float oldX = text.transform.position.x;
100  if (text.isCenterAligned())
101  text.transform.position.x -= text.getLength() / 2;
102 
103  glm::mat4 model = text.transform.compute();
104  text.transform.position.x = oldX;
105 
106  auto& shader = text.getFont()->getShader();
107  Shader::use(shader);
108  shader->setUniformMatrix4f("u_model", model);
109  shader->setUniformMatrix4f("u_view", _camera->view);
110  shader->setUniformMatrix4f("u_projection", _camera->projection);
111  text.draw(shader);
112 
113  Shader::use(_shader);
114  _shader->setUniformMatrix4f("u_view", _camera->view);
115  _shader->setUniformMatrix4f("u_projection", _camera->projection);
116 
118  }
119 
120  Shader::disable();
121  _meshes.clear();
122  _texts.clear();
123  _funcTexts.clear();
124 }
125 
127 
128  unsigned int pos = 0;
129  for (pos = 0; pos < _meshes.size(); ++pos) {
130  auto& pair = _meshes[pos];
131  Mesh* m = pair.second;
132 
133  if (m->transform.position.z > mesh.transform.position.z) {
134  break;
135  }
136  }
137 
138  _meshes.insert(_meshes.begin() + pos, std::make_pair(mesh.transform.compute(), &mesh));
139 }
140 
142  _texts.push_back(text);
143 }
144 
146  _funcTexts.push_back(std::make_pair(text, function));
147 }
148 
149 } /* graphics */
150 } /* hx3d */
bool isCenterAligned()
Is the text center aligned ?
Definition: text.cpp:86
static void use(Ptr< Shader > shader)
Use the shader as the current shader.
Definition: shader.cpp:312
glm::vec3 position
Position.
Definition: transform.hpp:80
virtual void end() override
End the batching.
Text GUI element.
Definition: text.hpp:37
hx3d framework namespace
Definition: audio.hpp:26
glm::mat4 compute()
Build the model matrix using the position, scale, size and rotation.
Definition: transform.cpp:43
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
virtual void draw(Mesh &mesh) override
Draw the mesh.
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
virtual void begin() override
Begin the batching.
int getCharacterSize()
Get the character size.
Definition: text.cpp:74
static void disable()
Clear the current shader.
Definition: shader.cpp:316
Transform transform
Mesh transformation.
Definition: mesh.hpp:90
Displayable 2D/3D element.
Definition: mesh.hpp:41
Math function definition.
Definition: function.hpp:33
float getLength()
Get the text length.
Definition: text.cpp:78
virtual void draw(Ptr< Shader > shader)
Draw the mesh using a shader.
Definition: mesh.cpp:30
static void disable()
Clear the current texture for drawing.
Definition: texture.cpp:136
static void use(Ptr< Texture > texture)
Use the current texture for drawing.
Definition: texture.cpp:127
Ptr< Font > getFont()
Get the text font.
Definition: text.cpp:70
void functionDraw(Ptr< Shader > shader, math::Function function)
Draw the text following a function.
Definition: text.cpp:162