2014-10-30 22:54:19 +00:00
# include "level.hh"
2014-12-12 15:24:47 +00:00
# include <string>
2015-01-09 15:51:28 +00:00
extern " C " {
# include "extern/lua/src/lua.h"
# include "extern/lua/src/lualib.h"
# include "extern/lua/src/lauxlib.h"
}
# include "extern/luabridge/LuaBridge.h"
2014-12-05 13:45:44 +00:00
using namespace tinyxml2 ;
2014-10-30 22:54:19 +00:00
2014-12-15 18:11:28 +00:00
//dowadiddydiddydumdiddydo
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 ( ) {
}
2014-12-15 18:11:28 +00:00
//there she was just walking down the street singing
2014-10-30 22:54:19 +00:00
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-12-15 18:11:28 +00:00
//dowadiddydiddydumdiddydoo!
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 ( ) {
2015-01-09 15:51:28 +00:00
//Intialize lua state
lua_State * L = nullptr ;
// Check if there's an existing state and close it
if ( L ! = nullptr ) {
lua_close ( L ) ;
L = nullptr ;
}
// Create a new lua state
L = luaL_newstate ( ) ;
luaL_openlibs ( L ) ;
2015-01-13 16:50:15 +00:00
//Expose the class Level and its functions to Lua
2015-01-09 15:51:28 +00:00
luabridge : : getGlobalNamespace ( L )
. beginClass < Level > ( " Level " )
2015-01-13 12:52:22 +00:00
. addFunction ( " deleteObject " , & Level : : deleteObject )
. addFunction ( " getObjectCount " , & Level : : getObjectCount )
2015-01-17 12:30:33 +00:00
. addFunction ( " moveObject " , & Level : : moveObject )
2015-01-09 15:51:28 +00:00
. endClass ( ) ;
2015-01-13 16:50:15 +00:00
//Push the level to Lua as a global variable
2015-01-09 15:51:28 +00:00
luabridge : : push ( L , this ) ;
2015-01-13 16:50:15 +00:00
lua_setglobal ( L , " level " ) ;
2015-01-09 15:51:28 +00:00
2014-11-17 11:57:16 +00:00
this - > physics = Physics ( ) ;
2015-01-17 12:11:43 +00:00
this - > physics . init ( ) ;
2014-11-17 11:57:16 +00:00
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-08 13:10:08 +00:00
// 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 ,
2015-01-16 15:37:00 +00:00
glm : : vec3 ( - 0.5 * ( float ) this - > terrain . getHeightmapHeight ( ) , 0.0f , - 0.5f * ( float ) this - > terrain . getHeightmapWidth ( ) ) ,
2014-12-08 13:10:08 +00:00
glm : : vec3 ( 0.0f , 0.0f , 0.0f ) ) ;
objects . push_back ( terrainObject ) ;
//addTerrainPhysic
physics . addTerrain ( terrain . getHeightmapWidth ( ) , terrain . getHeightmapHeight ( ) , terrain . getHeightmap ( ) ) ;
2014-12-12 17:38:12 +00:00
2014-12-12 14:41:17 +00:00
//Loading from 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 ) ;
}
2015-01-06 12:14:30 +00:00
//load global physic parameter
float friction ;
XMLElement * physicsElement = doc - > FirstChildElement ( " physics " ) ;
errorCheck ( physicsElement - > FirstChildElement ( " strength " ) - > QueryFloatText ( & strength ) ) ;
errorCheck ( physicsElement - > FirstChildElement ( " friction " ) - > QueryFloatText ( & friction ) ) ;
2014-12-12 14:41:17 +00:00
//load the skydome
XMLElement * skydomeElement = doc - > FirstChildElement ( " skydome " ) ;
const char * charSkydomeTexture = skydomeElement - > FirstChildElement ( " texture " ) - > GetText ( ) ;
if ( charSkydomeTexture = = NULL ) {
printf ( " XMLError: No skydomeTexture found. \n " ) ;
}
std : : string skydomeTexture = charSkydomeTexture ;
Model skydomeModel = Model ( " skydome.obj " , skydomeSize ) ;
Material skydomeMaterial = Material ( skydomeTexture , 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 ;
2014-12-12 17:38:12 +00:00
2014-12-12 14:41:17 +00:00
//load lighting parameters
2014-12-12 15:24:47 +00:00
float rColour , gColour , bColour , alpha , xOffset , yOffset , zOffset , intensity ;
2014-12-12 14:41:17 +00:00
XMLElement * ambientElement = doc - > FirstChildElement ( " ambientLight " ) ;
errorCheck ( ambientElement - > FirstChildElement ( " rColour " ) - > QueryFloatText ( & rColour ) ) ;
errorCheck ( ambientElement - > FirstChildElement ( " gColour " ) - > QueryFloatText ( & gColour ) ) ;
errorCheck ( ambientElement - > FirstChildElement ( " bColour " ) - > QueryFloatText ( & bColour ) ) ;
ambientLight = glm : : vec3 ( rColour , gColour , bColour ) ;
2014-12-12 17:38:12 +00:00
XMLElement * fogElement = doc - > FirstChildElement ( " fogColour " ) ;
2014-12-12 14:41:17 +00:00
errorCheck ( fogElement - > FirstChildElement ( " rColour " ) - > QueryFloatText ( & rColour ) ) ;
errorCheck ( fogElement - > FirstChildElement ( " gColour " ) - > QueryFloatText ( & gColour ) ) ;
errorCheck ( fogElement - > FirstChildElement ( " bColour " ) - > QueryFloatText ( & bColour ) ) ;
errorCheck ( fogElement - > FirstChildElement ( " alpha " ) - > QueryFloatText ( & alpha ) ) ;
fogColour = glm : : vec4 ( rColour , gColour , bColour , alpha ) ;
2014-12-12 15:24:47 +00:00
XMLElement * directionalElement = doc - > FirstChildElement ( " directionalLight " ) ;
errorCheck ( directionalElement - > FirstChildElement ( " xOffset " ) - > QueryFloatText ( & xOffset ) ) ;
errorCheck ( directionalElement - > FirstChildElement ( " yOffset " ) - > QueryFloatText ( & yOffset ) ) ;
errorCheck ( directionalElement - > FirstChildElement ( " zOffset " ) - > QueryFloatText ( & zOffset ) ) ;
errorCheck ( directionalElement - > FirstChildElement ( " rColour " ) - > QueryFloatText ( & rColour ) ) ;
errorCheck ( directionalElement - > FirstChildElement ( " gColour " ) - > QueryFloatText ( & gColour ) ) ;
errorCheck ( directionalElement - > FirstChildElement ( " bColour " ) - > QueryFloatText ( & bColour ) ) ;
errorCheck ( directionalElement - > FirstChildElement ( " intensity " ) - > QueryFloatText ( & intensity ) ) ;
directionalLight = Light ( glm : : vec3 ( xOffset , yOffset , zOffset ) , glm : : vec3 ( rColour , gColour , bColour ) , intensity ) ;
2014-12-12 17:38:12 +00:00
2014-12-12 16:47:33 +00:00
//load Objects
2014-12-15 17:43:10 +00:00
std : : vector < std : : vector < int > > objectIdentifiers = std : : vector < std : : vector < int > > ( ) ; //The first entry is the index in objects, the others are idGreen, idBlue and objectNum.
2014-12-04 14:07:31 +00:00
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-12 16:47:33 +00:00
//iterate over all compositions in Level.xml
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 ;
2014-12-08 14:48:46 +00:00
errorCheck ( thisComposition - > FirstChildElement ( " typeID " ) - > QueryIntText ( & thisType ) ) ;
2014-12-12 16:47:33 +00:00
//iterate over all compositions in Compositions.xml to find the one corresponding to the current composition
2014-12-05 16:14:43 +00:00
XMLElement * composition = compositions - > FirstChildElement ( " composition " ) ;
for ( ; composition ; composition = composition - > NextSiblingElement ( " composition " ) ) {
int compositionType = 0 ;
2014-12-08 14:48:46 +00:00
errorCheck ( composition - > FirstChildElement ( " typeID " ) - > QueryIntText ( & compositionType ) ) ;
2014-12-12 16:47:33 +00:00
//corect composition found
2014-12-05 16:14:43 +00:00
if ( thisType = = compositionType ) {
2014-12-12 16:47:33 +00:00
//iterate over all objects of the composition
2014-12-15 12:08:35 +00:00
XMLElement * xmlObject = composition - > FirstChildElement ( " object " ) ;
2014-12-12 16:47:33 +00:00
int objectNum = 0 ;
2014-12-15 12:08:35 +00:00
for ( ; xmlObject ; xmlObject = xmlObject - > NextSiblingElement ( " object " ) ) {
const char * charModelPath = xmlObject - > FirstChildElement ( " modelPath " ) - > GetText ( ) ;
2014-12-05 13:45:44 +00:00
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-08 13:10:08 +00:00
float objectScale , compScale ;
2014-12-15 12:08:35 +00:00
errorCheck ( xmlObject - > FirstChildElement ( " scale " ) - > QueryFloatText ( & objectScale ) ) ;
2014-12-08 14:48:46 +00:00
errorCheck ( thisComposition - > FirstChildElement ( " scale " ) - > QueryFloatText ( & compScale ) ) ;
2014-12-08 14:19:50 +00:00
Model model = Model ( modelPath , objectScale * compScale ) ;
2014-12-12 16:47:33 +00:00
//find the objectData for the current object
2014-12-05 16:14:43 +00:00
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 ;
2014-12-12 16:47:33 +00:00
//objectData found
2014-12-15 13:59:03 +00:00
if ( dataModelPath . compare ( modelPath ) = = 0 ) {
2014-12-12 16:47:33 +00:00
//create the object
2014-12-05 16:14:43 +00:00
float ambientFactor , diffuseFactor , specularFactor , shininess ;
2014-12-08 14:48:46 +00:00
errorCheck ( objectData - > FirstChildElement ( " ambientFactor " ) - > QueryFloatText ( & ambientFactor ) ) ;
errorCheck ( objectData - > FirstChildElement ( " diffuseFactor " ) - > QueryFloatText ( & diffuseFactor ) ) ;
errorCheck ( objectData - > FirstChildElement ( " specularFactor " ) - > QueryFloatText ( & specularFactor ) ) ;
errorCheck ( objectData - > FirstChildElement ( " shininess " ) - > QueryFloatText ( & shininess ) ) ;
2014-12-05 16:14:43 +00:00
const char * charTexturePath = objectData - > FirstChildElement ( " texturePath " ) - > GetText ( ) ;
if ( charTexturePath = = NULL ) {
printf ( " XMLError: No texturePath found in objectData. \n " ) ;
}
std : : string texturePath = charTexturePath ;
2014-12-12 15:24:47 +00:00
Material material = Material ( texturePath , ambientFactor , diffuseFactor , specularFactor , shininess ) ;
float compXPos , compYOffset , compZPos ;
glm : : vec3 objectOffset , compRot ;
2014-12-15 12:08:35 +00:00
errorCheck ( xmlObject - > FirstChildElement ( " xOffset " ) - > QueryFloatText ( & objectOffset [ 0 ] ) ) ;
errorCheck ( xmlObject - > FirstChildElement ( " yOffset " ) - > QueryFloatText ( & objectOffset [ 1 ] ) ) ;
errorCheck ( xmlObject - > FirstChildElement ( " zOffset " ) - > QueryFloatText ( & objectOffset [ 2 ] ) ) ;
2014-12-12 15:24:47 +00:00
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 ] ) ) ;
2015-01-17 11:24:54 +00:00
bool ignoreHeightmap ;
errorCheck ( composition - > FirstChildElement ( " ignoreHeightmap " ) - > QueryBoolText ( & ignoreHeightmap ) ) ;
if ( ! ignoreHeightmap ) {
compYOffset = compYOffset + terrain . getHeightmap ( ) [ int ( compXPos - 0.5 + 0.5 * terrain . getHeightmapHeight ( ) ) ]
[ int ( compZPos - 0.5 + 0.5 * terrain . getHeightmapWidth ( ) ) ] ;
}
glm : : vec3 compPos = glm : : vec3 ( compXPos , compYOffset , compZPos ) ;
2014-12-12 15:24:47 +00:00
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 ) ;
2015-01-19 14:55:03 +00:00
glm : : vec3 objectRot ;
errorCheck ( xmlObject - > FirstChildElement ( " xRot " ) - > QueryFloatText ( & objectRot [ 0 ] ) ) ;
errorCheck ( xmlObject - > FirstChildElement ( " yRot " ) - > QueryFloatText ( & objectRot [ 1 ] ) ) ;
errorCheck ( xmlObject - > FirstChildElement ( " zRot " ) - > QueryFloatText ( & objectRot [ 2 ] ) ) ;
Object * object = new Object ( model , material , objectPosition , compRot + objectRot ) ;
2014-12-12 15:24:47 +00:00
objects . push_back ( object ) ;
2014-12-12 16:47:33 +00:00
//create an identifier for this object
2014-12-15 17:43:10 +00:00
std : : vector < int > objectIdentifier = std : : vector < int > ( 4 ) ;
2014-12-12 16:47:33 +00:00
objectIdentifier [ 0 ] = objects . size ( ) - 1 ;
int idGreen , idBlue ;
errorCheck ( thisComposition - > FirstChildElement ( " idGreen " ) - > QueryIntText ( & idGreen ) ) ;
errorCheck ( thisComposition - > FirstChildElement ( " idBlue " ) - > QueryIntText ( & idBlue ) ) ;
objectIdentifier [ 1 ] = idGreen ;
objectIdentifier [ 2 ] = idBlue ;
objectIdentifier [ 3 ] = objectNum ;
objectIdentifiers . push_back ( objectIdentifier ) ;
2014-12-15 12:08:35 +00:00
2014-12-12 15:52:05 +00:00
physicObjects . push_back ( object ) ;
2014-12-12 15:24:47 +00:00
const char * charPhysicType = objectData - > FirstChildElement ( " physicType " ) - > GetText ( ) ;
if ( charPhysicType = = NULL ) {
printf ( " XMLError: No physicType found. \n " ) ;
}
std : : string physicType = charPhysicType ;
2014-12-12 16:47:33 +00:00
//add object to physics
2014-12-15 12:08:35 +00:00
float mass ;
errorCheck ( xmlObject - > FirstChildElement ( " mass " ) - > QueryFloatText ( & mass ) ) ;
2015-01-06 12:31:53 +00:00
float dampningL , dampningA ;
errorCheck ( objectData - > FirstChildElement ( " dampningL " ) - > QueryFloatText ( & dampningL ) ) ;
errorCheck ( objectData - > FirstChildElement ( " dampningA " ) - > QueryFloatText ( & dampningA ) ) ;
2014-12-12 15:52:05 +00:00
if ( physicType . compare ( " Player " ) = = 0 ) {
2014-12-15 12:08:35 +00:00
float radius ;
2014-12-12 15:52:05 +00:00
errorCheck ( objectData - > FirstChildElement ( " radius " ) - > QueryFloatText ( & radius ) ) ;
2015-01-06 12:31:53 +00:00
this - > physics . addPlayer ( friction , radius , * object , mass , dampningL , dampningA , physicObjects . size ( ) ) ;
2014-12-12 17:38:12 +00:00
} else if ( physicType . compare ( " Box " ) = = 0 ) {
2014-12-15 12:08:35 +00:00
float width , height , length ;
2014-12-12 15:52:05 +00:00
errorCheck ( objectData - > FirstChildElement ( " width " ) - > QueryFloatText ( & width ) ) ;
errorCheck ( objectData - > FirstChildElement ( " height " ) - > QueryFloatText ( & height ) ) ;
errorCheck ( objectData - > FirstChildElement ( " length " ) - > QueryFloatText ( & length ) ) ;
2015-01-06 12:31:53 +00:00
this - > physics . addBox ( width , height , length , * object , mass , dampningL , dampningA , physicObjects . size ( ) ) ;
2014-12-15 12:08:35 +00:00
} else if ( physicType . compare ( " TriangleMesh " ) = = 0 ) {
2015-01-06 12:31:53 +00:00
2014-12-15 17:43:10 +00:00
this - > physics . addTriangleMeshBody ( * object , modelPath , mass , dampningL , dampningA , physicObjects . size ( ) ) ;
2014-12-12 17:38:12 +00:00
} else {
printf ( " XMLError: Not a valid physicType. \n " ) ;
2014-12-12 15:52:05 +00:00
}
2014-12-15 12:08:35 +00:00
2014-12-12 15:52:05 +00:00
if ( compositionType = = 20 ) {
cameraCenter = object ;
}
2014-12-12 17:02:31 +00:00
} //objectData found
} //finding the objectData
2014-12-12 16:47:33 +00:00
objectNum = objectNum + 1 ;
2014-12-12 17:02:31 +00:00
} //iterating over all objects of the composition
2014-12-12 17:38:12 +00:00
2014-12-12 17:02:31 +00:00
//iterate over all lights of the composition
2014-12-15 13:59:03 +00:00
XMLElement * xmlLight = composition - > FirstChildElement ( " light " ) ;
for ( ; xmlLight ; xmlLight = xmlLight - > NextSiblingElement ( " light " ) ) {
2014-12-08 14:19:50 +00:00
glm : : vec3 compRot , lightOffset , lightColour ;
float compScale , compXPos , compYOffset , compZPos , lightIntensity ;
2014-12-08 14:48:46 +00:00
errorCheck ( thisComposition - > FirstChildElement ( " scale " ) - > QueryFloatText ( & compScale ) ) ;
2014-12-15 13:59:03 +00:00
errorCheck ( xmlLight - > FirstChildElement ( " xOffset " ) - > QueryFloatText ( & lightOffset [ 0 ] ) ) ;
errorCheck ( xmlLight - > FirstChildElement ( " yOffset " ) - > QueryFloatText ( & lightOffset [ 1 ] ) ) ;
errorCheck ( xmlLight - > FirstChildElement ( " zOffset " ) - > QueryFloatText ( & lightOffset [ 2 ] ) ) ;
2014-12-08 14:48:46 +00:00
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 ] ) ) ;
2014-12-15 13:59:03 +00:00
errorCheck ( xmlLight - > FirstChildElement ( " rColour " ) - > QueryFloatText ( & lightColour [ 0 ] ) ) ;
errorCheck ( xmlLight - > FirstChildElement ( " gColour " ) - > QueryFloatText ( & lightColour [ 1 ] ) ) ;
errorCheck ( xmlLight - > FirstChildElement ( " bColour " ) - > QueryFloatText ( & lightColour [ 2 ] ) ) ;
errorCheck ( xmlLight - > FirstChildElement ( " intensity " ) - > QueryFloatText ( & lightIntensity ) ) ;
2014-12-08 14:19:50 +00:00
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 ) ;
2014-12-12 17:02:31 +00:00
} //iterating over all lights of the composition
} //corect composition found
} //iterating over all compositions in Compositions.xml
} //iterating over all compositions in Level.xml
//load triggers
2014-12-16 12:46:49 +00:00
XMLElement * composition = doc - > FirstChildElement ( " composition " ) ;
for ( ; composition ; composition = composition - > NextSiblingElement ( " composition " ) ) {
XMLElement * xmlTrigger = composition - > FirstChildElement ( " trigger " ) ;
for ( ; xmlTrigger ; xmlTrigger = xmlTrigger - > NextSiblingElement ( " trigger " ) ) {
const char * charName = xmlTrigger - > FirstChildElement ( " name " ) - > GetText ( ) ;
if ( charName = = NULL ) {
printf ( " XMLError: No name found for a trigger. \n " ) ;
2014-12-15 17:43:10 +00:00
}
2014-12-16 12:46:49 +00:00
std : : string name = charName ;
if ( name . compare ( " - " ) ! = 0 ) {
float xPos , yPos , zPos , distance ;
bool isBigger ;
2015-01-13 12:52:22 +00:00
int idGreen , idBlue , objectNum ;
2014-12-19 15:15:29 +00:00
2014-12-16 12:46:49 +00:00
errorCheck ( xmlTrigger - > FirstChildElement ( " xPosition " ) - > QueryFloatText ( & xPos ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " yPosition " ) - > QueryFloatText ( & yPos ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " zPosition " ) - > QueryFloatText ( & zPos ) ) ;
2014-12-19 15:15:29 +00:00
glm : : vec3 position = glm : : vec3 ( xPos , yPos , zPos ) ;
const char * charTarget = xmlTrigger - > FirstChildElement ( " targetIdGreen " ) - > GetText ( ) ;
if ( charTarget = = NULL ) {
printf ( " XMLError: No targetIdGreen found for a trigger. \n " ) ;
}
std : : string stringTarget = charTarget ;
if ( stringTarget . compare ( " - " ) ! = 0 ) {
int targetIdGreen = 0 , targetIdBlue = 0 ;
errorCheck ( xmlTrigger - > FirstChildElement ( " targetIdGreen " ) - > QueryIntText ( & targetIdGreen ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " targetIdBlue " ) - > QueryIntText ( & targetIdBlue ) ) ;
XMLElement * thisComposition = doc - > FirstChildElement ( " composition " ) ;
for ( ; thisComposition ; thisComposition = thisComposition - > NextSiblingElement ( " composition " ) ) {
int thisIdGreen , thisIdBlue ;
errorCheck ( thisComposition - > FirstChildElement ( " idGreen " ) - > QueryIntText ( & thisIdGreen ) ) ;
errorCheck ( thisComposition - > FirstChildElement ( " idBlue " ) - > QueryIntText ( & thisIdBlue ) ) ;
if ( targetIdGreen = = thisIdGreen & & targetIdBlue = = thisIdBlue ) {
glm : : vec3 targetPosition ;
errorCheck ( thisComposition - > FirstChildElement ( " xPos " ) - > QueryFloatText ( & targetPosition [ 0 ] ) ) ;
errorCheck ( thisComposition - > FirstChildElement ( " yOffset " ) - > QueryFloatText ( & targetPosition [ 1 ] ) ) ;
errorCheck ( thisComposition - > FirstChildElement ( " zPos " ) - > QueryFloatText ( & targetPosition [ 2 ] ) ) ;
targetPosition [ 1 ] + = terrain . getHeightmap ( ) [ int ( targetPosition [ 0 ] - 0.5 + 0.5 * terrain . getHeightmapHeight ( ) ) ]
[ int ( targetPosition [ 2 ] - 0.5 + 0.5 * terrain . getHeightmapWidth ( ) ) ] ;
position + = targetPosition ;
}
}
}
2014-12-16 12:46:49 +00:00
errorCheck ( xmlTrigger - > FirstChildElement ( " distance " ) - > QueryFloatText ( & distance ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " isBiggerThan " ) - > QueryBoolText ( & isBigger ) ) ;
errorCheck ( composition - > FirstChildElement ( " idGreen " ) - > QueryIntText ( & idGreen ) ) ;
errorCheck ( composition - > FirstChildElement ( " idBlue " ) - > QueryIntText ( & idBlue ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " objectNum " ) - > QueryIntText ( & objectNum ) ) ;
Object * object = 0 ;
for ( unsigned int i = 0 ; i < objectIdentifiers . size ( ) ; i + + ) {
if ( objectIdentifiers [ i ] [ 1 ] = = idGreen & & objectIdentifiers [ i ] [ 2 ] = = idBlue & & objectIdentifiers [ i ] [ 3 ] = = objectNum ) {
object = objects [ objectIdentifiers [ i ] [ 0 ] ] ;
}
}
2015-01-13 12:52:22 +00:00
const char * charLuaScript = xmlTrigger - > FirstChildElement ( " luaScript " ) - > GetText ( ) ;
if ( charLuaScript = = NULL ) {
printf ( " XMLError: No Lua script found for a trigger. \n " ) ;
}
std : : string luaScript = charLuaScript ;
2015-01-13 17:18:13 +00:00
int toChangeIdGreen , toChangeIdBlue , toChangeObjNum , objectToChange = - 1 ;
2015-01-13 12:52:22 +00:00
errorCheck ( xmlTrigger - > FirstChildElement ( " toChangeIdGreen " ) - > QueryIntText ( & toChangeIdGreen ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " toChangeIdBlue " ) - > QueryIntText ( & toChangeIdBlue ) ) ;
errorCheck ( xmlTrigger - > FirstChildElement ( " toChangeObjNum " ) - > QueryIntText ( & toChangeObjNum ) ) ;
for ( unsigned int i = 0 ; i < objectIdentifiers . size ( ) ; i + + ) {
if ( objectIdentifiers [ i ] [ 1 ] = = toChangeIdGreen & & objectIdentifiers [ i ] [ 2 ] = = toChangeIdBlue & & objectIdentifiers [ i ] [ 3 ] = = toChangeObjNum ) {
objectToChange = objectIdentifiers [ i ] [ 0 ] ;
}
}
2014-12-16 12:46:49 +00:00
if ( object ! = 0 ) {
2015-01-13 17:18:13 +00:00
Trigger trigger = Trigger ( position , distance , isBigger , object , luaScript , L , objectToChange ) ;
triggers . push_back ( trigger ) ;
2014-12-16 12:46:49 +00:00
}
else {
2015-01-13 12:52:22 +00:00
printf ( " Triggering object not found. \n " ) ;
2014-12-16 12:46:49 +00:00
}
2014-12-15 17:43:10 +00:00
}
2014-12-04 14:07:31 +00:00
}
2015-01-17 13:07:12 +00:00
} //triggers
//load positionConstraints
composition = doc - > FirstChildElement ( " composition " ) ;
for ( ; composition ; composition = composition - > NextSiblingElement ( " composition " ) ) {
XMLElement * positionConstraint = composition - > FirstChildElement ( " positionConstraint " ) ;
for ( ; positionConstraint ; positionConstraint = positionConstraint - > NextSiblingElement ( " positionConstraint " ) ) {
float xPos , yPos , zPos , strength ;
int objectNum , idGreen , idBlue , objectIndex ;
errorCheck ( positionConstraint - > FirstChildElement ( " xPosition " ) - > QueryFloatText ( & xPos ) ) ;
errorCheck ( positionConstraint - > FirstChildElement ( " yPosition " ) - > QueryFloatText ( & yPos ) ) ;
errorCheck ( positionConstraint - > FirstChildElement ( " zPosition " ) - > QueryFloatText ( & zPos ) ) ;
errorCheck ( positionConstraint - > FirstChildElement ( " strength " ) - > QueryFloatText ( & strength ) ) ;
errorCheck ( positionConstraint - > FirstChildElement ( " objectNum " ) - > QueryIntText ( & objectNum ) ) ;
errorCheck ( composition - > FirstChildElement ( " idGreen " ) - > QueryIntText ( & idGreen ) ) ;
errorCheck ( composition - > FirstChildElement ( " idBlue " ) - > QueryIntText ( & idBlue ) ) ;
for ( unsigned int i = 0 ; i < objectIdentifiers . size ( ) ; i + + ) {
if ( objectIdentifiers [ i ] [ 1 ] = = idGreen & & objectIdentifiers [ i ] [ 2 ] = = idBlue & & objectIdentifiers [ i ] [ 3 ] = = objectNum ) {
objectIndex = objectIdentifiers [ i ] [ 0 ] ;
}
}
glm : : vec3 position = glm : : vec3 ( xPos , yPos , zPos ) ;
physics . addPositionConstraint ( objectIndex , strength , position ) ;
}
} //positionConstraints
2014-10-30 22:54:19 +00:00
}
2014-12-15 11:41:30 +00:00
void Level : : render ( ACGL : : OpenGL : : SharedShaderProgram shader , bool lightingPass ,
glm : : mat4 * viewProjectionMatrix , std : : vector < glm : : mat4 > * shadowVPs ) {
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
2015-01-22 20:21:09 +00:00
if ( lightingPass | | ( objects . at ( i ) ! = skydome /*&& i!=0*/ ) ) {
2014-12-15 11:41:30 +00:00
objects . at ( i ) - > render ( shader , lightingPass , viewProjectionMatrix , shadowVPs ) ;
2014-12-04 00:13:59 +00:00
}
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-17 15:07:40 +00:00
if ( wPressed ) {
2015-01-06 12:14:30 +00:00
physics . rollForward ( camera . getVector ( ) , strength ) ;
2014-11-17 15:07:40 +00:00
}
if ( aPressed ) {
2015-01-06 12:14:30 +00:00
physics . rollLeft ( camera . getVector ( ) , strength ) ;
2014-11-17 15:07:40 +00:00
}
if ( sPressed ) {
2015-01-06 12:14:30 +00:00
physics . rollBack ( camera . getVector ( ) , strength ) ;
2014-11-17 15:07:40 +00:00
}
if ( dPressed ) {
2015-01-06 12:14:30 +00:00
physics . rollRight ( camera . getVector ( ) , strength ) ;
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-12-15 17:43:10 +00:00
for ( unsigned int i = 0 ; i < triggers . size ( ) ; i + + ) {
triggers . at ( i ) . triggerUpdate ( ) ;
}
2014-11-12 23:40:28 +00:00
}
2014-10-30 22:59:03 +00:00
glm : : vec3 Level : : getAmbientLight ( ) {
return ambientLight ;
}
2014-12-15 00:12:51 +00:00
std : : vector < Light > * Level : : getLights ( ) {
return & lights ;
2014-10-30 22:59:03 +00:00
}
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-12-12 14:41:17 +00:00
glm : : vec4 Level : : getFogColour ( ) {
return fogColour ;
2014-11-19 00:58:48 +00:00
}
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
}
2014-12-15 17:43:10 +00:00
2014-12-16 11:19:48 +00:00
std : : vector < Object * > * Level : : getObjects ( ) {
return & objects ;
}
2015-01-13 12:52:22 +00:00
void Level : : deleteObject ( int objectIndex ) {
objects . erase ( objects . begin ( ) + objectIndex ) ;
2015-01-13 17:18:13 +00:00
for ( unsigned int i = 0 ; i < triggers . size ( ) ; i + + ) {
if ( triggers . at ( i ) . deleteNotification ( objectIndex ) ) {
triggers . erase ( triggers . begin ( ) + i ) ;
}
}
2015-01-13 12:52:22 +00:00
}
int Level : : getObjectCount ( ) {
return objects . size ( ) ;
}
2015-01-17 12:30:33 +00:00
void Level : : moveObject ( int objectIndex , float strength , float xPos , float yPos , float zPos ) {
glm : : vec3 position = glm : : vec3 ( xPos , yPos , zPos ) ;
physics . removePositionConstraint ( objectIndex ) ;
physics . addPositionConstraint ( objectIndex , strength , position ) ;
}
2015-01-13 12:52:22 +00:00