From 44d829e5649b209126fb6a092d3a3761d29fdcfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Thu, 6 Nov 2014 19:21:24 +0100 Subject: [PATCH] added most of the functionallity to make a triangle strip from the heightmap --- terrain.cc | 87 +++++++++++++++++++++++++++++++++++++++++++++++------- terrain.hh | 6 ++++ 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/terrain.cc b/terrain.cc index 53f258d..9e655ea 100644 --- a/terrain.cc +++ b/terrain.cc @@ -9,26 +9,91 @@ Terrain::~Terrain() { void Terrain::load() { std::ifstream terrain_png(this->filePath); - unsigned int width, height, rowNum, columnNum; + unsigned int rowNum, columnNum; - terrain_png.seekg(16); //skip part of the header - terrain_png.read((char *)&width, 4); //read width - terrain_png.read((char *)&height, 4); //read height - width = ntohl(width); //convert from host to network byte order - height = ntohl(height); + 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); - heightmap = new unsigned int*[height]; //initialize the heightmap - for(rowNum=0; rowNumheightmapHeight]; //initialize the heightmap + for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap + heightmap[rowNum] = new unsigned int[this->heightmapWidth]; + for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){ terrain_png.read((char *)&heightmap[rowNum][columnNum], 1); } } + this->makeTriangleMesh(); + heightmapChanged = false; + texture.load(); +} - +void Terrain::makeTriangleMesh(){ + + ACGL::OpenGL::ArrayBuffer arrayBuffer = ACGL::OpenGL::ArrayBuffer(); + arrayBuffer.defineAttribute("pos", GL_UNSIGNED_INT, 3); + + 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; + newPos[2] = heightmap[rowNum][columnNum]; + 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); + movingRight = false; + rowNum = rowNum + 1; + } else{ + rowNum = rowNum - 1; + columnNum = columnNum + 1; + isUp = true; + } + }else{ + if (columnNum = 0){ + arrayBuffer.setDataElements(1, &newPos); + arrayBuffer.setDataElements(1, &newPos); + movingRight = true; + rowNum = rowNum + 1; + }else{ + rowNum = rowNum - 1; + columnNum = columnNum - 1; + isUp = true; + } + } + } + + //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); + //TODO unbind? } void Terrain::render() { + if (heightmapChanged) + this->makeTriangleMesh(); + this->triangleMesh.render(); } + + + + + + + + + + + + + diff --git a/terrain.hh b/terrain.hh index 07a85be..481e2ed 100644 --- a/terrain.hh +++ b/terrain.hh @@ -6,6 +6,7 @@ #include //#include //on Windows #include //on Unix +#include class Terrain { public: @@ -17,7 +18,12 @@ class Terrain { float friction; Texture texture; std::string filePath; + unsigned int heightmapWidth, heightmapHeight; unsigned int** heightmap; //can be accessed like 'unsigned int[][]' + bool heightmapChanged; + ACGL::OpenGL::VertexArrayObject triangleMesh; + + void makeTriangleMesh(); }; #endif