-
Notifications
You must be signed in to change notification settings - Fork 91
/
DEM.cpp
164 lines (137 loc) · 5.2 KB
/
DEM.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
/***********************************************************************
DEM - Class to represent digital elevation models (DEMs) as float-valued
texture objects.
Copyright (c) 2013-2016 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 "DEM.h"
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <GL/gl.h>
#include <GL/GLContextData.h>
#include <GL/Extensions/GLARBTextureFloat.h>
#include <GL/Extensions/GLARBTextureRectangle.h>
#include <GL/Extensions/GLARBTextureRg.h>
#include <GL/Extensions/GLARBShaderObjects.h>
#include <Geometry/Matrix.h>
/******************************
Methods of class DEM::DataItem:
******************************/
DEM::DataItem::DataItem(void)
:textureObjectId(0)
{
/* Check for and initialize all required OpenGL extensions: */
GLARBTextureFloat::initExtension();
GLARBTextureRectangle::initExtension();
GLARBTextureRg::initExtension();
GLARBShaderObjects::initExtension();
/* Create the texture object: */
glGenTextures(1,&textureObjectId);
}
DEM::DataItem::~DataItem(void)
{
/* Destroy the texture object: */
glDeleteTextures(1,&textureObjectId);
}
/********************
Methods of class DEM:
********************/
void DEM::calcMatrix(void)
{
/* Convert the DEM transformation into a projective transformation matrix: */
demTransform=PTransform(transform);
PTransform::Matrix& dtm=demTransform.getMatrix();
/* Pre-multiply the projective transformation matrix with the DEM space to DEM pixel space transformation: */
PTransform dem;
dem.getMatrix()(0,0)=Scalar(demSize[0]-1)/(demBox[2]-demBox[0]);
dem.getMatrix()(0,3)=Scalar(0.5)-Scalar(demSize[0]-1)/(demBox[2]-demBox[0])*demBox[0];
dem.getMatrix()(1,1)=Scalar(demSize[1]-1)/(demBox[3]-demBox[1]);
dem.getMatrix()(1,3)=Scalar(0.5)-Scalar(demSize[1]-1)/(demBox[3]-demBox[1])*demBox[1];
dem.getMatrix()(2,2)=Scalar(1)/verticalScale;
dem.getMatrix()(2,3)=verticalScaleBase-verticalScaleBase/verticalScale;
demTransform.leftMultiply(dem);
/* Convert the full transformation to column-major OpenGL format: */
GLfloat* dtmPtr=demTransformMatrix;
for(int j=0;j<4;++j)
for(int i=0;i<4;++i,++dtmPtr)
*dtmPtr=GLfloat(dtm(i,j));
}
DEM::DEM(void)
:dem(0),
transform(OGTransform::identity),
verticalScale(1),verticalScaleBase(0)
{
demSize[0]=demSize[1]=0;
}
DEM::~DEM(void)
{
delete[] dem;
}
void DEM::initContext(GLContextData& contextData) const
{
/* Create and register a data item: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Upload the DEM array into the texture object: */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,dataItem->textureObjectId);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_T,GL_CLAMP);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_LUMINANCE32F_ARB,demSize[0],demSize[1],0,GL_LUMINANCE,GL_FLOAT,dem);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,0);
}
void DEM::load(const char* demFileName)
{
/* Read the DEM file: */
IO::FilePtr demFile=IO::openFile(demFileName);
demFile->setEndianness(Misc::LittleEndian);
demFile->read<int>(demSize,2);
dem=new float[demSize[1]*demSize[0]];
for(int i=0;i<4;++i)
demBox[i]=double(demFile->read<float>());
demFile->read<float>(dem,demSize[1]*demSize[0]);
/* Update the DEM transformation: */
calcMatrix();
}
float DEM::calcAverageElevation(void) const
{
/* Sum all elevation measurements: */
double elevSum=0.0;
const float* demPtr=dem;
for(int i=demSize[1]*demSize[0];i>0;--i,++demPtr)
elevSum+=double(*demPtr);
/* Return the average elevation: */
return float(elevSum/double(demSize[1]*demSize[0]));
}
void DEM::setTransform(const OGTransform& newTransform,Scalar newVerticalScale,Scalar newVerticalScaleBase)
{
transform=newTransform;
verticalScale=newVerticalScale;
verticalScaleBase=newVerticalScaleBase;
/* Update the DEM transformation: */
calcMatrix();
}
void DEM::bindTexture(GLContextData& contextData) const
{
/* Get the context data item: */
DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this);
/* Bind the DEM texture: */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,dataItem->textureObjectId);
}
void DEM::uploadDemTransform(GLint location) const
{
/* Upload the matrix to OpenGL: */
glUniformMatrix4fvARB(location,1,GL_FALSE,demTransformMatrix);
}