Now also using the correct chunks for the objects and rendering only the chunks which are necessary.

This commit is contained in:
Faerbit 2015-03-17 14:54:18 +01:00
parent d0fed9a321
commit ac48ab8cc0
5 changed files with 46 additions and 13 deletions

View File

@ -22,7 +22,7 @@ void Application::init()
}
void Application::initLevel() {
this->level = Level(levelXmlPath);
this->level = Level(levelXmlPath, farPlane);
level.getPhysics()->init(geometryPath);
// Don't change this!
ignoredMouseUpdates = 0;

View File

@ -2,11 +2,12 @@
#include "loader.hh"
#include <string>
Level::Level(std::string xmlFilePath){
Level::Level(std::string xmlFilePath, float farPlane){
// default value
skydomeSize = 50.0f;
physics = Physics();
this->xmlFilePath = xmlFilePath;
this->farPlane = farPlane;
}
Level::Level() {
@ -52,8 +53,33 @@ void Level::load() {
void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
glm::mat4* viewProjectionMatrix, std::vector<glm::mat4>* shadowVPs) {
for(unsigned int i = 0; i<chunks.size(); i++) {
for(unsigned int j = 0; j<chunks.at(i).size(); j++) {
int renderDistance = 0;
if ((int)farPlane % chunkSize == 0) {
renderDistance = farPlane/chunkSize;
}
else {
renderDistance = (farPlane/chunkSize) + 1;
}
int xPosition = ((int)cameraCenter->getPosition().x + (terrain.getHeightmapWidth()/2))/chunkSize;
int zPosition = ((int)cameraCenter->getPosition().z + (terrain.getHeightmapHeight()/2))/chunkSize;
int xStart = xPosition - renderDistance;
unsigned int xEnd = xPosition + renderDistance;
int zStart = zPosition - renderDistance;
unsigned int zEnd = zPosition + renderDistance;
if (xStart < 0) {
xStart = 0;
}
if (zStart < 0) {
zStart = 0;
}
if (xEnd >= chunks.size()) {
xEnd = chunks.size()-1;
}
if (zEnd >= chunks.at(0).size()) {
zEnd = chunks.at(0).size()-1;
}
for(unsigned int i = xStart; i<=xEnd; i++) {
for(unsigned int j = zStart; j<=zEnd; j++) {
if (lightingPass) {
chunks.at(i).at(j).render(shader, lightingPass, true, viewProjectionMatrix, shadowVPs);
}
@ -229,8 +255,13 @@ 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);
int xPosition = ((int)object->getPosition().x + (terrain.getHeightmapWidth()/2))/chunkSize;
int zPosition = ((int)object->getPosition().z + (terrain.getHeightmapHeight()/2))/chunkSize;
chunks.at(xPosition).at(zPosition).addObject(object);
}
void Level::addToSpecificChunk(Object* object, int xPosition, int zPosition) {
chunks.at(xPosition).at(zPosition).addObject(object);
}
void Level::addPhysicsObject(Object* object) {

View File

@ -23,7 +23,7 @@ extern "C" {
class Level {
public:
Level(std::string xmlFilePath);
Level(std::string xmlFilePath, float farPlane);
Level();
~Level();
void load();
@ -76,6 +76,7 @@ class Level {
void printPosition();
void generateChunks(int chunkSize);
std::vector<std::vector<Chunk>>* getChunks();
void addToSpecificChunk(Object* object, int xPosition, int zPosition);
private:
lua_State* luaState=nullptr;
std::vector<Object*> crossChunkObjects;
@ -100,7 +101,8 @@ class Level {
float strength;
std::string xmlFilePath;
glm::vec3 nextLightPosition;
float chunkSize;
int chunkSize;
float farPlane;
};
#endif

View File

@ -103,7 +103,7 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
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->addToSpecificChunk(terrainObject, i, j);
}
}
level->getPhysics()->addTerrain(level->getTerrain()->getHeightmapWidth(), level->getTerrain()->getHeightmapHeight(), level->getTerrain()->getHeightmap());

View File

@ -38,11 +38,11 @@ SharedVertexArrayObject Terrain::makeTriangleMesh(int startX, int startZ, int en
if (startZ < 0) {
startZ = 0;
}
if (endX > heightmapHeight) {
endX = heightmapHeight;
if (endX > heightmapWidth) {
endX = heightmapWidth;
}
if (endZ > heightmapWidth) {
endZ = heightmapWidth;
if (endZ > heightmapHeight) {
endZ = heightmapHeight;
}
SharedArrayBuffer ab = SharedArrayBuffer(new ArrayBuffer());