-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cpp
More file actions
115 lines (90 loc) · 3.09 KB
/
Sprite.cpp
File metadata and controls
115 lines (90 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Sprite.h"
#include "Vertex.h"
#include "ResourceManager.h"
#include <cstddef>
Sprite::Sprite() : _vboID(0), _iboID(0), _vaoID(0)
{
}
Sprite::~Sprite()
{
// Free up space occupied by buffers
if (_vboID) glDeleteBuffers(1, &_vboID);
if (_iboID) glDeleteBuffers(1, &_iboID);
if (_vaoID) glDeleteBuffers(1, &_vaoID);
}
void Sprite::init(float x, float y, float width, float height, std::string texturePath)
{
_x = x;
_y = y;
_width = width;
_height = height;
texture = ResourceManager::texCache->getTexture(texturePath);
// Create buffer to store Vertex Array Object
if (!_vaoID) glGenBuffers(1, &_vaoID);
// Create buffer to store Vertex Buffer Object
if (!_vboID) glGenBuffers(1, &_vboID);
// Create buffer to store Index Buffer Object
if (!_iboID) glGenBuffers(1, &_iboID);
// Index Buffer data
GLuint indices[6] = { 0, 1, 2, 2, 3, 1 };
// Vertex coordinates
// Top Left
float tlX = x;
float tlY = y + height;
// Top Right
float trX = x + width;
float trY = y + height;
// Bottom Left
float blX = x;
float blY = y;
// Bottom Right
float brX = x + width;
float brY = y;
// OpenGL will read this as 2 triangles, which make a square
Vertex vertexBufferData[4];
vertexBufferData[0].position = {tlX, tlY};
vertexBufferData[0].uv = {0.0f, 1.0f};
vertexBufferData[1].position = {trX, trY};
vertexBufferData[1].uv = {1.0f, 1.0f};
vertexBufferData[2].position = {blX, blY};
vertexBufferData[2].uv = {0.0f, 0.0f};
vertexBufferData[3].position = {brX, brY};
vertexBufferData[3].uv = {1.0f, 0.0f};
// Make them all white by default
for (Vertex &v : vertexBufferData)
{
v.color = {255, 255, 255, 255};
}
// Create and bind Vertex Array Object
glGenVertexArrays(1, &_vaoID);
glBindVertexArray(_vaoID);
// Upload VBO to the GPU
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
// Position pointer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
// Color pointer
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
// UV pointer
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
// Upload IBO to the GPU
glGenBuffers(1, &_iboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void Sprite::draw()
{
// Check if texture is already bound
if (texture->id != ResourceManager::boundTexture) {
glBindTexture(GL_TEXTURE_2D, texture->id);
ResourceManager::boundTexture = texture->id;
}
// Setup
glBindVertexArray(_vaoID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboID);
// Draw stuff
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}