FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
GLKeeper/src/EnvironmentMeshObject.cpp at main · codenamecpp/GLKeeper · GitHub
codenamecpp
/
GLKeeper
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
40
Code
Issues
0
Pull requests
0
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Security and quality
Insights
Expand file tree
Breadcrumbs
GLKeeper
/
src
/
EnvironmentMeshObject.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
337 lines (286 loc) · 9.69 KB
Breadcrumbs
GLKeeper
/
src
/
EnvironmentMeshObject.cpp
Copy path
File metadata and controls
337 lines (286 loc) · 9.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
stdafx.h
"
#
include
"
EnvironmentMeshObject.h
"
#
include
"
MapTile.h
"
#
include
"
MapUtils.h
"
EnvironmentMeshObject::EnvironmentMeshObject
()
{
}
void
EnvironmentMeshObject::ConfigureLava
()
{
mIsWater
=
false
;
const
float
DFLT_SURF_LEVEL
=
0
.
82f
;
const
float
DFLT_SURF_TRANSLUCENCY
=
1
.
0f
;
const
float
DFLT_SURF_WAVE_WIDTH
=
0
.
76f
;
const
float
DFLT_SURF_WAVE_HEIGHT
=
0
.
15f
;
const
float
DFLT_SURF_WAVE_FREQ
=
2
.
5f
;
Params params;
{
params.
mTranslucency
=
DFLT_SURF_TRANSLUCENCY
;
params.
mWaveWidth
=
DFLT_SURF_WAVE_WIDTH
;
params.
mWaveHeight
=
DFLT_SURF_WAVE_HEIGHT
;
params.
mWaveFreq
=
DFLT_SURF_WAVE_FREQ
;
params.
mWaterlineHeight
=
DFLT_SURF_LEVEL
;
}
//
debug info
mDebugColor
=
mIsWater
?
COLOR_CYAN
:
COLOR_ORANGE
;
ConfigureParams
(params);
}
void
EnvironmentMeshObject::ConfigureWater
()
{
mIsWater
=
true
;
const
float
DFLT_SURF_LEVEL
=
0
.
92f
;
const
float
DFLT_SURF_TRANSLUCENCY
=
0
.
65f
;
const
float
DFLT_SURF_WAVE_WIDTH
=
0
.
0f
;
const
float
DFLT_SURF_WAVE_HEIGHT
=
0
.
0f
;
const
float
DFLT_SURF_WAVE_FREQ
=
2
.
4f
;
Params params;
{
params.
mTranslucency
=
DFLT_SURF_TRANSLUCENCY
;
params.
mWaveWidth
=
DFLT_SURF_WAVE_WIDTH
;
params.
mWaveHeight
=
DFLT_SURF_WAVE_HEIGHT
;
params.
mWaveFreq
=
DFLT_SURF_WAVE_FREQ
;
params.
mWaterlineHeight
=
DFLT_SURF_LEVEL
;
}
//
debug info
mDebugColor
=
mIsWater
?
COLOR_CYAN
:
COLOR_ORANGE
;
ConfigureParams
(params);
}
void
EnvironmentMeshObject::ConfigureParams
(
const
Params& params)
{
mParams
= params;
}
void
EnvironmentMeshObject::ConfigureMapTiles
(cxx::span<MapTile*> mapTiles)
{
if
(mapTiles.
empty
())
{
RemoveMapTiles
();
return
;
}
//
remove old
const
size_t
prevSize =
mCoveredTiles
.
size
();
cxx::erase_if
(
mCoveredTiles
, [&mapTiles](
const
MapTile* srcTile)
{
return
!
cxx::contains
(mapTiles, srcTile);
});
if
(prevSize !=
mCoveredTiles
.
size
())
{
InvalidateMesh
();
}
//
add new
for
(MapTile* roller: mapTiles)
{
if
(!
cxx::contains
(
mCoveredTiles
, roller))
{
mCoveredTiles
.
push_back
(roller);
InvalidateMesh
();
}
}
RefreshLocalBounds
();
}
void
EnvironmentMeshObject::AppendMapTiles
(cxx::span<MapTile*> mapTiles)
{
for
(MapTile* roller: mapTiles)
{
if
(!
cxx::contains
(
mCoveredTiles
, roller))
{
mCoveredTiles
.
push_back
(roller);
InvalidateMesh
();
}
}
RefreshLocalBounds
();
}
void
EnvironmentMeshObject::RemoveMapTiles
(cxx::span<MapTile*> mapTiles)
{
//
remove old
const
size_t
prevSize =
mCoveredTiles
.
size
();
cxx::erase_if
(
mCoveredTiles
, [&mapTiles](
const
MapTile* srcTile)
{
return
!
cxx::contains
(mapTiles, srcTile);
});
if
(prevSize !=
mCoveredTiles
.
size
())
{
InvalidateMesh
();
}
RefreshLocalBounds
();
}
void
EnvironmentMeshObject::RemoveMapTiles
()
{
if
(
mCoveredTiles
.
empty
())
return
;
mCoveredTiles
.
clear
();
InvalidateMesh
();
RefreshLocalBounds
();
}
void
EnvironmentMeshObject::OnRecycle
()
{
SceneObject::OnRecycle
();
mMeshDirty
=
false
;
mCoveredTiles
.
clear
();
mMeshTris
.
clear
();
mMeshVerts
.
clear
();
mGpuVertexBuffer
.
reset
();
mGpuIndexBuffer
.
reset
();
}
void
EnvironmentMeshObject::UpdateFrame
(
float
deltaTime)
{
//
animate waves
mParams
.
mWaveTime
+=
mParams
.
mWaveFreq
* deltaTime;
}
void
EnvironmentMeshObject::RegisterForRendering
(SceneRenderLists& renderLists,
float
distanceToCamera2)
{
if
(
mCoveredTiles
.
empty
())
return
;
eRenderPass targetPass = (
mParams
.
mTranslucency
<
1
.
0f
) ? eRenderPass_Translucent : eRenderPass_Opaque;
renderLists.
Register
(targetPass,
this
, distanceToCamera2);
}
void
EnvironmentMeshObject::PrepareRenderdata
()
{
if
(!
mMeshDirty
)
return
;
mMeshDirty
=
false
;
ReBuildMesh
();
if
(
mMeshTris
.
empty
() ||
mMeshVerts
.
empty
())
return
;
//
uploading tris to gpu
const
unsigned
int
Sizeof_Triangle =
sizeof
(glm::ivec3);
const
unsigned
int
TrisBufferLength = Sizeof_Triangle *
mMeshTris
.
size
();
//
allocate new
if
(!
mGpuIndexBuffer
)
{
mGpuIndexBuffer
=
gRenderDevice
.
CreateIndexBuffer
(eBufferUsage_Static, TrisBufferLength);
cxx_assert
(
mGpuIndexBuffer
);
if
(
mGpuIndexBuffer
&& !
mGpuIndexBuffer
->
SubData
(
0
, TrisBufferLength,
mMeshTris
.
data
()))
{
cxx_assert
(
false
);
}
}
//
refresh
else
if
(!
mGpuIndexBuffer
->
Create
(eBufferUsage_Static, TrisBufferLength,
mMeshTris
.
data
()))
{
cxx_assert
(
false
);
}
//
uploading verts to gpu
const
unsigned
int
VertsBufferLength = Sizeof_WaterLavaVertex *
mMeshVerts
.
size
();
//
allocate new
if
(!
mGpuVertexBuffer
)
{
mGpuVertexBuffer
=
gRenderDevice
.
CreateVertexBuffer
(eBufferUsage_Static, VertsBufferLength);
if
(
mGpuVertexBuffer
&&
mGpuVertexBuffer
->
SubData
(
0
, VertsBufferLength,
mMeshVerts
.
data
()))
{
mGpuVertexBuffer
->
ConfigureVertexDefinitions
(
WaterLavaVertex3D_Format::Get
());
}
else
{
cxx_assert
(
false
);
}
}
//
refresh
else
if
(!
mGpuVertexBuffer
->
Create
(eBufferUsage_Static, VertsBufferLength,
mMeshVerts
.
data
()))
{
cxx_assert
(
false
);
}
}
void
EnvironmentMeshObject::RefreshLocalBounds
()
{
bool
wasActive =
IsObjectActive
();
//
force apply bounds
SetObjectActive
(
false
);
cxx::aabbox bounds;
bounds.
set_to_zero
();
if
(!
mCoveredTiles
.
empty
())
{
MapPoint2D minTilePos =
mCoveredTiles
.
front
()->
mLocation
;
MapPoint2D maxTilePos =
mCoveredTiles
.
front
()->
mLocation
;
for
(MapTile* currentTile:
mCoveredTiles
)
{
minTilePos.
x
=
std::min
(minTilePos.
x
, currentTile->
mLocation
.
x
);
minTilePos.
y
=
std::min
(minTilePos.
y
, currentTile->
mLocation
.
y
);
maxTilePos.
x
=
std::max
(maxTilePos.
x
, currentTile->
mLocation
.
x
);
maxTilePos.
y
=
std::max
(maxTilePos.
y
, currentTile->
mLocation
.
y
);
}
bounds =
MapUtils::ComputeBlockBounds
(minTilePos);
bounds.
extend
(
MapUtils::ComputeBlockBounds
(maxTilePos));
}
SetLocalBoundingBox
(bounds);
SetObjectActive
(wasActive);
}
void
EnvironmentMeshObject::InvalidateMesh
()
{
mMeshDirty
=
true
;
}
void
EnvironmentMeshObject::ReBuildMesh
()
{
//
process data
mMeshTris
.
clear
();
mMeshVerts
.
clear
();
if
(
mCoveredTiles
.
empty
())
return
;
//
generate geometry
for
(MapTile* tile :
mCoveredTiles
)
{
const
MapPoint2D& mapPos = tile->
mLocation
;
const
glm::vec3 middlep = { mapPos.
x
*
1
.
0f
,
0
.
0f
, mapPos.
y
*
1
.
0f
};
const
glm::vec3 positions[
9
] = {
{middlep.
x
-
MAP_TILE_HALF_SIZE
, middlep.
y
, middlep.
z
-
MAP_TILE_HALF_SIZE
},
{middlep.
x
, middlep.
y
, middlep.
z
-
MAP_TILE_HALF_SIZE
},
{middlep.
x
+
MAP_TILE_HALF_SIZE
, middlep.
y
, middlep.
z
-
MAP_TILE_HALF_SIZE
},
{middlep.
x
-
MAP_TILE_HALF_SIZE
, middlep.
y
, middlep.
z
},
middlep,
{middlep.
x
+
MAP_TILE_HALF_SIZE
, middlep.
y
, middlep.
z
},
{middlep.
x
-
MAP_TILE_HALF_SIZE
, middlep.
y
, middlep.
z
+
MAP_TILE_HALF_SIZE
},
{middlep.
x
, middlep.
y
, middlep.
z
+
MAP_TILE_HALF_SIZE
},
{middlep.
x
+
MAP_TILE_HALF_SIZE
, middlep.
y
, middlep.
z
+
MAP_TILE_HALF_SIZE
},
};
const
glm::vec2 tcoordsp = { mapPos.
x
*
1
.
0f
, mapPos.
y
*
1
.
0f
};
const
glm::vec2 texturecoords[
9
] = {
tcoordsp,
{tcoordsp.
x
+
0
.
5f
, tcoordsp.
y
},
{tcoordsp.
x
+
1
.
0f
, tcoordsp.
y
},
{tcoordsp.
x
, tcoordsp.
y
+
0
.
5f
},
{tcoordsp.
x
+
0
.
5f
, tcoordsp.
y
+
0
.
5f
},
{tcoordsp.
x
+
1.0
, tcoordsp.
y
+
0
.
5f
},
{tcoordsp.
x
, tcoordsp.
y
+
1
.
0f
},
{tcoordsp.
x
+
0
.
5f
, tcoordsp.
y
+
1
.
0f
},
{tcoordsp.
x
+
1
.
0f
, tcoordsp.
y
+
1
.
0f
},
};
int32_t
tile_vert_indices[
9
];
//
process vertices
for
(
int
ipoint =
0
; ipoint <
9
; ++ipoint)
{
const
glm::vec3& pointPos = positions[ipoint];
//
find same vertex
//
todo: optimize
int32_t
vidx =
cxx::get_first_index_if
(
mMeshVerts
, [&pointPos](
const
WaterLavaVertex& v)
{
return
v.
mPosition
== pointPos;
});
if
(vidx == -
1
)
{
//
allocate new
tile_vert_indices[ipoint] =
static_cast
<
int32_t
>(
mMeshVerts
.
size
());
WaterLavaVertex& meshVertex =
mMeshVerts
.
emplace_back
();
meshVertex.
mPosition
= positions[ipoint];
meshVertex.
mTexcoord
= texturecoords[ipoint];
}
else
{
//
reuse vertex
tile_vert_indices[ipoint] = vidx;
}
}
//
create triangles
const
glm::ivec3 pointindices[
8
] = {
{
3
,
4
,
0
}, {
4
,
1
,
0
},
//
1
{
4
,
2
,
1
}, {
4
,
5
,
2
},
//
2
{
6
,
4
,
3
}, {
6
,
7
,
4
},
//
3
{
7
,
8
,
4
}, {
8
,
5
,
4
},
//
4
};
for
(
const
glm::ivec3& pointindex : pointindices)
{
mMeshTris
.
emplace_back
(
tile_vert_indices[pointindex.
x
],
tile_vert_indices[pointindex.
y
],
tile_vert_indices[pointindex.
z
]);
}
}
int
bp =
0
;
}
Back
|
FazBrowse Home
|
New Git URL