hx3d  1
2D/3D Simple Game Framework
batch.cpp
1 /*
2  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/batch.hpp"
22 
23 #include "hx3d/graphics/cameras/camera.hpp"
24 
25 #include "hx3d/core/core.hpp"
26 #include "hx3d/graphics/shader.hpp"
27 #include "hx3d/graphics/texture.hpp"
28 #include "hx3d/graphics/font.hpp"
29 
30 #include "hx3d/utils/log.hpp"
31 #include "hx3d/utils/assets.hpp"
32 
33 namespace hx3d {
34 namespace graphics {
35 
36 Batch::Batch(): BaseBatch() {}
37 
38 void Batch::begin() {
39  if (_shader == nullptr) {
40  Log.Error("Attempt to draw without shader.");
41  return;
42  }
43 
45  _shader->setUniformMatrix4f("u_projection", _camera->projection);
46  _shader->setUniformMatrix4f("u_view", _camera->view);
47 }
48 
49 void Batch::end() {
51 }
52 
53 void Batch::draw(Mesh& mesh) {
54 
55  glm::mat4 model = mesh.transform.compute();
56  _shader->setUniformMatrix4f("u_model", model);
57 
58  mesh.draw(_shader);
59 }
60 
61 void Batch::draw(gui::Text& text) {
62 
63  Texture::use(text.getFont(), text.getCharacterSize());
64 
65  Ptr<Shader> prevShader = _shader;
66 
67  setShader(text.getFont()->getShader());
68  begin();
69 
70  float oldX = text.transform.position.x;
71 
72  if (text.isCenterAligned())
73  text.transform.position.x -= text.getLength() / 2;
74 
75  glm::mat4 model = text.transform.compute();
76  text.transform.position.x = oldX;
77 
78  _shader->setUniformMatrix4f("u_model", model);
79 
80  text.draw(_shader);
81 
82  setShader(prevShader);
83  begin();
84 
86 }
87 
88 void Batch::draw(gui::Text& text, math::Function function) {
89  Texture::use(text.getFont(), text.getCharacterSize());
90 
91  Ptr<Shader> prevShader = _shader;
92 
93  setShader(text.getFont()->getShader());
94  begin();
95 
96  float oldX = text.transform.position.x;
97 
98  if (text.isCenterAligned())
99  text.transform.position.x -= text.getLength() / 2;
100 
101  glm::mat4 model = text.transform.compute();
102  text.transform.position.x = oldX;
103 
104  _shader->setUniformMatrix4f("u_model", model);
105 
106  text.functionDraw(_shader, function);
107 
108  setShader(prevShader);
109  begin();
110 
112 }
113 
114 } /* graphics */
115 } /* hx3d */
bool isCenterAligned()
Is the text center aligned ?
Definition: text.cpp:86
virtual void begin()=0
Begin the batching.
Ptr< Shader > _shader
Shader.
Definition: base_batch.hpp:118
static void use(Ptr< Shader > shader)
Use the shader as the current shader.
Definition: shader.cpp:312
glm::mat4 view
View matrix.
Definition: camera.hpp:101
glm::vec3 position
Position.
Definition: transform.hpp:80
virtual void end() override
End the batching.
Definition: batch.cpp:49
glm::mat4 projection
Projection matrix.
Definition: camera.hpp:99
Text GUI element.
Definition: text.hpp:37
hx3d framework namespace
Definition: audio.hpp:26
void setShader(const Ptr< Shader > &shader)
Set the shader for the next batching.
Definition: base_batch.cpp:38
glm::mat4 compute()
Build the model matrix using the position, scale, size and rotation.
Definition: transform.cpp:43
virtual void begin() override
Begin the batching.
Definition: batch.cpp:38
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
virtual void draw(Mesh &mesh) override
Draw the mesh.
Definition: batch.cpp:53
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
Camera * _camera
Camera.
Definition: base_batch.hpp:116
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
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
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