-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.cpp
52 lines (40 loc) · 1.46 KB
/
Sprite.cpp
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
#include "Sprite.h"
Sprite::Sprite(float x0, float y0, float x1, float y1, GLuint textureId) : m_textureId(textureId) {
glGenBuffers(1, &m_vbo);
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
float vertices[] = {
x1, y0, 1.0f, 1.0f,
x1, y1, 1.0f, 0.0f,
x0, y0, 0.0f, 1.0f,
x1, y1, 1.0f, 0.0f,
x0, y1, 0.0f, 0.0f,
x0, y0, 0.0f, 1.0f
// x1, y0, 0.5f, 0.5f,
// x1, y1, 0.5f, 0.0f,
// x0, y0, 0.0f, 0.5f,
// x1, y1, 0.5f, 0.0f,
// x0, y1, 0.0f, 0.0f,
// x0, y0, 0.0f, 0.5f
};
// Texture coords are windowSize / textureSize
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
// (void *) 0 can be changed to nullptr
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *) (2 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(m_vao);
}
Sprite::~Sprite() {
glDeleteBuffers(1, &this->m_vbo);
glDeleteVertexArrays(1, &this->m_vao);
}
void Sprite::draw() {
glBindTexture(GL_TEXTURE_2D, this->m_textureId);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}