diff --git a/converter/converter.cc b/converter/converter.cc index 78c5d3f..d50fe0a 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -264,6 +264,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ XMLElement* trigger = doc->NewElement("trigger"); newComposition->InsertEndChild(trigger); XMLElement* name = doc->NewElement("name"); + XMLElement* undo = doc->NewElement("undo"); XMLElement* xPosition = doc->NewElement("xPosition"); XMLElement* yPosition = doc->NewElement("yPosition"); XMLElement* zPosition = doc->NewElement("zPosition"); @@ -278,6 +279,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ XMLElement* toChangeObjNum = doc->NewElement("toChangeObjNum"); name->SetText("-"); + undo->SetText("false"); xPosition->SetText("0"); yPosition->SetText("0"); zPosition->SetText("0"); @@ -292,6 +294,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ toChangeObjNum->SetText("0"); trigger->InsertEndChild(name); + trigger->InsertEndChild(undo); trigger->InsertEndChild(xPosition); trigger->InsertEndChild(yPosition); trigger->InsertEndChild(zPosition); diff --git a/data/levels/Level1.xml b/data/levels/Level1.xml index 16ca875..72aaecd 100644 --- a/data/levels/Level1.xml +++ b/data/levels/Level1.xml @@ -11921,18 +11921,19 @@ 105 20 - - + resetPlayer + false 0 - 0 + -100000 0 - - - 1.0 + 100015 false 0 - - + resetPlayer.lua 0 - 0 + 105 0 @@ -12114,7 +12115,7 @@ - skydome.png + skydomeNew.png diff --git a/data/levels/scripts/resetPlayer.lua b/data/levels/scripts/resetPlayer.lua new file mode 100644 index 0000000..4428ee6 --- /dev/null +++ b/data/levels/scripts/resetPlayer.lua @@ -0,0 +1,17 @@ +local global = require( "global" ) +if(global.triggeredResetPlayer == nil) then + global.triggeredResetPlayer = false +end +function trigger(objectToChange) + if(global.triggeredResetPlayer == false) then + if(not level) then + print("No level found in Lua!") + return + end + + level:resetPlayer() + + --global.triggeredResetPlayer = true + print("reset player") + end +end diff --git a/data/shader/skydome.fsh b/data/shader/skydome.fsh new file mode 100644 index 0000000..aa8b140 --- /dev/null +++ b/data/shader/skydome.fsh @@ -0,0 +1,18 @@ +#version 150 + +in vec2 vTexCoord; +in vec4 fragPosition; + +out vec4 oColor; + +uniform sampler2D uTexture; +uniform float farPlane; +uniform vec4 fogColor; +uniform vec3 cameraCenter; + +void main() { + float distanceCameraCenter = distance(cameraCenter, vec3(fragPosition)); + float fogFactor = clamp((1.0 - ((farPlane - 35.0) -distanceCameraCenter)/30.0), 0.0, 1.0); + fogFactor *= clamp((1.0-((fragPosition.y-40.0)/30.0)), 0.0, 1.0); + oColor = mix(texture(uTexture, vTexCoord), fogColor, fogFactor); +} diff --git a/data/shader/skydome.vsh b/data/shader/skydome.vsh new file mode 100644 index 0000000..594040e --- /dev/null +++ b/data/shader/skydome.vsh @@ -0,0 +1,17 @@ +#version 150 + +in vec3 aPosition; +in vec3 aNormal; +in vec2 aTexCoord; + +uniform mat4 modelMatrix; +uniform mat4 modelViewProjectionMatrix; + +out vec2 vTexCoord; +out vec4 fragPosition; + +void main() { + fragPosition = modelMatrix * vec4(aPosition, 1.0); + vTexCoord = aTexCoord; + gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); +} diff --git a/graphics.cc b/graphics.cc index 19d4e14..a1c6d4c 100644 --- a/graphics.cc +++ b/graphics.cc @@ -69,6 +69,9 @@ void Graphics::init(Level* level) { // look up all shader files starting with 'phong' and build a ShaderProgram from it: lightingShader = ShaderProgramCreator("phong").attributeLocations( vao->getAttributeLocations()).create(); + + skydomeShader = ShaderProgramCreator("skydome").attributeLocations( + vao->getAttributeLocations()).create(); depthShader = ShaderProgramCreator("depth") .attributeLocations(vao->getAttributeLocations()).create(); @@ -235,6 +238,17 @@ void Graphics::render(double time) // lighting render pass framebuffer_light->bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + //set view and projection matrix + glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level); + + //render skydome + skydomeShader->use(); + // set fog Parameters + skydomeShader->setUniform("farPlane", farPlane); + skydomeShader->setUniform("fogColor", level->getFogColour()); + skydomeShader->setUniform("cameraCenter", level->getCameraCenter()->getPosition()); + level->getSkydome()->render(skydomeShader, false, true, &lightingViewProjectionMatrix); lightingShader->use(); @@ -273,9 +287,6 @@ void Graphics::render(double time) lightingShader->setUniform("ambientColor", level->getAmbientLight()); lightingShader->setUniform("camera", level->getPhysics()->getCameraPosition()); - //set view and projection matrix - glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level); - // render the level level->render(lightingShader, true, &lightingViewProjectionMatrix, &depthBiasVPs); @@ -285,7 +296,6 @@ void Graphics::render(double time) glEnable(GL_CULL_FACE); glCullFace(GL_BACK); - // draw with colors flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix); flameShader->setUniform("modelViewProjectionMatrix", lightingViewProjectionMatrix); diff --git a/graphics.hh b/graphics.hh index 0c48ac4..3e5358a 100644 --- a/graphics.hh +++ b/graphics.hh @@ -31,6 +31,7 @@ class Graphics { float farPlane; std::vector closestLights; SharedShaderProgram lightingShader; + SharedShaderProgram skydomeShader; SharedShaderProgram depthCubeShader; SharedShaderProgram depthShader; SharedShaderProgram flameShader; diff --git a/level.cc b/level.cc index f02597b..90c3ba4 100644 --- a/level.cc +++ b/level.cc @@ -38,6 +38,7 @@ void Level::load() { .addFunction("deleteObject", &Level::deleteObject) .addFunction("getObjectCount", &Level::getPhysicsObjectsVectorSize) .addFunction("moveObject", &Level::moveObject) + .addFunction("resetPlayer", &Level::resetPlayer) .endClass(); //Push the level to Lua as a global variable luabridge::push(luaState, this); @@ -49,9 +50,11 @@ void Level::load() { void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { for(unsigned int i = 0; irender(shader, lightingPass, viewProjectionMatrix, shadowVPs); + if (lightingPass) { + objects.at(i)->render(shader, lightingPass, true, viewProjectionMatrix, shadowVPs); + } + else { + objects.at(i)->render(shader, lightingPass, false, viewProjectionMatrix, shadowVPs); } } } @@ -185,7 +188,12 @@ void Level::deleteObject(int objectIndex){ void Level::resetPlayer(){ Loader loader = Loader(); glm::vec3 newPosition = loader.reloadPlayerPosition(xmlFilePath, this); - //TODO cameraCenter.setPosition(newPosition); + physics.forceMove(newPosition, playerIndex); + physics.forceMoveCamera(newPosition + glm::vec3(1,0,0)); +} + +void Level::setPlayerIndex(int index){ + playerIndex = index; } void Level::setStrength(float strength) { @@ -247,3 +255,7 @@ lua_State* Level::getLuaState() { Terrain* Level::getTerrain() { return &terrain; } + +Object* Level::getSkydome() { + return skydome; +} diff --git a/level.hh b/level.hh index 1062013..6e60bbf 100644 --- a/level.hh +++ b/level.hh @@ -36,6 +36,7 @@ class Level { glm::vec4 getFogColour(); void setSkydomeSize(float size); float getSkydomeSize(); + Object* getSkydome(); std::vector* getObjects(); std::vector* getPhysicsObjects(); void deleteObject(int objectIndex); @@ -56,6 +57,7 @@ class Level { lua_State* getLuaState(); Terrain* getTerrain(); void resetPlayer(); + void setPlayerIndex(int index); private: lua_State* luaState=nullptr; std::vector objects; @@ -66,6 +68,7 @@ class Level { glm::vec4 fogColour; Light directionalLight; Object* cameraCenter; + int playerIndex; Object* skydome; Physics physics; Camera camera; diff --git a/loader.cc b/loader.cc index d70b40f..cbba55a 100644 --- a/loader.cc +++ b/loader.cc @@ -85,7 +85,6 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa 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), true); - level->addObject(skydomeObject); level->setSkydomeObject(skydomeObject); //load lighting parameters @@ -259,6 +258,7 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa if(compositionType == 20){ level->setCameraCenter(object); + level->setPlayerIndex(objectIdentifier[1]); } }//objectData found }//finding the objectData diff --git a/object.cc b/object.cc index 3419786..d5daa34 100644 --- a/object.cc +++ b/object.cc @@ -14,18 +14,22 @@ Object::~Object() { } void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - glm::mat4* viewProjectionMatrix, std::vector* additionalMatrices) { + bool texturePass, glm::mat4* viewProjectionMatrix, + std::vector* additionalMatrices) { if (!renderable) { return; } glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale(glm::vec3(model.getScale())); + if(texturePass) { + shader->setTexture("uTexture", material.getReference(), 0); + shader->setUniform("modelMatrix", modelMatrix); + } if (lightingPass) { // set lightning parameters for this object shader->setUniform("ambientFactor", material.getAmbientFactor()); shader->setUniform("diffuseFactor", material.getDiffuseFactor()); shader->setUniform("specularFactor", material.getSpecularFactor()); shader->setUniform("shininess", material.getShininess()); - shader->setTexture("uTexture", material.getReference(), 0); // set model matrix shader->setUniform("modelMatrix", modelMatrix); // set shadowMVPs diff --git a/object.hh b/object.hh index 3b80f2b..f3d9c28 100644 --- a/object.hh +++ b/object.hh @@ -16,7 +16,8 @@ class Object : public Entity { Object(); ~Object(); void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - glm::mat4* viewProjcetionMatrix, std::vector* additionalMatrices); + bool texturePass, glm::mat4* viewProjcetionMatrix, + std::vector* additionalMatrices=0); private: Model model; Material material; diff --git a/physics.cc b/physics.cc index 24f681f..ed6c952 100644 --- a/physics.cc +++ b/physics.cc @@ -414,6 +414,26 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f throw std::invalid_argument( "Bodies out of Sync" ); } +void Physics::prepareCollisionDetection() +{ + playerTerrainCol = playerObjectColision = false; + int numManifods = world->getDispatcher()->getNumManifolds(); + + for (int i=0;isetCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z))); + bodies[indice]->setLinearVelocity(btVector3(0,0,0)); +} + +void Physics::forcePlayer(glm::vec3 newPosition)//ugly, but needed for reset +{ + playerBall->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z))); + playerBall->setLinearVelocity(btVector3(0,0,0)); + forceMoveCamera(newPosition + glm::vec3(currentDirection.x()*cameraDistance,currentDirection.y()*cameraDistance,currentDirection.z()*cameraDistance)); } void Physics::forceMoveCamera(glm::vec3 newPosition) diff --git a/physics.hh b/physics.hh index d8f287e..de2be9c 100644 --- a/physics.hh +++ b/physics.hh @@ -74,6 +74,10 @@ class Physics { void forceMove(glm::vec3 newPosition, unsigned indice); void forceMoveCamera(glm::vec3 newPosition); void addConvexBody(Entity entity, std::string path, float mass, float dampningL, float dampningA, unsigned indice, float scaling, bool rotate); + void prepareCollisionDetection(); + bool playerWithGround(); + bool playerWithObject(); + void forcePlayer(glm::vec3 newPosition); struct positionConstraint{btRigidBody* body; float strength; btVector3 position;}; @@ -98,6 +102,8 @@ class Physics { int counter = 0; std::string geometryPath; float cameraDistance = 5; //distance of the camera to the player. + bool playerTerrainCol = false; + bool playerObjectColision = false; }; enum collisionTypes{