diff --git a/Blenderfiles/torch.blend b/Blenderfiles/torch.blend new file mode 100644 index 0000000..5803e11 Binary files /dev/null and b/Blenderfiles/torch.blend differ diff --git a/Levels/ObjectSetups/Compositions.xml b/Levels/ObjectSetups/Compositions.xml index bfe11a5..3b47bf5 100644 --- a/Levels/ObjectSetups/Compositions.xml +++ b/Levels/ObjectSetups/Compositions.xml @@ -5,7 +5,7 @@ 0.0 0.0 0.0 - 0.75 + 1.0 8.0 @@ -17,8 +17,8 @@ 0.0 1.0 2.0 - 1.0 - 2.0 + 1.5 + 0.0 @@ -35,7 +35,7 @@ - 99 + 80 torch.obj 0.0 @@ -55,14 +55,15 @@ + - 100 + 99 column.obj 0.0 0.0 0.0 - 1.0 + 1.5 0.0 @@ -70,7 +71,7 @@ 2.0 0.0 0.0 - 1.0 + 1.5 0.0 @@ -78,7 +79,7 @@ 1.0 3.0 0.0 - 1.0 + 1.5 0.0 @@ -126,9 +127,9 @@ 0.4 2.0 Box - 1.0 + 5.0 1.0 - 3.0 + 1.8 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/physics.cc b/physics.cc index 7fd76b2..dad6de8 100644 --- a/physics.cc +++ b/physics.cc @@ -1,4 +1,5 @@ #include "physics.hh" +#include "extern/bullet/Extras/Serialize/BulletWorldImporter/btBulletWorldImporter.h" Physics::Physics() { @@ -23,6 +24,44 @@ void Physics::takeUpdateStep(float timeDiff) world->stepSimulation(timeDiff); } +//TERRAIN SUBSET +void Physics::addTerrainTriangles(int width, int length, float** heightData) +{//not working correctly something with offset wrong? + btTriangleMesh* trimesh = new btTriangleMesh(); + for(int i = 0; i < width-1;i++) + { + for(int j = 0; j < length-1; j++) + { + btVector3 v0(i,heightData[j][i],j); + btVector3 v1(i+1,heightData[j][i+1],j); + btVector3 v2(i,heightData[j+1][i],j+1); + + trimesh->addTriangle(v0,v1,v2); + } + } + for(int i = 1; i < width;i++) + { + for(int j = 1; j < length; j++) + { + btVector3 v0(i,heightData[j][i],j); + btVector3 v1(i-1,heightData[j][i-1],j); + btVector3 v2(i,heightData[j-1][i],j-1); + + trimesh->addTriangle(v0,v1,v2); + } + } + + btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(trimesh, true); + btRigidBody* tBody = new btRigidBody(0, new btDefaultMotionState(),shape); + + tBody->getWorldTransform().setOrigin(btVector3(-length/2,0,-width/2)); + + terrainBody = tBody; + + world->addRigidBody(tBody); + +} + void Physics::addTerrain(int width, int length, float** heightData) { @@ -50,7 +89,7 @@ void Physics::addTerrain(int width, int length, float** heightData) terrainBody = tBody; - world->addRigidBody(terrainBody); + world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS); } void Physics::addStaticGroundPlane() @@ -63,6 +102,8 @@ void Physics::addStaticGroundPlane() world->addRigidBody(staticGroundBody); } + +//players and objects void Physics::addPlayer(float rad, Entity entity, float mass, unsigned indice) { if(bodies.size() == indice) @@ -79,6 +120,8 @@ void Physics::addPlayer(float rad, Entity entity, float mass, unsigned indice) btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); + info.m_friction = 5; + playerBall = new btRigidBody(info); playerBall->setDamping(0.1f,0.3f); @@ -93,6 +136,77 @@ void Physics::addPlayer(float rad, Entity entity, float mass, unsigned indice) } +void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, float dampningL, float dampningA,unsigned indice) +{//TODO look at convexHullShapes + + if(bodies.size() == indice) + throw std::invalid_argument( "Bodies out of Sync" ); + + std::vector< unsigned int > vertexIndices; + std::vector< btVector3 > temp_vertices; + + path = "../Levels/Geometry/" + path; + FILE * file = fopen(path.c_str(), "r"); + if( file == NULL ){ + printf("Impossible to open the file !\n"); + } + + while( 1 ){ + + char lineHeader[128]; + // read the first word of the line + int res = fscanf(file, "%s", lineHeader); + if (res == EOF) + break; // EOF = End Of File. Quit the loop. + // else : parse lineHeader + if ( strcmp( lineHeader, "v" ) == 0 ){ + glm::vec3 vertex; + fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z ); + temp_vertices.push_back(btVector3(vertex.x,vertex.y,vertex.z)); + } + else if ( strcmp( lineHeader, "f" ) == 0 ){ + std::string vertex1, vertex2, vertex3; + unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; + int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] ); + vertexIndices.push_back(vertexIndex[0]); + vertexIndices.push_back(vertexIndex[1]); + vertexIndices.push_back(vertexIndex[2]); + } + } + printf("olla"); + //finally start making body + btTriangleMesh* triMesh = new btTriangleMesh(); + + for(unsigned i = 2; i < vertexIndices.size();i++) + { + triMesh->addTriangle(temp_vertices[vertexIndices[i]],temp_vertices[vertexIndices[i-1]],temp_vertices[vertexIndices[i-2]]); + } + + btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(triMesh,true); + + btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); + + btVector3 inertia(0,0,0); + if(mass != 0.0) + { + shape->calculateLocalInertia((btScalar)mass,inertia); + } + + btRigidBody::btRigidBodyConstructionInfo info(mass,motion,shape,inertia); + + btRigidBody* body = new btRigidBody(info); + + body->setDamping(dampningL,dampningA); + + bodies.push_back(body); + + world->addRigidBody(body); + + + if(bodies.size() != indice) + throw std::invalid_argument( "Bodies out of Sync" ); +} + void Physics::addBox(float width, float height, float length, Entity entity, float mass, unsigned indice) { @@ -138,7 +252,7 @@ void Physics::addSphere(float rad, Entity entity, float mass, unsigned indice) btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); - //info. + btRigidBody* body = new btRigidBody(info); @@ -154,7 +268,7 @@ void Physics::addSphere(float rad, Entity entity, float mass, unsigned indice) throw std::invalid_argument( "Bodies out of Sync" ); } - +/* void Physics::addTriangleMeshBody(Entity entity, float mass, float dampningL, float dampningA,unsigned indice) { btTriangleMesh* trimesh = new btTriangleMesh(); @@ -180,7 +294,7 @@ void Physics::addTriangleMeshBody(Entity entity, float mass, float dampningL, fl body->setDamping(dampningL,dampningA); -} +}*/ void Physics::addCamera(float rad, float distance) { @@ -206,6 +320,8 @@ void Physics::addCamera(float rad, float distance) world->addConstraint(pdc); } + +//update functions glm::vec3 Physics::getCameraPosition() { btVector3 origin = cameraBody->getCenterOfMassPosition(); diff --git a/physics.hh b/physics.hh index 9eff59c..9d96467 100644 --- a/physics.hh +++ b/physics.hh @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "entity.hh" @@ -17,6 +19,8 @@ #include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btTriangleMesh.h" +#include "extern/bullet/src/BulletCollision/CollisionShapes/btCollisionShape.h" +#include "extern/bullet/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btConstraintSolver.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY! @@ -51,20 +55,21 @@ class Physics { void addStaticGroundPlane(); void addCamera(float rad,float distance); //Do NOT impliment before Player has been created; glm::vec3 getCameraPosition(); - void addTriangleMeshBody(Entity entity, float mass, float dampningL, float dampningA,unsigned indice); + void addRigidBodyFromFile(Entity entity, float mass, float dampningL, float dampningA, std::string modelLocation,unsigned indice); + void addTriangleMeshBody(Entity entity, std::string path, float mass, float dampningL, float dampningA,unsigned indice); void addTerrain(int width, int length, float** heightData); + void addTerrainTriangles(int width, int length, float** heightData); //add the terrain as a trimesh instead of a heightmap void addPlayer(float rad, Entity entity, float mass, unsigned indice); //use these AFTER physicObjects.push_back(object)! if mass == 0 then the object is unmoveable void addSphere(float rad, Entity entity, float mass, unsigned indice); //The Indice should be set to physicObjects.size() void addBox(float width, float height, float length, Entity entity, float mass, unsigned indice); //this is used to ensuer that the system is synchronized private: - btRigidBody* playerBall; //allows for quicker access to the ball + btRigidBody* playerBall; //allows for easier access to the ball btRigidBody* terrainBody; //duh btRigidBody* cameraBody; std::vector bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process. btRigidBody* staticGroundBody; - - + btDynamicsWorld* world; //contains physical attributes of the world. btDispatcher* dispatcher; // btCollisionConfiguration* colConfig; //defines the type of collision detection. @@ -72,6 +77,12 @@ class Physics { btConstraintSolver* solver; //solver for forces and impulses. }; +enum collisionTypes{ + COL_NOTHING = 0, + COL_TERRAIN = 1, + COL_OBJECTS = 2, + COL_OBJECTS_NO_TERRAIN = 4 +}; class btDistanceConstraint : public btPoint2PointConstraint {