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 12:27:11 +00:00
|
|
|
this->filePath = "../Levels/LevelTest/terrain"; //TODO remove this, its only for testing
|
2014-11-11 11:22:47 +00:00
|
|
|
|
2014-11-14 17:32:23 +00:00
|
|
|
std::ifstream terrain_png(this->filePath + "/heightmap2.png");
|
2014-11-07 12:11:06 +00:00
|
|
|
unsigned int rowNum, columnNum, heightmapValue;
|
2014-11-06 18:21:24 +00:00
|
|
|
|
2014-11-11 12:27:11 +00:00
|
|
|
terrain_png.seekg(16); //skip part of the header
|
2014-11-07 15:55:56 +00:00
|
|
|
|
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
|
|
|
|
|
2014-11-11 12:27:11 +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>();
|
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);
|
2014-11-04 18:40:35 +00:00
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
/* unsigned int rowNum=0, columnNum=0, dataCount=0, abNumFloats=8; //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;
|
2014-11-14 17:32:23 +00:00
|
|
|
float* abData = new float[numVertices * floatsPerVertex];
|
2014-11-11 11:22:47 +00:00
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
|
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
2014-11-14 17:32:23 +00:00
|
|
|
dataCount += floatsPerVertex;
|
2014-11-06 18:21:24 +00:00
|
|
|
if (isUp){
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
isUp = false;
|
|
|
|
}
|
2014-11-15 12:46:20 +00:00
|
|
|
else if (movingRight) {
|
|
|
|
if (columnNum == this->heightmapWidth - 1) {
|
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
|
|
|
dataCount += abNumFloats;
|
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
|
|
|
dataCount += abNumFloats;
|
|
|
|
movingRight = false;
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum + 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (columnNum == 0){
|
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
|
|
|
dataCount += abNumFloats;
|
|
|
|
set_abData(abData, dataCount, rowNum, columnNum);
|
|
|
|
dataCount += abNumFloats;
|
|
|
|
movingRight = true;
|
|
|
|
rowNum = rowNum + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rowNum = rowNum - 1;
|
|
|
|
columnNum = columnNum - 1;
|
|
|
|
isUp = true;
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
ab->setDataElements(numVertices, abData);*/
|
|
|
|
float abData[32] = {0.0f, 0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f,
|
2014-11-14 17:32:23 +00:00
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
1.0f, 0.0f, 0.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 1.0f, 0.0f,
|
2014-11-14 17:32:23 +00:00
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
0.0f, 0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f,
|
2014-11-14 17:32:23 +00:00
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
1.0f, 0.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 1.0f, 0.0f};
|
|
|
|
ab->setDataElements(4, abData);
|
2014-11-11 11:22:47 +00:00
|
|
|
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-14 16:36:30 +00:00
|
|
|
}
|
2014-11-11 12:27:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
void Terrain::set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum){
|
|
|
|
//set Position
|
|
|
|
abData[dataCount] = (float)rowNum;
|
|
|
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
|
|
|
abData[dataCount+2] = (float)columnNum;
|
|
|
|
|
2014-11-15 12:46:20 +00:00
|
|
|
//set Texture Coordinate
|
|
|
|
abData[dataCount+3] = (float)(rowNum % 2);
|
|
|
|
abData[dataCount+4] = (float)(columnNum % 2);
|
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
//setNormal
|
|
|
|
if (rowNum==0 || rowNum==(this->heightmapHeight-1) || columnNum==0 || columnNum==(this->heightmapWidth-1)){
|
|
|
|
abData[dataCount+5] = 0.0;
|
2014-11-15 12:46:20 +00:00
|
|
|
abData[dataCount+6] = 1.0;
|
|
|
|
abData[dataCount+7] = 0.0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glm::vec3 sumNormals;
|
|
|
|
for (int i=-1; i<2; i+=1) {
|
|
|
|
for (int j=-1; j<2; j+=1) {
|
|
|
|
glm::vec3 vecA, vecB, normal;
|
|
|
|
vecA = glm::vec3((float)i, (heightmap[rowNum+i][columnNum] - heightmap[rowNum][columnNum]), 0.0f);
|
|
|
|
vecB = glm::vec3(0.0f, (heightmap[rowNum][columnNum+j] - heightmap[rowNum][columnNum]), (float)j);
|
|
|
|
normal = glm::normalize(glm::cross(vecA, vecB));
|
|
|
|
if(i+j==0)
|
|
|
|
normal = normal*(-1.0f);
|
|
|
|
sumNormals += normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sumNormals = sumNormals*0.111f;
|
|
|
|
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
|
|
|
Model Terrain::getModel(){
|
|
|
|
return Model(this->triangleMesh);
|
2014-11-04 18:40:35 +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
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
unsigned int Terrain::getHeightmapHeight(){
|
2014-11-14 17:32:23 +00:00
|
|
|
//return this->heightmapHeight;
|
|
|
|
return 2;
|
2014-11-14 16:36:30 +00:00
|
|
|
}
|
2014-11-06 18:21:24 +00:00
|
|
|
|
2014-11-14 16:36:30 +00:00
|
|
|
unsigned int Terrain::getHeightmapWidth(){
|
2014-11-14 17:32:23 +00:00
|
|
|
//return this->heightmapWidth;
|
|
|
|
return 2;
|
2014-11-14 16:36:30 +00:00
|
|
|
}
|