fixed terrain class and removed load from texture

This commit is contained in:
Steffen Fündgens 2014-11-07 13:11:06 +01:00
parent 8dca1705fb
commit 2d1328b96b
4 changed files with 33 additions and 33 deletions

View File

@ -1,5 +1,9 @@
#include "terrain.hh" #include "terrain.hh"
//Terrain::Terrain(std::string filePath){
// this->filePath = filePath;
//}
Terrain::Terrain(){ Terrain::Terrain(){
} }
@ -8,8 +12,8 @@ Terrain::~Terrain() {
void Terrain::load() { void Terrain::load() {
std::ifstream terrain_png(this->filePath); std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization
unsigned int rowNum, columnNum; unsigned int rowNum, columnNum, heightmapValue;
terrain_png.seekg(16); //skip part of the header terrain_png.seekg(16); //skip part of the header
terrain_png.read((char *)&this->heightmapWidth, 4); //read width terrain_png.read((char *)&this->heightmapWidth, 4); //read width
@ -17,40 +21,40 @@ void Terrain::load() {
this->heightmapWidth = ntohl(this->heightmapWidth); //convert from host to network byte order this->heightmapWidth = ntohl(this->heightmapWidth); //convert from host to network byte order
this->heightmapHeight = ntohl(this->heightmapHeight); this->heightmapHeight = ntohl(this->heightmapHeight);
heightmap = new unsigned int*[this->heightmapHeight]; //initialize the heightmap heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
heightmap[rowNum] = new unsigned int[this->heightmapWidth]; heightmap[rowNum] = new float[this->heightmapWidth];
for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){ for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
terrain_png.read((char *)&heightmap[rowNum][columnNum], 1); terrain_png.read((char *)&heightmapValue, 1);
heightmap[rowNum][columnNum] = (float)heightmapValue / 5;
} }
} }
this->makeTriangleMesh(); this->makeTriangleMesh();
heightmapChanged = false; heightmapChanged = false; //no need to make a TriangleMesh again before rendering
texture.load();
} }
void Terrain::makeTriangleMesh(){ void Terrain::makeTriangleMesh(){
ACGL::OpenGL::ArrayBuffer arrayBuffer = ACGL::OpenGL::ArrayBuffer(); ACGL::OpenGL::SharedArrayBuffer arrayBuffer = ACGL::OpenGL::SharedArrayBuffer();
arrayBuffer.defineAttribute("pos", GL_UNSIGNED_INT, 3); arrayBuffer->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
unsigned int rowNum=0, columnNum=0; //initializing: unsigned int rowNum=0, columnNum=0; //initializing:
bool movingRight = true, isUp = true; bool movingRight = true, isUp = true;
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip! while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
unsigned int newPos[3]; float newPos[3];
newPos[0] = rowNum; newPos[0] = (float)rowNum;
newPos[1] = columnNum; newPos[1] = (float)columnNum;
newPos[2] = heightmap[rowNum][columnNum]; newPos[2] = heightmap[rowNum][columnNum];
arrayBuffer.setDataElements(1, &newPos); arrayBuffer->setDataElements(1, &newPos);
if (isUp){ if (isUp){
rowNum = rowNum + 1; rowNum = rowNum + 1;
isUp = false; isUp = false;
}else if (movingRight){ }else if (movingRight){
if (columnNum = this->heightmapWidth - 1){ if (columnNum == this->heightmapWidth - 1){
arrayBuffer.setDataElements(1, &newPos); arrayBuffer->setDataElements(1, &newPos);
arrayBuffer.setDataElements(1, &newPos); arrayBuffer->setDataElements(1, &newPos);
movingRight = false; movingRight = false;
rowNum = rowNum + 1; rowNum = rowNum + 1;
} else{ } else{
@ -59,9 +63,9 @@ void Terrain::makeTriangleMesh(){
isUp = true; isUp = true;
} }
}else{ }else{
if (columnNum = 0){ if (columnNum == 0){
arrayBuffer.setDataElements(1, &newPos); arrayBuffer->setDataElements(1, &newPos);
arrayBuffer.setDataElements(1, &newPos); arrayBuffer->setDataElements(1, &newPos);
movingRight = true; movingRight = true;
rowNum = rowNum + 1; rowNum = rowNum + 1;
}else{ }else{
@ -72,17 +76,17 @@ void Terrain::makeTriangleMesh(){
} }
} }
//this->triangleMesh = ACGL::OpenGL::VertexArrayObject(); //does not work since ACGL::OpenGL::VertexArrayObject is ACGL_NOT_COPYABLE this->triangleMesh = ACGL::OpenGL::SharedVertexArrayObject();
this->triangleMesh.bind(); this->triangleMesh->bind();
this->triangleMesh.setMode(GL_TRIANGLE_STRIP); this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
//this->triangleMesh.attachAllAttributes(arrayBuffer); this->triangleMesh->attachAllAttributes(arrayBuffer);
//TODO unbind? //TODO unbind?
} }
void Terrain::render() { void Terrain::render() {
if (heightmapChanged) if (heightmapChanged)
this->makeTriangleMesh(); this->makeTriangleMesh();
this->triangleMesh.render(); this->triangleMesh->render();
} }

View File

@ -10,18 +10,18 @@
class Terrain { class Terrain {
public: public:
Terrain(std::string filePath);
Terrain(); Terrain();
~Terrain(); ~Terrain();
void load(); void load();
void render(); void render();
private: private:
float friction;
Texture texture; Texture texture;
std::string filePath; std::string filePath;
unsigned int heightmapWidth, heightmapHeight; unsigned int heightmapWidth, heightmapHeight;
unsigned int** heightmap; //can be accessed like 'unsigned int[][]' float** heightmap; //can be accessed like 'float[][]'
bool heightmapChanged; bool heightmapChanged;
ACGL::OpenGL::VertexArrayObject triangleMesh; ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
void makeTriangleMesh(); void makeTriangleMesh();
}; };

View File

@ -13,6 +13,3 @@ Texture::~Texture() {
ACGL::OpenGL::SharedTexture2D Texture::getReference() { ACGL::OpenGL::SharedTexture2D Texture::getReference() {
return reference; return reference;
} }
void Texture::load(){
}

View File

@ -12,7 +12,6 @@ class Texture{
Texture(); Texture();
ACGL::OpenGL::SharedTexture2D getReference(); ACGL::OpenGL::SharedTexture2D getReference();
~Texture(); ~Texture();
void load();
private: private:
ACGL::OpenGL::SharedTexture2D reference; ACGL::OpenGL::SharedTexture2D reference;
}; };