2014-10-30 22:32:52 +00:00
|
|
|
#include "terrain.hh"
|
2015-03-12 10:41:19 +00:00
|
|
|
#include <lodepng/lodepng.h>
|
2014-10-30 22:32:52 +00:00
|
|
|
|
2015-02-06 17:00:14 +00:00
|
|
|
Terrain::Terrain(std::string heightmapFilePath){
|
|
|
|
this->heightmapFilePath = heightmapFilePath;
|
2014-11-07 13:06:40 +00:00
|
|
|
}
|
2014-11-07 12:11:06 +00:00
|
|
|
|
2014-11-04 18:40:35 +00:00
|
|
|
Terrain::Terrain(){
|
2014-10-30 22:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Terrain::~Terrain() {
|
|
|
|
}
|
2014-11-04 18:40:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Terrain::load() {
|
2014-11-17 11:50:37 +00:00
|
|
|
std::vector<unsigned char> image; //the raw pixels
|
2015-03-16 14:26:30 +00:00
|
|
|
unsigned int heightmapHeightLoading = 0;
|
|
|
|
unsigned int heightmapWidthLoading = 0;
|
|
|
|
unsigned error = lodepng::decode(image, heightmapWidthLoading, heightmapHeightLoading, heightmapFilePath);
|
2014-11-17 11:50:37 +00:00
|
|
|
if (error) {
|
2015-02-06 17:00:14 +00:00
|
|
|
std::cout << "Decoder error " << error << " from Terrain::load: " << lodepng_error_text(error) << std::endl;
|
2014-11-17 11:50:37 +00:00
|
|
|
}
|
2015-03-16 14:26:30 +00:00
|
|
|
this->heightmapHeight = heightmapHeightLoading;
|
|
|
|
this->heightmapWidth = heightmapWidthLoading;
|
2015-02-27 15:53:46 +00:00
|
|
|
this->heightmap = new float*[this->heightmapWidth]; //initialize the heightmap
|
2015-03-16 14:26:30 +00:00
|
|
|
for(int columnNum = 0; columnNum < this->heightmapWidth; columnNum++){ //read in the heightmap
|
2015-02-27 15:53:46 +00:00
|
|
|
this->heightmap[columnNum] = new float[this->heightmapHeight];
|
2015-03-16 14:26:30 +00:00
|
|
|
for(int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){
|
2015-02-27 15:53:46 +00:00
|
|
|
this->heightmap[columnNum][rowNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here
|
2014-11-05 11:50:25 +00:00
|
|
|
}
|
2014-11-04 18:40:35 +00:00
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:08:49 +00:00
|
|
|
SharedVertexArrayObject Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
|
2015-03-16 14:26:30 +00:00
|
|
|
if (startX < 0) {
|
|
|
|
startX = 0;
|
|
|
|
}
|
|
|
|
if (startZ < 0) {
|
|
|
|
startZ = 0;
|
|
|
|
}
|
2015-03-17 13:54:18 +00:00
|
|
|
if (endX > heightmapWidth) {
|
|
|
|
endX = heightmapWidth;
|
2015-03-16 14:26:30 +00:00
|
|
|
}
|
2015-03-17 13:54:18 +00:00
|
|
|
if (endZ > heightmapHeight) {
|
|
|
|
endZ = heightmapHeight;
|
2015-03-16 14:26:30 +00:00
|
|
|
}
|
2014-11-04 18:40:35 +00:00
|
|
|
|
2015-03-17 11:08:49 +00:00
|
|
|
SharedArrayBuffer ab = SharedArrayBuffer(new ArrayBuffer());
|
|
|
|
SharedVertexArrayObject vao = SharedVertexArrayObject(new VertexArrayObject());
|
2014-11-15 12:46:20 +00:00
|
|
|
// Do NOT change the order of this!
|
|
|
|
ab->defineAttribute("aPosition", GL_FLOAT, 3);
|
2014-11-14 16:36:30 +00:00
|
|
|
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
2014-11-15 12:46:20 +00:00
|
|
|
ab->defineAttribute("aNormal", GL_FLOAT, 3);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-03-16 14:26:30 +00:00
|
|
|
int rowNum = startX, columnNum = startZ, dataCount=0, floatsPerVertex=8; //initializing:
|
2014-11-06 18:21:24 +00:00
|
|
|
bool movingRight = true, isUp = true;
|
2015-03-17 11:08:49 +00:00
|
|
|
int numVertices = ((endX - startX) - 1) * ((endZ - startZ) * 2 + 1) + 1;
|
2014-11-14 17:32:23 +00:00
|
|
|
float* abData = new float[numVertices * floatsPerVertex];
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2015-03-16 14:26:30 +00:00
|
|
|
while(rowNum < endX){ //traversing the Triangle Strip!
|
2015-02-13 12:46:41 +00:00
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
|
|
|
dataCount += floatsPerVertex;
|
|
|
|
if (isUp){
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
isUp = false;
|
|
|
|
}
|
2014-11-15 12:46:20 +00:00
|
|
|
else if (movingRight) {
|
2015-03-16 14:26:30 +00:00
|
|
|
if (columnNum == endZ - 1) {
|
2014-11-15 12:46:20 +00:00
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
2014-11-17 12:36:05 +00:00
|
|
|
dataCount += floatsPerVertex;
|
2014-11-15 12:46:20 +00:00
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
2014-11-17 12:36:05 +00:00
|
|
|
dataCount += floatsPerVertex;
|
2014-11-15 12:46:20 +00:00
|
|
|
movingRight = false;
|
|
|
|
rowNum = rowNum + 1;
|
2015-02-13 12:46:41 +00:00
|
|
|
}
|
2014-11-15 12:46:20 +00:00
|
|
|
else {
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum + 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-03-16 14:26:30 +00:00
|
|
|
if (columnNum == startZ){
|
2014-11-15 12:46:20 +00:00
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
2014-11-17 12:36:05 +00:00
|
|
|
dataCount += floatsPerVertex;
|
2014-11-15 12:46:20 +00:00
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
2014-11-17 12:36:05 +00:00
|
|
|
dataCount += floatsPerVertex;
|
2014-11-15 12:46:20 +00:00
|
|
|
movingRight = true;
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum - 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
}
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-11-17 11:50:37 +00:00
|
|
|
ab->setDataElements(numVertices, abData);
|
2015-03-22 12:25:43 +00:00
|
|
|
delete[] abData;
|
2015-03-17 11:08:49 +00:00
|
|
|
vao->bind();
|
|
|
|
vao->setMode(GL_TRIANGLE_STRIP);
|
|
|
|
vao->attachAllAttributes(ab);
|
|
|
|
return vao;
|
2014-11-14 16:36:30 +00:00
|
|
|
}
|
2014-11-11 12:27:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-03-16 14:26:30 +00:00
|
|
|
void Terrain::set_abData(float* abData, int dataCount, int rowNum, int columnNum){
|
2014-11-14 16:36:30 +00:00
|
|
|
//set Position
|
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
//set Texture Coordinate
|
|
|
|
abData[dataCount+3] = (float)(rowNum % 2);
|
|
|
|
abData[dataCount+4] = (float)(columnNum % 2);
|
2015-02-13 12:46:41 +00:00
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
//setNormal
|
|
|
|
if (rowNum==0 || rowNum==(this->heightmapHeight-1) || columnNum==0 || columnNum==(this->heightmapWidth-1)){
|
2015-02-13 12:46:41 +00:00
|
|
|
abData[dataCount+5] = 0.0;
|
|
|
|
abData[dataCount+6] = 1.0;
|
|
|
|
abData[dataCount+7] = 0.0;
|
2014-11-15 12:46:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-11-17 11:50:37 +00:00
|
|
|
glm::vec3 sumNormals = glm::vec3(0.0f, 0.0f, 0.0f);
|
|
|
|
for (int i=-1; i<2; i+=2) {
|
|
|
|
for (int j=-1; j<2; j+=2) {
|
2014-11-18 12:09:55 +00:00
|
|
|
glm::vec3 vecA, vecB, vecC, normal;
|
|
|
|
vecA = glm::normalize(glm::vec3((float)i, (heightmap[rowNum+i][columnNum] - heightmap[rowNum][columnNum]), 0.0f));
|
|
|
|
vecB = glm::normalize(glm::vec3((float)i, (heightmap[rowNum+i][columnNum+j] - heightmap[rowNum][columnNum]), (float)j));
|
2014-11-15 12:46:20 +00:00
|
|
|
normal = glm::normalize(glm::cross(vecA, vecB));
|
2014-11-18 12:09:55 +00:00
|
|
|
if(i+j!=0){
|
2014-11-15 12:46:20 +00:00
|
|
|
normal = normal*(-1.0f);
|
2014-11-18 12:09:55 +00:00
|
|
|
}
|
|
|
|
sumNormals += normal;
|
|
|
|
|
|
|
|
vecC = glm::normalize(glm::vec3(0.0f, (heightmap[rowNum][columnNum+j] - heightmap[rowNum][columnNum]), (float)j));
|
|
|
|
normal = glm::normalize(glm::cross(vecB, vecC));
|
|
|
|
if(i+j!=0){
|
|
|
|
normal = normal*(-1.0f);
|
|
|
|
}
|
2014-11-15 12:46:20 +00:00
|
|
|
sumNormals += normal;
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 11:50:37 +00:00
|
|
|
sumNormals = glm::normalize(sumNormals);
|
2014-11-15 12:46:20 +00:00
|
|
|
abData[dataCount+5] = sumNormals[0];
|
|
|
|
abData[dataCount+6] = sumNormals[1];
|
|
|
|
abData[dataCount+7] = sumNormals[2];
|
2014-11-11 12:27:11 +00:00
|
|
|
}
|
2014-11-14 16:36:30 +00:00
|
|
|
}
|
2014-11-11 12:27:11 +00:00
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
float** Terrain::getHeightmap(){
|
|
|
|
return this->heightmap;
|
2014-11-04 18:40:35 +00:00
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
|
2015-03-16 14:26:30 +00:00
|
|
|
int Terrain::getHeightmapHeight(){
|
2014-11-14 16:36:30 +00:00
|
|
|
return this->heightmapHeight;
|
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
|
2015-03-16 14:26:30 +00:00
|
|
|
int Terrain::getHeightmapWidth(){
|
2014-11-14 16:36:30 +00:00
|
|
|
return this->heightmapWidth;
|
|
|
|
}
|