2015-04-08 18:47:55 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
|
|
|
* Copyright (c) 2010-2015 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 "TestbedApplication.h"
|
|
|
|
#include "openglframework.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <sstream>
|
2015-04-15 21:11:27 +00:00
|
|
|
#include "cubes/CubesScene.h"
|
|
|
|
#include "joints/JointsScene.h"
|
|
|
|
#include "collisionshapes/CollisionShapesScene.h"
|
2016-01-28 22:06:17 +00:00
|
|
|
#include "heightfield/HeightFieldScene.h"
|
2015-04-15 21:11:27 +00:00
|
|
|
#include "raycast/RaycastScene.h"
|
2015-10-26 17:15:56 +00:00
|
|
|
#include "concavemesh/ConcaveMeshScene.h"
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
using namespace openglframework;
|
2015-04-15 21:11:27 +00:00
|
|
|
using namespace jointsscene;
|
|
|
|
using namespace cubesscene;
|
|
|
|
using namespace raycastscene;
|
|
|
|
using namespace collisionshapesscene;
|
2015-10-26 17:15:56 +00:00
|
|
|
using namespace trianglemeshscene;
|
2016-01-28 22:06:17 +00:00
|
|
|
using namespace heightfieldscene;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Initialization of static variables
|
|
|
|
const float TestbedApplication::SCROLL_SENSITIVITY = 0.02f;
|
|
|
|
|
|
|
|
// Constructor
|
2016-02-22 17:44:29 +00:00
|
|
|
TestbedApplication::TestbedApplication(bool isFullscreen)
|
|
|
|
: Screen(Vector2i(1280, 760), "Testbed ReactPhysics3D", true, isFullscreen),
|
|
|
|
mIsInitialized(false), mFPS(0), mNbFrames(0), mPreviousTime(0),
|
2016-03-01 06:41:15 +00:00
|
|
|
mLastTimeComputedFPS(0), mFrameTime(0), mPhysicsTime(0), mGui(this) {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-04-22 18:54:17 +00:00
|
|
|
mCurrentScene = NULL;
|
2015-04-08 18:47:55 +00:00
|
|
|
mIsMultisamplingActive = true;
|
2015-05-24 15:23:55 +00:00
|
|
|
mWidth = 1280;
|
|
|
|
mHeight = 720;
|
2015-06-30 17:50:17 +00:00
|
|
|
mSinglePhysicsStepEnabled = false;
|
|
|
|
mSinglePhysicsStepDone = false;
|
2015-07-11 16:35:58 +00:00
|
|
|
mWindowToFramebufferRatio = Vector2(1, 1);
|
2015-08-06 19:06:35 +00:00
|
|
|
mIsShadowMappingEnabled = true;
|
2016-03-01 06:41:15 +00:00
|
|
|
mIsVSyncEnabled = false;
|
2015-08-11 16:32:45 +00:00
|
|
|
mIsContactPointsDisplayed = false;
|
2016-02-22 17:44:29 +00:00
|
|
|
|
|
|
|
init();
|
2016-03-01 06:41:15 +00:00
|
|
|
|
|
|
|
resizeEvent(Vector2i(0, 0));
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
TestbedApplication::~TestbedApplication() {
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
// Destroy all the scenes
|
|
|
|
destroyScenes();
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the viewer
|
|
|
|
void TestbedApplication::init() {
|
|
|
|
|
2016-02-22 17:44:29 +00:00
|
|
|
// Create all the scenes
|
|
|
|
createScenes();
|
2015-04-28 21:47:29 +00:00
|
|
|
|
2016-02-28 20:39:39 +00:00
|
|
|
// Initialize the GUI
|
|
|
|
mGui.init();
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
mTimer.start();
|
2016-02-22 17:44:29 +00:00
|
|
|
|
|
|
|
mIsInitialized = true;
|
2015-04-15 21:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the scenes
|
|
|
|
void TestbedApplication::createScenes() {
|
|
|
|
|
|
|
|
// Cubes scene
|
|
|
|
CubesScene* cubeScene = new CubesScene("Cubes");
|
|
|
|
mScenes.push_back(cubeScene);
|
|
|
|
|
|
|
|
// Joints scene
|
|
|
|
JointsScene* jointsScene = new JointsScene("Joints");
|
|
|
|
mScenes.push_back(jointsScene);
|
|
|
|
|
|
|
|
// Collision shapes scene
|
|
|
|
CollisionShapesScene* collisionShapesScene = new CollisionShapesScene("Collision Shapes");
|
|
|
|
mScenes.push_back(collisionShapesScene);
|
|
|
|
|
2016-01-28 22:06:17 +00:00
|
|
|
// Heightfield shape scene
|
|
|
|
HeightFieldScene* heightFieldScene = new HeightFieldScene("Heightfield");
|
|
|
|
mScenes.push_back(heightFieldScene);
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
// Raycast scene
|
|
|
|
RaycastScene* raycastScene = new RaycastScene("Raycast");
|
|
|
|
mScenes.push_back(raycastScene);
|
|
|
|
|
2015-10-26 17:15:56 +00:00
|
|
|
// Raycast scene
|
|
|
|
ConcaveMeshScene* concaveMeshScene = new ConcaveMeshScene("Concave Mesh");
|
|
|
|
mScenes.push_back(concaveMeshScene);
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
assert(mScenes.size() > 0);
|
2015-07-11 22:05:49 +00:00
|
|
|
mCurrentScene = mScenes[0];
|
2015-07-11 16:35:58 +00:00
|
|
|
|
|
|
|
// Get the engine settings from the scene
|
|
|
|
mEngineSettings = mCurrentScene->getEngineSettings();
|
|
|
|
mEngineSettings.timeStep = DEFAULT_TIMESTEP;
|
2015-04-15 21:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all the scenes
|
|
|
|
void TestbedApplication::destroyScenes() {
|
|
|
|
|
|
|
|
for (int i=0; i<mScenes.size(); i++) {
|
|
|
|
delete mScenes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentScene = NULL;
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:50:17 +00:00
|
|
|
void TestbedApplication::updateSinglePhysicsStep() {
|
|
|
|
|
|
|
|
assert(!mTimer.isRunning());
|
|
|
|
|
|
|
|
mCurrentScene->updatePhysics();
|
|
|
|
}
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
// Update the physics of the current scene
|
|
|
|
void TestbedApplication::updatePhysics() {
|
|
|
|
|
|
|
|
// Set the engine settings
|
|
|
|
mEngineSettings.elapsedTime = mTimer.getPhysicsTime();
|
|
|
|
mCurrentScene->setEngineSettings(mEngineSettings);
|
|
|
|
|
|
|
|
if (mTimer.isRunning()) {
|
|
|
|
|
|
|
|
// Compute the time since the last update() call and update the timer
|
|
|
|
mTimer.update();
|
|
|
|
|
|
|
|
// While the time accumulator is not empty
|
2015-04-22 18:54:17 +00:00
|
|
|
while(mTimer.isPossibleToTakeStep(mEngineSettings.timeStep)) {
|
2015-04-20 21:23:46 +00:00
|
|
|
|
|
|
|
// Take a physics simulation step
|
|
|
|
mCurrentScene->updatePhysics();
|
|
|
|
|
|
|
|
// Update the timer
|
2015-04-22 18:54:17 +00:00
|
|
|
mTimer.nextStep(mEngineSettings.timeStep);
|
2015-04-20 21:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
void TestbedApplication::update() {
|
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
double currentTime = glfwGetTime();
|
|
|
|
|
2015-04-22 18:54:17 +00:00
|
|
|
// Update the physics
|
2015-06-30 17:50:17 +00:00
|
|
|
if (mSinglePhysicsStepEnabled && !mSinglePhysicsStepDone) {
|
|
|
|
updateSinglePhysicsStep();
|
|
|
|
mSinglePhysicsStepDone = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
updatePhysics();
|
|
|
|
}
|
2015-04-22 18:54:17 +00:00
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
// Compute the physics update time
|
2016-03-01 06:41:15 +00:00
|
|
|
mPhysicsTime = glfwGetTime() - currentTime;
|
2015-08-06 19:06:35 +00:00
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
// Compute the interpolation factor
|
|
|
|
float factor = mTimer.computeInterpolationFactor(mEngineSettings.timeStep);
|
|
|
|
assert(factor >= 0.0f && factor <= 1.0f);
|
|
|
|
|
|
|
|
// Notify the scene about the interpolation factor
|
|
|
|
mCurrentScene->setInterpolationFactor(factor);
|
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
// Enable/Disable shadow mapping
|
|
|
|
mCurrentScene->setIsShadowMappingEnabled(mIsShadowMappingEnabled);
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
// Display/Hide contact points
|
|
|
|
mCurrentScene->setIsContactPointsDisplayed(mIsContactPointsDisplayed);
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
// Update the scene
|
2015-04-08 18:47:55 +00:00
|
|
|
mCurrentScene->update();
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:44:29 +00:00
|
|
|
void TestbedApplication::drawContents() {
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
int bufferWidth, bufferHeight;
|
|
|
|
glfwMakeContextCurrent(mGLFWWindow);
|
|
|
|
glfwGetFramebufferSize(mGLFWWindow, &bufferWidth, &bufferHeight);
|
|
|
|
|
|
|
|
// Set the viewport of the scene
|
|
|
|
mCurrentScene->setViewport(0, 0, bufferWidth, bufferHeight);
|
|
|
|
|
|
|
|
// Render the scene
|
|
|
|
mCurrentScene->render();
|
|
|
|
|
|
|
|
// Check the OpenGL errors
|
|
|
|
checkOpenGLErrors();
|
2016-03-01 06:41:15 +00:00
|
|
|
|
2016-03-01 22:19:45 +00:00
|
|
|
mGui.update();
|
|
|
|
|
2016-03-01 06:41:15 +00:00
|
|
|
// Compute the current framerate
|
|
|
|
computeFPS();
|
2016-02-22 17:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Window resize event handler
|
|
|
|
bool TestbedApplication::resizeEvent(const Vector2i& size) {
|
|
|
|
|
|
|
|
if (!mIsInitialized) return false;
|
|
|
|
|
|
|
|
// Get the framebuffer dimension
|
|
|
|
int width, height;
|
|
|
|
glfwGetFramebufferSize(mGLFWWindow, &width, &height);
|
|
|
|
|
|
|
|
// Resize the camera viewport
|
|
|
|
mCurrentScene->reshape(width, height);
|
|
|
|
|
|
|
|
// Update the window size of the scene
|
|
|
|
int windowWidth, windowHeight;
|
|
|
|
glfwGetWindowSize(mGLFWWindow, &windowWidth, &windowHeight);
|
|
|
|
mCurrentScene->setWindowDimension(windowWidth, windowHeight);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-01 21:40:44 +00:00
|
|
|
// Change the current scene
|
|
|
|
void TestbedApplication::switchScene(Scene* newScene) {
|
|
|
|
|
|
|
|
if (newScene == mCurrentScene) return;
|
|
|
|
|
|
|
|
mCurrentScene = newScene;
|
|
|
|
|
2015-07-11 16:35:58 +00:00
|
|
|
// Get the engine settings of the scene
|
|
|
|
float currentTimeStep = mEngineSettings.timeStep;
|
|
|
|
mEngineSettings = mCurrentScene->getEngineSettings();
|
|
|
|
mEngineSettings.timeStep = currentTimeStep;
|
|
|
|
|
2015-06-01 21:40:44 +00:00
|
|
|
// Reset the scene
|
|
|
|
mCurrentScene->reset();
|
2016-03-01 06:41:15 +00:00
|
|
|
|
|
|
|
resizeEvent(Vector2i(0, 0));
|
2015-06-01 21:40:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Check the OpenGL errors
|
2016-02-22 17:44:29 +00:00
|
|
|
void TestbedApplication::checkOpenGLErrorsInternal(const char* file, int line) {
|
2015-04-08 18:47:55 +00:00
|
|
|
GLenum glError;
|
|
|
|
|
|
|
|
// Get the OpenGL errors
|
|
|
|
glError = glGetError();
|
|
|
|
|
|
|
|
// While there are errors
|
|
|
|
while (glError != GL_NO_ERROR) {
|
|
|
|
|
2016-02-22 17:44:29 +00:00
|
|
|
std::string error;
|
|
|
|
|
|
|
|
switch(glError) {
|
|
|
|
case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
|
|
|
|
case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
|
|
|
|
case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
|
|
|
|
case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
|
|
|
|
case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
|
|
|
|
}
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2016-02-22 17:44:29 +00:00
|
|
|
std::cerr << "OpenGL Error #" << error.c_str() << " - " << file << ": " << line << std::endl;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Get the next error
|
|
|
|
glError = glGetError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compute the FPS
|
|
|
|
void TestbedApplication::computeFPS() {
|
|
|
|
|
2016-03-01 22:19:45 +00:00
|
|
|
// Note : By default the nanogui library is using glfwWaitEvents() to process
|
|
|
|
// events and sleep to target a framerate of 50 ms (using a thread
|
|
|
|
// sleeping). However, for games we prefer to use glfwPollEvents()
|
|
|
|
// instead and remove the update. Therefore the file common.cpp of the
|
|
|
|
// nanogui library has been modified to have a faster framerate
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
mNbFrames++;
|
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
// Get the number of seconds since start
|
2015-04-08 18:47:55 +00:00
|
|
|
mCurrentTime = glfwGetTime();
|
|
|
|
|
|
|
|
// Calculate time passed
|
2016-03-01 06:41:15 +00:00
|
|
|
mFrameTime = mCurrentTime - mPreviousTime;
|
|
|
|
double timeInterval = (mCurrentTime - mLastTimeComputedFPS) * 1000.0;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Update the FPS counter each second
|
2016-03-01 06:41:15 +00:00
|
|
|
if(timeInterval > 1000) {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// calculate the number of frames per second
|
|
|
|
mFPS = static_cast<double>(mNbFrames) / timeInterval;
|
2015-08-06 19:06:35 +00:00
|
|
|
mFPS *= 1000.0;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Reset frame count
|
|
|
|
mNbFrames = 0;
|
2016-03-01 06:41:15 +00:00
|
|
|
|
|
|
|
mLastTimeComputedFPS = mCurrentTime;
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
2016-03-01 06:41:15 +00:00
|
|
|
|
|
|
|
// Set time
|
|
|
|
mPreviousTime = mCurrentTime;
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 17:44:29 +00:00
|
|
|
bool TestbedApplication::keyboardEvent(int key, int scancode, int action, int modifiers) {
|
|
|
|
|
|
|
|
if (Screen::keyboardEvent(key, scancode, action, modifiers)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-28 20:39:39 +00:00
|
|
|
// Close application on escape key
|
|
|
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
|
|
|
glfwSetWindowShouldClose(mGLFWWindow, GL_TRUE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:44:29 +00:00
|
|
|
return mCurrentScene->keyboardEvent(key, scancode, action, modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a mouse button event (default implementation: propagate to children)
|
|
|
|
bool TestbedApplication::mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) {
|
|
|
|
|
|
|
|
if (Screen::mouseButtonEvent(p, button, down, modifiers)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the mouse cursor position
|
|
|
|
double x, y;
|
|
|
|
glfwGetCursorPos(mGLFWWindow, &x, &y);
|
|
|
|
|
|
|
|
return mCurrentScene->mouseButtonEvent(button, down, modifiers, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a mouse motion event (default implementation: propagate to children)
|
|
|
|
bool TestbedApplication::mouseMotionEvent(const Vector2i &p, const Vector2i &rel, int button, int modifiers) {
|
|
|
|
|
|
|
|
if (Screen::mouseMotionEvent(p, rel, button, modifiers)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int leftButtonState = glfwGetMouseButton(mGLFWWindow, GLFW_MOUSE_BUTTON_LEFT);
|
|
|
|
int rightButtonState = glfwGetMouseButton(mGLFWWindow, GLFW_MOUSE_BUTTON_RIGHT);
|
|
|
|
int middleButtonState = glfwGetMouseButton(mGLFWWindow, GLFW_MOUSE_BUTTON_MIDDLE);
|
|
|
|
int altKeyState = glfwGetKey(mGLFWWindow, GLFW_KEY_LEFT_ALT);
|
|
|
|
|
|
|
|
return mCurrentScene->mouseMotionEvent(p[0], p[1], leftButtonState, rightButtonState,
|
|
|
|
middleButtonState, altKeyState);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a mouse scroll event (default implementation: propagate to children)
|
|
|
|
bool TestbedApplication::scrollEvent(const Vector2i &p, const Vector2f &rel) {
|
|
|
|
|
|
|
|
if (Screen::scrollEvent(p, rel)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCurrentScene->scrollingEvent(rel[0], rel[1], SCROLL_SENSITIVITY);
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|