2014-10-30 22:32:52 +00:00
|
|
|
#include "terrain.hh"
|
|
|
|
|
2014-11-07 13:06:40 +00:00
|
|
|
Terrain::Terrain(std::string filePath){
|
|
|
|
this->filePath = filePath;
|
|
|
|
}
|
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-11 11:22:47 +00:00
|
|
|
this->filePath = "../Levels/LevelTest/terrain"; //TODO remove this, its only for testing
|
|
|
|
|
2014-11-07 15:55:56 +00:00
|
|
|
std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization
|
2014-11-07 12:11:06 +00:00
|
|
|
unsigned int rowNum, columnNum, heightmapValue;
|
2014-11-06 18:21:24 +00:00
|
|
|
|
2014-11-07 15:55:56 +00:00
|
|
|
terrain_png.seekg(16); //skip part of the header
|
|
|
|
|
2014-11-11 11:22:47 +00:00
|
|
|
char temp[4];
|
|
|
|
terrain_png.read(temp, 4); //read width
|
|
|
|
this->heightmapWidth = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
|
|
|
|
terrain_png.read(temp, 4); //read height
|
|
|
|
this->heightmapHeight = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
|
|
|
|
|
|
|
|
printf("Width: %d ", (int)this->heightmapWidth);
|
|
|
|
printf("Height: %d ", (int)this->heightmapHeight);
|
2014-11-07 15:55:56 +00:00
|
|
|
|
|
|
|
heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
|
|
|
|
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
|
2014-11-07 12:11:06 +00:00
|
|
|
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);
|
2014-11-11 11:22:47 +00:00
|
|
|
heightmap[rowNum][columnNum] = (float)heightmapValue / 256;
|
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-11 11:22:47 +00:00
|
|
|
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
|
|
|
|
ab->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
|
2014-11-04 18:40:35 +00:00
|
|
|
|
2014-11-11 11:22:47 +00:00
|
|
|
unsigned int rowNum=0, columnNum=0, dataCount=0; //initializing:
|
2014-11-06 18:21:24 +00:00
|
|
|
bool movingRight = true, isUp = true;
|
2014-11-11 11:22:47 +00:00
|
|
|
int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1;
|
|
|
|
printf("NumberofVertices: %d ", numVertices);
|
|
|
|
float* abData = new float[numVertices * 3];
|
|
|
|
|
|
|
|
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
|
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
|
|
|
dataCount += 3;
|
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){
|
2014-11-11 11:22:47 +00:00
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
|
|
|
dataCount += 3;
|
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
|
|
|
dataCount += 3;
|
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){
|
2014-11-11 11:22:47 +00:00
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
|
|
|
dataCount += 3;
|
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
|
|
|
dataCount += 3;
|
2014-11-06 18:21:24 +00:00
|
|
|
movingRight = true;
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
}else{
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum - 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 11:22:47 +00:00
|
|
|
ab->setDataElements(numVertices, abData);
|
|
|
|
this->triangleMesh = std::make_shared<ACGL::OpenGL::VertexArrayObject>();
|
2014-11-07 12:11:06 +00:00
|
|
|
this->triangleMesh->bind();
|
|
|
|
this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
|
2014-11-11 11:22:47 +00:00
|
|
|
this->triangleMesh->attachAllAttributes(ab);
|
2014-11-07 15:55:56 +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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|