2014-10-30 22:54:19 +00:00
|
|
|
#include "level.hh"
|
2014-12-05 13:45:44 +00:00
|
|
|
using namespace tinyxml2;
|
2014-10-30 22:54:19 +00:00
|
|
|
|
2014-11-17 11:57:16 +00:00
|
|
|
|
2014-12-04 14:07:31 +00:00
|
|
|
Level::Level(std::string levelNum){
|
|
|
|
this->levelNum = levelNum;
|
|
|
|
this->terrain = Terrain(levelNum);
|
2014-11-21 23:39:58 +00:00
|
|
|
skydomeSize = 50.0f;
|
2014-10-30 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Level::Level() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Level::~Level() {
|
2014-11-22 23:59:54 +00:00
|
|
|
for(unsigned int i = 0; i<objects.size(); i++) {
|
|
|
|
delete(objects.at(i));
|
|
|
|
}
|
2014-10-30 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 11:57:16 +00:00
|
|
|
|
2014-12-05 13:45:44 +00:00
|
|
|
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() {
|
|
|
|
XMLError error=XML_NO_ERROR;
|
2014-11-17 11:57:16 +00:00
|
|
|
this->physics = Physics();
|
|
|
|
this->physics.init();
|
|
|
|
|
2014-10-30 22:54:19 +00:00
|
|
|
// currently hard coded should later read this stuff out of a file
|
2014-11-14 21:55:29 +00:00
|
|
|
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
|
2014-11-21 15:22:36 +00:00
|
|
|
|
2014-12-05 11:47:02 +00:00
|
|
|
//Loading Objects via xml:
|
2014-12-04 14:07:31 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-12-05 11:47:02 +00:00
|
|
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
|
|
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
2014-12-05 13:45:44 +00:00
|
|
|
int thisType = 0;
|
|
|
|
error = thisComposition->FirstChildElement("typeID")->QueryIntText(&thisType);
|
|
|
|
errorCheck(error);
|
2014-12-05 16:14:43 +00:00
|
|
|
XMLElement* composition = compositions->FirstChildElement("composition");
|
|
|
|
for(; composition; composition=composition->NextSiblingElement("composition")){
|
|
|
|
int compositionType = 0;
|
|
|
|
error = composition->FirstChildElement("typeID")->QueryIntText(&compositionType);
|
2014-12-05 13:45:44 +00:00
|
|
|
errorCheck(error);
|
2014-12-05 16:14:43 +00:00
|
|
|
if(thisType == compositionType){
|
|
|
|
XMLElement* object = composition->FirstChildElement("object");
|
2014-12-05 11:47:02 +00:00
|
|
|
for(; object; object=object->NextSiblingElement("object")){
|
2014-12-05 13:45:44 +00:00
|
|
|
const char* charModelPath = object->FirstChildElement("modelPath")->GetText();
|
|
|
|
if(charModelPath == NULL){
|
2014-12-05 16:14:43 +00:00
|
|
|
printf("XMLError: No modelPath found in object.\n");
|
2014-12-05 13:45:44 +00:00
|
|
|
}
|
|
|
|
std::string modelPath = charModelPath;
|
2014-12-05 16:14:43 +00:00
|
|
|
float scaleObj, scaleComp;
|
|
|
|
object->FirstChildElement("scale")->QueryFloatText(&scaleObj);
|
|
|
|
thisComposition->FirstChildElement("scale")->QueryFloatText(&scaleComp);
|
|
|
|
//Model model = Model(modelPath, scaleObj * scaleComp);
|
|
|
|
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;
|
|
|
|
objectData->FirstChildElement("ambientFactor")->QueryFloatText(&ambientFactor);
|
|
|
|
objectData->FirstChildElement("diffuseFactor")->QueryFloatText(&diffuseFactor);
|
|
|
|
objectData->FirstChildElement("specularFactor")->QueryFloatText(&specularFactor);
|
|
|
|
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 = Material(texturePath, ambientFactor, diffuseFactor, specularFactor, shininess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO calculate position and rotation
|
|
|
|
//Object* object = new Object(model, material, position, rotation);
|
|
|
|
//objects.push_back(object);
|
|
|
|
//TODO if object has physics: physicObjects.push_back(object);
|
|
|
|
//TODO add object to physics
|
|
|
|
//if(compositionType == 20){
|
|
|
|
// cameraCenter = object;
|
|
|
|
//}
|
2014-12-05 11:47:02 +00:00
|
|
|
}
|
2014-12-05 16:14:43 +00:00
|
|
|
XMLElement* light = composition->FirstChildElement("light");
|
2014-12-05 11:47:02 +00:00
|
|
|
for(; light; light=light->NextSiblingElement("light")){
|
2014-12-05 16:14:43 +00:00
|
|
|
//TODO add lights
|
2014-12-05 11:47:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 14:07:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-05 11:47:02 +00:00
|
|
|
|
2014-12-04 14:07:31 +00:00
|
|
|
|
2014-11-21 15:22:36 +00:00
|
|
|
//add player
|
2014-12-05 15:36:31 +00:00
|
|
|
Model marbleModel = Model("marbleSmooth.obj", 0.75f);
|
2014-12-05 11:47:02 +00:00
|
|
|
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),
|
2014-11-27 23:17:56 +00:00
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
2014-11-21 15:22:36 +00:00
|
|
|
objects.push_back(object);
|
2014-11-28 11:06:17 +00:00
|
|
|
physicObjects.push_back(object);
|
|
|
|
this->physics.addPlayer(1.25f,*object,8.0f,physicObjects.size());
|
2014-11-22 23:59:54 +00:00
|
|
|
cameraCenter = object;
|
2014-11-21 15:22:36 +00:00
|
|
|
|
2014-11-21 23:39:58 +00:00
|
|
|
Model skydomeModel = Model("skydome.obj", skydomeSize);
|
|
|
|
Material skydomeMaterial = Material("skydome.png", 0.7f, 0.0f, 0.0f, 0.0f);
|
2014-11-22 23:59:54 +00:00
|
|
|
Object* skydomeObject = new Object(skydomeModel, skydomeMaterial, glm::vec3(0.0f, 0.0f, 0.0f),
|
2014-11-27 23:17:56 +00:00
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
2014-11-21 23:39:58 +00:00
|
|
|
objects.push_back(skydomeObject);
|
2014-11-22 23:57:16 +00:00
|
|
|
skydome = skydomeObject;
|
2014-11-17 13:23:23 +00:00
|
|
|
|
|
|
|
Model torchModel = Model("torch.obj", 0.75f);
|
|
|
|
Material torchMaterial = Material("torchTexture.png", 0.1f, 0.3f, 0.7f, 10.0f);
|
|
|
|
//Create object
|
2014-11-22 23:59:54 +00:00
|
|
|
Object* torchObject = new Object(torchModel, torchMaterial, glm::vec3(-3.0f, 6.0f, 0.0f),
|
2014-11-27 23:17:56 +00:00
|
|
|
glm::vec3(0.0f, 1.0472f, 0.0f));
|
2014-11-17 13:23:23 +00:00
|
|
|
objects.push_back(torchObject);
|
2014-11-17 11:57:16 +00:00
|
|
|
|
2014-12-05 15:36:31 +00:00
|
|
|
Model blockModel = Model("block.obj", 1.0f);
|
2014-11-18 18:39:44 +00:00
|
|
|
Material blockMaterial = Material("blockTexture_small.png", 0.1f, 0.6, 0.4f, 2.0f);
|
2014-11-28 11:06:17 +00:00
|
|
|
Object* blockObject = new Object(blockModel, blockMaterial, glm::vec3(0.0f, 10.0f, 0.0f),
|
2014-11-27 23:17:56 +00:00
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
2014-11-17 14:29:38 +00:00
|
|
|
objects.push_back(blockObject);
|
2014-11-28 11:06:17 +00:00
|
|
|
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());
|
2014-11-17 14:29:38 +00:00
|
|
|
|
2014-12-05 15:36:31 +00:00
|
|
|
Model columnModel = Model("column.obj", 1.0f);
|
|
|
|
Material columnMaterial = Material("columnTexture2.png", 0.1f, 0.6, 0.4f, 2.0f);
|
2014-11-22 23:59:54 +00:00
|
|
|
Object* columnObject = new Object(columnModel, columnMaterial, glm::vec3(-2.0f, 7.0f, -2.0f),
|
2014-11-27 23:17:56 +00:00
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
2014-11-17 14:29:38 +00:00
|
|
|
objects.push_back(columnObject);
|
2014-11-14 16:36:30 +00:00
|
|
|
|
2014-11-21 15:22:36 +00:00
|
|
|
//make non physics objects
|
|
|
|
|
|
|
|
|
2014-10-31 09:38:57 +00:00
|
|
|
//set lighting parameters
|
2014-11-08 00:15:09 +00:00
|
|
|
ambientLight = glm::vec3(1.0f, 1.0f, 1.0f);
|
2014-11-21 01:38:03 +00:00
|
|
|
fogColor = glm::vec4(0.10f, 0.14f, 0.14f, 1.0f);
|
2014-12-04 11:19:39 +00:00
|
|
|
directionalLight = Light(glm::vec3(-1.0f, 1.5f, 1.0f), glm::vec3(1.0f, 1.0f, 0.9f), 0.2f);
|
2014-11-18 23:59:48 +00:00
|
|
|
Light light = Light(glm::vec3(-3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 5.0f);
|
2014-10-31 11:56:09 +00:00
|
|
|
lights.push_back(light);
|
2014-11-18 23:59:48 +00:00
|
|
|
Light light2 = Light(glm::vec3(3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f);
|
2014-10-31 11:56:09 +00:00
|
|
|
lights.push_back(light2);
|
2014-11-14 16:36:30 +00:00
|
|
|
|
2014-11-13 00:22:33 +00:00
|
|
|
// load terrain
|
|
|
|
this->terrain.load();
|
2014-11-15 12:46:20 +00:00
|
|
|
Model terrainModel = Model(this->terrain.getModel());
|
2014-11-14 16:36:30 +00:00
|
|
|
// load a texture:
|
2014-11-17 17:18:57 +00:00
|
|
|
Material terrainMaterial = Material("seamlessTerrain.png", 0.1f, 0.8f, 0.2f, 3.0f);
|
2014-11-14 16:36:30 +00:00
|
|
|
//Create object
|
2014-11-22 23:59:54 +00:00
|
|
|
Object* terrainObject = new Object(terrainModel, terrainMaterial,
|
2014-11-14 16:36:30 +00:00
|
|
|
glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), 0.0f, -0.5f*(float)this->terrain.getHeightmapWidth()),
|
2014-11-27 23:17:56 +00:00
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f));
|
2014-11-14 16:36:30 +00:00
|
|
|
objects.push_back(terrainObject);
|
2014-11-17 15:07:40 +00:00
|
|
|
|
|
|
|
//addTerrainPhysic
|
|
|
|
physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap());
|
2014-10-30 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass) {
|
2014-11-08 01:45:32 +00:00
|
|
|
for(unsigned int i = 0; i<objects.size(); i++) {
|
2014-12-04 00:13:59 +00:00
|
|
|
// do not project shadow of skydome
|
|
|
|
if(lightingPass || objects.at(i) != skydome) {
|
|
|
|
objects.at(i)->render(shader, lightingPass);
|
|
|
|
}
|
2014-10-30 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-30 22:59:03 +00:00
|
|
|
|
2014-11-17 15:07:40 +00:00
|
|
|
void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed) {
|
2014-11-14 21:55:29 +00:00
|
|
|
// Ignore first two mouse updates, because they are incorrect
|
2014-11-25 15:01:13 +00:00
|
|
|
// DON'T try to move this functionallity elsewhere
|
2014-11-14 21:55:29 +00:00
|
|
|
static int i = 0;
|
|
|
|
if (i <2) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
2014-11-17 15:28:31 +00:00
|
|
|
mouseDelta.x = -mouseDelta.x;
|
2014-11-14 21:55:29 +00:00
|
|
|
camera.updateRotation(mouseDelta/100.0f);
|
2014-11-17 11:57:16 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 15:22:36 +00:00
|
|
|
float str = 20;
|
|
|
|
|
2014-11-17 15:07:40 +00:00
|
|
|
if(wPressed){
|
2014-11-21 15:22:36 +00:00
|
|
|
physics.rollForward(camera.getVector(),str);
|
2014-11-17 15:07:40 +00:00
|
|
|
}
|
|
|
|
if(aPressed) {
|
2014-11-21 15:22:36 +00:00
|
|
|
physics.rollLeft(camera.getVector(),str);
|
2014-11-17 15:07:40 +00:00
|
|
|
}
|
|
|
|
if(sPressed) {
|
2014-11-21 15:22:36 +00:00
|
|
|
physics.rollBack(camera.getVector(),str);
|
2014-11-17 15:07:40 +00:00
|
|
|
}
|
|
|
|
if(dPressed){
|
2014-11-21 15:22:36 +00:00
|
|
|
physics.rollRight(camera.getVector(),str);
|
2014-11-17 12:12:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 11:57:16 +00:00
|
|
|
physics.takeUpdateStep(runTime);
|
|
|
|
|
2014-11-22 23:59:54 +00:00
|
|
|
cameraCenter->setPosition(physics.getPos(0));
|
|
|
|
cameraCenter->setRotation(physics.getRotation(0));
|
2014-11-21 15:22:36 +00:00
|
|
|
|
2014-11-28 11:06:17 +00:00
|
|
|
for(unsigned i = 0; i < physicObjects.size();i++)
|
|
|
|
{
|
|
|
|
physicObjects[i]->setPosition(physics.getPos(i));
|
|
|
|
physicObjects[i]->setRotation(physics.getRotation(i));
|
|
|
|
}
|
|
|
|
|
2014-11-22 23:57:16 +00:00
|
|
|
skydome->setPosition(glm::vec3(cameraCenter->getPosition().x,
|
2014-11-21 01:38:03 +00:00
|
|
|
0.0f, cameraCenter->getPosition().z));
|
2014-11-12 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 22:59:03 +00:00
|
|
|
glm::vec3 Level::getAmbientLight() {
|
|
|
|
return ambientLight;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Light> Level::getLights() {
|
|
|
|
return lights;
|
|
|
|
}
|
2014-11-13 00:22:33 +00:00
|
|
|
|
2014-11-15 13:54:44 +00:00
|
|
|
Camera* Level::getCamera() {
|
|
|
|
return &camera;
|
2014-11-13 00:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object* Level::getCameraCenter() {
|
|
|
|
return cameraCenter;
|
|
|
|
}
|
2014-11-17 16:51:15 +00:00
|
|
|
|
|
|
|
Light* Level::getDirectionalLight() {
|
|
|
|
return &directionalLight;
|
|
|
|
}
|
2014-11-19 00:57:38 +00:00
|
|
|
|
2014-11-21 01:38:03 +00:00
|
|
|
glm::vec4 Level::getFogColor() {
|
2014-11-19 00:58:48 +00:00
|
|
|
return fogColor;
|
|
|
|
}
|
|
|
|
|
2014-11-19 00:57:38 +00:00
|
|
|
glm::vec3 Level::getCameraPosition() {
|
|
|
|
return cameraCenter->getPosition() + camera.getVector();
|
|
|
|
}
|
2014-11-21 01:38:03 +00:00
|
|
|
|
2014-11-21 23:39:58 +00:00
|
|
|
void Level::setSkydomeSize(float size) {
|
|
|
|
skydomeSize = size;
|
2014-11-21 01:38:03 +00:00
|
|
|
}
|