Adding parameters to the makeTrinagleMesh to control the area for which it creates the terrain. Also changing most unsigned ints to ints to make said more roboust in use.
This commit is contained in:
parent
15412b9b45
commit
ab7d1c81df
@ -14,22 +14,38 @@ Terrain::~Terrain() {
|
|||||||
|
|
||||||
void Terrain::load() {
|
void Terrain::load() {
|
||||||
std::vector<unsigned char> image; //the raw pixels
|
std::vector<unsigned char> image; //the raw pixels
|
||||||
unsigned error = lodepng::decode(image, heightmapWidth, heightmapHeight, heightmapFilePath);
|
unsigned int heightmapHeightLoading = 0;
|
||||||
|
unsigned int heightmapWidthLoading = 0;
|
||||||
|
unsigned error = lodepng::decode(image, heightmapWidthLoading, heightmapHeightLoading, heightmapFilePath);
|
||||||
if (error) {
|
if (error) {
|
||||||
std::cout << "Decoder error " << error << " from Terrain::load: " << lodepng_error_text(error) << std::endl;
|
std::cout << "Decoder error " << error << " from Terrain::load: " << lodepng_error_text(error) << std::endl;
|
||||||
}
|
}
|
||||||
|
this->heightmapHeight = heightmapHeightLoading;
|
||||||
|
this->heightmapWidth = heightmapWidthLoading;
|
||||||
this->heightmap = new float*[this->heightmapWidth]; //initialize the heightmap
|
this->heightmap = new float*[this->heightmapWidth]; //initialize the heightmap
|
||||||
for(unsigned int columnNum = 0; columnNum < this->heightmapWidth; columnNum++){ //read in the heightmap
|
for(int columnNum = 0; columnNum < this->heightmapWidth; columnNum++){ //read in the heightmap
|
||||||
this->heightmap[columnNum] = new float[this->heightmapHeight];
|
this->heightmap[columnNum] = new float[this->heightmapHeight];
|
||||||
for(unsigned int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){
|
for(int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){
|
||||||
this->heightmap[columnNum][rowNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here
|
this->heightmap[columnNum][rowNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->makeTriangleMesh();
|
this->makeTriangleMesh(0, 0, heightmapHeight, heightmapWidth);
|
||||||
heightmapChanged = false; //no need to make a TriangleMesh again before rendering
|
heightmapChanged = false; //no need to make a TriangleMesh again before rendering
|
||||||
}
|
}
|
||||||
|
|
||||||
void Terrain::makeTriangleMesh(){
|
void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
|
||||||
|
if (startX < 0) {
|
||||||
|
startX = 0;
|
||||||
|
}
|
||||||
|
if (startZ < 0) {
|
||||||
|
startZ = 0;
|
||||||
|
}
|
||||||
|
if (endX > heightmapHeight) {
|
||||||
|
endX = heightmapHeight;
|
||||||
|
}
|
||||||
|
if (endZ > heightmapWidth) {
|
||||||
|
endZ = heightmapWidth;
|
||||||
|
}
|
||||||
|
|
||||||
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
|
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
|
||||||
// Do NOT change the order of this!
|
// Do NOT change the order of this!
|
||||||
@ -37,12 +53,12 @@ void Terrain::makeTriangleMesh(){
|
|||||||
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
|
||||||
ab->defineAttribute("aNormal", GL_FLOAT, 3);
|
ab->defineAttribute("aNormal", GL_FLOAT, 3);
|
||||||
|
|
||||||
unsigned int rowNum=0, columnNum=0, dataCount=0, floatsPerVertex=8; //initializing:
|
int rowNum = startX, columnNum = startZ, dataCount=0, floatsPerVertex=8; //initializing:
|
||||||
bool movingRight = true, isUp = true;
|
bool movingRight = true, isUp = true;
|
||||||
int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1;
|
int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1;
|
||||||
float* abData = new float[numVertices * floatsPerVertex];
|
float* abData = new float[numVertices * floatsPerVertex];
|
||||||
|
|
||||||
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
|
while(rowNum < endX){ //traversing the Triangle Strip!
|
||||||
set_abData(abData, dataCount, rowNum, columnNum);
|
set_abData(abData, dataCount, rowNum, columnNum);
|
||||||
dataCount += floatsPerVertex;
|
dataCount += floatsPerVertex;
|
||||||
if (isUp){
|
if (isUp){
|
||||||
@ -50,7 +66,7 @@ void Terrain::makeTriangleMesh(){
|
|||||||
isUp = false;
|
isUp = false;
|
||||||
}
|
}
|
||||||
else if (movingRight) {
|
else if (movingRight) {
|
||||||
if (columnNum == this->heightmapWidth - 1) {
|
if (columnNum == endZ - 1) {
|
||||||
set_abData(abData, dataCount, rowNum, columnNum);
|
set_abData(abData, dataCount, rowNum, columnNum);
|
||||||
dataCount += floatsPerVertex;
|
dataCount += floatsPerVertex;
|
||||||
set_abData(abData, dataCount, rowNum, columnNum);
|
set_abData(abData, dataCount, rowNum, columnNum);
|
||||||
@ -65,7 +81,7 @@ void Terrain::makeTriangleMesh(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (columnNum == 0){
|
if (columnNum == startZ){
|
||||||
set_abData(abData, dataCount, rowNum, columnNum);
|
set_abData(abData, dataCount, rowNum, columnNum);
|
||||||
dataCount += floatsPerVertex;
|
dataCount += floatsPerVertex;
|
||||||
set_abData(abData, dataCount, rowNum, columnNum);
|
set_abData(abData, dataCount, rowNum, columnNum);
|
||||||
@ -91,7 +107,7 @@ void Terrain::makeTriangleMesh(){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Terrain::set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum){
|
void Terrain::set_abData(float* abData, int dataCount, int rowNum, int columnNum){
|
||||||
//set Position
|
//set Position
|
||||||
abData[dataCount] = (float)rowNum;
|
abData[dataCount] = (float)rowNum;
|
||||||
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
abData[dataCount+1] = heightmap[rowNum][columnNum];
|
||||||
@ -143,10 +159,10 @@ float** Terrain::getHeightmap(){
|
|||||||
return this->heightmap;
|
return this->heightmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Terrain::getHeightmapHeight(){
|
int Terrain::getHeightmapHeight(){
|
||||||
return this->heightmapHeight;
|
return this->heightmapHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Terrain::getHeightmapWidth(){
|
int Terrain::getHeightmapWidth(){
|
||||||
return this->heightmapWidth;
|
return this->heightmapWidth;
|
||||||
}
|
}
|
||||||
|
@ -14,18 +14,17 @@ class Terrain {
|
|||||||
void render();
|
void render();
|
||||||
Model getModel();
|
Model getModel();
|
||||||
float** getHeightmap();
|
float** getHeightmap();
|
||||||
unsigned int getHeightmapHeight();
|
int getHeightmapHeight();
|
||||||
unsigned int getHeightmapWidth();
|
int getHeightmapWidth();
|
||||||
|
void makeTriangleMesh(int startX, int startZ, int endX, int endZ);
|
||||||
private:
|
private:
|
||||||
Material material;
|
Material material;
|
||||||
std::string heightmapFilePath;
|
std::string heightmapFilePath;
|
||||||
unsigned int heightmapHeight, heightmapWidth;
|
int heightmapHeight, heightmapWidth;
|
||||||
float** heightmap; //can be accessed like 'float[][]'
|
float** heightmap; //can be accessed like 'float[][]'
|
||||||
bool heightmapChanged;
|
bool heightmapChanged;
|
||||||
ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
|
ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
|
||||||
void makeTriangleMesh();
|
void set_abData(float* abData, int dataCount, int rowNum, int columnNum);
|
||||||
void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user