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. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
#ifndef RAYCAST_SCENE_H
|
|
|
|
#define RAYCAST_SCENE_H
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#include <cmath>
|
|
|
|
#include "openglframework.h"
|
|
|
|
#include "reactphysics3d.h"
|
2015-07-29 16:15:20 +00:00
|
|
|
#include "SceneDemo.h"
|
2015-04-08 18:47:55 +00:00
|
|
|
#include "Sphere.h"
|
|
|
|
#include "Box.h"
|
|
|
|
#include "Cone.h"
|
|
|
|
#include "Cylinder.h"
|
|
|
|
#include "Capsule.h"
|
|
|
|
#include "Line.h"
|
|
|
|
#include "ConvexMesh.h"
|
2015-12-02 21:25:52 +00:00
|
|
|
#include "ConcaveMesh.h"
|
2016-02-08 21:27:11 +00:00
|
|
|
#include "HeightField.h"
|
2015-04-08 18:47:55 +00:00
|
|
|
#include "Dumbbell.h"
|
|
|
|
#include "VisualContactPoint.h"
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
namespace raycastscene {
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Constants
|
2015-07-29 16:15:20 +00:00
|
|
|
const float SCENE_RADIUS = 30.0f;
|
2015-04-08 18:47:55 +00:00
|
|
|
const openglframework::Vector3 BOX_SIZE(4, 2, 1);
|
|
|
|
const float SPHERE_RADIUS = 3.0f;
|
|
|
|
const float CONE_RADIUS = 3.0f;
|
|
|
|
const float CONE_HEIGHT = 5.0f;
|
|
|
|
const float CYLINDER_RADIUS = 3.0f;
|
|
|
|
const float CYLINDER_HEIGHT = 5.0f;
|
|
|
|
const float CAPSULE_RADIUS = 3.0f;
|
|
|
|
const float CAPSULE_HEIGHT = 5.0f;
|
|
|
|
const float DUMBBELL_HEIGHT = 5.0f;
|
|
|
|
const int NB_RAYS = 100;
|
|
|
|
const float RAY_LENGTH = 30.0f;
|
2016-02-08 21:27:11 +00:00
|
|
|
const int NB_BODIES = 9;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Raycast manager
|
|
|
|
class RaycastManager : public rp3d::RaycastCallback {
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// All the visual contact points
|
2015-08-11 16:32:45 +00:00
|
|
|
std::vector<ContactPoint> mHitPoints;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// All the normals at hit points
|
|
|
|
std::vector<Line*> mNormals;
|
|
|
|
|
2015-06-25 20:28:11 +00:00
|
|
|
/// Contact point mesh folder path
|
|
|
|
std::string mMeshFolderPath;
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
public:
|
|
|
|
|
2015-06-25 20:28:11 +00:00
|
|
|
RaycastManager(openglframework::Shader& shader,
|
|
|
|
const std::string& meshFolderPath)
|
2015-08-17 04:44:26 +00:00
|
|
|
: mMeshFolderPath(meshFolderPath) {
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual rp3d::decimal notifyRaycastHit(const rp3d::RaycastInfo& raycastInfo) override {
|
2015-04-08 18:47:55 +00:00
|
|
|
rp3d::Vector3 hitPos = raycastInfo.worldPoint;
|
|
|
|
openglframework::Vector3 position(hitPos.x, hitPos.y, hitPos.z);
|
2015-08-11 16:32:45 +00:00
|
|
|
mHitPoints.push_back(ContactPoint(position));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Create a line to display the normal at hit point
|
|
|
|
rp3d::Vector3 n = raycastInfo.worldNormal;
|
|
|
|
openglframework::Vector3 normal(n.x, n.y, n.z);
|
|
|
|
Line* normalLine = new Line(position, position + normal);
|
|
|
|
mNormals.push_back(normalLine);
|
|
|
|
|
|
|
|
return raycastInfo.hitFraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetPoints() {
|
|
|
|
|
|
|
|
mHitPoints.clear();
|
|
|
|
|
|
|
|
// Destroy all the normals
|
|
|
|
for (std::vector<Line*>::iterator it = mNormals.begin();
|
|
|
|
it != mNormals.end(); ++it) {
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
mNormals.clear();
|
|
|
|
}
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
std::vector<ContactPoint> getHitPoints() const {
|
|
|
|
return mHitPoints;
|
|
|
|
}
|
2015-04-08 18:47:55 +00:00
|
|
|
};
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
// Class RaycastScene
|
2015-07-29 16:15:20 +00:00
|
|
|
class RaycastScene : public SceneDemo {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
private :
|
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
2015-06-25 20:28:11 +00:00
|
|
|
/// Contact point mesh folder path
|
|
|
|
std::string mMeshFolderPath;
|
|
|
|
|
|
|
|
/// Raycast manager
|
|
|
|
RaycastManager mRaycastManager;
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
/// All the raycast lines
|
|
|
|
std::vector<Line*> mLines;
|
|
|
|
|
|
|
|
/// Current body index
|
|
|
|
int mCurrentBodyIndex;
|
|
|
|
|
|
|
|
/// True if the hit points normals are displayed
|
|
|
|
bool mAreNormalsDisplayed;
|
|
|
|
|
|
|
|
/// Raycast manager
|
|
|
|
|
|
|
|
/// All objects on the scene
|
|
|
|
Box* mBox;
|
|
|
|
Sphere* mSphere;
|
|
|
|
Cone* mCone;
|
|
|
|
Cylinder* mCylinder;
|
|
|
|
Capsule* mCapsule;
|
|
|
|
ConvexMesh* mConvexMesh;
|
|
|
|
Dumbbell* mDumbbell;
|
2015-12-02 21:25:52 +00:00
|
|
|
ConcaveMesh* mConcaveMesh;
|
2016-02-08 21:27:11 +00:00
|
|
|
HeightField* mHeightField;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Collision world used for the physics simulation
|
|
|
|
rp3d::CollisionWorld* mCollisionWorld;
|
|
|
|
|
2015-06-25 20:28:11 +00:00
|
|
|
/// All the points to render the lines
|
|
|
|
std::vector<openglframework::Vector3> mLinePoints;
|
|
|
|
|
|
|
|
/// Vertex Buffer Object for the vertices data
|
|
|
|
openglframework::VertexBufferObject mVBOVertices;
|
|
|
|
|
|
|
|
/// Vertex Array Object for the vertex data
|
|
|
|
openglframework::VertexArrayObject mVAO;
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
/// Create the raycast lines
|
|
|
|
void createLines();
|
|
|
|
|
2015-06-25 20:28:11 +00:00
|
|
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
|
|
|
void createVBOAndVAO(openglframework::Shader& shader);
|
|
|
|
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Constructor
|
2015-04-15 21:11:27 +00:00
|
|
|
RaycastScene(const std::string& name);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Destructor
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual ~RaycastScene() override;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
/// Update the physics world (take a simulation step)
|
|
|
|
/// Can be called several times per frame
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual void updatePhysics() override;
|
2015-04-20 21:23:46 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
/// Take a step for the simulation
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual void update() override;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-07-29 16:15:20 +00:00
|
|
|
/// Render the scene in a single pass
|
|
|
|
virtual void renderSinglePass(openglframework::Shader& shader,
|
2016-07-11 06:33:24 +00:00
|
|
|
const openglframework::Matrix4& worldToCameraMatrix) override;
|
2015-04-15 21:11:27 +00:00
|
|
|
|
|
|
|
/// Reset the scene
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual void reset() override;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
/// Change the body to raycast
|
|
|
|
void changeBody();
|
|
|
|
|
|
|
|
/// Display or not the surface normals at hit points
|
|
|
|
void showHideNormals();
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
/// Called when a keyboard event occurs
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual bool keyboardEvent(int key, int scancode, int action, int mods) override;
|
2015-08-06 19:06:35 +00:00
|
|
|
|
|
|
|
/// Enabled/Disable the shadow mapping
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual void setIsShadowMappingEnabled(bool isShadowMappingEnabled) override;
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
/// Display/Hide the contact points
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual void setIsContactPointsDisplayed(bool display) override;
|
2015-08-11 16:32:45 +00:00
|
|
|
|
|
|
|
/// Return all the contact points of the scene
|
2016-07-11 06:33:24 +00:00
|
|
|
virtual std::vector<ContactPoint> getContactPoints() const override;
|
2015-04-08 18:47:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Display or not the surface normals at hit points
|
2015-04-15 21:11:27 +00:00
|
|
|
inline void RaycastScene::showHideNormals() {
|
2015-04-08 18:47:55 +00:00
|
|
|
mAreNormalsDisplayed = !mAreNormalsDisplayed;
|
|
|
|
}
|
|
|
|
|
2015-08-06 19:06:35 +00:00
|
|
|
// Enabled/Disable the shadow mapping
|
|
|
|
inline void RaycastScene::setIsShadowMappingEnabled(bool isShadowMappingEnabled) {
|
|
|
|
SceneDemo::setIsShadowMappingEnabled(false);
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:32:45 +00:00
|
|
|
// Display/Hide the contact points
|
|
|
|
inline void RaycastScene::setIsContactPointsDisplayed(bool display) {
|
|
|
|
SceneDemo::setIsContactPointsDisplayed(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return all the contact points of the scene
|
|
|
|
inline std::vector<ContactPoint> RaycastScene::getContactPoints() const {
|
|
|
|
return mRaycastManager.getHitPoints();
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
}
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
#endif
|