From 925eb6283188d750d9bf9facf215338867868664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Mon, 3 Nov 2014 17:19:22 +0100 Subject: [PATCH 1/2] implemented loading of the heightmap from a png to unsigned int** --- terrain.cc | 29 ++++++++++++++++++++++++++++- terrain.hh | 6 ++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/terrain.cc b/terrain.cc index 9b11ed1..c6190ae 100644 --- a/terrain.cc +++ b/terrain.cc @@ -1,7 +1,34 @@ #include "terrain.hh" -Terrain::Terrain() { +Terrain::Terrain(){ } Terrain::~Terrain() { } + + +void Terrain::load() { + std::ifstream terrain_png(this->filePath); + unsigned int width, height, rowNum, columnNum; + + terrain_png.seekg(16); + terrain_png.read((char *)&width, 4); + terrain_png.read((char *)&height, 4); + width = ntohl(width); + height = ntohl(height); + + heightmap = new unsigned int*[height]; + for(rowNum=0; rowNum #include "texture.hh" +#include +#include +#include "png.h" +//#include //on windows +#include //on Unix class Terrain { public: @@ -14,6 +19,7 @@ class Terrain { float friction; Texture texture; std::string filePath; + unsigned int** heightmap; }; #endif From 5d2acc0e53c259da5aab2b2871e53d49c9308852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Tue, 4 Nov 2014 12:08:47 +0100 Subject: [PATCH 2/2] Fixed a bug in loading the heightmap and added comments --- terrain.cc | 18 +++++++++--------- terrain.hh | 6 ++---- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/terrain.cc b/terrain.cc index c6190ae..90bd462 100644 --- a/terrain.cc +++ b/terrain.cc @@ -11,17 +11,17 @@ void Terrain::load() { std::ifstream terrain_png(this->filePath); unsigned int width, height, rowNum, columnNum; - terrain_png.seekg(16); - terrain_png.read((char *)&width, 4); - terrain_png.read((char *)&height, 4); - width = ntohl(width); - height = ntohl(height); + 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); - heightmap = new unsigned int*[height]; - for(rowNum=0; rowNum #include "texture.hh" #include -#include -#include "png.h" -//#include //on windows +//#include //on Windows #include //on Unix class Terrain { @@ -19,7 +17,7 @@ class Terrain { float friction; Texture texture; std::string filePath; - unsigned int** heightmap; + unsigned int** heightmap; //can be accessed like 'unsigned int[][]' }; #endif