From 2d1328b96bf8c7b1cfb2af71aab509d977fb97a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Fri, 7 Nov 2014 13:11:06 +0100 Subject: [PATCH] fixed terrain class and removed load from texture --- terrain.cc | 54 +++++++++++++++++++++++++++++------------------------- terrain.hh | 8 ++++---- texture.cc | 3 --- texture.hh | 1 - 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/terrain.cc b/terrain.cc index 9e655ea..cf811c4 100644 --- a/terrain.cc +++ b/terrain.cc @@ -1,5 +1,9 @@ #include "terrain.hh" +//Terrain::Terrain(std::string filePath){ +// this->filePath = filePath; +//} + Terrain::Terrain(){ } @@ -8,8 +12,8 @@ Terrain::~Terrain() { void Terrain::load() { - std::ifstream terrain_png(this->filePath); - unsigned int rowNum, columnNum; + std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization + unsigned int rowNum, columnNum, heightmapValue; terrain_png.seekg(16); //skip part of the header 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->heightmapHeight = ntohl(this->heightmapHeight); - heightmap = new unsigned int*[this->heightmapHeight]; //initialize the heightmap - for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap - heightmap[rowNum] = new unsigned int[this->heightmapWidth]; + 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]; 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(); - heightmapChanged = false; + heightmapChanged = false; //no need to make a TriangleMesh again before rendering - texture.load(); } void Terrain::makeTriangleMesh(){ - ACGL::OpenGL::ArrayBuffer arrayBuffer = ACGL::OpenGL::ArrayBuffer(); - arrayBuffer.defineAttribute("pos", GL_UNSIGNED_INT, 3); + ACGL::OpenGL::SharedArrayBuffer arrayBuffer = ACGL::OpenGL::SharedArrayBuffer(); + arrayBuffer->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates unsigned int rowNum=0, columnNum=0; //initializing: bool movingRight = true, isUp = true; while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip! - unsigned int newPos[3]; - newPos[0] = rowNum; - newPos[1] = columnNum; + float newPos[3]; + newPos[0] = (float)rowNum; + newPos[1] = (float)columnNum; newPos[2] = heightmap[rowNum][columnNum]; - arrayBuffer.setDataElements(1, &newPos); + arrayBuffer->setDataElements(1, &newPos); if (isUp){ rowNum = rowNum + 1; isUp = false; }else if (movingRight){ - if (columnNum = this->heightmapWidth - 1){ - arrayBuffer.setDataElements(1, &newPos); - arrayBuffer.setDataElements(1, &newPos); + if (columnNum == this->heightmapWidth - 1){ + arrayBuffer->setDataElements(1, &newPos); + arrayBuffer->setDataElements(1, &newPos); movingRight = false; rowNum = rowNum + 1; } else{ @@ -59,9 +63,9 @@ void Terrain::makeTriangleMesh(){ isUp = true; } }else{ - if (columnNum = 0){ - arrayBuffer.setDataElements(1, &newPos); - arrayBuffer.setDataElements(1, &newPos); + if (columnNum == 0){ + arrayBuffer->setDataElements(1, &newPos); + arrayBuffer->setDataElements(1, &newPos); movingRight = true; rowNum = rowNum + 1; }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.bind(); - this->triangleMesh.setMode(GL_TRIANGLE_STRIP); - //this->triangleMesh.attachAllAttributes(arrayBuffer); + this->triangleMesh = ACGL::OpenGL::SharedVertexArrayObject(); + this->triangleMesh->bind(); + this->triangleMesh->setMode(GL_TRIANGLE_STRIP); + this->triangleMesh->attachAllAttributes(arrayBuffer); //TODO unbind? } void Terrain::render() { if (heightmapChanged) this->makeTriangleMesh(); - this->triangleMesh.render(); + this->triangleMesh->render(); } diff --git a/terrain.hh b/terrain.hh index 481e2ed..da831e5 100644 --- a/terrain.hh +++ b/terrain.hh @@ -10,18 +10,18 @@ class Terrain { public: - Terrain(); + Terrain(std::string filePath); + Terrain(); ~Terrain(); void load(); void render(); private: - float friction; Texture texture; std::string filePath; unsigned int heightmapWidth, heightmapHeight; - unsigned int** heightmap; //can be accessed like 'unsigned int[][]' + float** heightmap; //can be accessed like 'float[][]' bool heightmapChanged; - ACGL::OpenGL::VertexArrayObject triangleMesh; + ACGL::OpenGL::SharedVertexArrayObject triangleMesh; void makeTriangleMesh(); }; diff --git a/texture.cc b/texture.cc index 561e0a3..69aa5d0 100644 --- a/texture.cc +++ b/texture.cc @@ -13,6 +13,3 @@ Texture::~Texture() { ACGL::OpenGL::SharedTexture2D Texture::getReference() { return reference; } - -void Texture::load(){ -} diff --git a/texture.hh b/texture.hh index aebe098..3de1717 100644 --- a/texture.hh +++ b/texture.hh @@ -12,7 +12,6 @@ class Texture{ Texture(); ACGL::OpenGL::SharedTexture2D getReference(); ~Texture(); - void load(); private: ACGL::OpenGL::SharedTexture2D reference; };