2013-04-14 19:32:40 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010-2013 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
|
|
* use of this software. *
|
|
|
|
* *
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
|
|
* freely, subject to the following restrictions: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* that you wrote the original software. If you use this software in a *
|
|
|
|
* product, an acknowledgment in the product documentation would be *
|
|
|
|
* appreciated but is not required. *
|
|
|
|
* *
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
|
|
* misrepresented as being the original software. *
|
|
|
|
* *
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "Scene.h"
|
|
|
|
|
|
|
|
// Namespaces
|
|
|
|
using namespace openglframework;
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
|
|
|
|
mPhongShader("shaders/phong.vert",
|
2013-04-18 20:22:45 +00:00
|
|
|
"shaders/phong.frag"), mIsRunning(false) {
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Move the light 0
|
|
|
|
mLight0.translateWorld(Vector3(7, 15, 15));
|
|
|
|
|
|
|
|
// Compute the radius and the center of the scene
|
2013-04-18 20:22:45 +00:00
|
|
|
float radiusScene = 10.0f;
|
2013-04-14 19:32:40 +00:00
|
|
|
openglframework::Vector3 center(0, 5, 0);
|
|
|
|
|
|
|
|
// Set the center of the scene
|
2013-04-18 20:22:45 +00:00
|
|
|
mViewer->setScenePosition(center, radiusScene);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Gravity vector in the dynamics world
|
2013-08-06 18:51:56 +00:00
|
|
|
rp3d::Vector3 gravity(0, rp3d::decimal(-9.81), 0);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Time step for the physics simulation
|
2013-04-18 20:22:45 +00:00
|
|
|
rp3d::decimal timeStep = 1.0f / 60.0f;
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Create the dynamics world for the physics simulation
|
|
|
|
mDynamicsWorld = new rp3d::DynamicsWorld(gravity, timeStep);
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
// Set the number of iterations of the constraint solver
|
2013-05-08 21:33:04 +00:00
|
|
|
mDynamicsWorld->setNbIterationsVelocitySolver(15);
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
float radius = 2.0f;
|
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Create all the cubes of the scene
|
2013-07-09 22:17:14 +00:00
|
|
|
for (int i=0; i<NB_SPHERES; i++) {
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Position of the cubes
|
2013-04-18 20:22:45 +00:00
|
|
|
float angle = i * 30.0f;
|
|
|
|
openglframework::Vector3 position(radius * cos(angle),
|
2013-11-21 22:24:11 +00:00
|
|
|
10 + i * (BOX_SIZE.y + 0.3f),
|
2013-09-10 19:53:34 +00:00
|
|
|
0);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Create a cube and a corresponding rigid in the dynamics world
|
2013-09-10 19:53:34 +00:00
|
|
|
Box* cube = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-07-09 22:17:14 +00:00
|
|
|
// Change the material properties of the rigid body
|
|
|
|
rp3d::Material& material = cube->getRigidBody()->getMaterial();
|
2013-08-06 18:51:56 +00:00
|
|
|
material.setBounciness(rp3d::decimal(0.4));
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Add the box the list of box in the scene
|
|
|
|
mBoxes.push_back(cube);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the floor
|
|
|
|
openglframework::Vector3 floorPosition(0, 0, 0);
|
|
|
|
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
|
|
|
|
2013-11-21 22:24:11 +00:00
|
|
|
// The floor must be a static rigid body
|
|
|
|
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-07-09 22:17:14 +00:00
|
|
|
// Change the material properties of the floor rigid body
|
|
|
|
rp3d::Material& material = mFloor->getRigidBody()->getMaterial();
|
2013-08-06 18:51:56 +00:00
|
|
|
material.setBounciness(rp3d::decimal(0.3));
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Start the simulation
|
2013-04-18 20:22:45 +00:00
|
|
|
startSimulation();
|
2013-04-14 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
Scene::~Scene() {
|
|
|
|
|
|
|
|
// Stop the physics simulation
|
2013-04-18 20:22:45 +00:00
|
|
|
stopSimulation();
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Destroy the shader
|
|
|
|
mPhongShader.destroy();
|
|
|
|
|
|
|
|
// Destroy all the cubes of the scene
|
|
|
|
for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) {
|
|
|
|
|
|
|
|
// Destroy the corresponding rigid body from the dynamics world
|
|
|
|
mDynamicsWorld->destroyRigidBody((*it)->getRigidBody());
|
|
|
|
|
|
|
|
// Destroy the cube
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the rigid body of the floor
|
|
|
|
mDynamicsWorld->destroyRigidBody(mFloor->getRigidBody());
|
|
|
|
|
|
|
|
// Destroy the floor
|
|
|
|
delete mFloor;
|
|
|
|
|
|
|
|
// Destroy the dynamics world
|
|
|
|
delete mDynamicsWorld;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take a step for the simulation
|
|
|
|
void Scene::simulate() {
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
// If the physics simulation is running
|
|
|
|
if (mIsRunning) {
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
// Take a simulation step
|
|
|
|
mDynamicsWorld->update();
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
// Update the position and orientation of the boxes
|
|
|
|
for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) {
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
// Update the transform used for the rendering
|
|
|
|
(*it)->updateTransform();
|
|
|
|
}
|
|
|
|
|
|
|
|
mFloor->updateTransform();
|
|
|
|
|
2013-09-10 19:53:34 +00:00
|
|
|
// Set the color of the awake/sleeping bodies
|
|
|
|
for (uint i=0; i<mBoxes.size(); i++) {
|
|
|
|
if (mBoxes[i]->getRigidBody()->isSleeping()) {
|
|
|
|
mBoxes[i]->setColor(Color(1, 0, 0, 1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mBoxes[i]->setColor(Color(0, 1, 0, 1));
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 20:22:45 +00:00
|
|
|
}
|
2013-04-14 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the scene
|
|
|
|
void Scene::render() {
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2013-04-18 20:22:45 +00:00
|
|
|
glEnable(GL_CULL_FACE);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-07-02 21:14:08 +00:00
|
|
|
// Get the world-space to camera-space matrix
|
|
|
|
const Camera& camera = mViewer->getCamera();
|
|
|
|
const openglframework::Matrix4 worldToCameraMatrix = camera.getTransformMatrix().getInverse();
|
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Bind the shader
|
|
|
|
mPhongShader.bind();
|
|
|
|
|
|
|
|
// Set the variables of the shader
|
|
|
|
mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix());
|
2013-07-03 20:42:58 +00:00
|
|
|
mPhongShader.setVector3Uniform("light0PosCameraSpace",worldToCameraMatrix * mLight0.getOrigin());
|
2013-04-14 19:32:40 +00:00
|
|
|
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
|
2013-09-12 20:45:43 +00:00
|
|
|
const Color& diffCol = mLight0.getDiffuseColor();
|
|
|
|
const Color& specCol = mLight0.getSpecularColor();
|
2013-07-03 20:42:58 +00:00
|
|
|
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
|
|
|
|
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specCol.r, specCol.g, specCol.b));
|
2013-04-14 19:32:40 +00:00
|
|
|
mPhongShader.setFloatUniform("shininess", 60.0f);
|
|
|
|
|
|
|
|
// Render all the cubes of the scene
|
|
|
|
for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) {
|
2013-07-02 21:14:08 +00:00
|
|
|
(*it)->render(mPhongShader, worldToCameraMatrix);
|
2013-04-14 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the floor
|
2013-07-02 21:14:08 +00:00
|
|
|
mFloor->render(mPhongShader, worldToCameraMatrix);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Unbind the shader
|
|
|
|
mPhongShader.unbind();
|
|
|
|
}
|