-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpriteBatch.cpp
213 lines (174 loc) · 6.45 KB
/
SpriteBatch.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "SpriteBatch.h"
#include <algorithm>
namespace NeroEngine{
Glyph::Glyph(const glm::vec4& descRect, const glm::vec4& uvRect, GLuint Texture, float Depth, const Color& color) :
texture(Texture), depth(Depth){
topLeft.color = color;
topLeft.setPosition(descRect.x, descRect.y + descRect.w);
topLeft.setUV(uvRect.x, uvRect.y + uvRect.w);
bottomLeft.color = color;
bottomLeft.setPosition(descRect.x, descRect.y);
bottomLeft.setUV(uvRect.x, uvRect.y);
bottomRight.color = color;
bottomRight.setPosition(descRect.x + descRect.z, descRect.y);
bottomRight.setUV(uvRect.x + uvRect.z, uvRect.y);
topRight.color = color;
topRight.setPosition(descRect.x + descRect.z, descRect.y + descRect.w);
topRight.setUV(uvRect.x + uvRect.z, uvRect.y + uvRect.w);
}
Glyph::Glyph(const glm::vec4& descRect, const glm::vec4& uvRect, GLuint Texture, float Depth, const Color& color,float angle) :
texture(Texture), depth(Depth){
glm::vec2 halfDims(descRect.z/2.0f,descRect.w/2.0f);
glm::vec2 tl(-halfDims.x, halfDims.y);
glm::vec2 bl(-halfDims.x, -halfDims.y);
glm::vec2 br(halfDims.x, -halfDims.y);
glm::vec2 tr(halfDims.x, halfDims.y);
//Ðýת×ø±ê
tl = rotatePoint(tl, angle)+halfDims;
bl = rotatePoint(bl, angle) + halfDims;
br = rotatePoint(br, angle) + halfDims;
tr = rotatePoint(tr, angle) + halfDims;
topLeft.color = color;
topLeft.setPosition(descRect.x+tl.x, descRect.y + tl.y);
topLeft.setUV(uvRect.x, uvRect.y + uvRect.w);
bottomLeft.color = color;
bottomLeft.setPosition(descRect.x+bl.x, descRect.y+bl.y);
bottomLeft.setUV(uvRect.x, uvRect.y);
bottomRight.color = color;
bottomRight.setPosition(descRect.x +br.x, descRect.y+br.y);
bottomRight.setUV(uvRect.x + uvRect.z, uvRect.y);
topRight.color = color;
topRight.setPosition(descRect.x+tr.x, descRect.y + tr.y);
topRight.setUV(uvRect.x + uvRect.z, uvRect.y + uvRect.w);
}
glm::vec2 Glyph::rotatePoint(glm::vec2 pos, float angle){
glm::vec2 newv;
newv.x = pos.x*cos(angle)-pos.y*sin(angle);
newv.y = pos.x*sin(angle)+pos.y*cos(angle);
return newv;
}
SpriteBatch::SpriteBatch():_vbo(0),_vao(0){
}
SpriteBatch::~SpriteBatch(){
}
void SpriteBatch::init(){
createVertexArray();
}
void SpriteBatch::begin(GlyphSortType sortType /*GlyphSortType::TEXTURE*/){
_sortType = sortType;
_renderBatches.clear();
_glyphs.clear();
}
void SpriteBatch::end(){
_glyphsPointers.resize(_glyphs.size());
for (int i = 0; i < _glyphs.size();i++){
_glyphsPointers[i] = &_glyphs[i];
}
sortGlyphs();
createRenderBatches();
}
void SpriteBatch::draw(const glm::vec4& descRect, const glm::vec4& uvRect, GLuint texture,float depth, const Color& color){
_glyphs.emplace_back(descRect,uvRect,texture,depth,color);
}
void SpriteBatch::draw(const glm::vec4& descRect, const glm::vec4& uvRect, GLuint texture, float depth, const Color& color, float angle){
_glyphs.emplace_back(descRect, uvRect, texture, depth, color,angle);
}
void SpriteBatch::draw(const glm::vec4& descRect, const glm::vec4& uvRect, GLuint texture, float depth, const Color& color, glm::vec2& dir){
const glm::vec2 right(1.0f, 0.0f);
float angle = acos(glm::dot(right, dir));
if (dir.y<0){
angle = -angle;
}
_glyphs.emplace_back(descRect, uvRect, texture, depth, color,angle);
}
void SpriteBatch::renderBatch(){
glBindVertexArray(_vao);
for (int i = 0; i < _renderBatches.size();i++){
glBindTexture(GL_TEXTURE_2D, _renderBatches[i].texture);
glDrawArrays(GL_TRIANGLES, _renderBatches[i].offset, _renderBatches[i].numVertices);
}
}
void SpriteBatch::createRenderBatches(){
std::vector<Vertex> vertices;
vertices.resize(_glyphsPointers.size() * 6);
if (_glyphsPointers.empty()){
return;
}
int offset = 0;
int cv = 0;//current vertex
_renderBatches.emplace_back(offset, 6, _glyphsPointers[0]->texture);
vertices[cv++] = _glyphsPointers[0]->topLeft;
vertices[cv++] = _glyphsPointers[0]->bottomLeft;
vertices[cv++] = _glyphsPointers[0]->bottomRight;
vertices[cv++] = _glyphsPointers[0]->bottomRight;
vertices[cv++] = _glyphsPointers[0]->topRight;
vertices[cv++] = _glyphsPointers[0]->topLeft;
offset += 6;
for (int cg = 1;/*current glyph*/cg < _glyphsPointers.size(); cg++){
if (_glyphsPointers[cg]->texture != _glyphsPointers[cg - 1]->texture){
_renderBatches.emplace_back(offset, 6, _glyphsPointers[cg]->texture);
}
else{
_renderBatches.back().numVertices += 6;
}
vertices[cv++] = _glyphsPointers[cg]->topLeft;
vertices[cv++] = _glyphsPointers[cg]->bottomLeft;
vertices[cv++] = _glyphsPointers[cg]->bottomRight;
vertices[cv++] = _glyphsPointers[cg]->bottomRight;
vertices[cv++] = _glyphsPointers[cg]->topRight;
vertices[cv++] = _glyphsPointers[cg]->topLeft;
offset += 6;
}
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), vertices.data(), GL_DYNAMIC_DRAW);
//upload the data
glBufferSubData(GL_ARRAY_BUFFER,0,vertices.size()*sizeof(Vertex),vertices.data());
glBindBuffer(GL_ARRAY_BUFFER,0) ;
}
void SpriteBatch::createVertexArray(){
if (_vao==0){
glGenVertexArrays(1, &_vao);
}
glBindVertexArray(_vao);
if (_vbo == 0){
glGenBuffers(1, &_vbo);
}
glBindBuffer(GL_ARRAY_BUFFER,_vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
//position attrib pointer
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, position));
//color attrib pointer
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void *)offsetof(Vertex, color));
//uv attrib pointer
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, uv));
glBindVertexArray(0);
}
void SpriteBatch::sortGlyphs(){
switch (_sortType)
{
case GlyphSortType::FRONT_TO_BACK:
std::stable_sort(_glyphsPointers.begin(), _glyphsPointers.end(), compareFrontToBack);
break;
case GlyphSortType::BACK_TO_FRONT:
std::stable_sort(_glyphsPointers.begin(), _glyphsPointers.end(), compareBackToFront);
break;
case GlyphSortType::TEXTURE:
std::stable_sort(_glyphsPointers.begin(), _glyphsPointers.end(), comapreTexture);
break;
default:
break;
}
//std::stable_sort()
}
bool SpriteBatch::compareFrontToBack(Glyph* a, Glyph* b){
return(a->depth < b->depth);
}
bool SpriteBatch::compareBackToFront(Glyph* a, Glyph* b){
return(a->depth > b->depth);
}
bool SpriteBatch::comapreTexture(Glyph* a, Glyph* b){
return(a->texture < b->texture);
}
}