forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cpp
More file actions
116 lines (86 loc) · 3.31 KB
/
Sprite.cpp
File metadata and controls
116 lines (86 loc) · 3.31 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
116
#include "Sprite.h"
#include "Vertex.h"
#include "ResourceManager.h"
#include <cstddef>
namespace Bengine {
Sprite::Sprite()
{
_vboID = 0;
}
Sprite::~Sprite()
{
//Always remember to delete your buffers when
//you are done!
if (_vboID != 0) {
glDeleteBuffers(1, &_vboID);
}
}
//Initializes the sprite VBO. x, y, width, and height are
//in the normalized device coordinate space. so, [-1, 1]
void Sprite::init(float x, float y, float width, float height, std::string texturePath) {
//Set up our private vars
_x = x;
_y = y;
_width = width;
_height = height;
_texture = ResourceManager::getTexture(texturePath);
//Generate the buffer if it hasn't already been generated
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
//This array will hold our vertex data.
//We need 6 vertices, and each vertex has 2
//floats for X and Y
Vertex vertexData[6];
//First Triangle
vertexData[0].setPosition(x + width, y + height);
vertexData[0].setUV(1.0f, 1.0f);
vertexData[1].setPosition(x, y + height);
vertexData[1].setUV(0.0f, 1.0f);
vertexData[2].setPosition(x, y);
vertexData[2].setUV(0.0f, 0.0f);
//Second Triangle
vertexData[3].setPosition(x, y);
vertexData[3].setUV(0.0f, 0.0f);
vertexData[4].setPosition(x + width, y);
vertexData[4].setUV(1.0f, 0.0f);
vertexData[5].setPosition(x + width, y + height);
vertexData[5].setUV(1.0f, 1.0f);
//Set all vertex colors to magenta
for (int i = 0; i < 6; i++) {
vertexData[4].setColor(255, 0, 255, 255);
}
vertexData[4].setColor(0, 0, 255, 255);
vertexData[4].setColor(0, 255, 0, 255);
//Tell opengl to bind our vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Upload the data to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
//Unbind the buffer (optional)
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//Draws the sprite to the screen
void Sprite::draw() {
glBindTexture(GL_TEXTURE_2D, _texture.id);
//bind the buffer object
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Tell opengl what attribute arrays we need
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
//This is the position attribute pointer
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
//This is the color attribute pointer
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
//This is the UV attribute pointer
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
//Draw the 6 vertices to the screen
glDrawArrays(GL_TRIANGLES, 0, 6);
//Disable the vertex attrib arrays. This is not optional.
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
//Unbind the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}