hx3d  1
2D/3D Simple Game Framework
mesh.cpp
1 /*
2  Mesh.
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/mesh.hpp"
22 
23 #include "hx3d/graphics/texture.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 
28 Mesh::Mesh() {}
29 
30 void Mesh::draw(Ptr<Shader> shader) {
31 
32  if (_geometry->getAttribute("Texture").size() == 0) {
34  _geometry->draw(shader);
36  }
37 
38  else {
39  _geometry->draw(shader);
40  }
41 }
42 
43 void Mesh::setTint(Color tint) {
44  _tint = tint;
45 
46  updateColor();
47 }
48 
50  _geometry = geometry;
51 }
52 
54  return _geometry;
55 }
56 
58 
59  glm::vec4 floatColor = _tint.toFloat();
60 
61  AttributeArrayBuffer& colors = _geometry->getAttribute("Color");
62  float* colorsData = colors.data();
63  if (colorsData[0] == floatColor.r
64  && colorsData[1] == floatColor.g
65  && colorsData[2] == floatColor.b
66  && colorsData[3] == floatColor.a)
67  return;
68 
69  for (unsigned int i = 0; i < colors.size(); i += 4) {
70  colorsData[i] = floatColor.r;
71  colorsData[i+1] = floatColor.g;
72  colorsData[i+2] = floatColor.b;
73  colorsData[i+3] = floatColor.a;
74  }
75 
76  _geometry->uploadAll();
77 }
78 
80  return _tint;
81 }
82 
83 } /* graphics */
84 } /* hx3d */
glm::vec4 toFloat()
Convert the color to a float format (between 0 and 1).
Definition: color.cpp:42
Color _tint
Tint color.
Definition: mesh.hpp:94
void setTint(Color tint)
Set the mesh tint.
Definition: mesh.cpp:43
void updateColor()
Update the mesh color from the tint.
Definition: mesh.cpp:57
Color & getTint()
Get the mesh tint.
Definition: mesh.cpp:79
Four [0..255] components defined color.
Definition: color.hpp:32
hx3d framework namespace
Definition: audio.hpp:26
void setGeometry(Ptr< geom::BaseGeometry > geometry)
Set the mesh geometry.
Definition: mesh.cpp:49
Ptr< geom::BaseGeometry > _geometry
Current geometry.
Definition: mesh.hpp:96
static Ptr< Texture > Blank
Default blank texture.
Definition: texture.hpp:92
virtual void draw(Ptr< Shader > shader)
Draw the mesh using a shader.
Definition: mesh.cpp:30
Ptr< geom::BaseGeometry > & getGeometry()
Get the mesh geometry.
Definition: mesh.cpp:53
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
T * data()
Get the buffer values.
Definition: buffer.inl.hpp:57
unsigned int size()
Get the buffer size.
Definition: buffer.inl.hpp:62
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34