hx3d  1
2D/3D Simple Game Framework
sprite_geometry.cpp
1 /*
2  Sprite model.
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/geometries/sprite_geometry.hpp"
22 
23 #include "hx3d/graphics/texture.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 namespace geom {
28 
29 SpriteGeometry::SpriteGeometry(): Geometry() {
30 
31  setAttribute("Position", std::vector<float> {
32  -0.5f, 0.5f, 0.f,
33  0.5, 0.5f, 0.f,
34  0.5f, -0.5f, 0.f,
35  -0.5f, -0.5f, 0.f
36  });
37 
38  setIndices(std::vector<GLushort> {
39  0, 2, 1,
40  0, 3, 2
41  });
42 
43  setAttribute("Color", std::vector<float> {
44  1, 1, 1, 1,
45  1, 1, 1, 1,
46  1, 1, 1, 1,
47  1, 1, 1, 1
48  });
49 
50  setAttribute("Texture", std::vector<float> {
51  0, 0,
52  1, 0,
53  1, 1,
54  0, 1
55  });
56 
57  // setAttribute("Normal", std::vector<float> {
58  // 0.f, 0.f, 1.f,
59  // 0.f, 0.f, 1.f,
60  // 0.f, 0.f, 1.f,
61  // 0.f, 0.f, 1.f
62  // });
63 
64  uploadAll();
65 }
66 
68  setAttribute("Texture", std::vector<float> {
69  1, 0,
70  0, 0,
71  0, 1,
72  1, 1
73  });
74 }
75 
77  setAttribute("Texture", std::vector<float> {
78  0, 0,
79  1, 0,
80  1, 1,
81  0, 1
82  });
83 }
84 
86 
87  // Calculate texture points;
88  unsigned int width = region.getTexture()->getWidth();
89  unsigned int height = region.getTexture()->getHeight();
90 
91  setAttribute("Texture", std::vector<float> {
92  region.getMinX() / (float)width, region.getMinY() / float(height),
93  region.getMaxX() / (float)width, region.getMinY() / float(height),
94  region.getMaxX() / (float)width, region.getMaxY() / float(height),
95  region.getMinX() / (float)width, region.getMaxY() / float(height)
96  });
97 }
98 
99 } /* geom */
100 } /* graphics */
101 } /* hx3d */
void activateFramebufferMode()
Prepare the sprite for render-to-texture mode.
float getMaxX()
Get the right coordinate.
float getMaxY()
Get the top coordinate.
float getMinY()
Get the bottom coordinate.
hx3d framework namespace
Definition: audio.hpp:26
void activateTextureMode()
Prepare the sprite for simple texture mode.
void setIndices(std::vector< GLushort > values)
Set the indices.
const Ptr< Texture > & getTexture()
Get the texture.
Defines a rectangle in a texture.
void setAttribute(std::string name, std::vector< float > values)
Set an attribute with values.
void setFromRegion(TextureRegion &region)
Set the geometry for a texture region.
float getMinX()
Get the left coordinate.
void uploadAll()
Upload all the buffers to the GPU.