-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeometryCache.cpp
245 lines (182 loc) · 6.67 KB
/
GeometryCache.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <sfcg/GeometryCache.hpp>
#include <SFML/Graphics/PrimitiveType.hpp>
#include <SFML/Graphics/Vertex.hpp>
#include <sfcg/GLCheck.hpp>
#include <cmath>
static const char *BASE_VERTEX_SHADER_SOURCE = R"(
#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoord;
out vec2 frag_texCoord;
out vec4 frag_vertexColor;
uniform mat4 sfcg_modelViewMatrix;
uniform mat4 sfcg_projectionMatrix;
void main()
{
gl_Position = sfcg_projectionMatrix * sfcg_modelViewMatrix * vec4(position.x, position.y, 0.0f, 1.0f);
frag_texCoord = texCoord;
frag_vertexColor = color;
}
)";
static const char *BASE_FRAGMENT_SHADER_SOURCE = R"(
#version 330 core
layout (location = 0) out vec4 fragColor;
in vec2 frag_texCoord;
uniform vec4 sfcg_color;
uniform sampler2D sfcg_texture;
void main()
{
fragColor = texture(sfcg_texture, frag_texCoord) * sfcg_color;
}
)";
static const char *TEXT_FRAGMENT_SHADER_SOURCE = R"(
#version 330 core
layout (location = 0) out vec4 fragColor;
in vec2 frag_texCoord;
in vec4 frag_vertexColor;
uniform vec4 sfcg_color;
uniform sampler2D sfcg_texture;
void main()
{
vec4 sampled = texture(sfcg_texture, frag_texCoord);
fragColor = sampled * frag_vertexColor;
}
)";
namespace sfcg
{
GeometryCache *GeometryCache::m_instance = nullptr;
GeometryCache::GeometryCache()
{
if (m_instance)
{
delete m_instance;
}
m_instance = this;
m_unitRectangleVao = 0;
m_unitCircleVertexBuffers.reserve(4);
m_unitCircleVaos.reserve(4);
sf::Image whitePixel;
whitePixel.create(1, 1, sf::Color::White);
m_whitePixelTexture.loadFromImage(whitePixel);
}
GeometryCache::~GeometryCache()
{
m_instance = nullptr;
glCheck(glDeleteVertexArrays(1, &m_unitRectangleVao));
for (auto &pair : m_unitCircleVaos)
{
glCheck(glDeleteVertexArrays(1, &pair.second));
}
m_unitCircleVaos.clear();
}
GeometryCache &GeometryCache::getInstance()
{
return *m_instance;
}
const sfcg::VertexBuffer &GeometryCache::getUnitCircleVertexBuffer(int pointCount)
{
auto it = m_unitCircleVertexBuffers.find(pointCount);
if (it != m_unitCircleVertexBuffers.end())
{
return *it->second;
}
// Create the unit circle vertices (+2 for center and last point to close the circle)
sf::Vertex vertices[pointCount + 2];
vertices[0] = sf::Vertex(sf::Vector2f(0.f, 0.f));
for (int i = 0; i < pointCount; i++)
{
float angle = static_cast<float>(i) * 2.f * M_PI / static_cast<float>(pointCount) - M_PI / 2.f;
float x = std::cos(angle);
float y = std::sin(angle);
vertices[i + 1] = sf::Vertex(sf::Vector2f(x, y));
}
// Last point always finishes the circle at this position
vertices[pointCount + 1] = sf::Vertex(sf::Vector2f(0, -1));
sfcg::VertexBuffer *vertexBuffer = new sfcg::VertexBuffer();
m_unitCircleVertexBuffers.insert(std::make_pair(pointCount, vertexBuffer));
m_unitCircleVertexBuffers[pointCount]->setPrimitiveType(sf::PrimitiveType::TriangleFan);
m_unitCircleVertexBuffers[pointCount]->setUsage(sfcg::VertexBuffer::Usage::Static);
m_unitCircleVertexBuffers[pointCount]->create(pointCount + 2);
m_unitCircleVertexBuffers[pointCount]->update(vertices);
GLuint unitCircleVao = 0;
glCheck(glGenVertexArrays(1, &unitCircleVao));
glCheck(glBindVertexArray(unitCircleVao));
m_unitCircleVertexBuffers[pointCount]->bind();
configureVaoAttributesForVertices();
glCheck(glBindVertexArray(0));
m_unitCircleVaos.insert(std::make_pair(pointCount, unitCircleVao));
return *m_unitCircleVertexBuffers[pointCount];
}
GLuint GeometryCache::getUnitCircleVao(int pointCount)
{
return m_unitCircleVaos[pointCount];
}
const sfcg::VertexBuffer &GeometryCache::getUnitRectangleVertexBuffer()
{
// Use cache if needed
if (m_unitRectangleVertexBuffer.getNativeHandle())
{
return m_unitRectangleVertexBuffer;
}
sf::Vertex vertices[] = {
sf::Vertex(sf::Vector2f(0.0f, 0.0f)),
sf::Vertex(sf::Vector2f(1.0f, 0.0f)),
sf::Vertex(sf::Vector2f(1.0f, 1.0f)),
sf::Vertex(sf::Vector2f(0.0f, 1.0f))};
// Create VBO for unit rectangle and fill it
m_unitRectangleVertexBuffer.setPrimitiveType(sf::PrimitiveType::TrianglesFan);
m_unitRectangleVertexBuffer.setUsage(sfcg::VertexBuffer::Usage::Static);
m_unitRectangleVertexBuffer.create(4);
m_unitRectangleVertexBuffer.update(vertices);
// The VAO
glCheck(glGenVertexArrays(1, &m_unitRectangleVao));
glCheck(glBindVertexArray(m_unitRectangleVao));
m_unitRectangleVertexBuffer.bind();
configureVaoAttributesForVertices();
glCheck(glBindVertexArray(0));
return m_unitRectangleVertexBuffer;
}
const Shader *GeometryCache::getBaseShader()
{
if (m_baseShader.getNativeHandle())
{
return &m_baseShader;
}
m_baseShader.loadFromMemory(
BASE_VERTEX_SHADER_SOURCE,
BASE_FRAGMENT_SHADER_SOURCE);
return &m_baseShader;
}
const Shader *GeometryCache::getTextShader()
{
if (m_textShader.getNativeHandle())
{
return &m_textShader;
}
m_textShader.loadFromMemory(
BASE_VERTEX_SHADER_SOURCE,
TEXT_FRAGMENT_SHADER_SOURCE);
return &m_textShader;
}
void GeometryCache::configureVaoAttributesForVertices()
{
// Position
glCheck(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(sf::Vertex), (void *)0));
glCheck(glEnableVertexAttribArray(0));
// Color
glCheck(glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(sf::Vertex), (void *)sizeof(sf::Vector2f)));
glCheck(glEnableVertexAttribArray(1));
// Texture Coord
glCheck(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(sf::Vertex), (void *)(sizeof(sf::Vector2f) + sizeof(sf::Color))));
glCheck(glEnableVertexAttribArray(2));
}
GLuint GeometryCache::getUnitRectangleVao()
{
return m_unitRectangleVao;
}
const sf::Texture &GeometryCache::getWhitePixelTexture()
{
return m_whitePixelTexture;
}
} // namespace sfcg