Merge branch 'master' of github.com:Faerbit/swp

This commit is contained in:
Steffen Fündgens 2014-11-17 13:36:09 +01:00
commit 0c4184477b
8 changed files with 104 additions and 31 deletions

View File

@ -19,6 +19,7 @@ float Camera::getDistance() {
void Camera::setDistance(float distance) {
this->distance = distance;
updatePosition();
}
glm::vec2 Camera::getRotation() {
@ -27,9 +28,11 @@ glm::vec2 Camera::getRotation() {
void Camera::setRotation(glm::vec2 rotation) {
this->rotation = rotation;
updatePosition();
}
void Camera::updateRotation(glm::vec2 rotation) {
this->rotation += rotation;
if((this->rotation.x + rotation.x) >= 1.57f) {
this->rotation.x = 1.57;
this->rotation.y += rotation.y;
@ -41,6 +44,7 @@ void Camera::updateRotation(glm::vec2 rotation) {
else {
this-> rotation += rotation;
}
updatePosition();
}
void Camera:: updateDistance(float distance) {
@ -53,4 +57,18 @@ void Camera:: updateDistance(float distance) {
else {
this->distance += distance;
}
updatePosition();
}
void Camera::updatePosition() {
glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, distance, 0.0f);
// rotate vector
glm::mat4 rotationMatrix =
glm::rotate<float>(rotation[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate<float>(rotation[0], glm::vec3(1.0f, 0.0f, 0.0f));
this->vector = glm::vec3(rotationMatrix * cameraVector);
}
glm::vec3 Camera::getVector() {
return vector;
}

View File

@ -14,9 +14,12 @@ class Camera {
glm::vec2 getRotation();
void setRotation(glm::vec2 rotation);
void updateRotation(glm::vec2 rotation); //adds to current rotation
glm::vec3 getVector();
private:
void updatePosition();
float distance;
glm::vec2 rotation;
glm::vec3 vector;
};
#endif

View File

@ -154,11 +154,7 @@ glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, fl
}
glm::mat4 Graphics::buildViewMatrix(Level* level) {
glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, level->getCamera()->getDistance(), 0.0f);
// rotate vector
glm::mat4 rotationMatrix =
glm::rotate<float>(level->getCamera()->getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) *glm::rotate<float>(level->getCamera()->getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f));
cameraVector = rotationMatrix * cameraVector;
//construct lookAt (cameraPosition = cameraCenter + cameraVector
return glm::lookAt(level->getCameraCenter()->getPosition() + glm::vec3(cameraVector), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f));
return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()),
level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f));
}

View File

@ -1,5 +1,7 @@
#include "level.hh"
Level::Level(std::string filePath){
this->filePath = filePath;
this->terrain = Terrain(filePath + "/terrain");
@ -12,6 +14,10 @@ Level::~Level() {
}
void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
this->physics = Physics();
this->physics.init();
// currently hard coded should later read this stuff out of a file
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
// load the geometry of the stanford bunny and build a VAO:
@ -19,9 +25,14 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
// load a texture:
Material material = Material("stoneTexture.png", 0.1f, 0.5f, 0.5f, 3.0f);
//Create object
Object object = Object(model, material, glm::vec3(0.0f, 7.0f, -2.0f),
Object object = Object(model, material, glm::vec3(0.0f, 5.0f, 0.0f),
glm::vec3(0.0f, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f), shader);
//add player to phy
this->physics.addPlayer(0.75f,0.0f,5.0f,0.0f,1.0f,0);
physics.addStaticGroundPlane();
//set lighting parameters
@ -53,7 +64,7 @@ void Level::render() {
}
}
void Level::update(float runTime, glm::vec2 mouseDelta) {
void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool dPressed, bool sPressed) {
// rotate bunny
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
// Ignore first two mouse updates, because they are incorrect
@ -63,7 +74,18 @@ void Level::update(float runTime, glm::vec2 mouseDelta) {
}
else {
camera.updateRotation(mouseDelta/100.0f);
}
if(wPressed)
{
//physics.rollForward(camera.getRotation);
}
physics.takeUpdateStep(runTime);
objects[0].setPosition(physics.getPos(0));
}
glm::vec3 Level::getAmbientLight() {

View File

@ -8,6 +8,7 @@
#include "terrain.hh"
#include "material.hh"
#include "camera.hh"
#include "physics.hh"
class Level {
public:
@ -15,7 +16,7 @@ class Level {
Level();
~Level();
void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning
void update(float runTime, glm::vec2 mouseDelta);
void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed);
void render();
glm::vec3 getAmbientLight();
std::vector<Light> getLights();
@ -27,6 +28,7 @@ class Level {
std::vector<Light> lights;
glm::vec3 ambientLight;
Object* cameraCenter;
Physics physics;
Camera camera;
Terrain terrain;
};

19
main.cc
View File

@ -119,10 +119,18 @@ int main( int argc, char *argv[] )
const double FPSdelay = 2.0;
double startTimeInSeconds = glfwGetTime();
double showNextFPS = startTimeInSeconds + FPSdelay;
double lastUpdate=0.0f;
int stateW = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_W);
int stateA = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_A);
int stateS = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_S);
int stateD = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_D);
do {
double now = glfwGetTime();
double now = glfwGetTime()- startTimeInSeconds;
if (showNextFPS <= now) {
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
sstream << std::setprecision(1) << std::fixed
@ -136,10 +144,13 @@ int main( int argc, char *argv[] )
glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos);
glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2);
app.getLevel()->update(now - startTimeInSeconds, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
(float)xpos-app.getGraphics()->getWindowSize().x/2));
app.getLevel()->update(now - lastUpdate,
glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
(float)xpos-app.getGraphics()->getWindowSize().x/2),
stateW == GLFW_PRESS,stateA == GLFW_PRESS,stateS == GLFW_PRESS,stateD == GLFW_PRESS);
lastUpdate = now;
app.getGraphics()->render(app.getLevel(), app.getShader());
openGLCriticalError();
// MacOS X will not swap correctly is another FBO is bound:

View File

@ -1,7 +1,5 @@
#include "physics.hh"
#include <vector>
btDynamicsWorld* world; //contains physical attributes of the world.
btDispatcher* dispatcher; //
@ -12,8 +10,16 @@ btConstraintSolver* solver; //solver for forces and impulses.
std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
btRigidBody* playerBall;
btRigidBody* terrainBody;
btRigidBody* staticGroundBody;
void init()
Physics::Physics() {
}
Physics::~Physics() {
}
void Physics::init()
{
colConfig = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(colConfig);
@ -21,17 +27,15 @@ void init()
solver = new btSequentialImpulseConstraintSolver();
world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig);
world->setGravity(btVector3(0,-10,-0));
world->setGravity(btVector3(0,-10,-0));
}
void takeUpdateStep(float timeDiff)
void Physics::takeUpdateStep(float timeDiff)
{
world->stepSimulation(timeDiff);
}
void addTerrain(int width, int length, float** heightData)
void Physics::addTerrain(int width, int length, float** heightData)
{
float* heightfield = new float[width * length];
int highest = -999999, j = 0, i = 0;
@ -70,7 +74,17 @@ void addTerrain(int width, int length, float** heightData)
}
void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice)
void Physics::addStaticGroundPlane()
{
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
staticGroundBody = new btRigidBody(groundRigidBodyCI);
world->addRigidBody(staticGroundBody);
}
void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsigned indice)
{
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
@ -98,7 +112,7 @@ void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice
}
void addSphere(float rad, float x, float y, float z, float mass, unsigned indice)
void Physics::addSphere(float rad, float x, float y, float z, float mass, unsigned indice)
{
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
@ -126,30 +140,34 @@ void addSphere(float rad, float x, float y, float z, float mass, unsigned indice
}
glm::vec3 getPos(int i)
glm::vec3 Physics::getPos(int i)
{
btVector3 origin = bodies[i]->getCenterOfMassPosition();
glm::vec3 save(origin.getX(),origin.getY(),origin.getZ());
return save;
}
glm::mat4 getRotation(int i)
glm::mat4 Physics::getRotation(int i)
{
btQuaternion quat = bodies[i]->getOrientation();
glm::mat4 matrix = glm::rotate(
matrix,
quat.getAngle(),
glm::vec3(quat.getAxis().getX(), quat.getAxis().getY(), quat.getAxis().getZ())
);
return matrix;
}
void rollForward(glm::mat3 rotCamera)
void Physics::rollForward(glm::vec3 camPos)
{
glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera;
btVector3 pos(camPos.x,camPos.y,camPos.z);
pos -= playerBody->getCenterOfMassPosition();
pos.cross(btVector3(0,1,0));
playerBall->applyTorque(pos);
/* glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera;
saveVector = glm::cross(glm::vec3(0,1,0),saveVector);
playerBall->applyTorque(btVector3(saveVector[0],saveVector[1],saveVector[2]));
playerBall->applyTorque(btVector3(saveVector[0],saveVector[1],saveVector[2]));*/
}
/*

View File

@ -11,6 +11,7 @@
#include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.h"
#include "extern/bullet/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
#include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h"
#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btConstraintSolver.h"
#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY!
@ -31,14 +32,15 @@
class Physics {
public:
Physics();
Physics();
~Physics();
void init();
void takeUpdateStep(float timeDiff);
void rollForward(glm::vec3 camPos, float strength);
glm::vec3 getPos(int i);
glm::mat4 getRotation(int i);
void rollForward(glm::mat3 rotCamera);
void rollForward(glm::vec3 camPos);
void addStaticGroundPlane();
void addTerrain(int width, int length, float** heightData);
void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice);
void addSphere(float rad, float x, float y, float z, float mass, unsigned indice);
@ -47,6 +49,7 @@ class Physics {
btRigidBody* playerBody;
btRigidBody* terrainBody;
std::vector<btRigidBody*> 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.