2015-04-08 18:47:55 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
2016-04-11 18:15:20 +00:00
|
|
|
* Copyright (c) 2010-2016 Daniel Chappuis *
|
2015-04-08 18:47:55 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
#ifndef SCENE_H
|
|
|
|
#define SCENE_H
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "openglframework.h"
|
2020-03-18 06:28:34 +00:00
|
|
|
#include <reactphysics3d/reactphysics3d.h>
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
// Structure ContactPoint
|
2019-06-25 21:26:06 +00:00
|
|
|
struct SceneContactPoint {
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
openglframework::Vector3 point;
|
2017-05-17 21:40:17 +00:00
|
|
|
openglframework::Vector3 normal;
|
|
|
|
openglframework::Color color;
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
/// Constructor
|
2019-06-25 21:26:06 +00:00
|
|
|
SceneContactPoint(const openglframework::Vector3& pointWorld, const openglframework::Vector3& normalWorld, const openglframework::Color colorPoint)
|
2017-05-17 21:40:17 +00:00
|
|
|
: point(pointWorld), normal(normalWorld), color(colorPoint) {
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
/// Structure EngineSettings
|
|
|
|
/// This structure contains several physics engine parameters
|
|
|
|
struct EngineSettings {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-11-24 06:03:02 +00:00
|
|
|
long double elapsedTime; // Elapsed time (in seconds)
|
2015-06-30 17:50:17 +00:00
|
|
|
float timeStep; // Current time step (in seconds)
|
2017-11-10 18:57:50 +00:00
|
|
|
unsigned int nbVelocitySolverIterations; // Nb of velocity solver iterations
|
|
|
|
unsigned int nbPositionSolverIterations; // Nb of position solver iterations
|
2015-06-30 17:50:17 +00:00
|
|
|
bool isSleepingEnabled; // True if sleeping technique is enabled
|
|
|
|
float timeBeforeSleep; // Time of inactivity before a body sleep
|
|
|
|
float sleepLinearVelocity; // Sleep linear velocity
|
|
|
|
float sleepAngularVelocity; // Sleep angular velocity
|
|
|
|
bool isGravityEnabled; // True if gravity is enabled
|
|
|
|
openglframework::Vector3 gravity; // Gravity vector
|
2015-04-22 18:54:17 +00:00
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
EngineSettings() : elapsedTime(0.0f), timeStep(0.0f) {
|
|
|
|
|
|
|
|
}
|
2017-11-02 21:58:41 +00:00
|
|
|
|
|
|
|
/// Return default engine settings
|
|
|
|
static EngineSettings defaultSettings() {
|
|
|
|
|
|
|
|
EngineSettings defaultSettings;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
rp3d::PhysicsWorld::WorldSettings worldSettings;
|
2017-11-02 21:58:41 +00:00
|
|
|
defaultSettings.timeStep = 1.0f / 60.0f;
|
2018-03-04 18:10:32 +00:00
|
|
|
defaultSettings.nbVelocitySolverIterations = worldSettings.defaultVelocitySolverNbIterations;
|
|
|
|
defaultSettings.nbPositionSolverIterations = worldSettings.defaultPositionSolverNbIterations;
|
|
|
|
defaultSettings.isSleepingEnabled = worldSettings.isSleepingEnabled;
|
|
|
|
defaultSettings.timeBeforeSleep = worldSettings.defaultTimeBeforeSleep;
|
|
|
|
defaultSettings.sleepLinearVelocity = worldSettings.defaultSleepLinearVelocity;
|
|
|
|
defaultSettings.sleepAngularVelocity = worldSettings.defaultSleepAngularVelocity;
|
2017-11-02 21:58:41 +00:00
|
|
|
defaultSettings.isGravityEnabled = true;
|
|
|
|
|
|
|
|
return defaultSettings;
|
|
|
|
}
|
2015-04-20 21:23:46 +00:00
|
|
|
};
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Class Scene
|
|
|
|
// Abstract class that represents a 3D scene.
|
2019-06-25 21:26:06 +00:00
|
|
|
class Scene : public rp3d::EventListener {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
|
|
|
/// Scene name
|
|
|
|
std::string mName;
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
/// Physics engine settings
|
2017-11-02 21:58:41 +00:00
|
|
|
EngineSettings& mEngineSettings;
|
2015-04-20 21:23:46 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
/// Camera
|
|
|
|
openglframework::Camera mCamera;
|
|
|
|
|
|
|
|
/// Center of the scene
|
|
|
|
openglframework::Vector3 mCenterScene;
|
|
|
|
|
|
|
|
/// Last mouse coordinates on the windows
|
|
|
|
double mLastMouseX, mLastMouseY;
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
/// Window dimension
|
|
|
|
int mWindowWidth, mWindowHeight;
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
/// Last point computed on a sphere (for camera rotation)
|
|
|
|
openglframework::Vector3 mLastPointOnSphere;
|
|
|
|
|
|
|
|
/// True if the last point computed on a sphere (for camera rotation) is valid
|
|
|
|
bool mIsLastPointOnSphereValid;
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
/// Interpolation factor for the bodies in the current frame
|
|
|
|
float mInterpolationFactor;
|
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
/// Viewport x,y, width and height values
|
|
|
|
int mViewportX, mViewportY, mViewportWidth, mViewportHeight;
|
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
/// True if the shadow mapping is enabled
|
|
|
|
bool mIsShadowMappingEnabled;
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
/// True if contact points are displayed
|
|
|
|
bool mIsContactPointsDisplayed;
|
|
|
|
|
2019-06-25 21:26:06 +00:00
|
|
|
/// True if the AABBs of the physics objects are displayed
|
2017-11-02 21:58:41 +00:00
|
|
|
bool mIsAABBsDisplayed;
|
|
|
|
|
2017-01-27 23:22:22 +00:00
|
|
|
/// True if we render shapes in wireframe mode
|
|
|
|
bool mIsWireframeEnabled;
|
|
|
|
|
2019-06-25 21:26:06 +00:00
|
|
|
/// Contact points
|
|
|
|
std::vector<SceneContactPoint> mContactPoints;
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Set the scene position (where the camera needs to look at)
|
|
|
|
void setScenePosition(const openglframework::Vector3& position, float sceneRadius);
|
|
|
|
|
|
|
|
/// Set the camera so that we can view the whole scene
|
|
|
|
void resetCameraToViewAll();
|
|
|
|
|
|
|
|
/// Map mouse coordinates to coordinates on the sphere
|
|
|
|
bool mapMouseCoordinatesToSphere(double xMouse, double yMouse,
|
|
|
|
openglframework::Vector3& spherePoint) const;
|
|
|
|
|
|
|
|
/// Zoom the camera
|
|
|
|
void zoom(float zoomDiff);
|
|
|
|
|
|
|
|
/// Translate the camera
|
|
|
|
void translate(int xMouse, int yMouse);
|
|
|
|
|
|
|
|
/// Rotate the camera
|
|
|
|
void rotate(int xMouse, int yMouse);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Constructor
|
2017-11-02 21:58:41 +00:00
|
|
|
Scene(const std::string& name, EngineSettings& engineSettings, bool isShadowMappingEnabled = false);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Destructor
|
2019-06-25 21:26:06 +00:00
|
|
|
virtual ~Scene() override;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Reshape the view
|
|
|
|
virtual void reshape(int width, int height);
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
/// Update the physics world (take a simulation step)
|
|
|
|
/// Can be called several times per frame
|
|
|
|
virtual void updatePhysics()=0;
|
|
|
|
|
|
|
|
/// Update the scene
|
2015-04-08 18:47:55 +00:00
|
|
|
virtual void update()=0;
|
|
|
|
|
|
|
|
/// Render the scene
|
|
|
|
virtual void render()=0;
|
|
|
|
|
|
|
|
/// Reset the scene
|
2019-06-25 21:26:06 +00:00
|
|
|
virtual void reset();
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Called when a keyboard event occurs
|
2016-02-22 17:44:29 +00:00
|
|
|
virtual bool keyboardEvent(int key, int scancode, int action, int mods);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Called when a mouse button event occurs
|
2016-02-22 17:44:29 +00:00
|
|
|
virtual bool mouseButtonEvent(int button, bool down, int mods,
|
2015-04-15 21:11:27 +00:00
|
|
|
double mousePosX, double mousePosY);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Called when a mouse motion event occurs
|
2016-02-22 17:44:29 +00:00
|
|
|
virtual bool mouseMotionEvent(double xMouse, double yMouse, int leftButtonState,
|
2015-04-15 21:11:27 +00:00
|
|
|
int rightButtonState, int middleButtonState, int altKeyState);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Called when a scrolling event occurs
|
2016-02-22 17:44:29 +00:00
|
|
|
virtual bool scrollingEvent(float xAxis, float yAxis, float scrollSensitivy);
|
2015-04-15 21:11:27 +00:00
|
|
|
|
|
|
|
/// Set the window dimension
|
|
|
|
void setWindowDimension(int width, int height);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
/// Set the viewport to render the scene
|
|
|
|
void setViewport(int x, int y, int width, int height);
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
/// Return a reference to the camera
|
|
|
|
const openglframework::Camera& getCamera() const;
|
2015-04-20 21:23:46 +00:00
|
|
|
|
|
|
|
/// Set the interpolation factor
|
|
|
|
void setInterpolationFactor(float interpolationFactor);
|
2015-06-01 21:40:44 +00:00
|
|
|
|
|
|
|
/// Return the name of the scene
|
|
|
|
std::string getName() const;
|
2015-08-06 19:06:35 +00:00
|
|
|
|
|
|
|
/// Return true if the shadow mapping is enabled
|
|
|
|
bool getIsShadowMappingEnabled() const;
|
|
|
|
|
|
|
|
/// Enabled/Disable the shadow mapping
|
|
|
|
void virtual setIsShadowMappingEnabled(bool isShadowMappingEnabled);
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
/// Display/Hide the contact points
|
|
|
|
void virtual setIsContactPointsDisplayed(bool display);
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
/// Display/Hide the AABBs
|
|
|
|
void setIsAABBsDisplayed(bool display);
|
|
|
|
|
2017-01-27 23:22:22 +00:00
|
|
|
/// Return true if wireframe rendering is enabled
|
|
|
|
bool getIsWireframeEnabled() const;
|
|
|
|
|
|
|
|
/// Enable/disbale wireframe rendering
|
|
|
|
void setIsWireframeEnabled(bool isEnabled);
|
|
|
|
|
2018-03-15 22:11:26 +00:00
|
|
|
/// Update the engine settings
|
|
|
|
virtual void updateEngineSettings() = 0;
|
2019-06-25 21:26:06 +00:00
|
|
|
|
|
|
|
/// Called when some contacts occur
|
|
|
|
virtual void onContact(const rp3d::CollisionCallback::CallbackData& callbackData) override;
|
2020-04-18 18:43:13 +00:00
|
|
|
|
|
|
|
virtual void onTrigger(const rp3d::OverlapCallback::CallbackData& callbackData) override;
|
2015-04-08 18:47:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Called when a keyboard event occurs
|
2016-02-22 17:44:29 +00:00
|
|
|
inline bool Scene::keyboardEvent(int key, int scancode, int action, int mods) {
|
|
|
|
return false;
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reshape the view
|
|
|
|
inline void Scene::reshape(int width, int height) {
|
|
|
|
mCamera.setDimensions(width, height);
|
|
|
|
}
|
|
|
|
|
2019-06-25 21:26:06 +00:00
|
|
|
// Reset the scene
|
|
|
|
inline void Scene::reset() {
|
|
|
|
mContactPoints.clear();
|
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Return a reference to the camera
|
|
|
|
inline const openglframework::Camera& Scene::getCamera() const {
|
|
|
|
return mCamera;
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
// Set the window dimension
|
|
|
|
inline void Scene::setWindowDimension(int width, int height) {
|
|
|
|
mWindowWidth = width;
|
|
|
|
mWindowHeight = height;
|
|
|
|
}
|
|
|
|
|
2015-07-25 00:26:26 +00:00
|
|
|
// Set the viewport to render the scene
|
|
|
|
inline void Scene::setViewport(int x, int y, int width, int height) {
|
|
|
|
mViewportX = x;
|
|
|
|
mViewportY = y;
|
|
|
|
mViewportWidth = width;
|
|
|
|
mViewportHeight = height;
|
|
|
|
}
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
// Set the interpolation factor
|
|
|
|
inline void Scene::setInterpolationFactor(float interpolationFactor) {
|
|
|
|
mInterpolationFactor = interpolationFactor;
|
|
|
|
}
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-06-01 21:40:44 +00:00
|
|
|
// Return the name of the scene
|
|
|
|
inline std::string Scene::getName() const {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
// Return true if the shadow mapping is enabled
|
|
|
|
inline bool Scene::getIsShadowMappingEnabled() const {
|
|
|
|
return mIsShadowMappingEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enabled/Disable the shadow mapping
|
|
|
|
inline void Scene::setIsShadowMappingEnabled(bool isShadowMappingEnabled) {
|
|
|
|
mIsShadowMappingEnabled = isShadowMappingEnabled;
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
// Display/Hide the contact points
|
|
|
|
inline void Scene::setIsContactPointsDisplayed(bool display) {
|
|
|
|
mIsContactPointsDisplayed = display;
|
|
|
|
}
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
// Display/Hide the AABBs
|
|
|
|
inline void Scene::setIsAABBsDisplayed(bool display) {
|
|
|
|
mIsAABBsDisplayed = display;
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:22:22 +00:00
|
|
|
// Return true if wireframe rendering is enabled
|
|
|
|
inline bool Scene::getIsWireframeEnabled() const {
|
|
|
|
return mIsWireframeEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable/disbale wireframe rendering
|
|
|
|
inline void Scene::setIsWireframeEnabled(bool isEnabled) {
|
|
|
|
mIsWireframeEnabled = isEnabled;
|
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
#endif
|