Merge branch 'master' of github.com:Faerbit/swp
This commit is contained in:
commit
0c4184477b
18
camera.cc
18
camera.cc
@ -19,6 +19,7 @@ float Camera::getDistance() {
|
|||||||
|
|
||||||
void Camera::setDistance(float distance) {
|
void Camera::setDistance(float distance) {
|
||||||
this->distance = distance;
|
this->distance = distance;
|
||||||
|
updatePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Camera::getRotation() {
|
glm::vec2 Camera::getRotation() {
|
||||||
@ -27,9 +28,11 @@ glm::vec2 Camera::getRotation() {
|
|||||||
|
|
||||||
void Camera::setRotation(glm::vec2 rotation) {
|
void Camera::setRotation(glm::vec2 rotation) {
|
||||||
this->rotation = rotation;
|
this->rotation = rotation;
|
||||||
|
updatePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateRotation(glm::vec2 rotation) {
|
void Camera::updateRotation(glm::vec2 rotation) {
|
||||||
|
this->rotation += rotation;
|
||||||
if((this->rotation.x + rotation.x) >= 1.57f) {
|
if((this->rotation.x + rotation.x) >= 1.57f) {
|
||||||
this->rotation.x = 1.57;
|
this->rotation.x = 1.57;
|
||||||
this->rotation.y += rotation.y;
|
this->rotation.y += rotation.y;
|
||||||
@ -41,6 +44,7 @@ void Camera::updateRotation(glm::vec2 rotation) {
|
|||||||
else {
|
else {
|
||||||
this-> rotation += rotation;
|
this-> rotation += rotation;
|
||||||
}
|
}
|
||||||
|
updatePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera:: updateDistance(float distance) {
|
void Camera:: updateDistance(float distance) {
|
||||||
@ -53,4 +57,18 @@ void Camera:: updateDistance(float distance) {
|
|||||||
else {
|
else {
|
||||||
this->distance += distance;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -14,9 +14,12 @@ class Camera {
|
|||||||
glm::vec2 getRotation();
|
glm::vec2 getRotation();
|
||||||
void setRotation(glm::vec2 rotation);
|
void setRotation(glm::vec2 rotation);
|
||||||
void updateRotation(glm::vec2 rotation); //adds to current rotation
|
void updateRotation(glm::vec2 rotation); //adds to current rotation
|
||||||
|
glm::vec3 getVector();
|
||||||
private:
|
private:
|
||||||
|
void updatePosition();
|
||||||
float distance;
|
float distance;
|
||||||
glm::vec2 rotation;
|
glm::vec2 rotation;
|
||||||
|
glm::vec3 vector;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -154,11 +154,7 @@ glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, fl
|
|||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 Graphics::buildViewMatrix(Level* level) {
|
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
|
//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));
|
||||||
}
|
}
|
||||||
|
26
level.cc
26
level.cc
@ -1,5 +1,7 @@
|
|||||||
#include "level.hh"
|
#include "level.hh"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Level::Level(std::string filePath){
|
Level::Level(std::string filePath){
|
||||||
this->filePath = filePath;
|
this->filePath = filePath;
|
||||||
this->terrain = Terrain(filePath + "/terrain");
|
this->terrain = Terrain(filePath + "/terrain");
|
||||||
@ -12,6 +14,10 @@ Level::~Level() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
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
|
// currently hard coded should later read this stuff out of a file
|
||||||
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
|
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
|
||||||
// load the geometry of the stanford bunny and build a VAO:
|
// 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:
|
// load a texture:
|
||||||
Material material = Material("stoneTexture.png", 0.1f, 0.5f, 0.5f, 3.0f);
|
Material material = Material("stoneTexture.png", 0.1f, 0.5f, 0.5f, 3.0f);
|
||||||
//Create object
|
//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, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
|
||||||
glm::vec3(0.0f, 0.0f, 0.0f), shader);
|
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
|
//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
|
// rotate bunny
|
||||||
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
|
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
|
||||||
// Ignore first two mouse updates, because they are incorrect
|
// Ignore first two mouse updates, because they are incorrect
|
||||||
@ -64,6 +75,17 @@ void Level::update(float runTime, glm::vec2 mouseDelta) {
|
|||||||
else {
|
else {
|
||||||
camera.updateRotation(mouseDelta/100.0f);
|
camera.updateRotation(mouseDelta/100.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(wPressed)
|
||||||
|
{
|
||||||
|
//physics.rollForward(camera.getRotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
physics.takeUpdateStep(runTime);
|
||||||
|
|
||||||
|
objects[0].setPosition(physics.getPos(0));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Level::getAmbientLight() {
|
glm::vec3 Level::getAmbientLight() {
|
||||||
|
4
level.hh
4
level.hh
@ -8,6 +8,7 @@
|
|||||||
#include "terrain.hh"
|
#include "terrain.hh"
|
||||||
#include "material.hh"
|
#include "material.hh"
|
||||||
#include "camera.hh"
|
#include "camera.hh"
|
||||||
|
#include "physics.hh"
|
||||||
|
|
||||||
class Level {
|
class Level {
|
||||||
public:
|
public:
|
||||||
@ -15,7 +16,7 @@ class Level {
|
|||||||
Level();
|
Level();
|
||||||
~Level();
|
~Level();
|
||||||
void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning
|
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();
|
void render();
|
||||||
glm::vec3 getAmbientLight();
|
glm::vec3 getAmbientLight();
|
||||||
std::vector<Light> getLights();
|
std::vector<Light> getLights();
|
||||||
@ -27,6 +28,7 @@ class Level {
|
|||||||
std::vector<Light> lights;
|
std::vector<Light> lights;
|
||||||
glm::vec3 ambientLight;
|
glm::vec3 ambientLight;
|
||||||
Object* cameraCenter;
|
Object* cameraCenter;
|
||||||
|
Physics physics;
|
||||||
Camera camera;
|
Camera camera;
|
||||||
Terrain terrain;
|
Terrain terrain;
|
||||||
};
|
};
|
||||||
|
17
main.cc
17
main.cc
@ -120,8 +120,16 @@ int main( int argc, char *argv[] )
|
|||||||
double startTimeInSeconds = glfwGetTime();
|
double startTimeInSeconds = glfwGetTime();
|
||||||
double showNextFPS = startTimeInSeconds + FPSdelay;
|
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 {
|
do {
|
||||||
double now = glfwGetTime();
|
|
||||||
|
double now = glfwGetTime()- startTimeInSeconds;
|
||||||
|
|
||||||
if (showNextFPS <= now) {
|
if (showNextFPS <= now) {
|
||||||
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
||||||
@ -136,8 +144,11 @@ int main( int argc, char *argv[] )
|
|||||||
glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos);
|
glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos);
|
||||||
glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2);
|
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,
|
app.getLevel()->update(now - lastUpdate,
|
||||||
(float)xpos-app.getGraphics()->getWindowSize().x/2));
|
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());
|
app.getGraphics()->render(app.getLevel(), app.getShader());
|
||||||
|
|
||||||
openGLCriticalError();
|
openGLCriticalError();
|
||||||
|
48
physics.cc
48
physics.cc
@ -1,7 +1,5 @@
|
|||||||
#include "physics.hh"
|
#include "physics.hh"
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
|
|
||||||
btDynamicsWorld* world; //contains physical attributes of the world.
|
btDynamicsWorld* world; //contains physical attributes of the world.
|
||||||
btDispatcher* dispatcher; //
|
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.
|
std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
|
||||||
btRigidBody* playerBall;
|
btRigidBody* playerBall;
|
||||||
btRigidBody* terrainBody;
|
btRigidBody* terrainBody;
|
||||||
|
btRigidBody* staticGroundBody;
|
||||||
|
|
||||||
void init()
|
|
||||||
|
Physics::Physics() {
|
||||||
|
}
|
||||||
|
|
||||||
|
Physics::~Physics() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void Physics::init()
|
||||||
{
|
{
|
||||||
colConfig = new btDefaultCollisionConfiguration();
|
colConfig = new btDefaultCollisionConfiguration();
|
||||||
dispatcher = new btCollisionDispatcher(colConfig);
|
dispatcher = new btCollisionDispatcher(colConfig);
|
||||||
@ -22,16 +28,14 @@ void init()
|
|||||||
world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig);
|
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);
|
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];
|
float* heightfield = new float[width * length];
|
||||||
int highest = -999999, j = 0, i = 0;
|
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)
|
if(bodies.size() != indice)
|
||||||
throw std::invalid_argument( "Bodies out of Sync" );
|
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)
|
if(bodies.size() != indice)
|
||||||
throw std::invalid_argument( "Bodies out of Sync" );
|
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();
|
btVector3 origin = bodies[i]->getCenterOfMassPosition();
|
||||||
glm::vec3 save(origin.getX(),origin.getY(),origin.getZ());
|
glm::vec3 save(origin.getX(),origin.getY(),origin.getZ());
|
||||||
return save;
|
return save;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 getRotation(int i)
|
glm::mat4 Physics::getRotation(int i)
|
||||||
{
|
{
|
||||||
btQuaternion quat = bodies[i]->getOrientation();
|
btQuaternion quat = bodies[i]->getOrientation();
|
||||||
|
|
||||||
glm::mat4 matrix = glm::rotate(
|
glm::mat4 matrix = glm::rotate(
|
||||||
matrix,
|
|
||||||
quat.getAngle(),
|
quat.getAngle(),
|
||||||
glm::vec3(quat.getAxis().getX(), quat.getAxis().getY(), quat.getAxis().getZ())
|
glm::vec3(quat.getAxis().getX(), quat.getAxis().getY(), quat.getAxis().getZ())
|
||||||
);
|
);
|
||||||
return matrix;
|
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);
|
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]));*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.h"
|
#include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.h"
|
||||||
#include "extern/bullet/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.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/btConstraintSolver.h"
|
||||||
#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY!
|
#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY!
|
||||||
@ -38,7 +39,8 @@ class Physics {
|
|||||||
void rollForward(glm::vec3 camPos, float strength);
|
void rollForward(glm::vec3 camPos, float strength);
|
||||||
glm::vec3 getPos(int i);
|
glm::vec3 getPos(int i);
|
||||||
glm::mat4 getRotation(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 addTerrain(int width, int length, float** heightData);
|
||||||
void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice);
|
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 addSphere(float rad, float x, float y, float z, float mass, unsigned indice);
|
||||||
@ -47,6 +49,7 @@ class Physics {
|
|||||||
btRigidBody* playerBody;
|
btRigidBody* playerBody;
|
||||||
btRigidBody* terrainBody;
|
btRigidBody* terrainBody;
|
||||||
std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
|
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.
|
btDynamicsWorld* world; //contains physical attributes of the world.
|
||||||
|
Loading…
Reference in New Issue
Block a user