/
SeaPatch.cpp
152 lines (128 loc) · 3.25 KB
/
SeaPatch.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
#include "SeaPatch.h"
using namespace std;
using namespace chr;
SeaPatch::SeaPatch(SeaSurface *surface)
:
surface(surface),
gridBatch(GL_LINES, vertexBuffer, gridIndexBuffer),
fillBatch(GL_TRIANGLES, vertexBuffer, fillIndexBuffer)
{}
SeaPatch::~SeaPatch()
{
delete[] samples;
}
void SeaPatch::setFrontFace(GLenum mode)
{
frontFace = mode;
}
void SeaPatch::setup(float ox, float oy, float w, float h, int gridSize, int mode)
{
this->ox = ox;
this->oy = oy;
this->gridSize = gridSize;
nx = w / gridSize;
ny = h / gridSize;
// ---
samples = new SeaSurface::Sample[nx * ny];
for (int iy = 0; iy < ny; iy++)
{
float yy = oy + iy * gridSize;
for (int ix = 0; ix < nx; ix++)
{
float xx = ox + ix * gridSize;
int i = ix + iy * nx;
surface->sample(xx, yy, samples[i]);
}
}
// ---
auto &gridIndices = gridIndexBuffer->storage;
gridIndices.clear();
gridIndices.reserve(ny * (nx - 1) * 2 * ((mode & MODE_GRID_H) ? 1 : 0) + nx * (ny - 1) * 2 * ((mode & MODE_GRID_V) ? 1 : 0));
if (mode & MODE_GRID_H)
{
for (int iy = 0; iy < ny; iy++)
{
for (int ix = 0; ix < nx - 1; ix++)
{
gridIndices.push_back(iy * nx + ix);
gridIndices.push_back(iy * nx + ix + 1);
}
}
}
if (mode & MODE_GRID_V)
{
for (int ix = 0; ix < nx; ix++)
{
for (int iy = 0; iy < ny - 1; iy++)
{
gridIndices.push_back(iy * nx + ix);
gridIndices.push_back((iy + 1) * nx + ix);
}
}
}
if ((mode & MODE_GRID_H) || (mode & MODE_GRID_V))
{
gridIndexBuffer.requestUpload();
}
// ---
if (mode & MODE_FILL)
{
auto &fillIndices = fillIndexBuffer->storage;
fillIndices.clear();
fillIndices.reserve((nx - 1) * (ny - 1) * 6);
fillIndexBuffer.requestUpload();
for (int iy = 0; iy < ny - 1; iy++)
{
for (int ix = 0; ix < nx - 1; ix++)
{
if (frontFace == CCW)
{
fillIndices.push_back(iy * nx + ix);
fillIndices.push_back(iy * nx + ix + 1);
fillIndices.push_back((iy + 1) * nx + ix + 1);
fillIndices.push_back((iy + 1) * nx + ix + 1);
fillIndices.push_back((iy + 1) * nx + ix);
fillIndices.push_back(iy * nx + ix);
}
else
{
fillIndices.push_back(iy * nx + ix);
fillIndices.push_back((iy + 1) * nx + ix);
fillIndices.push_back((iy + 1) * nx + ix + 1);
fillIndices.push_back((iy + 1) * nx + ix + 1);
fillIndices.push_back(iy * nx + ix + 1);
fillIndices.push_back(iy * nx + ix);
}
}
}
}
}
void SeaPatch::update(float t, float swellFactor)
{
auto &vertices = vertexBuffer->storage;
vertices.clear();
vertices.reserve(nx * ny);
vertexBuffer.requestUpload();
for (int iy = 0; iy < ny; iy++)
{
float yy = oy + iy * gridSize;
for (int ix = 0; ix < nx; ix++)
{
float xx = ox + ix * gridSize;
int i = ix + iy * nx;
vertices.push_back(glm::vec3(xx, yy, 0) + surface->getDisplacement(samples[i], t, swellFactor));
}
}
}
void SeaPatch::drawFill(const glm::vec4 &color)
{
fillBatch
.setShaderColor(color)
.flush();
}
void SeaPatch::drawGrid(const glm::vec4 &color)
{
gridBatch
.setShaderColor(color)
.flush();
}