Now generating seperate terrain meshes for chunks.
This commit is contained in:
parent
a7fb739204
commit
d0fed9a321
@ -229,6 +229,7 @@ void Level::setSkydomeObject(Skydome object){
|
||||
|
||||
void Level::addObject(Object* object) {
|
||||
allObjects.push_back(object);
|
||||
//int xPosition = object->getPosition().x - terrain.getHeightmapHeight
|
||||
chunks.at(0).at(0).addObject(object);
|
||||
}
|
||||
|
||||
@ -335,12 +336,13 @@ void Level::printPosition() {
|
||||
}
|
||||
|
||||
void Level::generateChunks(int chunkSize) {
|
||||
this->chunkSize = chunkSize;
|
||||
int numberChunksX = 0;
|
||||
if (terrain.getHeightmapHeight() % chunkSize == 0) {
|
||||
numberChunksX = terrain.getHeightmapHeight()/chunkSize;
|
||||
if (terrain.getHeightmapWidth() % chunkSize == 0) {
|
||||
numberChunksX = terrain.getHeightmapWidth()/chunkSize;
|
||||
}
|
||||
else {
|
||||
numberChunksX = (terrain.getHeightmapHeight()/chunkSize) + 1;
|
||||
numberChunksX = (terrain.getHeightmapWidth()/chunkSize) + 1;
|
||||
}
|
||||
int numberChunksZ = 0;
|
||||
if (terrain.getHeightmapHeight() % chunkSize == 0) {
|
||||
@ -358,3 +360,7 @@ void Level::generateChunks(int chunkSize) {
|
||||
chunks.at(i) = zChunks;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<Chunk>>* Level::getChunks() {
|
||||
return &chunks;
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ class Level {
|
||||
void setTerrain(Terrain terrain);
|
||||
void printPosition();
|
||||
void generateChunks(int chunkSize);
|
||||
std::vector<std::vector<Chunk>>* getChunks();
|
||||
private:
|
||||
lua_State* luaState=nullptr;
|
||||
std::vector<Object*> crossChunkObjects;
|
||||
@ -99,6 +100,7 @@ class Level {
|
||||
float strength;
|
||||
std::string xmlFilePath;
|
||||
glm::vec3 nextLightPosition;
|
||||
float chunkSize;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
||||
using namespace tinyxml2;
|
||||
|
||||
const int chunkSize = 50;
|
||||
|
||||
Loader::Loader() {
|
||||
}
|
||||
|
||||
@ -80,10 +82,10 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
|
||||
}
|
||||
Terrain terrain = Terrain(levelHeightmapPath);
|
||||
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->generateChunks(50.0f);
|
||||
Model terrainModel = Model(level->getTerrain()->getModel());
|
||||
// generate appropriate amount of chunks depending on the heightmap size.
|
||||
level->generateChunks(chunkSize);
|
||||
std::string terrainTexture = queryString(terrainElement, "texture");
|
||||
float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor");
|
||||
float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor");
|
||||
@ -95,10 +97,15 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
|
||||
exit(-1);
|
||||
}
|
||||
Material terrainMaterial = Material(terrainTexture, terrainAmbientFactor, terrainDiffuseFactor, terrainSpecularFactor, terrainShininess);
|
||||
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);
|
||||
for (unsigned int i = 0; i<level->getChunks()->size(); i++) {
|
||||
for (unsigned int j = 0; j<level->getChunks()->at(i).size(); j++) {
|
||||
Model terrainModel = Model(level->getTerrain()->makeTriangleMesh(i*chunkSize, j*chunkSize, (i+1)*chunkSize+1, (j+1)*chunkSize+1));
|
||||
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());
|
||||
|
||||
//load the skydome
|
||||
|
@ -29,10 +29,9 @@ void Terrain::load() {
|
||||
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) {
|
||||
startX = 0;
|
||||
}
|
||||
@ -46,7 +45,8 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) {
|
||||
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!
|
||||
ab->defineAttribute("aPosition", GL_FLOAT, 3);
|
||||
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:
|
||||
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];
|
||||
|
||||
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);
|
||||
delete abData;
|
||||
this->triangleMesh = std::make_shared<ACGL::OpenGL::VertexArrayObject>();
|
||||
this->triangleMesh->bind();
|
||||
this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
|
||||
this->triangleMesh->attachAllAttributes(ab);
|
||||
vao->bind();
|
||||
vao->setMode(GL_TRIANGLE_STRIP);
|
||||
vao->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(){
|
||||
return this->heightmap;
|
||||
}
|
||||
|
@ -5,6 +5,9 @@
|
||||
#include "material.hh"
|
||||
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
|
||||
#include "model.hh"
|
||||
|
||||
using namespace ACGL::OpenGL;
|
||||
|
||||
class Terrain {
|
||||
public:
|
||||
Terrain(std::string heightmapFilePath);
|
||||
@ -12,17 +15,15 @@ class Terrain {
|
||||
~Terrain();
|
||||
void load();
|
||||
void render();
|
||||
Model getModel();
|
||||
float** getHeightmap();
|
||||
int getHeightmapHeight();
|
||||
int getHeightmapWidth();
|
||||
void makeTriangleMesh(int startX, int startZ, int endX, int endZ);
|
||||
SharedVertexArrayObject makeTriangleMesh(int startX, int startZ, int endX, int endZ);
|
||||
private:
|
||||
Material material;
|
||||
std::string heightmapFilePath;
|
||||
int heightmapHeight, heightmapWidth;
|
||||
float** heightmap; //can be accessed like 'float[][]'
|
||||
ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
|
||||
void set_abData(float* abData, int dataCount, int rowNum, int columnNum);
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user