/
terrain.cpp
224 lines (171 loc) · 7.82 KB
/
terrain.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
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
#include "terrain.h"
#include <iostream>
#include <QFile>
#include <QGLWidget>
#include <QImage>
Terrain::Terrain(std::string name, int id, int tesselation, float3 color) :
Primitive(0, name, id, tesselation, color),
horizontalScale_(256),
verticalScale_(300),
gridSize_(128),
width_(0),
height_(0)
{
hasVBO_[TEXCOORDS] = true;
isTerrain_ = true;
vertexPositions_.resize(0);
indicesList_.resize(0);
vertexTextureCoordinates_.resize(0);
int halfGridSize = gridSize_ / 2;
/*
for (int i = 0; i < gridSize_; i++) {
for (int j = 0; j > gridSize_; j++) {
vertexPositions_.push_back(float3(i, 0, j));
vertexPositions_.push_back(float3(i + 1, 0, j));
vertexPositions_.push_back(float3(i + 1, 0, j - 1));
vertexPositions_.push_back(float3(i, 0, j - 1));
// texture coordinates range from [-0.5, 0.5] here, as they will be added to the global tex coords of the camera
vertexTextureCoordinates_.push_back(float3((i) / (2.0f * halfGridSize), (-j) / (2.0f * halfGridSize), 0));
vertexTextureCoordinates_.push_back(float3((i + 1) / (2.0f * halfGridSize), (-j) / (2.0f * halfGridSize), 0));
vertexTextureCoordinates_.push_back(float3((i + 1) / (2.0f * halfGridSize), (-j + 1) / (2.0f * halfGridSize), 0));
vertexTextureCoordinates_.push_back(float3((i) / (2.0f * halfGridSize), (-j + 1) / (2.0f * halfGridSize), 0));
}
}*/
for (int i = -halfGridSize; i < halfGridSize; i++) {
for (int j = halfGridSize; j > -halfGridSize; j--) {
vertexPositions_.push_back(float3(i, 0, j));
vertexPositions_.push_back(float3(i + 1, 0, j));
vertexPositions_.push_back(float3(i + 1, 0, j - 1));
vertexPositions_.push_back(float3(i, 0, j - 1));
// texture coordinates range from [-0.5, 0.5] here, as they will be added to the global tex coords of the camera
vertexTextureCoordinates_.push_back(float3((i) / (2.0f * halfGridSize), (-j) / (2.0f * halfGridSize), 0));
vertexTextureCoordinates_.push_back(float3((i + 1) / (2.0f * halfGridSize), (-j) / (2.0f * halfGridSize), 0));
vertexTextureCoordinates_.push_back(float3((i + 1) / (2.0f * halfGridSize), (-j + 1) / (2.0f * halfGridSize), 0));
vertexTextureCoordinates_.push_back(float3((i) / (2.0f * halfGridSize), (-j + 1) / (2.0f * halfGridSize), 0));
}
}
// set indices list
for (uint i = 0; i < vertexPositions_.size(); i++) {
indicesList_.push_back(i);
}
}
void Terrain::parseHeightMap(QString fileName) {
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
// up to 64 - 1 = 63 ASCII characteres can be read in this line. One is reserved for the \0 termination.
QByteArray line = file.readLine(64); // do nothing, throw this line away
line = file.readLine(64);
width_ = line.trimmed().toInt();
line = file.readLine(64);
height_ = line.trimmed().toInt();
line = file.readLine(64);
int maxValue = line.trimmed().toInt();
std::cout << "width: " << width_ << ", height: " << height_ << ", maxValue: " << maxValue << std::endl;
int sizeOfTexture = width_ * height_;
// allocate array for height values
heightValues_ = (unsigned short*) malloc(sizeOfTexture * sizeof(unsigned short));
unsigned short currentHeightValue = 0;
char currentByte;
unsigned short minHeight = 65535;
unsigned short maxHeight = 0;
for (uint i = 0; i < sizeOfTexture && !file.atEnd(); i++) {
// copy first byte into short
if (!file.getChar(¤tByte)) std::cout << "getChar() failed at pos " << i << std::endl;
currentHeightValue = (uchar) currentByte;
// bit shift by 1 byte
currentHeightValue = currentHeightValue << 8;
//std::cout << "byte 1: " << (unsigned short) currentByte;
// copy second byte into short
if (!file.getChar(¤tByte)) std::cout << "getChar() failed at pos " << i << std::endl;
currentHeightValue += (uchar) currentByte;
heightValues_[i] = currentHeightValue;
if (heightValues_[i] > maxHeight) maxHeight = heightValues_[i];
if (heightValues_[i] < minHeight) minHeight = heightValues_[i];
//std::cout << ", byte 2: " << (unsigned short) currentByte;
//std::cout << ", height value[" << i << "]: " << currentHeightValue << std::endl;
//uchar temp = (uchar) ((float) currentHeightValue / 65536.0f * 256.0f);
//uchar temp = (uchar) currentHeightValue;
// std::cout << "tmp: " << (uint) temp << std::endl;
// copy into float array
//floatData_[i] = temp; // == 2^16
}
std::cout << "minHeight: " << minHeight << ", maxHeight: " << maxHeight << std::endl;
for (uint i = 0; i < sizeOfTexture; i++) {
heightValues_[i] -= minHeight;
}
} else {
std::cout << "Failed to open file " << fileName.toStdString() << std::endl;
}
}
void Terrain::createTextures(GLuint id) {
checkGLErrors("before height map upload");
// glGenTextures(1, &heightTexture_);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, width_, height_, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, heightValues_);
glGenerateMipmap(GL_TEXTURE_2D);
std::cout << "height map uploaded.." << std::endl;
checkGLErrors("after height map upload");
glBindTexture(GL_TEXTURE_2D, 0);
checkGLErrors("after uploading material textures");
}
void Terrain::draw(bufferIDs buffIDs) {
bindVAOToShader(buffIDs);
checkGLErrors("before drawing terrain");
glDrawElements(
GL_PATCHES, // mode
indicesList_.size(), // count
GL_UNSIGNED_INT, // type
(void*)0 // element array buffer offset
);
checkGLErrors("drawing terrain");
}
void Terrain::setHorizontalScale(int horizontalScale) {
horizontalScale_ = horizontalScale;
if (horizontalScale_ % 2 != 0) horizontalScale_--;
}
void Terrain::setVerticalScale(int verticalScale) {
verticalScale_ = verticalScale;
}
void Terrain::uploadMaterial(QString fileName, GLuint textureID) {
QImage material = QGLWidget::convertToGLFormat(QImage(fileName));
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, material.width(), material.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, material.bits());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
checkGLErrors("material upload");
}
void Terrain::checkGLErrors(const char *label) {
GLenum errCode;
const GLubyte *errStr;
if ((errCode = glGetError()) != GL_NO_ERROR) {
errStr = gluErrorString(errCode);
printf("OpenGL ERROR: ");
printf((char*)errStr);
printf("(Label: ");
printf(label);
printf(")\n.");
}
}
int Terrain::getHorizontalScale() {
return horizontalScale_;
}
int Terrain::getVerticalScale() {
return verticalScale_;
}
int Terrain::getGridSize() {
return gridSize_;
}
unsigned short* Terrain::getHeightValues() {
return heightValues_;
}
int Terrain::getWidth() {
return width_;
}