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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#include "HMPlane.h"
#include "Renderer.h"
#include "VertexBuffer.h"
#include "VertexBufferLayout.h"
#include "IndexBuffer.h"
#include "VertexArray.h"
#include "Shader.h"
#include "OpenGLText.h"
#include <wx/msgdlg.h>
HMPlane::HMPlane(Shader* shader, Shader* labelShader, const float& width, const float& height, const float limits[2])
: m_width(width), m_height(height), m_shader(shader), m_labelShader(labelShader)
{
// Fill mesh coords
for (auto accHeight = 0; accHeight <= m_height + m_meshSize; accHeight += m_meshSize) {
std::vector<BufferMeshCoords*> line;
for (auto accWidth = 0; accWidth <= m_width + m_meshSize; accWidth += m_meshSize) {
auto* bmc = new BufferMeshCoords;
bmc->x = accWidth;
bmc->y = accHeight;
bmc->z = 0.0f;
line.emplace_back(bmc);
if (accHeight < 0.1f) m_meshTickX++;
}
m_meshTickY++;
m_coords.emplace_back(line);
}
m_limits[0] = limits[0];
m_limits[1] = limits[1];
FillCoordsBuffer();
FillIndexBuffer();
BindOpenGLBuffers();
CreateLabel();
}
HMPlane::~HMPlane()
{
delete m_ib;
delete m_vb;
delete m_layout;
delete m_va;
delete m_ibL;
delete m_vbL;
delete m_layoutL;
delete m_vaL;
for (auto glText : m_glTexts) {
delete glText;
}
m_glTexts.clear();
for (const auto& line : m_coords) {
for (auto* bmv : line) {
delete bmv;
}
}
m_coords.clear();
}
void HMPlane::Draw(const Renderer& renderer, const glm::mat4& projectionViewMatrix) const
{
//const glm::mat4 mvp = projectionViewMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(m_width / 2.0f, m_height / 2.0f, 0.0f));
const glm::mat4 mvp = projectionViewMatrix;
m_shader->Bind();
//m_shader->SetUniform1f("u_scale", m_scale);
m_shader->SetUniformMatrix4fv("u_mvpMatrix", mvp);
m_va->UpdateBuffer(*m_vb, m_bufferCoords.data(), m_bufferCoords.size() * sizeof(float));
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
renderer.Draw(*m_va, *m_ib, *m_shader);
// Unbind
m_va->Unbind();
m_vb->Unbind();
m_ib->Unbind();
m_shader->Unbind();
}
void HMPlane::DrawLabel(const Renderer& renderer, const glm::mat4& projectionViewMatrix, const float& x, const float& y) const
{
const glm::mat4 mvp = projectionViewMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(x, y, 0.0f));
//const glm::mat4 mvp = projectionViewMatrix;
m_labelShader->Bind();
//m_shader->SetUniform1f("u_scale", m_scale);
m_labelShader->SetUniformMatrix4fv("u_mvpMatrix", mvp);
m_labelShader->SetUniform4f("u_offset", x, y, m_width, m_height);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
renderer.Draw(*m_vaL, *m_ibL, *m_labelShader);
// Unbind
m_vaL->Unbind();
m_vbL->Unbind();
m_ibL->Unbind();
m_labelShader->Unbind();
glColor4d(0.0, 0.0, 0.0, 1.0);
m_glTexts[0]->Draw(wxPoint2DDouble(x + 35.0 + m_glTexts[0]->GetWidth() / 2.0, y));
m_glTexts[1]->Draw(wxPoint2DDouble(x + 35.0 + m_glTexts[1]->GetWidth() / 2.0, y + 150.0));
m_glTexts[2]->Draw(wxPoint2DDouble(x + 35.0 + m_glTexts[2]->GetWidth() / 2.0, y + 300.0));
}
void HMPlane::SetLabelLimits(const float& min, const float& max)
{
m_limits[0] = max;
m_limits[1] = min;
m_glTexts[0]->SetText(wxString::Format("%.2f p.u.", m_limits[0]));
m_glTexts[1]->SetText(wxString::Format("%.2f p.u.", (m_limits[0] + m_limits[1]) / 2.0f));
m_glTexts[2]->SetText(wxString::Format("%.2f p.u.", m_limits[1]));
}
void HMPlane::SetRectSlope(const wxRect2DDouble& rect, const float& angle, const float& depth)
{
for (const auto& line : m_coords) {
for (auto* coord : line) {
wxPoint2DDouble pt(coord->x, coord->y);
if (std::abs(angle) > 0.01) {
wxPoint2DDouble rotPt;
rotPt.m_x = cos(angle) * (pt.m_x - rect.GetCentre().m_x) + sin(angle) * (pt.m_y - rect.GetCentre().m_y) + rect.GetCentre().m_x;
rotPt.m_y = sin(angle) * (pt.m_x - rect.GetCentre().m_x) - cos(angle) * (pt.m_y - rect.GetCentre().m_y) + rect.GetCentre().m_y;
pt = rotPt;
}
if (rect.Contains(pt)) {
coord->z += depth;
if (coord->z > 1.0f) coord->z = 1.0f;
if (coord->z < -1.0f) coord->z = -1.0f;
}
}
}
m_isClear = false;
//FillCoordsBuffer();
//SmoothPlane();
}
void HMPlane::Resize(const float& width, const float& height)
{
//Clear();
m_width = width;
m_height = height;
m_meshTickX = 0;
m_meshTickY = 0;
for (const auto& line : m_coords) {
for (auto* bmv : line) {
delete bmv;
}
}
m_coords.clear();
// Fill mesh coords
for (auto accHeight = 0; accHeight < m_height + m_meshSize; accHeight += m_meshSize) {
std::vector<BufferMeshCoords*> line;
for (auto accWidth = 0; accWidth < m_width + m_meshSize; accWidth += m_meshSize) {
auto* bmc = new BufferMeshCoords;
bmc->x = accWidth;
bmc->y = accHeight;
bmc->z = 0.0f;
line.emplace_back(bmc);
if (accHeight < 0.1f) m_meshTickX++;
}
m_meshTickY++;
m_coords.emplace_back(line);
}
FillCoordsBuffer();
FillIndexBuffer();
BindOpenGLBuffers();
//m_va->Bind();
//m_vb->Bind();
//
//delete m_ib;
//m_ib = new IndexBuffer(m_indexBuffer.data(), m_indexBuffer.size());
//m_ib->Unbind();
m_isClear = false;
}
void HMPlane::SmoothPlane(const unsigned int& iterations)
{
const int maxTickX = static_cast<int>(m_meshTickX);
const int maxTickY = static_cast<int>(m_meshTickY);
std::vector< std::vector<BufferMeshCoords> > tmpCoords;
for (const auto& line : m_coords) {
std::vector<BufferMeshCoords> tmpCoordsLine;
for (auto* bmv : line) {
tmpCoordsLine.push_back(*bmv);
}
tmpCoords.push_back(tmpCoordsLine);
}
//Mean Blur
//for (int i = 0; i < maxTick; ++i) {
// for (int j = 0; j < maxTick; ++j) {
// // Get the 8 neighbors and smooth z
// float mid = 0.0f;
// float div = 0.0f;
//
// for (int ii = i - 1; ii <= i + 1; ++ii) {
// for (int jj = j - 1; jj <= j + 1; ++jj) {
//
// if (ii >= 0 && ii < maxTick && jj >= 0 && jj < maxTick) {
// mid += tmpCoords[ii][jj].z;
// div += 1.0;
// }
// }
// }
// m_coords[i][j]->z = mid / div;
// }
//}
// Gaussian Blur
float gaussianKernel[5][5] =
{
{1.0f / 256.0f, 4.0f / 256.0f, 6 / 256.0f, 4.0f / 256, 1.0f / 256.0f},
{4.0f / 256.0f, 16.0f / 256.0f, 24 / 256.0f, 16.0f / 256, 4.0f / 256.0f},
{6.0f / 256.0f, 24.0f / 256.0f, 36 / 256.0f, 24.0f / 256, 6.0f / 256.0f},
{4.0f / 256.0f, 16.0f / 256.0f, 24 / 256.0f, 16.0f / 256, 4.0f / 256.0f},
{1.0f / 256.0f, 4.0f / 256.0f, 6 / 256.0f, 4.0f / 256, 1.0f / 256.0f}
};
for (unsigned int it = 0; it < iterations; ++it) {
for (int i = 0; i < m_meshTickY; ++i) {
for (int j = 0; j < m_meshTickX; ++j) {
// Get the 24 neighbors and smooth z
float value = 0.0f;
for (int ii = i - 2; ii <= i + 2; ++ii) {
for (int jj = j - 2; jj <= j + 2; ++jj) {
if (ii >= 0 && ii < m_meshTickY && jj >= 0 && jj < m_meshTickX) {
value += tmpCoords[ii][jj].z * gaussianKernel[ii - i + 2][jj - j + 2];
}
}
}
m_coords[i][j]->z = value;
}
}
if (it < iterations - 1) {
for (int i = 0; i < m_meshTickY; ++i) {
for (int j = 0; j < m_meshTickX; ++j) {
tmpCoords[i][j].z = m_coords[i][j]->z;
}
}
}
}
FillCoordsBuffer();
m_isClear = false;
}
void HMPlane::Clear()
{
if (!m_isClear) {
for (const auto& line : m_coords) {
for (auto* bmv : line) {
bmv->z = 0.0f;
}
}
FillCoordsBuffer();
m_isClear = true;
}
}
void HMPlane::FillCoordsBuffer()
{
m_bufferCoords.clear();
for (const auto& line : m_coords) {
for (auto* bmv : line) {
m_bufferCoords.push_back(bmv->x);
m_bufferCoords.push_back(bmv->y);
m_bufferCoords.push_back(bmv->z);
}
}
}
void HMPlane::FillIndexBuffer()
{
m_indexBuffer.clear();
for (auto i = 0; i < m_meshTickY - 1; ++i) {
for (auto j = 0; j < m_meshTickX - 1; ++j) {
m_indexBuffer.push_back(i * m_meshTickX + j);
m_indexBuffer.push_back(i * m_meshTickX + j + 1);
m_indexBuffer.push_back((i + 1) * m_meshTickX + j + 1);
m_indexBuffer.push_back(i * m_meshTickX + j);
m_indexBuffer.push_back((i + 1) * m_meshTickX + j);
m_indexBuffer.push_back((i + 1) * m_meshTickX + j + 1);
}
}
}
void HMPlane::BindOpenGLBuffers()
{
delete m_va;
delete m_vb;
delete m_ib;
// Generate vertex array (this will bind vertex buffer with its layout)
m_va = new VertexArray();
// Generate vertex buffer
m_vb = new VertexBuffer(m_bufferCoords.data(), m_bufferCoords.size() * sizeof(float), GL_DYNAMIC_DRAW);
m_layout = new VertexBufferLayout();
m_layout->Push<float>(3); // 3 = each triplet from array set the coords -> [x1, y1, z1, x2, y2, z2, ...]
m_va->AddBuffer(*m_vb, *m_layout); // Add buffer and bind vertex array
// Set index buffer
m_ib = new IndexBuffer(m_indexBuffer.data(), m_indexBuffer.size());
// Unbind
m_va->Unbind();
m_vb->Unbind();
m_ib->Unbind();
}
void HMPlane::CreateLabel()
{
float vertexBufferLabel[4 * 3] =
{
0.0f, 0.0f, 1.0f,
30.0f, 0.0f, 1.0f,
0.0f, 300.0f, -1.0f,
30.0f, 300.0f, -1.0f
};
std::copy(vertexBufferLabel, vertexBufferLabel + 3 * 4, m_vertexBufferLabel);
unsigned int indexBufferLabel[6] =
{
0, 1, 2,
1, 2, 3
};
std::copy(indexBufferLabel, indexBufferLabel + 6, m_indexBufferLabel);
delete m_vaL;
delete m_vbL;
delete m_ibL;
// Generate vertex array (this will bind vertex buffer with its layout)
m_vaL = new VertexArray();
// Generate vertex buffer
m_vbL = new VertexBuffer(m_vertexBufferLabel, 4 * 3 * sizeof(float), GL_STATIC_DRAW);
m_layoutL = new VertexBufferLayout();
m_layoutL->Push<float>(3); // 3 = each triplet from array set the coords -> [x1, y1, z1, x2, y2, z2, ...]
m_vaL->AddBuffer(*m_vbL, *m_layoutL); // Add buffer and bind vertex array
// Set index buffer
m_ibL = new IndexBuffer(m_indexBufferLabel, 6);
// Unbind
m_vaL->Unbind();
m_vbL->Unbind();
m_ibL->Unbind();
m_glTexts.emplace_back(new OpenGLText(wxString::Format("%.2f p.u.", m_limits[0])));
m_glTexts.emplace_back(new OpenGLText(wxString::Format("%.2f p.u.", (m_limits[0] + m_limits[1]) / 2.0)));
m_glTexts.emplace_back(new OpenGLText(wxString::Format("%.2f p.u.", m_limits[1])));
}
|