2014-10-30 22:32:52 +00:00
|
|
|
#include "terrain.hh"
|
|
|
|
|
2014-11-07 12:11:06 +00:00
|
|
|
//Terrain::Terrain(std::string filePath){
|
|
|
|
// this->filePath = filePath;
|
|
|
|
//}
|
|
|
|
|
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-07 12:11:06 +00:00
|
|
|
std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization
|
|
|
|
unsigned int rowNum, columnNum, heightmapValue;
|
2014-11-06 18:21:24 +00:00
|
|
|
|
|
|
|
terrain_png.seekg(16); //skip part of the header
|
|
|
|
terrain_png.read((char *)&this->heightmapWidth, 4); //read width
|
|
|
|
terrain_png.read((char *)&this->heightmapHeight, 4); //read height
|
|
|
|
this->heightmapWidth = ntohl(this->heightmapWidth); //convert from host to network byte order
|
|
|
|
this->heightmapHeight = ntohl(this->heightmapHeight);
|
|
|
|
|
2014-11-07 12:11:06 +00:00
|
|
|
heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
|
|
|
|
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
|
|
|
|
heightmap[rowNum] = new float[this->heightmapWidth];
|
2014-11-06 18:21:24 +00:00
|
|
|
for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
|
2014-11-07 12:11:06 +00:00
|
|
|
terrain_png.read((char *)&heightmapValue, 1);
|
|
|
|
heightmap[rowNum][columnNum] = (float)heightmapValue / 5;
|
2014-11-05 11:50:25 +00:00
|
|
|
}
|
2014-11-04 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:21:24 +00:00
|
|
|
this->makeTriangleMesh();
|
2014-11-07 12:11:06 +00:00
|
|
|
heightmapChanged = false; //no need to make a TriangleMesh again before rendering
|
2014-11-06 18:21:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Terrain::makeTriangleMesh(){
|
2014-11-04 18:40:35 +00:00
|
|
|
|
2014-11-07 12:11:06 +00:00
|
|
|
ACGL::OpenGL::SharedArrayBuffer arrayBuffer = ACGL::OpenGL::SharedArrayBuffer();
|
|
|
|
arrayBuffer->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
|
2014-11-04 18:40:35 +00:00
|
|
|
|
2014-11-06 18:21:24 +00:00
|
|
|
unsigned int rowNum=0, columnNum=0; //initializing:
|
|
|
|
bool movingRight = true, isUp = true;
|
|
|
|
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
|
2014-11-07 12:11:06 +00:00
|
|
|
float newPos[3];
|
|
|
|
newPos[0] = (float)rowNum;
|
|
|
|
newPos[1] = (float)columnNum;
|
2014-11-06 18:21:24 +00:00
|
|
|
newPos[2] = heightmap[rowNum][columnNum];
|
2014-11-07 12:11:06 +00:00
|
|
|
arrayBuffer->setDataElements(1, &newPos);
|
2014-11-06 18:21:24 +00:00
|
|
|
if (isUp){
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
isUp = false;
|
|
|
|
}else if (movingRight){
|
2014-11-07 12:11:06 +00:00
|
|
|
if (columnNum == this->heightmapWidth - 1){
|
|
|
|
arrayBuffer->setDataElements(1, &newPos);
|
|
|
|
arrayBuffer->setDataElements(1, &newPos);
|
2014-11-06 18:21:24 +00:00
|
|
|
movingRight = false;
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
} else{
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum + 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}else{
|
2014-11-07 12:11:06 +00:00
|
|
|
if (columnNum == 0){
|
|
|
|
arrayBuffer->setDataElements(1, &newPos);
|
|
|
|
arrayBuffer->setDataElements(1, &newPos);
|
2014-11-06 18:21:24 +00:00
|
|
|
movingRight = true;
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
}else{
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum - 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 12:11:06 +00:00
|
|
|
this->triangleMesh = ACGL::OpenGL::SharedVertexArrayObject();
|
|
|
|
this->triangleMesh->bind();
|
|
|
|
this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
|
|
|
|
this->triangleMesh->attachAllAttributes(arrayBuffer);
|
2014-11-06 18:21:24 +00:00
|
|
|
//TODO unbind?
|
2014-11-04 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Terrain::render() {
|
2014-11-06 18:21:24 +00:00
|
|
|
if (heightmapChanged)
|
|
|
|
this->makeTriangleMesh();
|
2014-11-07 12:11:06 +00:00
|
|
|
this->triangleMesh->render();
|
2014-11-04 18:40:35 +00:00
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|