327 lines
15 KiB
C++
327 lines
15 KiB
C++
#include "level.hh"
|
|
using namespace tinyxml2;
|
|
|
|
|
|
Level::Level(std::string levelNum){
|
|
this->levelNum = levelNum;
|
|
this->terrain = Terrain(levelNum);
|
|
skydomeSize = 50.0f;
|
|
}
|
|
|
|
Level::Level() {
|
|
}
|
|
|
|
Level::~Level() {
|
|
for(unsigned int i = 0; i<objects.size(); i++) {
|
|
delete(objects.at(i));
|
|
}
|
|
}
|
|
|
|
|
|
void Level::errorCheck(XMLError error){
|
|
if (error) {
|
|
printf("XMLError: ");
|
|
if (error == XML_WRONG_ATTRIBUTE_TYPE) {
|
|
printf("Wrong attribute type.\n");
|
|
}
|
|
else if (error == XML_NO_ATTRIBUTE) {
|
|
printf("No attribute.\n");
|
|
}
|
|
else if (error == XML_CAN_NOT_CONVERT_TEXT) {
|
|
printf("Can not convert text.\n");
|
|
}
|
|
else if (error == XML_NO_TEXT_NODE) {
|
|
printf("No text.\n");
|
|
}
|
|
else {
|
|
printf("Unknown error.\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
void Level::load() {
|
|
this->physics = Physics();
|
|
this->physics.init();
|
|
|
|
// currently hard coded should later read this stuff out of a file
|
|
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
|
|
|
|
// load terrain
|
|
this->terrain.load();
|
|
Model terrainModel = Model(this->terrain.getModel());
|
|
// load a texture:
|
|
Material terrainMaterial = Material("seamlessTerrain.png", 0.1f, 0.8f, 0.2f, 3.0f);
|
|
//Create object
|
|
Object* terrainObject = new Object(terrainModel, terrainMaterial,
|
|
glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), 0.0f, -0.5f*(float)this->terrain.getHeightmapWidth()),
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
|
objects.push_back(terrainObject);
|
|
|
|
//addTerrainPhysic
|
|
physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap());
|
|
|
|
//Loading Objects via xml
|
|
XMLDocument* doc = new XMLDocument();
|
|
const char* xmlFile = ("../Levels/ObjectSetups/Level" + levelNum + ".xml").c_str();
|
|
doc->LoadFile(xmlFile);
|
|
if (doc->ErrorID()!=0){
|
|
printf("Could not open ObjectSetupXml!\n");
|
|
exit(-1);
|
|
}
|
|
XMLDocument* compositions = new XMLDocument();
|
|
const char* compositionsFile = "../Levels/ObjectSetups/Compositions.xml";
|
|
compositions->LoadFile(compositionsFile);
|
|
if (compositions->ErrorID()!=0){
|
|
printf("Could not open Compositions!\n");
|
|
exit(-1);
|
|
}
|
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
|
int thisType = 0;
|
|
errorCheck(thisComposition->FirstChildElement("typeID")->QueryIntText(&thisType));
|
|
XMLElement* composition = compositions->FirstChildElement("composition");
|
|
for(; composition; composition=composition->NextSiblingElement("composition")){
|
|
int compositionType = 0;
|
|
errorCheck(composition->FirstChildElement("typeID")->QueryIntText(&compositionType));
|
|
if(thisType == compositionType){
|
|
XMLElement* object = composition->FirstChildElement("object");
|
|
for(; object; object=object->NextSiblingElement("object")){
|
|
const char* charModelPath = object->FirstChildElement("modelPath")->GetText();
|
|
if(charModelPath == NULL){
|
|
printf("XMLError: No modelPath found in object.\n");
|
|
}
|
|
std::string modelPath = charModelPath;
|
|
float objectScale, compScale;
|
|
errorCheck(object->FirstChildElement("scale")->QueryFloatText(&objectScale));
|
|
errorCheck(thisComposition->FirstChildElement("scale")->QueryFloatText(&compScale));
|
|
Model model = Model(modelPath, objectScale * compScale);
|
|
Material material;
|
|
XMLElement* objectData = compositions->FirstChildElement("objectData");
|
|
for(; objectData; objectData=objectData->NextSiblingElement("objectData")){
|
|
const char* charDataModelPath = objectData->FirstChildElement("modelPath")->GetText();
|
|
if(charDataModelPath == NULL){
|
|
printf("XMLError: No modelPath found in objectData.\n");
|
|
}
|
|
std::string dataModelPath = charDataModelPath;
|
|
if(dataModelPath == modelPath){
|
|
float ambientFactor, diffuseFactor, specularFactor, shininess;
|
|
errorCheck(objectData->FirstChildElement("ambientFactor")->QueryFloatText(&ambientFactor));
|
|
errorCheck(objectData->FirstChildElement("diffuseFactor")->QueryFloatText(&diffuseFactor));
|
|
errorCheck(objectData->FirstChildElement("specularFactor")->QueryFloatText(&specularFactor));
|
|
errorCheck(objectData->FirstChildElement("shininess")->QueryFloatText(&shininess));
|
|
const char* charTexturePath = objectData->FirstChildElement("texturePath")->GetText();
|
|
if(charTexturePath == NULL){
|
|
printf("XMLError: No texturePath found in objectData.\n");
|
|
}
|
|
std::string texturePath = charTexturePath;
|
|
material = Material(texturePath, ambientFactor, diffuseFactor, specularFactor, shininess);
|
|
}
|
|
}
|
|
float compXPos, compYOffset, compZPos;
|
|
glm::vec3 objectOffset, compRot;
|
|
errorCheck(object->FirstChildElement("xOffset")->QueryFloatText(&objectOffset[0]));
|
|
errorCheck(object->FirstChildElement("yOffset")->QueryFloatText(&objectOffset[1]));
|
|
errorCheck(object->FirstChildElement("zOffset")->QueryFloatText(&objectOffset[2]));
|
|
errorCheck(thisComposition->FirstChildElement("xPos")->QueryFloatText(&compXPos));
|
|
errorCheck(thisComposition->FirstChildElement("yOffset")->QueryFloatText(&compYOffset));
|
|
errorCheck(thisComposition->FirstChildElement("zPos")->QueryFloatText(&compZPos));
|
|
errorCheck(thisComposition->FirstChildElement("xRot")->QueryFloatText(&compRot[0]));
|
|
errorCheck(thisComposition->FirstChildElement("yRot")->QueryFloatText(&compRot[1]));
|
|
errorCheck(thisComposition->FirstChildElement("zRot")->QueryFloatText(&compRot[2]));
|
|
glm::vec3 compPos = glm::vec3(compXPos,
|
|
compYOffset+terrain.getHeightmap()[int(compXPos-0.5+0.5*terrain.getHeightmapHeight())]
|
|
[int(compZPos-0.5+0.5*terrain.getHeightmapWidth())],
|
|
compZPos);
|
|
objectOffset = objectOffset * compScale;
|
|
glm::vec4 rotatedObjectOffset = glm::rotate(compRot.x, glm::vec3(1.0f, 0.0f, 0.0f))
|
|
* glm::rotate(compRot.y, glm::vec3(0.0f, 1.0f, 0.0f))
|
|
* glm::rotate(compRot.z, glm::vec3(0.0f, 0.0f, 1.0f))
|
|
* glm::vec4(objectOffset, 0);
|
|
glm::vec3 objectPosition = compPos + glm::vec3(rotatedObjectOffset.x,rotatedObjectOffset.y,rotatedObjectOffset.z);
|
|
Object* object = new Object(model, material, objectPosition, compRot);
|
|
objects.push_back(object);
|
|
//TODO if object has physics: physicObjects.push_back(object); //should not all objects have physics in the end?
|
|
//TODO add object to physics
|
|
//if(compositionType == 20){
|
|
// cameraCenter = object;
|
|
// this->physics.addPlayer(1.25f,*object,8.0f,physicObjects.size());
|
|
//}
|
|
}
|
|
XMLElement* light = composition->FirstChildElement("light");
|
|
for(; light; light=light->NextSiblingElement("light")){
|
|
glm::vec3 compRot, lightOffset, lightColour;
|
|
float compScale, compXPos, compYOffset, compZPos, lightIntensity;
|
|
errorCheck(thisComposition->FirstChildElement("scale")->QueryFloatText(&compScale));
|
|
errorCheck(light->FirstChildElement("xOffset")->QueryFloatText(&lightOffset[0]));
|
|
errorCheck(light->FirstChildElement("yOffset")->QueryFloatText(&lightOffset[1]));
|
|
errorCheck(light->FirstChildElement("zOffset")->QueryFloatText(&lightOffset[2]));
|
|
errorCheck(thisComposition->FirstChildElement("xPos")->QueryFloatText(&compXPos));
|
|
errorCheck(thisComposition->FirstChildElement("yOffset")->QueryFloatText(&compYOffset));
|
|
errorCheck(thisComposition->FirstChildElement("zPos")->QueryFloatText(&compZPos));
|
|
errorCheck(thisComposition->FirstChildElement("xRot")->QueryFloatText(&compRot[0]));
|
|
errorCheck(thisComposition->FirstChildElement("yRot")->QueryFloatText(&compRot[1]));
|
|
errorCheck(thisComposition->FirstChildElement("zRot")->QueryFloatText(&compRot[2]));
|
|
errorCheck(light->FirstChildElement("rColour")->QueryFloatText(&lightColour[0]));
|
|
errorCheck(light->FirstChildElement("gColour")->QueryFloatText(&lightColour[1]));
|
|
errorCheck(light->FirstChildElement("bColour")->QueryFloatText(&lightColour[2]));
|
|
errorCheck(light->FirstChildElement("intensity")->QueryFloatText(&lightIntensity));
|
|
glm::vec3 compPos = glm::vec3(compXPos,
|
|
compYOffset+terrain.getHeightmap()[int(compXPos-0.5+0.5*terrain.getHeightmapHeight())]
|
|
[int(compZPos-0.5+0.5*terrain.getHeightmapWidth())],
|
|
compZPos);
|
|
lightOffset = lightOffset * compScale;
|
|
glm::vec4 rotatedLightOffset = glm::rotate(compRot.x, glm::vec3(1.0f, 0.0f, 0.0f))
|
|
* glm::rotate(compRot.y, glm::vec3(0.0f, 1.0f, 0.0f))
|
|
* glm::rotate(compRot.z, glm::vec3(0.0f, 0.0f, 1.0f))
|
|
* glm::vec4(lightOffset, 0);
|
|
glm::vec3 lightPosition = compPos + glm::vec3(rotatedLightOffset.x,rotatedLightOffset.y,rotatedLightOffset.z);
|
|
Light light = Light(lightPosition, lightColour, lightIntensity);
|
|
lights.push_back(light);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//add player
|
|
Model marbleModel = Model("marbleSmooth.obj", 0.75f);
|
|
Material marbleMaterial = Material("marbleTexture_small.png", 0.1f, 0.5f, 0.5f, 3.0f);
|
|
Object* object = new Object(marbleModel, marbleMaterial, glm::vec3(2.0f, 10.0f, 2.0f),
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
|
objects.push_back(object);
|
|
physicObjects.push_back(object);
|
|
this->physics.addPlayer(1.25f,*object,8.0f,physicObjects.size());
|
|
cameraCenter = object;
|
|
|
|
Model skydomeModel = Model("skydome.obj", skydomeSize);
|
|
Material skydomeMaterial = Material("skydome.png", 0.7f, 0.0f, 0.0f, 0.0f);
|
|
Object* skydomeObject = new Object(skydomeModel, skydomeMaterial, glm::vec3(0.0f, 0.0f, 0.0f),
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
|
objects.push_back(skydomeObject);
|
|
skydome = skydomeObject;
|
|
|
|
Model torchModel = Model("torch.obj", 0.75f);
|
|
Material torchMaterial = Material("torchTexture.png", 0.1f, 0.3f, 0.7f, 10.0f);
|
|
//Create object
|
|
Object* torchObject = new Object(torchModel, torchMaterial, glm::vec3(-3.0f, 6.0f, 0.0f),
|
|
glm::vec3(0.0f, 1.0472f, 0.0f));
|
|
objects.push_back(torchObject);
|
|
|
|
Model blockModel = Model("block.obj", 1.0f);
|
|
Material blockMaterial = Material("blockTexture_small.png", 0.1f, 0.6, 0.4f, 2.0f);
|
|
Object* blockObject = new Object(blockModel, blockMaterial, glm::vec3(0.0f, 10.0f, 0.0f),
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
|
objects.push_back(blockObject);
|
|
physicObjects.push_back(blockObject);
|
|
physics.addBox(1,3.0f,1,*blockObject,2,physicObjects.size());
|
|
|
|
Object* blockObject2 = new Object(blockModel, blockMaterial, glm::vec3(5.0f, 10.0f, 5.0f),
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
|
objects.push_back(blockObject2);
|
|
physicObjects.push_back(blockObject2);
|
|
physics.addBox(1,3.0f,1,*blockObject2,2,physicObjects.size());
|
|
|
|
Model columnModel = Model("column.obj", 1.0f);
|
|
Material columnMaterial = Material("columnTexture2.png", 0.1f, 0.6, 0.4f, 2.0f);
|
|
Object* columnObject = new Object(columnModel, columnMaterial, glm::vec3(-2.0f, 7.0f, -2.0f),
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
|
objects.push_back(columnObject);
|
|
|
|
//make non physics objects
|
|
|
|
|
|
//set lighting parameters
|
|
ambientLight = glm::vec3(1.0f, 1.0f, 1.0f);
|
|
fogColor = glm::vec4(0.10f, 0.14f, 0.14f, 1.0f);
|
|
directionalLight = Light(glm::vec3(-1.0f, 1.5f, 1.0f), glm::vec3(1.0f, 1.0f, 0.9f), 0.2f);
|
|
Light light = Light(glm::vec3(-3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 5.0f);
|
|
//lights.push_back(light);
|
|
Light light2 = Light(glm::vec3(3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f);
|
|
//lights.push_back(light2);
|
|
|
|
|
|
}
|
|
|
|
void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass) {
|
|
for(unsigned int i = 0; i<objects.size(); i++) {
|
|
// do not project shadow of skydome
|
|
if(lightingPass || objects.at(i) != skydome) {
|
|
objects.at(i)->render(shader, lightingPass);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed) {
|
|
// Ignore first two mouse updates, because they are incorrect
|
|
// DON'T try to move this functionallity elsewhere
|
|
static int i = 0;
|
|
if (i <2) {
|
|
i++;
|
|
}
|
|
else {
|
|
mouseDelta.x = -mouseDelta.x;
|
|
camera.updateRotation(mouseDelta/100.0f);
|
|
}
|
|
|
|
float str = 20;
|
|
|
|
if(wPressed){
|
|
physics.rollForward(camera.getVector(),str);
|
|
}
|
|
if(aPressed) {
|
|
physics.rollLeft(camera.getVector(),str);
|
|
}
|
|
if(sPressed) {
|
|
physics.rollBack(camera.getVector(),str);
|
|
}
|
|
if(dPressed){
|
|
physics.rollRight(camera.getVector(),str);
|
|
}
|
|
|
|
physics.takeUpdateStep(runTime);
|
|
|
|
cameraCenter->setPosition(physics.getPos(0));
|
|
cameraCenter->setRotation(physics.getRotation(0));
|
|
|
|
for(unsigned i = 0; i < physicObjects.size();i++)
|
|
{
|
|
physicObjects[i]->setPosition(physics.getPos(i));
|
|
physicObjects[i]->setRotation(physics.getRotation(i));
|
|
}
|
|
|
|
skydome->setPosition(glm::vec3(cameraCenter->getPosition().x,
|
|
0.0f, cameraCenter->getPosition().z));
|
|
}
|
|
|
|
glm::vec3 Level::getAmbientLight() {
|
|
return ambientLight;
|
|
}
|
|
|
|
std::vector<Light> Level::getLights() {
|
|
return lights;
|
|
}
|
|
|
|
Camera* Level::getCamera() {
|
|
return &camera;
|
|
}
|
|
|
|
Object* Level::getCameraCenter() {
|
|
return cameraCenter;
|
|
}
|
|
|
|
Light* Level::getDirectionalLight() {
|
|
return &directionalLight;
|
|
}
|
|
|
|
glm::vec4 Level::getFogColor() {
|
|
return fogColor;
|
|
}
|
|
|
|
glm::vec3 Level::getCameraPosition() {
|
|
return cameraCenter->getPosition() + camera.getVector();
|
|
}
|
|
|
|
void Level::setSkydomeSize(float size) {
|
|
skydomeSize = size;
|
|
}
|