forked from spring/spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeightMapTexture.cpp
More file actions
103 lines (73 loc) · 2.35 KB
/
HeightMapTexture.cpp
File metadata and controls
103 lines (73 loc) · 2.35 KB
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "HeightMapTexture.h"
#include "ReadMap.h"
#include "Rendering/GlobalRendering.h"
#include "System/EventHandler.h"
#include "System/Rectangle.h"
#include "System/TimeProfiler.h"
#include <cstring>
HeightMapTexture* heightMapTexture = nullptr;
HeightMapTexture::HeightMapTexture(): CEventClient("[HeightMapTexture]", 2718965, false)
{
eventHandler.AddClient(this);
Init();
}
HeightMapTexture::~HeightMapTexture()
{
Kill();
eventHandler.RemoveClient(this);
}
void HeightMapTexture::Init()
{
assert(readMap != nullptr);
// corner-heightmap dimensions
xSize = mapDims.mapxp1;
ySize = mapDims.mapyp1;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, xSize, ySize, 0, GL_RED, GL_FLOAT, readMap->GetCornerHeightMapUnsynced());
glBindTexture(GL_TEXTURE_2D, 0);
}
void HeightMapTexture::Kill()
{
glDeleteTextures(1, &texID);
texID = 0;
xSize = 0;
ySize = 0;
for (PBO& pbo: pbos) {
pbo.Release();
}
}
void HeightMapTexture::UnsyncedHeightMapUpdate(const SRectangle& rect)
{
if (texID == 0)
return;
SCOPED_TIMER("Update::HeightMapTexture");
// the upper bounds of UHM rectangles are clamped to
// map{x,y}; valid for indexing the corner heightmap
const int sizeX = rect.GetWidth() + 1;
const int sizeZ = rect.GetHeight() + 1;
assert(sizeX <= xSize);
assert(sizeZ <= ySize);
// RR update policy
PBO& pbo = pbos[globalRendering->drawFrame % 3];
pbo.Bind();
pbo.New(sizeX * sizeZ * sizeof(float));
const float* heightMap = readMap->GetCornerHeightMapUnsynced();
float* heightBuf = reinterpret_cast<float*>(pbo.MapBuffer());
if (heightBuf != nullptr) {
for (int z = 0; z < sizeZ; z++) {
const void* src = heightMap + rect.x1 + (z + rect.z1) * xSize;
void* dst = heightBuf + (z ) * sizeX;
memcpy(dst, src, sizeX * sizeof(float));
}
}
pbo.UnmapBuffer();
glBindTexture(GL_TEXTURE_2D, texID);
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x1, rect.z1, sizeX, sizeZ, GL_RED, GL_FLOAT, pbo.GetPtr());
pbo.Unbind();
}