Now generating seperate terrain meshes for chunks.

This commit is contained in:
Faerbit 2015-03-17 12:08:49 +01:00
parent a7fb739204
commit d0fed9a321
5 changed files with 37 additions and 25 deletions

View File

@ -229,6 +229,7 @@ void Level::setSkydomeObject(Skydome object){
void Level::addObject(Object* object) { void Level::addObject(Object* object) {
allObjects.push_back(object); allObjects.push_back(object);
//int xPosition = object->getPosition().x - terrain.getHeightmapHeight
chunks.at(0).at(0).addObject(object); chunks.at(0).at(0).addObject(object);
} }
@ -335,12 +336,13 @@ void Level::printPosition() {
} }
void Level::generateChunks(int chunkSize) { void Level::generateChunks(int chunkSize) {
this->chunkSize = chunkSize;
int numberChunksX = 0; int numberChunksX = 0;
if (terrain.getHeightmapHeight() % chunkSize == 0) { if (terrain.getHeightmapWidth() % chunkSize == 0) {
numberChunksX = terrain.getHeightmapHeight()/chunkSize; numberChunksX = terrain.getHeightmapWidth()/chunkSize;
} }
else { else {
numberChunksX = (terrain.getHeightmapHeight()/chunkSize) + 1; numberChunksX = (terrain.getHeightmapWidth()/chunkSize) + 1;
} }
int numberChunksZ = 0; int numberChunksZ = 0;
if (terrain.getHeightmapHeight() % chunkSize == 0) { if (terrain.getHeightmapHeight() % chunkSize == 0) {
@ -358,3 +360,7 @@ void Level::generateChunks(int chunkSize) {
chunks.at(i) = zChunks; chunks.at(i) = zChunks;
} }
} }
std::vector<std::vector<Chunk>>* Level::getChunks() {
return &chunks;
}

View File

@ -75,6 +75,7 @@ class Level {
void setTerrain(Terrain terrain); void setTerrain(Terrain terrain);
void printPosition(); void printPosition();
void generateChunks(int chunkSize); void generateChunks(int chunkSize);
std::vector<std::vector<Chunk>>* getChunks();
private: private:
lua_State* luaState=nullptr; lua_State* luaState=nullptr;
std::vector<Object*> crossChunkObjects; std::vector<Object*> crossChunkObjects;
@ -99,6 +100,7 @@ class Level {
float strength; float strength;
std::string xmlFilePath; std::string xmlFilePath;
glm::vec3 nextLightPosition; glm::vec3 nextLightPosition;
float chunkSize;
}; };
#endif #endif

View File

@ -2,6 +2,8 @@
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh> #include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
using namespace tinyxml2; using namespace tinyxml2;
const int chunkSize = 50;
Loader::Loader() { Loader::Loader() {
} }
@ -80,10 +82,10 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
} }
Terrain terrain = Terrain(levelHeightmapPath); Terrain terrain = Terrain(levelHeightmapPath);
level->setTerrain(terrain); level->setTerrain(terrain);
// Because object gets copied a lot load it when it has reached it's final destination // Because object gets copied a lot, load it when it has reached it's final destination
level->getTerrain()->load(); level->getTerrain()->load();
level->generateChunks(50.0f); // generate appropriate amount of chunks depending on the heightmap size.
Model terrainModel = Model(level->getTerrain()->getModel()); level->generateChunks(chunkSize);
std::string terrainTexture = queryString(terrainElement, "texture"); std::string terrainTexture = queryString(terrainElement, "texture");
float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor"); float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor");
float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor"); float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor");
@ -95,10 +97,15 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
exit(-1); exit(-1);
} }
Material terrainMaterial = Material(terrainTexture, terrainAmbientFactor, terrainDiffuseFactor, terrainSpecularFactor, terrainShininess); Material terrainMaterial = Material(terrainTexture, terrainAmbientFactor, terrainDiffuseFactor, terrainSpecularFactor, terrainShininess);
Object* terrainObject = new Object(terrainModel, terrainMaterial, for (unsigned int i = 0; i<level->getChunks()->size(); i++) {
glm::vec3(-0.5*((float)level->getTerrain()->getHeightmapHeight()-1), 0.0f, -0.5f*((float)level->getTerrain()->getHeightmapWidth()-1)), for (unsigned int j = 0; j<level->getChunks()->at(i).size(); j++) {
glm::vec3(0.0f, 0.0f, 0.0f), true); Model terrainModel = Model(level->getTerrain()->makeTriangleMesh(i*chunkSize, j*chunkSize, (i+1)*chunkSize+1, (j+1)*chunkSize+1));
level->addObject(terrainObject); Object* terrainObject = new Object(terrainModel, terrainMaterial,
glm::vec3(-0.5*((float)level->getTerrain()->getHeightmapHeight()-1), 0.0f, -0.5f*((float)level->getTerrain()->getHeightmapWidth()-1)),
glm::vec3(0.0f, 0.0f, 0.0f), true);
level->addObject(terrainObject);
}
}
level->getPhysics()->addTerrain(level->getTerrain()->getHeightmapWidth(), level->getTerrain()->getHeightmapHeight(), level->getTerrain()->getHeightmap()); level->getPhysics()->addTerrain(level->getTerrain()->getHeightmapWidth(), level->getTerrain()->getHeightmapHeight(), level->getTerrain()->getHeightmap());
//load the skydome //load the skydome

View File

@ -29,10 +29,9 @@ void Terrain::load() {
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(0, 0, heightmapHeight, heightmapWidth);
} }
void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) { SharedVertexArrayObject Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
if (startX < 0) { if (startX < 0) {
startX = 0; startX = 0;
} }
@ -46,7 +45,8 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
endZ = heightmapWidth; endZ = heightmapWidth;
} }
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>(); SharedArrayBuffer ab = SharedArrayBuffer(new ArrayBuffer());
SharedVertexArrayObject vao = SharedVertexArrayObject(new VertexArrayObject());
// Do NOT change the order of this! // Do NOT change the order of this!
ab->defineAttribute("aPosition", GL_FLOAT, 3); ab->defineAttribute("aPosition", GL_FLOAT, 3);
ab->defineAttribute("aTexCoord", GL_FLOAT, 2); ab->defineAttribute("aTexCoord", GL_FLOAT, 2);
@ -54,7 +54,7 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
int rowNum = startX, columnNum = startZ, 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 = ((endX - startX) - 1) * ((endZ - startZ) * 2 + 1) + 1;
float* abData = new float[numVertices * floatsPerVertex]; float* abData = new float[numVertices * floatsPerVertex];
while(rowNum < endX){ //traversing the Triangle Strip! while(rowNum < endX){ //traversing the Triangle Strip!
@ -98,10 +98,10 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
ab->setDataElements(numVertices, abData); ab->setDataElements(numVertices, abData);
delete abData; delete abData;
this->triangleMesh = std::make_shared<ACGL::OpenGL::VertexArrayObject>(); vao->bind();
this->triangleMesh->bind(); vao->setMode(GL_TRIANGLE_STRIP);
this->triangleMesh->setMode(GL_TRIANGLE_STRIP); vao->attachAllAttributes(ab);
this->triangleMesh->attachAllAttributes(ab); return vao;
} }
@ -150,10 +150,6 @@ void Terrain::set_abData(float* abData, int dataCount, int rowNum, int columnNum
} }
} }
Model Terrain::getModel(){
return Model(this->triangleMesh);
}
float** Terrain::getHeightmap(){ float** Terrain::getHeightmap(){
return this->heightmap; return this->heightmap;
} }

View File

@ -5,6 +5,9 @@
#include "material.hh" #include "material.hh"
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh> #include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
#include "model.hh" #include "model.hh"
using namespace ACGL::OpenGL;
class Terrain { class Terrain {
public: public:
Terrain(std::string heightmapFilePath); Terrain(std::string heightmapFilePath);
@ -12,17 +15,15 @@ class Terrain {
~Terrain(); ~Terrain();
void load(); void load();
void render(); void render();
Model getModel();
float** getHeightmap(); float** getHeightmap();
int getHeightmapHeight(); int getHeightmapHeight();
int getHeightmapWidth(); int getHeightmapWidth();
void makeTriangleMesh(int startX, int startZ, int endX, int endZ); SharedVertexArrayObject makeTriangleMesh(int startX, int startZ, int endX, int endZ);
private: private:
Material material; Material material;
std::string heightmapFilePath; std::string heightmapFilePath;
int heightmapHeight, heightmapWidth; int heightmapHeight, heightmapWidth;
float** heightmap; //can be accessed like 'float[][]' float** heightmap; //can be accessed like 'float[][]'
ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
void set_abData(float* abData, int dataCount, int rowNum, int columnNum); void set_abData(float* abData, int dataCount, int rowNum, int columnNum);
}; };