This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
forked from KeckCAVES/SARndbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElevationColorMap.cpp
173 lines (145 loc) · 6.55 KB
/
ElevationColorMap.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
/***********************************************************************
ElevationColorMap - Class to represent elevation color maps for
topographic maps.
Copyright (c) 2014-2018 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "ElevationColorMap.h"
#include <string>
#include <Misc/ThrowStdErr.h>
#include <Misc/FileNameExtensions.h>
#include <IO/ValueSource.h>
#include <IO/OpenFile.h>
#include <GL/gl.h>
#include <GL/GLContextData.h>
#include <GL/Extensions/GLARBShaderObjects.h>
#include "Types.h"
#include "DepthImageRenderer.h"
#include "Config.h"
/**********************************
Methods of class ElevationColorMap:
**********************************/
ElevationColorMap::ElevationColorMap(const char* heightMapName) {
/* Load the given height map: */
load(heightMapName);
}
void ElevationColorMap::initContext(GLContextData& contextData) const {
/* Initialize required OpenGL extensions: */
GLARBShaderObjects::initExtension();
/* Create the data item and associate it with this object: */
DataItem* dataItem = new DataItem;
contextData.addDataItem(this, dataItem);
}
void ElevationColorMap::load(const char* heightMapName) {
/* Open the height map file: */
std::string fullHeightMapName;
if (heightMapName[0] == '/') {
/* Use the absolute file name directly: */
fullHeightMapName = heightMapName;
} else {
/* Assemble a file name relative to the configuration file directory: */
fullHeightMapName = CONFIG_CONFIGDIR;
fullHeightMapName.push_back('/');
fullHeightMapName.append(heightMapName);
}
/* Open the height map file: */
IO::ValueSource heightMapSource(IO::openFile(fullHeightMapName.c_str()));
/* Load the height color map: */
std::vector<Color> heightMapColors;
std::vector<GLdouble> heightMapKeys;
if (Misc::hasCaseExtension(heightMapName, ".cpt")) {
heightMapSource.setPunctuation("\n");
heightMapSource.skipWs();
int line = 1;
while (!heightMapSource.eof()) {
/* Read the next color map key value: */
heightMapKeys.push_back(GLdouble(heightMapSource.readNumber()));
/* Read the next color map color value: */
Color color;
for (int i = 0; i < 3; ++i) {
color[i] = Color::Scalar(heightMapSource.readNumber() / 255.0);
}
color[3] = Color::Scalar(1);
heightMapColors.push_back(color);
if (!heightMapSource.isLiteral('\n')) {
Misc::throwStdErr("ElevationColorMap: Color map format error in line %d of file %s", line,
fullHeightMapName.c_str());
}
++line;
}
} else {
heightMapSource.setPunctuation(",\n");
heightMapSource.skipWs();
int line = 1;
while (!heightMapSource.eof()) {
/* Read the next color map key value: */
heightMapKeys.push_back(GLdouble(heightMapSource.readNumber()));
if (!heightMapSource.isLiteral(',')) {
Misc::throwStdErr("ElevationColorMap: Color map format error in line %d of file %s", line,
fullHeightMapName.c_str());
}
/* Read the next color map color value: */
Color color;
for (int i = 0; i < 3; ++i) {
color[i] = Color::Scalar(heightMapSource.readNumber());
}
color[3] = Color::Scalar(1);
heightMapColors.push_back(color);
if (!heightMapSource.isLiteral('\n')) {
Misc::throwStdErr("ElevationColorMap: Color map format error in line %d of file %s", line,
fullHeightMapName.c_str());
}
++line;
}
}
/* Create the color map: */
setColors(heightMapKeys.size(), &heightMapColors[0], &heightMapKeys[0], 256);
/* Invalidate the color map texture object: */
++textureVersion;
}
void ElevationColorMap::calcTexturePlane(const Plane& basePlane) {
/* Scale and offset the camera-space base plane equation: */
const Plane::Vector& bpn = basePlane.getNormal();
Scalar bpo = basePlane.getOffset();
Scalar hms = Scalar(getNumEntries() - 1) / ((getScalarRangeMax() - getScalarRangeMin()) * Scalar(
getNumEntries()));
Scalar hmo = Scalar(0.5) / Scalar(getNumEntries()) - hms * getScalarRangeMin();
for (int i = 0; i < 3; ++i) {
texturePlaneEq[i] = GLfloat(bpn[i] * hms);
}
texturePlaneEq[3] = GLfloat(-bpo * hms + hmo);
}
void ElevationColorMap::calcTexturePlane(const DepthImageRenderer* depthImageRenderer) {
/* Calculate texture plane based on the given depth image renderer's base plane: */
calcTexturePlane(depthImageRenderer->getBasePlane());
}
void ElevationColorMap::bindTexture(GLContextData& contextData) const {
/* Retrieve the data item: */
DataItem* dataItem = contextData.retrieveDataItem<DataItem>(this);
/* Bind the texture object: */
glBindTexture(GL_TEXTURE_1D, dataItem->textureObjectId);
/* Check if the color map texture is outdated: */
if (dataItem->textureObjectVersion != textureVersion) {
/* Upload the color map entries as a 1D texture: */
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB8, getNumEntries(), 0, GL_RGBA, GL_FLOAT, getColors());
dataItem->textureObjectVersion = textureVersion;
}
}
void ElevationColorMap::uploadTexturePlane(GLint location) const {
/* Upload the texture mapping plane equation: */
glUniformARB<4>(location, 1, texturePlaneEq);
}