Introduced chunk class.

This commit is contained in:
Faerbit 2015-03-16 16:58:50 +01:00
parent 73f7ec6a70
commit 7b742e913f
5 changed files with 59 additions and 18 deletions

21
game/chunk.cc Normal file
View File

@ -0,0 +1,21 @@
#include "chunk.hh"
Chunk::Chunk() {
}
Chunk::~Chunk() {
for(unsigned int i = 0; i<objects.size(); i++) {
delete objects.at(i);
}
}
void Chunk::render(SharedShaderProgram shader, bool lightingPass, bool texturePass,
glm::mat4* viewProjcetionMatrix, std::vector<glm::mat4>* additionalMatrices) {
for(unsigned int i = 0; i<objects.size(); i++) {
objects.at(i)->render(shader, lightingPass, texturePass, viewProjcetionMatrix, additionalMatrices);
}
}
void Chunk::addObject(Object* object) {
objects.push_back(object);
}

17
game/chunk.hh Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include "object.hh"
#include <ACGL/OpenGL/Objects.hh>
using namespace ACGL::OpenGL;
class Chunk {
public:
Chunk();
~Chunk();
void render(SharedShaderProgram shader, bool lightingPass, bool texturePass,
glm::mat4* viewProjcetionMatrix, std::vector<glm::mat4>* additionalMatrices=0);
void addObject(Object* object);
private:
std::vector<Object*> objects;
};

View File

@ -16,9 +16,6 @@ Level::~Level() {
if (luaState!=nullptr) { if (luaState!=nullptr) {
lua_close(luaState); lua_close(luaState);
} }
for(unsigned int i = 0; i<objects.size(); i++) {
delete(objects.at(i));
}
delete(waterPlane); delete(waterPlane);
} }
@ -55,12 +52,12 @@ void Level::load() {
void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
glm::mat4* viewProjectionMatrix, std::vector<glm::mat4>* shadowVPs) { glm::mat4* viewProjectionMatrix, std::vector<glm::mat4>* shadowVPs) {
for(unsigned int i = 0; i<objects.size(); i++) { for(unsigned int i = 0; i<chunks.size(); i++) {
if (lightingPass) { if (lightingPass) {
objects.at(i)->render(shader, lightingPass, true, viewProjectionMatrix, shadowVPs); chunks.at(i).render(shader, lightingPass, true, viewProjectionMatrix, shadowVPs);
} }
else { else {
objects.at(i)->render(shader, lightingPass, false, viewProjectionMatrix, shadowVPs); chunks.at(i).render(shader, lightingPass, false, viewProjectionMatrix, shadowVPs);
} }
} }
if (lightingPass && waterPlane) { if (lightingPass && waterPlane) {
@ -191,8 +188,8 @@ float Level::getSkydomeSize() {
return this->skydomeSize; return this->skydomeSize;
} }
std::vector<Object*>* Level::getObjects() { std::vector<Object*>* Level::getAllObjects() {
return &objects; return &allObjects;
} }
std::vector<Object*>* Level::getPhysicsObjects() { std::vector<Object*>* Level::getPhysicsObjects() {
@ -229,7 +226,8 @@ void Level::setSkydomeObject(Skydome object){
} }
void Level::addObject(Object* object) { void Level::addObject(Object* object) {
objects.push_back(object); allObjects.push_back(object);
chunks.at(0).addObject(object);
} }
void Level::addPhysicsObject(Object* object) { void Level::addPhysicsObject(Object* object) {
@ -267,10 +265,6 @@ Physics* Level::getPhysics() {
return &physics; return &physics;
} }
unsigned int Level::getObjectsVectorSize() {
return objects.size();
}
unsigned int Level::getPhysicsObjectsVectorSize() { unsigned int Level::getPhysicsObjectsVectorSize() {
return physicsObjects.size(); return physicsObjects.size();
} }
@ -337,3 +331,8 @@ void Level::printPosition() {
printf("Player position: %2.2f, %2.2f, %2.2f\n", cameraCenter->getPosition().x, printf("Player position: %2.2f, %2.2f, %2.2f\n", cameraCenter->getPosition().x,
cameraCenter->getPosition().y, cameraCenter->getPosition().z); cameraCenter->getPosition().y, cameraCenter->getPosition().z);
} }
void Level::generateChunks(int chunkSize) {
chunks = std::vector<Chunk>(1);
chunks.at(0) = Chunk();
}

View File

@ -12,6 +12,7 @@
#include "trigger.hh" #include "trigger.hh"
#include "skydome.hh" #include "skydome.hh"
#include "keyboardState.hh" #include "keyboardState.hh"
#include "chunk.hh"
extern "C" { extern "C" {
#include "lua.h" #include "lua.h"
@ -43,7 +44,7 @@ class Level {
float getSkydomeSize(); float getSkydomeSize();
void setWaterPlane(Object* water); void setWaterPlane(Object* water);
Skydome* getSkydome(); Skydome* getSkydome();
std::vector<Object*>* getObjects(); std::vector<Object*>* getAllObjects();
std::vector<Object*>* getPhysicsObjects(); std::vector<Object*>* getPhysicsObjects();
void moveObject(int objectIndex, float strength, float xPos, float yPos, float zPos); void moveObject(int objectIndex, float strength, float xPos, float yPos, float zPos);
void setStrength(float strength); void setStrength(float strength);
@ -56,9 +57,8 @@ class Level {
void setFogColourNight(glm::vec4 colour); void setFogColourNight(glm::vec4 colour);
void setDirectionalLight(Light light); void setDirectionalLight(Light light);
void setSunDirection(float x, float y, float z); void setSunDirection(float x, float y, float z);
Physics* getPhysics();
unsigned int getObjectsVectorSize();
unsigned int getPhysicsObjectsVectorSize(); unsigned int getPhysicsObjectsVectorSize();
Physics* getPhysics();
void setCameraCenter(Object* object); void setCameraCenter(Object* object);
void addLight(Light light); void addLight(Light light);
void preloadLightPosition(float xPos, float yPos, float zPos); void preloadLightPosition(float xPos, float yPos, float zPos);
@ -74,10 +74,13 @@ class Level {
void activateEndgame(); void activateEndgame();
void setTerrain(Terrain terrain); void setTerrain(Terrain terrain);
void printPosition(); void printPosition();
void generateChunks(int chunkSize);
private: private:
lua_State* luaState=nullptr; lua_State* luaState=nullptr;
std::vector<Object*> objects; std::vector<Object*> crossChunkObjects;
std::vector<Object*> allObjects;
std::vector<Object*> physicsObjects; std::vector<Object*> physicsObjects;
std::vector<Chunk> chunks;
std::vector<Light> lights; std::vector<Light> lights;
std::vector<Trigger> triggers; std::vector<Trigger> triggers;
Object* waterPlane; Object* waterPlane;

View File

@ -82,6 +82,7 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
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);
Model terrainModel = Model(level->getTerrain()->getModel()); Model terrainModel = Model(level->getTerrain()->getModel());
std::string terrainTexture = queryString(terrainElement, "texture"); std::string terrainTexture = queryString(terrainElement, "texture");
float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor"); float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor");
@ -341,7 +342,7 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
//create an identifier for this object //create an identifier for this object
std::vector<int> objectIdentifier = std::vector<int>(5); std::vector<int> objectIdentifier = std::vector<int>(5);
objectIdentifier[0] = level->getObjectsVectorSize()-1; objectIdentifier[0] = level->getAllObjects()->size()-1;
if (physicType.compare("None") == 0){ if (physicType.compare("None") == 0){
objectIdentifier[1] = 0; objectIdentifier[1] = 0;
}else{ }else{