diff --git a/Levels/ObjectSetups/Compositions.xml b/Levels/ObjectSetups/Compositions.xml index 9a53ec6..3b47bf5 100644 --- a/Levels/ObjectSetups/Compositions.xml +++ b/Levels/ObjectSetups/Compositions.xml @@ -163,9 +163,10 @@ 0.6 0.4 2.0 - TriangleMesh - 0.5 - 0.5 + Box + 0.5 + 0.5 + 0.5 diff --git a/Levels/heightmapLvl1.png b/Levels/heightmapLvl1.png new file mode 100644 index 0000000..fd365ac Binary files /dev/null and b/Levels/heightmapLvl1.png differ diff --git a/Shader/phong.fsh b/Shader/phong.fsh index af8a2e8..bd723e3 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -13,6 +13,7 @@ uniform sampler2D uTexture; uniform sampler2DShadow shadowMap_near; uniform sampler2DShadow shadowMap_middle; uniform sampler2DShadow shadowMap_far; +uniform samplerCubeShadow shadowMap_cube; uniform vec3 ambientColor; uniform float ambientFactor; uniform float diffuseFactor; @@ -49,7 +50,7 @@ vec2 poissonDisk[16] = vec2[]( vec2( 0.14383161, -0.14100790 ) ); -float sampleShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { +float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { float visibility = 1.0; float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0))); bias = clamp(bias, 0.0, 0.01); @@ -68,6 +69,11 @@ float sampleShadow(sampler2DShadow shadowMap, vec4 shadowCoord) { return visibility; } +float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) { + float bias = 0.005; + return texture(shadowMap, vec4(lightDirection.xyz , length(lightDirection) - bias)); +} + float distanceToBorder(vec2 vector) { float xDistance = min(vector.x, 1.0-vector.x); float yDistance = min(vector.y, 1.0-vector.y); @@ -92,8 +98,10 @@ void main() } // point lights + float visibility = 1.0; for(int i = 0; i 0.001f) { vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition)); @@ -103,21 +111,21 @@ void main() vec3 cameraVector = normalize(camera - vec3(fragPosition)); specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0) *specularFactor*intensity*lightColors[i]; + visibility = samplePointShadow(shadowMap_cube, lightDirection); } } // shadows - float visibility = 1.0; if (distanceToBorder(shadowCoord_middle.xy) <= 0.5 && distanceToBorder(shadowCoord_middle.xy) > 0.0) { if (distanceToBorder(shadowCoord_near.xy) <= 0.5 && distanceToBorder(shadowCoord_near.xy) > 0.0) { - visibility = sampleShadow(shadowMap_near, shadowCoord_near); + visibility *= sampleDirectionalShadow(shadowMap_near, shadowCoord_near); } else { - visibility = sampleShadow(shadowMap_middle, shadowCoord_middle); + visibility *= sampleDirectionalShadow(shadowMap_middle, shadowCoord_middle); } } else { - visibility = sampleShadow(shadowMap_far, shadowCoord_far); + visibility *= sampleDirectionalShadow(shadowMap_far, shadowCoord_far); } specularColor *= visibility; diff --git a/Shader/phong.vsh b/Shader/phong.vsh index d2806b9..11d9863 100644 --- a/Shader/phong.vsh +++ b/Shader/phong.vsh @@ -2,7 +2,7 @@ uniform mat4 modelMatrix; uniform mat4 modelViewProjectionMatrix; -uniform mat4 shadowMVPs[35]; +uniform mat4 shadowMVPs[5]; in vec3 aPosition; in vec3 aNormal; diff --git a/graphics.cc b/graphics.cc index 8f35b41..834f97a 100644 --- a/graphics.cc +++ b/graphics.cc @@ -70,8 +70,10 @@ void Graphics::init(Level* level) { framebuffer_far->setDepthTexture(depthTexture_far); framebuffer_far->validate(); - depth_cubeMaps = std::vector(level->getLights()->size()); - for (unsigned int i = 0; i(level->getLights()->size()); + for (unsigned int i = 0; i(1); + for (unsigned int i = 0; i<1; i++) { depth_cubeMaps.at(i) = SharedTextureCubeMap(new TextureCubeMap(glm::vec2(cube_size, cube_size), GL_DEPTH_COMPONENT16)); depth_cubeMaps.at(i)->setMinFilter(GL_NEAREST); depth_cubeMaps.at(i)->setMagFilter(GL_NEAREST); @@ -102,14 +104,14 @@ void Graphics::render() glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; framebuffer_cube->bind(); - for (unsigned int i_pointlight = 0; i_pointlightgetLights()->size(); i_pointlight++) { + //for (unsigned int i_pointlight = 0; i_pointlightgetLights()->size(); i_pointlight++) { + for (unsigned int i_pointlight = 0; i_pointlight<1; i_pointlight++) { // render each side of the cube for (int i_face = 0; i_face<6; i_face++) { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i_pointlight)->getObjectName(), 0); glClear(GL_DEPTH_BUFFER_BIT); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * glm::lookAt(level->getLights()->at(i_pointlight).getPosition(), level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], glm::vec3(0.0f, 1.0f, 0.0f)); - depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_face); level->render(depthShader, false, &depthViewProjectionMatrix_face); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); @@ -182,6 +184,8 @@ void Graphics::render() } glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), sizeof(lightIntensities), (GLfloat*) lightIntensities); + + lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4); } // set directional Light if(level->getDirectionalLight()) { diff --git a/level.cc b/level.cc index 13e82b8..264ecf9 100644 --- a/level.cc +++ b/level.cc @@ -149,7 +149,7 @@ void Level::load() { } std::string dataModelPath = charDataModelPath; //objectData found - if(dataModelPath == modelPath){ + if(dataModelPath.compare(modelPath) == 0){ //create the object float ambientFactor, diffuseFactor, specularFactor, shininess; errorCheck(objectData->FirstChildElement("ambientFactor")->QueryFloatText(&ambientFactor)); @@ -237,24 +237,24 @@ void Level::load() { }//iterating over all objects of the composition //iterate over all lights of the composition - XMLElement* light = composition->FirstChildElement("light"); - for(; light; light=light->NextSiblingElement("light")){ + XMLElement* xmlLight = composition->FirstChildElement("light"); + for(; xmlLight; xmlLight=xmlLight->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(xmlLight->FirstChildElement("xOffset")->QueryFloatText(&lightOffset[0])); + errorCheck(xmlLight->FirstChildElement("yOffset")->QueryFloatText(&lightOffset[1])); + errorCheck(xmlLight->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)); + 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)); 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())], @@ -273,15 +273,32 @@ void Level::load() { }//iterating over all compositions in Level.xml //load triggers - XMLElement* trigger = doc->FirstChildElement("trigger"); - for(; trigger; trigger=trigger->NextSiblingElement("trigger")){ - const char* charName = trigger->FirstChildElement("name")->GetText(); + XMLElement* xmlTrigger = doc->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"); } std::string name = charName; if (name.compare("-") != 0){ - //TODO add triggers + float xPos, yPos, zPos, distance; + std::vector position; + bool isBigger; + int idGreen, idBlue, objectNum; + errorCheck(xmlTrigger->FirstChildElement("xPosition")->QueryFloatText(&xPos)); + errorCheck(xmlTrigger->FirstChildElement("yPosition")->QueryFloatText(&yPos)); + errorCheck(xmlTrigger->FirstChildElement("zPosition")->QueryFloatText(&zPos)); + errorCheck(xmlTrigger->FirstChildElement("distance")->QueryFloatText(&distance)); + position.push_back(xPos); + position.push_back(yPos); + position.push_back(zPos); + errorCheck(xmlTrigger->FirstChildElement("isBiggerThan")->QueryBoolText(&isBigger)); + errorCheck(xmlTrigger->FirstChildElement("idGreen")->QueryIntText(&idGreen)); + errorCheck(xmlTrigger->FirstChildElement("idBlue")->QueryIntText(&idBlue)); + errorCheck(xmlTrigger->FirstChildElement("objectNum")->QueryIntText(&objectNum)); + //TODO find the object + //Trigger trigger = Trigger::Trigger(position, distance, isBigger, Object* object, void (*functionPointer)()); + //triggers.push_back(trigger); } } diff --git a/level.hh b/level.hh index 364d392..75b3815 100644 --- a/level.hh +++ b/level.hh @@ -10,6 +10,7 @@ #include "camera.hh" #include "physics.hh" #include "tinyxml2.hh" +#include "trigger.hh" class Level { public: @@ -34,6 +35,7 @@ class Level { std::vector objects; std::vector physicObjects; std::vector lights; + std::vector triggers; glm::vec3 ambientLight; glm::vec4 fogColour; Light directionalLight; diff --git a/trigger.cc b/trigger.cc index ab0618a..5296efd 100644 --- a/trigger.cc +++ b/trigger.cc @@ -1,7 +1,11 @@ #include "trigger.hh" -Trigger::Trigger(std::vector position, float distance, bool isBigger, int objectIndex, int functionPointer) { - +Trigger::Trigger(std::vector position, float distance, bool isBigger, Object* object, void (*functionPointer)()) { + this->position=position; + this->distance=distance; + this->isBigger=isBigger; + this->object=object; + this->functionPointer=functionPointer; } Trigger::Trigger(){ @@ -9,3 +13,7 @@ Trigger::Trigger(){ Trigger::~Trigger(){ } + +void Trigger::triggerUpdate(){ + +} diff --git a/trigger.hh b/trigger.hh index 05f1c3b..04fbac1 100644 --- a/trigger.hh +++ b/trigger.hh @@ -2,14 +2,20 @@ #define TRIGGER_HH_INCLUDED #include +#include "object.hh" class Trigger { public: - Trigger(std::vector position, float distance, bool isBigger, int objectIndex, int functionPointer); + Trigger(std::vector position, float distance, bool isBigger, Object* object, void (*functionPointer)()); Trigger(); ~Trigger(); + void triggerUpdate(); private: - + std::vector position; + float distance; + bool isBigger; + Object* object; + void (*functionPointer)(); }; #endif