fixed terrain class and removed load from texture
This commit is contained in:
parent
8dca1705fb
commit
2d1328b96b
54
terrain.cc
54
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();
|
||||
}
|
||||
|
||||
|
||||
|
@ -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();
|
||||
};
|
||||
|
@ -13,6 +13,3 @@ Texture::~Texture() {
|
||||
ACGL::OpenGL::SharedTexture2D Texture::getReference() {
|
||||
return reference;
|
||||
}
|
||||
|
||||
void Texture::load(){
|
||||
}
|
||||
|
@ -12,7 +12,6 @@ class Texture{
|
||||
Texture();
|
||||
ACGL::OpenGL::SharedTexture2D getReference();
|
||||
~Texture();
|
||||
void load();
|
||||
private:
|
||||
ACGL::OpenGL::SharedTexture2D reference;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user