Refactoring and adding colors to the testbed application
This commit is contained in:
parent
5d7cb3cfd5
commit
047f612c37
|
@ -74,6 +74,8 @@ SET(COMMON_SOURCES
|
||||||
common/Cylinder.cpp
|
common/Cylinder.cpp
|
||||||
common/Dumbbell.h
|
common/Dumbbell.h
|
||||||
common/Dumbbell.cpp
|
common/Dumbbell.cpp
|
||||||
|
common/PhysicsObject.h
|
||||||
|
common/PhysicsObject.cpp
|
||||||
common/VisualContactPoint.h
|
common/VisualContactPoint.h
|
||||||
common/VisualContactPoint.cpp
|
common/VisualContactPoint.cpp
|
||||||
)
|
)
|
||||||
|
|
|
@ -113,7 +113,7 @@ GLfloat Box::mCubeNormals[108] = {
|
||||||
// Constructor
|
// Constructor
|
||||||
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position,
|
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position,
|
||||||
reactphysics3d::CollisionWorld* world)
|
reactphysics3d::CollisionWorld* world)
|
||||||
: openglframework::Object3D(), mColor(0.01f, 0.62f, 0.39f, 1.0f) {
|
: openglframework::Object3D() {
|
||||||
|
|
||||||
// Initialize the size of the box
|
// Initialize the size of the box
|
||||||
mSize[0] = size.x * 0.5f;
|
mSize[0] = size.x * 0.5f;
|
||||||
|
@ -142,10 +142,10 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p
|
||||||
mPreviousTransform = transform;
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Create a rigid body in the dynamics world
|
// Create a rigid body in the dynamics world
|
||||||
mRigidBody = world->createCollisionBody(transform);
|
mBody = world->createCollisionBody(transform);
|
||||||
|
|
||||||
// Add the collision shape to the body
|
// Add the collision shape to the body
|
||||||
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
mBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
||||||
|
|
||||||
// If the Vertex Buffer object has not been created yet
|
// If the Vertex Buffer object has not been created yet
|
||||||
if (totalNbBoxes == 0) {
|
if (totalNbBoxes == 0) {
|
||||||
|
@ -162,7 +162,7 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p
|
||||||
// Constructor
|
// Constructor
|
||||||
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3& position,
|
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3& position,
|
||||||
float mass, reactphysics3d::DynamicsWorld* world)
|
float mass, reactphysics3d::DynamicsWorld* world)
|
||||||
: openglframework::Object3D(), mColor(0.01f, 0.62f, 0.39f, 1.0f) {
|
: openglframework::Object3D() {
|
||||||
|
|
||||||
// Initialize the size of the box
|
// Initialize the size of the box
|
||||||
mSize[0] = size.x * 0.5f;
|
mSize[0] = size.x * 0.5f;
|
||||||
|
@ -194,7 +194,7 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3& p
|
||||||
// Add the collision shape to the body
|
// Add the collision shape to the body
|
||||||
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
||||||
|
|
||||||
mRigidBody = body;
|
mBody = body;
|
||||||
|
|
||||||
// If the Vertex Buffer object has not been created yet
|
// If the Vertex Buffer object has not been created yet
|
||||||
if (totalNbBoxes == 0) {
|
if (totalNbBoxes == 0) {
|
||||||
|
@ -246,7 +246,8 @@ void Box::render(openglframework::Shader& shader,
|
||||||
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
|
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
|
||||||
|
|
||||||
// Set the vertex color
|
// Set the vertex color
|
||||||
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
|
openglframework::Color currentColor = mBody->isSleeping() ? mSleepingColor : mColor;
|
||||||
|
openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
|
||||||
shader.setVector4Uniform("vertexColor", color, false);
|
shader.setVector4Uniform("vertexColor", color, false);
|
||||||
|
|
||||||
// Get the location of shader attribute variables
|
// Get the location of shader attribute variables
|
||||||
|
@ -277,29 +278,7 @@ void Box::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the box
|
|
||||||
void Box::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mRigidBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the box
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct box dimensions
|
|
||||||
mTransformMatrix = newMatrix * mScalingMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render to box with OpenGL.
|
// Create the Vertex Buffer Objects used to render to box with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices) to render all the boxes
|
/// We create two VBOs (one for vertices and one for indices) to render all the boxes
|
||||||
|
@ -336,12 +315,12 @@ void Box::createVBOAndVAO() {
|
||||||
void Box::resetTransform(const rp3d::Transform& transform) {
|
void Box::resetTransform(const rp3d::Transform& transform) {
|
||||||
|
|
||||||
// Reset the transform
|
// Reset the transform
|
||||||
mRigidBody->setTransform(transform);
|
mBody->setTransform(transform);
|
||||||
|
|
||||||
mRigidBody->setIsSleeping(false);
|
mBody->setIsSleeping(false);
|
||||||
|
|
||||||
// Reset the velocity of the rigid body
|
// Reset the velocity of the rigid body
|
||||||
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
if (rigidBody != NULL) {
|
if (rigidBody != NULL) {
|
||||||
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
|
|
|
@ -29,9 +29,10 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class Box
|
// Class Box
|
||||||
class Box : public openglframework::Object3D {
|
class Box : public openglframework::Object3D, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -40,12 +41,6 @@ class Box : public openglframework::Object3D {
|
||||||
/// Size of each side of the box
|
/// Size of each side of the box
|
||||||
float mSize[3];
|
float mSize[3];
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the box
|
|
||||||
rp3d::CollisionBody* mRigidBody;
|
|
||||||
|
|
||||||
/// Previous transform of the body (for interpolation)
|
|
||||||
rp3d::Transform mPreviousTransform;
|
|
||||||
|
|
||||||
/// Scaling matrix (applied to a cube to obtain the correct box dimensions)
|
/// Scaling matrix (applied to a cube to obtain the correct box dimensions)
|
||||||
openglframework::Matrix4 mScalingMatrix;
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
@ -67,9 +62,6 @@ class Box : public openglframework::Object3D {
|
||||||
/// Total number of boxes created
|
/// Total number of boxes created
|
||||||
static int totalNbBoxes;
|
static int totalNbBoxes;
|
||||||
|
|
||||||
/// Main color of the box
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
/// Create a the VAO and VBOs to render to box with OpenGL
|
/// Create a the VAO and VBOs to render to box with OpenGL
|
||||||
|
@ -90,38 +82,20 @@ class Box : public openglframework::Object3D {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Box();
|
~Box();
|
||||||
|
|
||||||
/// Return a pointer to the collision body of the box
|
|
||||||
reactphysics3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the rigid body of the box
|
|
||||||
reactphysics3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the box
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the cube at the correct position and with the correct orientation
|
/// Render the cube at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader, const openglframework::Matrix4& worldToCameraMatrix);
|
void render(openglframework::Shader& shader, const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the color of the box
|
|
||||||
void setColor(const openglframework::Color& color);
|
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the collision body of the box
|
// Update the transform matrix of the object
|
||||||
inline rp3d::CollisionBody* Box::getCollisionBody() {
|
inline void Box::updateTransform(float interpolationFactor) {
|
||||||
return mRigidBody;
|
mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the box
|
|
||||||
inline rp3d::RigidBody* Box::getRigidBody() {
|
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the color of the box
|
|
||||||
inline void Box::setColor(const openglframework::Color& color) {
|
|
||||||
mColor = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -67,10 +67,10 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
|
||||||
mPreviousTransform = transform;
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Create a rigid body corresponding in the dynamics world
|
// Create a rigid body corresponding in the dynamics world
|
||||||
mRigidBody = world->createCollisionBody(transform);
|
mBody = world->createCollisionBody(transform);
|
||||||
|
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
mBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -86,8 +86,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
|
||||||
Capsule::Capsule(float radius, float height, const openglframework::Vector3& position,
|
Capsule::Capsule(float radius, float height, const openglframework::Vector3& position,
|
||||||
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
|
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mHeight(height),
|
: openglframework::Mesh(), mRadius(radius), mHeight(height) {
|
||||||
mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this);
|
||||||
|
@ -120,7 +119,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
||||||
|
|
||||||
mRigidBody = body;
|
mBody = body;
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -170,7 +169,8 @@ void Capsule::render(openglframework::Shader& shader,
|
||||||
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
|
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
|
||||||
|
|
||||||
// Set the vertex color
|
// Set the vertex color
|
||||||
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
|
openglframework::Color currentColor = mBody->isSleeping() ? mSleepingColor : mColor;
|
||||||
|
openglframework::Vector4 color(currentColor.r, currentColor.g, currentColor.b, currentColor.a);
|
||||||
shader.setVector4Uniform("vertexColor", color, false);
|
shader.setVector4Uniform("vertexColor", color, false);
|
||||||
|
|
||||||
// Bind the VAO
|
// Bind the VAO
|
||||||
|
@ -183,10 +183,12 @@ void Capsule::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
for (unsigned int i=0; i<getNbParts(); i++) {
|
for (unsigned int i=0; i<getNbParts(); i++) {
|
||||||
|
@ -196,6 +198,7 @@ void Capsule::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
@ -205,30 +208,6 @@ void Capsule::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the sphere
|
|
||||||
void Capsule::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mRigidBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct sphere dimensions
|
|
||||||
mTransformMatrix = newMatrix * mScalingMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices)
|
/// We create two VBOs (one for vertices and one for indices)
|
||||||
void Capsule::createVBOAndVAO() {
|
void Capsule::createVBOAndVAO() {
|
||||||
|
@ -289,12 +268,12 @@ void Capsule::createVBOAndVAO() {
|
||||||
void Capsule::resetTransform(const rp3d::Transform& transform) {
|
void Capsule::resetTransform(const rp3d::Transform& transform) {
|
||||||
|
|
||||||
// Reset the transform
|
// Reset the transform
|
||||||
mRigidBody->setTransform(transform);
|
mBody->setTransform(transform);
|
||||||
|
|
||||||
mRigidBody->setIsSleeping(false);
|
mBody->setIsSleeping(false);
|
||||||
|
|
||||||
// Reset the velocity of the rigid body
|
// Reset the velocity of the rigid body
|
||||||
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
if (rigidBody != NULL) {
|
if (rigidBody != NULL) {
|
||||||
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
|
|
|
@ -29,9 +29,10 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class Sphere
|
// Class Sphere
|
||||||
class Capsule : public openglframework::Mesh {
|
class Capsule : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -43,9 +44,6 @@ class Capsule : public openglframework::Mesh {
|
||||||
/// Height of the capsule
|
/// Height of the capsule
|
||||||
float mHeight;
|
float mHeight;
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the sphere
|
|
||||||
rp3d::CollisionBody* mRigidBody;
|
|
||||||
|
|
||||||
/// Scaling matrix (applied to a sphere to obtain the correct sphere dimensions)
|
/// Scaling matrix (applied to a sphere to obtain the correct sphere dimensions)
|
||||||
openglframework::Matrix4 mScalingMatrix;
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
@ -70,9 +68,6 @@ class Capsule : public openglframework::Mesh {
|
||||||
// Total number of capsules created
|
// Total number of capsules created
|
||||||
static int totalNbCapsules;
|
static int totalNbCapsules;
|
||||||
|
|
||||||
/// Color
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
|
@ -94,31 +89,20 @@ class Capsule : public openglframework::Mesh {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Capsule();
|
~Capsule();
|
||||||
|
|
||||||
/// Return a pointer to the collision body of the box
|
|
||||||
reactphysics3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the rigid body of the box
|
|
||||||
reactphysics3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the sphere
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the sphere at the correct position and with the correct orientation
|
/// Render the sphere at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader,
|
void render(openglframework::Shader& shader,
|
||||||
const openglframework::Matrix4& worldToCameraMatrix);
|
const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the collision body of the box
|
// Update the transform matrix of the object
|
||||||
inline rp3d::CollisionBody* Capsule::getCollisionBody() {
|
inline void Capsule::updateTransform(float interpolationFactor) {
|
||||||
return mRigidBody;
|
mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the box
|
|
||||||
inline rp3d::RigidBody* Capsule::getRigidBody() {
|
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -37,8 +37,7 @@ int Cone::totalNbCones = 0;
|
||||||
Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
||||||
reactphysics3d::CollisionWorld* world,
|
reactphysics3d::CollisionWorld* world,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mHeight(height),
|
: openglframework::Mesh(), mRadius(radius), mHeight(height) {
|
||||||
mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this);
|
||||||
|
@ -68,10 +67,10 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
||||||
mPreviousTransform = transform;
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Create a rigid body corresponding to the cone in the dynamics world
|
// Create a rigid body corresponding to the cone in the dynamics world
|
||||||
mRigidBody = world->createCollisionBody(transform);
|
mBody = world->createCollisionBody(transform);
|
||||||
|
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
mBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -87,8 +86,7 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
||||||
Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
||||||
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
|
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mHeight(height),
|
: openglframework::Mesh(), mRadius(radius), mHeight(height) {
|
||||||
mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this);
|
||||||
|
@ -121,7 +119,7 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
||||||
|
|
||||||
mRigidBody = body;
|
mBody = body;
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -183,10 +181,12 @@ void Cone::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
for (unsigned int i=0; i<getNbParts(); i++) {
|
for (unsigned int i=0; i<getNbParts(); i++) {
|
||||||
|
@ -196,6 +196,7 @@ void Cone::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
@ -205,31 +206,6 @@ void Cone::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the cone
|
|
||||||
void Cone::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mRigidBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the cone
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct cone dimensions
|
|
||||||
mTransformMatrix = newMatrix * mScalingMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices)
|
/// We create two VBOs (one for vertices and one for indices)
|
||||||
void Cone::createVBOAndVAO() {
|
void Cone::createVBOAndVAO() {
|
||||||
|
@ -290,12 +266,12 @@ void Cone::createVBOAndVAO() {
|
||||||
void Cone::resetTransform(const rp3d::Transform& transform) {
|
void Cone::resetTransform(const rp3d::Transform& transform) {
|
||||||
|
|
||||||
// Reset the transform
|
// Reset the transform
|
||||||
mRigidBody->setTransform(transform);
|
mBody->setTransform(transform);
|
||||||
|
|
||||||
mRigidBody->setIsSleeping(false);
|
mBody->setIsSleeping(false);
|
||||||
|
|
||||||
// Reset the velocity of the rigid body
|
// Reset the velocity of the rigid body
|
||||||
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
if (rigidBody != NULL) {
|
if (rigidBody != NULL) {
|
||||||
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
|
|
|
@ -29,9 +29,10 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class Cone
|
// Class Cone
|
||||||
class Cone : public openglframework::Mesh {
|
class Cone : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -43,9 +44,6 @@ class Cone : public openglframework::Mesh {
|
||||||
/// Height of the cone
|
/// Height of the cone
|
||||||
float mHeight;
|
float mHeight;
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the cone
|
|
||||||
rp3d::CollisionBody* mRigidBody;
|
|
||||||
|
|
||||||
/// Scaling matrix (applied to a sphere to obtain the correct cone dimensions)
|
/// Scaling matrix (applied to a sphere to obtain the correct cone dimensions)
|
||||||
openglframework::Matrix4 mScalingMatrix;
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
@ -70,9 +68,6 @@ class Cone : public openglframework::Mesh {
|
||||||
// Total number of cones created
|
// Total number of cones created
|
||||||
static int totalNbCones;
|
static int totalNbCones;
|
||||||
|
|
||||||
/// Color
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
|
@ -93,31 +88,20 @@ class Cone : public openglframework::Mesh {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Cone();
|
~Cone();
|
||||||
|
|
||||||
/// Return a pointer to the collision body of the box
|
|
||||||
reactphysics3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the rigid body of the box
|
|
||||||
reactphysics3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the cone
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the cone at the correct position and with the correct orientation
|
/// Render the cone at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader,
|
void render(openglframework::Shader& shader,
|
||||||
const openglframework::Matrix4& worldToCameraMatrix);
|
const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the collision body of the box
|
// Update the transform matrix of the object
|
||||||
inline rp3d::CollisionBody* Cone::getCollisionBody() {
|
inline void Cone::updateTransform(float interpolationFactor) {
|
||||||
return mRigidBody;
|
mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the box
|
|
||||||
inline rp3d::RigidBody* Cone::getRigidBody() {
|
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -32,7 +32,7 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER),
|
: openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER),
|
||||||
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
|
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
|
||||||
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "convexmesh.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "convexmesh.obj", *this);
|
||||||
|
@ -84,10 +84,10 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
|
||||||
mPreviousTransform = transform;
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Create a rigid body corresponding to the sphere in the dynamics world
|
// Create a rigid body corresponding to the sphere in the dynamics world
|
||||||
mRigidBody = world->createCollisionBody(transform);
|
mBody = world->createCollisionBody(transform);
|
||||||
|
|
||||||
// Add a collision shape to the body and specify the mass of the collision shape
|
// Add a collision shape to the body and specify the mass of the collision shape
|
||||||
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
mBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
||||||
|
|
||||||
// Create the VBOs and VAO
|
// Create the VBOs and VAO
|
||||||
createVBOAndVAO();
|
createVBOAndVAO();
|
||||||
|
@ -99,7 +99,7 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER),
|
: openglframework::Mesh(), mVBOVertices(GL_ARRAY_BUFFER),
|
||||||
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
|
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
|
||||||
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "convexmesh.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "convexmesh.obj", *this);
|
||||||
|
@ -153,7 +153,7 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass,
|
||||||
// Add a collision shape to the body and specify the mass of the collision shape
|
// Add a collision shape to the body and specify the mass of the collision shape
|
||||||
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
||||||
|
|
||||||
mRigidBody = body;
|
mBody = body;
|
||||||
|
|
||||||
// Create the VBOs and VAO
|
// Create the VBOs and VAO
|
||||||
createVBOAndVAO();
|
createVBOAndVAO();
|
||||||
|
@ -205,10 +205,12 @@ void ConvexMesh::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
for (unsigned int i=0; i<getNbParts(); i++) {
|
for (unsigned int i=0; i<getNbParts(); i++) {
|
||||||
|
@ -218,6 +220,7 @@ void ConvexMesh::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
@ -227,30 +230,6 @@ void ConvexMesh::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the sphere
|
|
||||||
void ConvexMesh::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mRigidBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct sphere dimensions
|
|
||||||
mTransformMatrix = newMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices)
|
/// We create two VBOs (one for vertices and one for indices)
|
||||||
void ConvexMesh::createVBOAndVAO() {
|
void ConvexMesh::createVBOAndVAO() {
|
||||||
|
@ -311,12 +290,12 @@ void ConvexMesh::createVBOAndVAO() {
|
||||||
void ConvexMesh::resetTransform(const rp3d::Transform& transform) {
|
void ConvexMesh::resetTransform(const rp3d::Transform& transform) {
|
||||||
|
|
||||||
// Reset the transform
|
// Reset the transform
|
||||||
mRigidBody->setTransform(transform);
|
mBody->setTransform(transform);
|
||||||
|
|
||||||
mRigidBody->setIsSleeping(false);
|
mBody->setIsSleeping(false);
|
||||||
|
|
||||||
// Reset the velocity of the rigid body
|
// Reset the velocity of the rigid body
|
||||||
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
if (rigidBody != NULL) {
|
if (rigidBody != NULL) {
|
||||||
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
|
|
|
@ -29,17 +29,15 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class ConvexMesh
|
// Class ConvexMesh
|
||||||
class ConvexMesh : public openglframework::Mesh {
|
class ConvexMesh : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
// -------------------- Attributes -------------------- //
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the mesh
|
|
||||||
rp3d::CollisionBody* mRigidBody;
|
|
||||||
|
|
||||||
/// Previous transform (for interpolation)
|
/// Previous transform (for interpolation)
|
||||||
rp3d::Transform mPreviousTransform;
|
rp3d::Transform mPreviousTransform;
|
||||||
|
|
||||||
|
@ -58,9 +56,6 @@ class ConvexMesh : public openglframework::Mesh {
|
||||||
/// Vertex Array Object for the vertex data
|
/// Vertex Array Object for the vertex data
|
||||||
openglframework::VertexArrayObject mVAO;
|
openglframework::VertexArrayObject mVAO;
|
||||||
|
|
||||||
/// Color
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
|
@ -81,31 +76,20 @@ class ConvexMesh : public openglframework::Mesh {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~ConvexMesh();
|
~ConvexMesh();
|
||||||
|
|
||||||
/// Return a pointer to the collision body of the box
|
|
||||||
reactphysics3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the rigid body of the box
|
|
||||||
reactphysics3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the mesh
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the mesh at the correct position and with the correct orientation
|
/// Render the mesh at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader,
|
void render(openglframework::Shader& shader,
|
||||||
const openglframework::Matrix4& worldToCameraMatrix);
|
const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the collision body of the box
|
// Update the transform matrix of the object
|
||||||
inline rp3d::CollisionBody* ConvexMesh::getCollisionBody() {
|
inline void ConvexMesh::updateTransform(float interpolationFactor) {
|
||||||
return mRigidBody;
|
mTransformMatrix = computeTransform(interpolationFactor, openglframework::Matrix4::identity());
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the box
|
|
||||||
inline rp3d::RigidBody* ConvexMesh::getRigidBody() {
|
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -37,8 +37,7 @@ int Cylinder::totalNbCylinders = 0;
|
||||||
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
|
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
|
||||||
reactphysics3d::CollisionWorld* world,
|
reactphysics3d::CollisionWorld* world,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mHeight(height),
|
: openglframework::Mesh(), mRadius(radius), mHeight(height) {
|
||||||
mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this);
|
||||||
|
@ -68,10 +67,10 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
|
||||||
mPreviousTransform = transform;
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Create a rigid body corresponding to the cylinder in the dynamics world
|
// Create a rigid body corresponding to the cylinder in the dynamics world
|
||||||
mRigidBody = world->createCollisionBody(transform);
|
mBody = world->createCollisionBody(transform);
|
||||||
|
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
mBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -87,8 +86,7 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
|
||||||
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
|
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
|
||||||
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
|
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mHeight(height),
|
: openglframework::Mesh(), mRadius(radius), mHeight(height) {
|
||||||
mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this);
|
||||||
|
@ -123,7 +121,7 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
mRigidBody = body;
|
mBody = body;
|
||||||
|
|
||||||
// Create the VBOs and VAO
|
// Create the VBOs and VAO
|
||||||
if (totalNbCylinders == 0) {
|
if (totalNbCylinders == 0) {
|
||||||
|
@ -184,10 +182,12 @@ void Cylinder::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
for (unsigned int i=0; i<getNbParts(); i++) {
|
for (unsigned int i=0; i<getNbParts(); i++) {
|
||||||
|
@ -197,6 +197,7 @@ void Cylinder::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
@ -206,30 +207,6 @@ void Cylinder::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the cylinder
|
|
||||||
void Cylinder::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mRigidBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the cylinder
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct cylinder dimensions
|
|
||||||
mTransformMatrix = newMatrix * mScalingMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices)
|
/// We create two VBOs (one for vertices and one for indices)
|
||||||
void Cylinder::createVBOAndVAO() {
|
void Cylinder::createVBOAndVAO() {
|
||||||
|
@ -290,12 +267,12 @@ void Cylinder::createVBOAndVAO() {
|
||||||
void Cylinder::resetTransform(const rp3d::Transform& transform) {
|
void Cylinder::resetTransform(const rp3d::Transform& transform) {
|
||||||
|
|
||||||
// Reset the transform
|
// Reset the transform
|
||||||
mRigidBody->setTransform(transform);
|
mBody->setTransform(transform);
|
||||||
|
|
||||||
mRigidBody->setIsSleeping(false);
|
mBody->setIsSleeping(false);
|
||||||
|
|
||||||
// Reset the velocity of the rigid body
|
// Reset the velocity of the rigid body
|
||||||
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
if (rigidBody != NULL) {
|
if (rigidBody != NULL) {
|
||||||
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
|
|
|
@ -29,9 +29,10 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class Cylinder
|
// Class Cylinder
|
||||||
class Cylinder : public openglframework::Mesh {
|
class Cylinder : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -43,9 +44,6 @@ class Cylinder : public openglframework::Mesh {
|
||||||
/// Height of the cylinder
|
/// Height of the cylinder
|
||||||
float mHeight;
|
float mHeight;
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the cylinder
|
|
||||||
rp3d::CollisionBody* mRigidBody;
|
|
||||||
|
|
||||||
/// Scaling matrix (applied to a sphere to obtain the correct cylinder dimensions)
|
/// Scaling matrix (applied to a sphere to obtain the correct cylinder dimensions)
|
||||||
openglframework::Matrix4 mScalingMatrix;
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
@ -70,9 +68,6 @@ class Cylinder : public openglframework::Mesh {
|
||||||
// Total number of capsules created
|
// Total number of capsules created
|
||||||
static int totalNbCylinders;
|
static int totalNbCylinders;
|
||||||
|
|
||||||
/// Color
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
|
@ -93,31 +88,20 @@ class Cylinder : public openglframework::Mesh {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Cylinder();
|
~Cylinder();
|
||||||
|
|
||||||
/// Return a pointer to the collision body of the box
|
|
||||||
reactphysics3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the rigid body of the box
|
|
||||||
reactphysics3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the cylinder
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the cylinder at the correct position and with the correct orientation
|
/// Render the cylinder at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader,
|
void render(openglframework::Shader& shader,
|
||||||
const openglframework::Matrix4& worldToCameraMatrix);
|
const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the collision body of the box
|
// Update the transform matrix of the object
|
||||||
inline rp3d::CollisionBody* Cylinder::getCollisionBody() {
|
inline void Cylinder::updateTransform(float interpolationFactor) {
|
||||||
return mRigidBody;
|
mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the box
|
|
||||||
inline rp3d::RigidBody* Cylinder::getRigidBody() {
|
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -36,7 +36,7 @@ int Dumbbell::totalNbDumbbells = 0;
|
||||||
// Constructor
|
// Constructor
|
||||||
Dumbbell::Dumbbell(const openglframework::Vector3 &position,
|
Dumbbell::Dumbbell(const openglframework::Vector3 &position,
|
||||||
reactphysics3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath)
|
reactphysics3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
: openglframework::Mesh() {
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "dumbbell.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "dumbbell.obj", *this);
|
||||||
|
@ -105,7 +105,7 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position,
|
||||||
// Constructor
|
// Constructor
|
||||||
Dumbbell::Dumbbell(const openglframework::Vector3 &position,
|
Dumbbell::Dumbbell(const openglframework::Vector3 &position,
|
||||||
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath)
|
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
: openglframework::Mesh() {
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "dumbbell.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "dumbbell.obj", *this);
|
||||||
|
@ -218,9 +218,11 @@ void Dumbbell::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
|
@ -231,6 +233,7 @@ void Dumbbell::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
@ -240,30 +243,6 @@ void Dumbbell::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the sphere
|
|
||||||
void Dumbbell::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct sphere dimensions
|
|
||||||
mTransformMatrix = newMatrix * mScalingMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices)
|
/// We create two VBOs (one for vertices and one for indices)
|
||||||
void Dumbbell::createVBOAndVAO() {
|
void Dumbbell::createVBOAndVAO() {
|
||||||
|
|
|
@ -29,9 +29,10 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class Sphere
|
// Class Sphere
|
||||||
class Dumbbell : public openglframework::Mesh {
|
class Dumbbell : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -40,9 +41,6 @@ class Dumbbell : public openglframework::Mesh {
|
||||||
/// Radius of the spheres
|
/// Radius of the spheres
|
||||||
float mRadius;
|
float mRadius;
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the sphere
|
|
||||||
rp3d::CollisionBody* mBody;
|
|
||||||
|
|
||||||
/// Scaling matrix (applied to a sphere to obtain the correct sphere dimensions)
|
/// Scaling matrix (applied to a sphere to obtain the correct sphere dimensions)
|
||||||
openglframework::Matrix4 mScalingMatrix;
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
@ -67,9 +65,6 @@ class Dumbbell : public openglframework::Mesh {
|
||||||
// Total number of capsules created
|
// Total number of capsules created
|
||||||
static int totalNbDumbbells;
|
static int totalNbDumbbells;
|
||||||
|
|
||||||
/// Color
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
|
@ -91,31 +86,20 @@ class Dumbbell : public openglframework::Mesh {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Dumbbell();
|
~Dumbbell();
|
||||||
|
|
||||||
/// Return a pointer to the rigid body
|
|
||||||
rp3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the body
|
|
||||||
rp3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the sphere
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the sphere at the correct position and with the correct orientation
|
/// Render the sphere at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader,
|
void render(openglframework::Shader& shader,
|
||||||
const openglframework::Matrix4& worldToCameraMatrix);
|
const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the sphere
|
// Update the transform matrix of the object
|
||||||
inline rp3d::RigidBody* Dumbbell::getRigidBody() {
|
inline void Dumbbell::updateTransform(float interpolationFactor) {
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mBody);
|
mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the body
|
|
||||||
inline rp3d::CollisionBody* Dumbbell::getCollisionBody() {
|
|
||||||
return mBody;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
59
testbed/common/PhysicsObject.cpp
Normal file
59
testbed/common/PhysicsObject.cpp
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* 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 "PhysicsObject.h"
|
||||||
|
|
||||||
|
/// Constructor
|
||||||
|
PhysicsObject::PhysicsObject() {
|
||||||
|
|
||||||
|
mColor = openglframework::Color(1, 1, 1, 1);
|
||||||
|
mSleepingColor = openglframework::Color(1, 0, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the new transform matrix
|
||||||
|
openglframework::Matrix4 PhysicsObject::computeTransform(float interpolationFactor,
|
||||||
|
const openglframework::Matrix4& scalingMatrix) {
|
||||||
|
|
||||||
|
// Get the transform of the rigid body
|
||||||
|
rp3d::Transform transform = mBody->getTransform();
|
||||||
|
|
||||||
|
// Interpolate the transform between the previous one and the new one
|
||||||
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
|
transform,
|
||||||
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
|
// Compute the transform used for rendering the box
|
||||||
|
rp3d::decimal matrix[16];
|
||||||
|
interpolatedTransform.getOpenGLMatrix(matrix);
|
||||||
|
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
||||||
|
matrix[1], matrix[5], matrix[9], matrix[13],
|
||||||
|
matrix[2], matrix[6], matrix[10], matrix[14],
|
||||||
|
matrix[3], matrix[7], matrix[11], matrix[15]);
|
||||||
|
|
||||||
|
// Apply the scaling matrix to have the correct box dimensions
|
||||||
|
return newMatrix * scalingMatrix;
|
||||||
|
}
|
96
testbed/common/PhysicsObject.h
Normal file
96
testbed/common/PhysicsObject.h
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* 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. *
|
||||||
|
* *
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
#ifndef PHYSICSOBJECT_H
|
||||||
|
#define PHYSICSOBJECT_H
|
||||||
|
|
||||||
|
// Libraries
|
||||||
|
#include "openglframework.h"
|
||||||
|
#include "reactphysics3d.h"
|
||||||
|
|
||||||
|
// Class PhysicsObject
|
||||||
|
class PhysicsObject {
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
/// Body used to simulate the dynamics of the box
|
||||||
|
rp3d::CollisionBody* mBody;
|
||||||
|
|
||||||
|
/// Previous transform of the body (for interpolation)
|
||||||
|
rp3d::Transform mPreviousTransform;
|
||||||
|
|
||||||
|
/// Main color of the box
|
||||||
|
openglframework::Color mColor;
|
||||||
|
|
||||||
|
/// Sleeping color
|
||||||
|
openglframework::Color mSleepingColor;
|
||||||
|
|
||||||
|
// Compute the new transform matrix
|
||||||
|
openglframework::Matrix4 computeTransform(float interpolationFactor,
|
||||||
|
const openglframework::Matrix4 &scalingMatrix);
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Constructor
|
||||||
|
PhysicsObject();
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor)=0;
|
||||||
|
|
||||||
|
/// Set the color of the box
|
||||||
|
void setColor(const openglframework::Color& color);
|
||||||
|
|
||||||
|
/// Set the sleeping color of the box
|
||||||
|
void setSleepingColor(const openglframework::Color& color);
|
||||||
|
|
||||||
|
/// Return a pointer to the collision body of the box
|
||||||
|
reactphysics3d::CollisionBody* getCollisionBody();
|
||||||
|
|
||||||
|
/// Return a pointer to the rigid body of the box
|
||||||
|
reactphysics3d::RigidBody* getRigidBody();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set the color of the box
|
||||||
|
inline void PhysicsObject::setColor(const openglframework::Color& color) {
|
||||||
|
mColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the sleeping color of the box
|
||||||
|
inline void PhysicsObject::setSleepingColor(const openglframework::Color& color) {
|
||||||
|
mSleepingColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a pointer to the collision body of the box
|
||||||
|
inline rp3d::CollisionBody* PhysicsObject::getCollisionBody() {
|
||||||
|
return mBody;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a pointer to the rigid body of the box (NULL if it's not a rigid body)
|
||||||
|
inline rp3d::RigidBody* PhysicsObject::getRigidBody() {
|
||||||
|
return dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -37,7 +37,7 @@ int Sphere::totalNbSpheres = 0;
|
||||||
Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
||||||
reactphysics3d::CollisionWorld* world,
|
reactphysics3d::CollisionWorld* world,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
: openglframework::Mesh(), mRadius(radius) {
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this);
|
||||||
|
@ -67,10 +67,10 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
||||||
mPreviousTransform = transform;
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Create a rigid body corresponding to the sphere in the dynamics world
|
// Create a rigid body corresponding to the sphere in the dynamics world
|
||||||
mRigidBody = world->createCollisionBody(transform);
|
mBody = world->createCollisionBody(transform);
|
||||||
|
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
mRigidBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
mBody->addCollisionShape(collisionShape, rp3d::Transform::identity());
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
||||||
Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
||||||
float mass, reactphysics3d::DynamicsWorld* world,
|
float mass, reactphysics3d::DynamicsWorld* world,
|
||||||
const std::string& meshFolderPath)
|
const std::string& meshFolderPath)
|
||||||
: openglframework::Mesh(), mRadius(radius), mColor(0.5f, 0.5f, 0.5f, 1.0f) {
|
: openglframework::Mesh(), mRadius(radius) {
|
||||||
|
|
||||||
// Load the mesh from a file
|
// Load the mesh from a file
|
||||||
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this);
|
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this);
|
||||||
|
@ -119,7 +119,7 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
|
||||||
// Add a collision shape to the body and specify the mass of the shape
|
// Add a collision shape to the body and specify the mass of the shape
|
||||||
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
body->addCollisionShape(collisionShape, rp3d::Transform::identity(), mass);
|
||||||
|
|
||||||
mRigidBody = body;
|
mBody = body;
|
||||||
|
|
||||||
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
mTransformMatrix = mTransformMatrix * mScalingMatrix;
|
||||||
|
|
||||||
|
@ -181,10 +181,12 @@ void Sphere::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
for (unsigned int i=0; i<getNbParts(); i++) {
|
for (unsigned int i=0; i<getNbParts(); i++) {
|
||||||
|
@ -194,6 +196,7 @@ void Sphere::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
@ -203,30 +206,6 @@ void Sphere::render(openglframework::Shader& shader,
|
||||||
shader.unbind();
|
shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the transform matrix of the sphere
|
|
||||||
void Sphere::updateTransform(float interpolationFactor) {
|
|
||||||
|
|
||||||
// Get the transform of the rigid body
|
|
||||||
rp3d::Transform transform = mRigidBody->getTransform();
|
|
||||||
|
|
||||||
// Interpolate the transform between the previous one and the new one
|
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
|
||||||
transform,
|
|
||||||
interpolationFactor);
|
|
||||||
mPreviousTransform = transform;
|
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
|
||||||
rp3d::decimal matrix[16];
|
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
|
||||||
openglframework::Matrix4 newMatrix(matrix[0], matrix[4], matrix[8], matrix[12],
|
|
||||||
matrix[1], matrix[5], matrix[9], matrix[13],
|
|
||||||
matrix[2], matrix[6], matrix[10], matrix[14],
|
|
||||||
matrix[3], matrix[7], matrix[11], matrix[15]);
|
|
||||||
|
|
||||||
// Apply the scaling matrix to have the correct sphere dimensions
|
|
||||||
mTransformMatrix = newMatrix * mScalingMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
/// We create two VBOs (one for vertices and one for indices)
|
/// We create two VBOs (one for vertices and one for indices)
|
||||||
void Sphere::createVBOAndVAO() {
|
void Sphere::createVBOAndVAO() {
|
||||||
|
@ -287,12 +266,12 @@ void Sphere::createVBOAndVAO() {
|
||||||
void Sphere::resetTransform(const rp3d::Transform& transform) {
|
void Sphere::resetTransform(const rp3d::Transform& transform) {
|
||||||
|
|
||||||
// Reset the transform
|
// Reset the transform
|
||||||
mRigidBody->setTransform(transform);
|
mBody->setTransform(transform);
|
||||||
|
|
||||||
mRigidBody->setIsSleeping(false);
|
mBody->setIsSleeping(false);
|
||||||
|
|
||||||
// Reset the velocity of the rigid body
|
// Reset the velocity of the rigid body
|
||||||
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
|
||||||
if (rigidBody != NULL) {
|
if (rigidBody != NULL) {
|
||||||
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
|
||||||
|
|
|
@ -29,9 +29,10 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "openglframework.h"
|
#include "openglframework.h"
|
||||||
#include "reactphysics3d.h"
|
#include "reactphysics3d.h"
|
||||||
|
#include "PhysicsObject.h"
|
||||||
|
|
||||||
// Class Sphere
|
// Class Sphere
|
||||||
class Sphere : public openglframework::Mesh {
|
class Sphere : public openglframework::Mesh, public PhysicsObject {
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
@ -40,9 +41,6 @@ class Sphere : public openglframework::Mesh {
|
||||||
/// Radius of the sphere
|
/// Radius of the sphere
|
||||||
float mRadius;
|
float mRadius;
|
||||||
|
|
||||||
/// Rigid body used to simulate the dynamics of the sphere
|
|
||||||
rp3d::CollisionBody* mRigidBody;
|
|
||||||
|
|
||||||
/// Scaling matrix (applied to a sphere to obtain the correct sphere dimensions)
|
/// Scaling matrix (applied to a sphere to obtain the correct sphere dimensions)
|
||||||
openglframework::Matrix4 mScalingMatrix;
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
@ -67,9 +65,6 @@ class Sphere : public openglframework::Mesh {
|
||||||
// Total number of capsules created
|
// Total number of capsules created
|
||||||
static int totalNbSpheres;
|
static int totalNbSpheres;
|
||||||
|
|
||||||
/// Color
|
|
||||||
openglframework::Color mColor;
|
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
// Create the Vertex Buffer Objects used to render with OpenGL.
|
// Create the Vertex Buffer Objects used to render with OpenGL.
|
||||||
|
@ -90,31 +85,20 @@ class Sphere : public openglframework::Mesh {
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Sphere();
|
~Sphere();
|
||||||
|
|
||||||
/// Return a pointer to the collision body of the box
|
|
||||||
reactphysics3d::CollisionBody* getCollisionBody();
|
|
||||||
|
|
||||||
/// Return a pointer to the rigid body of the box
|
|
||||||
reactphysics3d::RigidBody* getRigidBody();
|
|
||||||
|
|
||||||
/// Update the transform matrix of the sphere
|
|
||||||
void updateTransform(float interpolationFactor);
|
|
||||||
|
|
||||||
/// Render the sphere at the correct position and with the correct orientation
|
/// Render the sphere at the correct position and with the correct orientation
|
||||||
void render(openglframework::Shader& shader,
|
void render(openglframework::Shader& shader,
|
||||||
const openglframework::Matrix4& worldToCameraMatrix);
|
const openglframework::Matrix4& worldToCameraMatrix);
|
||||||
|
|
||||||
/// Set the position of the box
|
/// Set the position of the box
|
||||||
void resetTransform(const rp3d::Transform& transform);
|
void resetTransform(const rp3d::Transform& transform);
|
||||||
|
|
||||||
|
/// Update the transform matrix of the object
|
||||||
|
virtual void updateTransform(float interpolationFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a pointer to the collision body of the box
|
// Update the transform matrix of the object
|
||||||
inline rp3d::CollisionBody* Sphere::getCollisionBody() {
|
inline void Sphere::updateTransform(float interpolationFactor) {
|
||||||
return mRigidBody;
|
mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the rigid body of the box
|
|
||||||
inline rp3d::RigidBody* Sphere::getRigidBody() {
|
|
||||||
return dynamic_cast<rp3d::RigidBody*>(mRigidBody);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -119,10 +119,12 @@ void VisualContactPoint::render(openglframework::Shader& shader,
|
||||||
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
|
||||||
|
|
||||||
glEnableVertexAttribArray(vertexPositionLoc);
|
glEnableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
|
||||||
|
|
||||||
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
|
||||||
|
mVBONormals.bind();
|
||||||
|
|
||||||
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
|
||||||
|
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
// For each part of the mesh
|
// For each part of the mesh
|
||||||
for (unsigned int i=0; i<mMesh.getNbParts(); i++) {
|
for (unsigned int i=0; i<mMesh.getNbParts(); i++) {
|
||||||
|
@ -132,6 +134,7 @@ void VisualContactPoint::render(openglframework::Shader& shader,
|
||||||
glDisableVertexAttribArray(vertexPositionLoc);
|
glDisableVertexAttribArray(vertexPositionLoc);
|
||||||
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
|
||||||
|
|
||||||
|
mVBONormals.unbind();
|
||||||
mVBOVertices.unbind();
|
mVBOVertices.unbind();
|
||||||
|
|
||||||
// Unbind the VAO
|
// Unbind the VAO
|
||||||
|
|
|
@ -140,10 +140,10 @@ class Mesh : public Object3D {
|
||||||
void setNormal(uint i, const Vector3& normal);
|
void setNormal(uint i, const Vector3& normal);
|
||||||
|
|
||||||
// Return the color of a given vertex
|
// Return the color of a given vertex
|
||||||
const Color& getColor(uint i) const;
|
const Color& getVertexColor(uint i) const;
|
||||||
|
|
||||||
// Set the color of a given vertex
|
// Set the color of a given vertex
|
||||||
void setColor(uint i, const Color& color);
|
void setVertexColor(uint i, const Color& color);
|
||||||
|
|
||||||
// Set a color to all the vertices
|
// Set a color to all the vertices
|
||||||
void setColorToAllVertices(const Color& color);
|
void setColorToAllVertices(const Color& color);
|
||||||
|
@ -282,13 +282,13 @@ inline void Mesh::setNormal(uint i, const Vector3& normal) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the color of a given vertex
|
// Return the color of a given vertex
|
||||||
inline const Color& Mesh::getColor(uint i) const {
|
inline const Color& Mesh::getVertexColor(uint i) const {
|
||||||
assert(i < getNbVertices());
|
assert(i < getNbVertices());
|
||||||
return mColors[i];
|
return mColors[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the color of a given vertex
|
// Set the color of a given vertex
|
||||||
inline void Mesh::setColor(uint i, const Color& color) {
|
inline void Mesh::setVertexColor(uint i, const Color& color) {
|
||||||
|
|
||||||
// If the color array does not have the same size as
|
// If the color array does not have the same size as
|
||||||
// the vertices array
|
// the vertices array
|
||||||
|
|
|
@ -67,6 +67,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
// Create a convex mesh and a corresponding rigid in the dynamics world
|
// Create a convex mesh and a corresponding rigid in the dynamics world
|
||||||
Dumbbell* dumbbell = new Dumbbell(position, mDynamicsWorld, meshFolderPath);
|
Dumbbell* dumbbell = new Dumbbell(position, mDynamicsWorld, meshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
dumbbell->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
dumbbell->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = dumbbell->getRigidBody()->getMaterial();
|
rp3d::Material& material = dumbbell->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -87,6 +91,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
// Create a sphere and a corresponding rigid in the dynamics world
|
// Create a sphere and a corresponding rigid in the dynamics world
|
||||||
Box* box = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld);
|
Box* box = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
box->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
box->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = box->getRigidBody()->getMaterial();
|
rp3d::Material& material = box->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -108,6 +116,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
Sphere* sphere = new Sphere(SPHERE_RADIUS, position , BOX_MASS, mDynamicsWorld,
|
Sphere* sphere = new Sphere(SPHERE_RADIUS, position , BOX_MASS, mDynamicsWorld,
|
||||||
meshFolderPath);
|
meshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
sphere->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
sphere->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = sphere->getRigidBody()->getMaterial();
|
rp3d::Material& material = sphere->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -129,6 +141,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
Cone* cone = new Cone(CONE_RADIUS, CONE_HEIGHT, position, CONE_MASS, mDynamicsWorld,
|
Cone* cone = new Cone(CONE_RADIUS, CONE_HEIGHT, position, CONE_MASS, mDynamicsWorld,
|
||||||
meshFolderPath);
|
meshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
cone->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
cone->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = cone->getRigidBody()->getMaterial();
|
rp3d::Material& material = cone->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -150,6 +166,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
Cylinder* cylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position ,
|
Cylinder* cylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position ,
|
||||||
CYLINDER_MASS, mDynamicsWorld, meshFolderPath);
|
CYLINDER_MASS, mDynamicsWorld, meshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
cylinder->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
cylinder->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = cylinder->getRigidBody()->getMaterial();
|
rp3d::Material& material = cylinder->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -171,6 +191,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
Capsule* capsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position ,
|
Capsule* capsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position ,
|
||||||
CAPSULE_MASS, mDynamicsWorld, meshFolderPath);
|
CAPSULE_MASS, mDynamicsWorld, meshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
capsule->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
capsule->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = capsule->getRigidBody()->getMaterial();
|
rp3d::Material& material = capsule->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -191,6 +215,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
// Create a convex mesh and a corresponding rigid in the dynamics world
|
// Create a convex mesh and a corresponding rigid in the dynamics world
|
||||||
ConvexMesh* mesh = new ConvexMesh(position, MESH_MASS, mDynamicsWorld, meshFolderPath);
|
ConvexMesh* mesh = new ConvexMesh(position, MESH_MASS, mDynamicsWorld, meshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mesh->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
mesh->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = mesh->getRigidBody()->getMaterial();
|
rp3d::Material& material = mesh->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.2));
|
material.setBounciness(rp3d::decimal(0.2));
|
||||||
|
@ -202,7 +230,10 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
|
||||||
// Create the floor
|
// Create the floor
|
||||||
openglframework::Vector3 floorPosition(0, 0, 0);
|
openglframework::Vector3 floorPosition(0, 0, 0);
|
||||||
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
||||||
mFloor->setColor(Color(0.70f, 0.70f, 0.7f, 1.0f));
|
|
||||||
|
// Set the box color
|
||||||
|
mFloor->setColor(mGreyColorDemo);
|
||||||
|
mFloor->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// The floor must be a static rigid body
|
// The floor must be a static rigid body
|
||||||
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
||||||
|
|
|
@ -45,12 +45,12 @@ namespace collisionshapesscene {
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const float SCENE_RADIUS = 30.0f;
|
const float SCENE_RADIUS = 30.0f;
|
||||||
const int NB_BOXES = 3;
|
const int NB_BOXES = 5;
|
||||||
const int NB_CUBES = 5;
|
const int NB_CUBES = 4;
|
||||||
const int NB_CONES = 3;
|
const int NB_CONES = 4;
|
||||||
const int NB_CYLINDERS = 3;
|
const int NB_CYLINDERS = 6;
|
||||||
const int NB_CAPSULES = 4;
|
const int NB_CAPSULES = 5;
|
||||||
const int NB_MESHES = 4;
|
const int NB_MESHES = 7;
|
||||||
const int NB_COMPOUND_SHAPES = 3;
|
const int NB_COMPOUND_SHAPES = 3;
|
||||||
const openglframework::Vector3 BOX_SIZE(2, 2, 2);
|
const openglframework::Vector3 BOX_SIZE(2, 2, 2);
|
||||||
const float SPHERE_RADIUS = 1.5f;
|
const float SPHERE_RADIUS = 1.5f;
|
||||||
|
|
|
@ -66,6 +66,10 @@ CubesScene::CubesScene(const std::string& name)
|
||||||
// Create a cube and a corresponding rigid in the dynamics world
|
// Create a cube and a corresponding rigid in the dynamics world
|
||||||
Box* cube = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld);
|
Box* cube = new Box(BOX_SIZE, position , BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
cube->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
cube->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = cube->getRigidBody()->getMaterial();
|
rp3d::Material& material = cube->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.4));
|
material.setBounciness(rp3d::decimal(0.4));
|
||||||
|
@ -77,7 +81,8 @@ CubesScene::CubesScene(const std::string& name)
|
||||||
// Create the floor
|
// Create the floor
|
||||||
openglframework::Vector3 floorPosition(0, 0, 0);
|
openglframework::Vector3 floorPosition(0, 0, 0);
|
||||||
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
||||||
mFloor->setColor(Color(0.70f, 0.70f, 0.7f, 1.0f));
|
mFloor->setColor(mGreyColorDemo);
|
||||||
|
mFloor->setSleepingColor(mGreyColorDemo);
|
||||||
|
|
||||||
// The floor must be a static rigid body
|
// The floor must be a static rigid body
|
||||||
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
||||||
|
@ -152,16 +157,6 @@ void CubesScene::update() {
|
||||||
}
|
}
|
||||||
|
|
||||||
mFloor->updateTransform(mInterpolationFactor);
|
mFloor->updateTransform(mInterpolationFactor);
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the scene in a single pass
|
// Render the scene in a single pass
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace cubesscene {
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const float SCENE_RADIUS = 30.0f; // Radius of the scene in meters
|
const float SCENE_RADIUS = 30.0f; // Radius of the scene in meters
|
||||||
const int NB_CUBES = 20; // Number of boxes in the scene
|
const int NB_CUBES = 30; // Number of boxes in the scene
|
||||||
const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters
|
const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters
|
||||||
const openglframework::Vector3 FLOOR_SIZE(50, 0.5f, 50); // Floor dimensions in meters
|
const openglframework::Vector3 FLOOR_SIZE(50, 0.5f, 50); // Floor dimensions in meters
|
||||||
const float BOX_MASS = 1.0f; // Box mass in kilograms
|
const float BOX_MASS = 1.0f; // Box mass in kilograms
|
||||||
|
|
|
@ -269,6 +269,10 @@ void JointsScene::createBallAndSocketJoints() {
|
||||||
mBallAndSocketJointChainBoxes[i] = new Box(boxDimension, positionBox , boxMass,
|
mBallAndSocketJointChainBoxes[i] = new Box(boxDimension, positionBox , boxMass,
|
||||||
mDynamicsWorld);
|
mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mBallAndSocketJointChainBoxes[i]->setColor(mDemoColors[i % mNbDemoColors]);
|
||||||
|
mBallAndSocketJointChainBoxes[i]->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// The fist box cannot move (static body)
|
// The fist box cannot move (static body)
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
mBallAndSocketJointChainBoxes[i]->getRigidBody()->setType(rp3d::STATIC);
|
mBallAndSocketJointChainBoxes[i]->getRigidBody()->setType(rp3d::STATIC);
|
||||||
|
@ -314,6 +318,10 @@ void JointsScene::createSliderJoint() {
|
||||||
openglframework::Vector3 box1Dimension(2, 4, 2);
|
openglframework::Vector3 box1Dimension(2, 4, 2);
|
||||||
mSliderJointBottomBox = new Box(box1Dimension, positionBox1 , BOX_MASS, mDynamicsWorld);
|
mSliderJointBottomBox = new Box(box1Dimension, positionBox1 , BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mSliderJointBottomBox->setColor(mBlueColorDemo);
|
||||||
|
mSliderJointBottomBox->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// The fist box cannot move
|
// The fist box cannot move
|
||||||
mSliderJointBottomBox->getRigidBody()->setType(rp3d::STATIC);
|
mSliderJointBottomBox->getRigidBody()->setType(rp3d::STATIC);
|
||||||
|
|
||||||
|
@ -330,6 +338,10 @@ void JointsScene::createSliderJoint() {
|
||||||
openglframework::Vector3 box2Dimension(1.5f, 4, 1.5f);
|
openglframework::Vector3 box2Dimension(1.5f, 4, 1.5f);
|
||||||
mSliderJointTopBox = new Box(box2Dimension, positionBox2, BOX_MASS, mDynamicsWorld);
|
mSliderJointTopBox = new Box(box2Dimension, positionBox2, BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mSliderJointTopBox->setColor(mOrangeColorDemo);
|
||||||
|
mSliderJointTopBox->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material2 = mSliderJointTopBox->getRigidBody()->getMaterial();
|
rp3d::Material& material2 = mSliderJointTopBox->getRigidBody()->getMaterial();
|
||||||
material2.setBounciness(0.4f);
|
material2.setBounciness(0.4f);
|
||||||
|
@ -366,6 +378,10 @@ void JointsScene::createPropellerHingeJoint() {
|
||||||
openglframework::Vector3 boxDimension(10, 1, 1);
|
openglframework::Vector3 boxDimension(10, 1, 1);
|
||||||
mPropellerBox = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld);
|
mPropellerBox = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mPropellerBox->setColor(mYellowColorDemo);
|
||||||
|
mPropellerBox->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material = mPropellerBox->getRigidBody()->getMaterial();
|
rp3d::Material& material = mPropellerBox->getRigidBody()->getMaterial();
|
||||||
material.setBounciness(rp3d::decimal(0.4));
|
material.setBounciness(rp3d::decimal(0.4));
|
||||||
|
@ -401,6 +417,10 @@ void JointsScene::createFixedJoints() {
|
||||||
openglframework::Vector3 boxDimension(1.5, 1.5, 1.5);
|
openglframework::Vector3 boxDimension(1.5, 1.5, 1.5);
|
||||||
mFixedJointBox1 = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld);
|
mFixedJointBox1 = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mFixedJointBox1->setColor(mPinkColorDemo);
|
||||||
|
mFixedJointBox1->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material1 = mFixedJointBox1->getRigidBody()->getMaterial();
|
rp3d::Material& material1 = mFixedJointBox1->getRigidBody()->getMaterial();
|
||||||
material1.setBounciness(rp3d::decimal(0.4));
|
material1.setBounciness(rp3d::decimal(0.4));
|
||||||
|
@ -413,6 +433,10 @@ void JointsScene::createFixedJoints() {
|
||||||
// Create a box and a corresponding rigid in the dynamics world
|
// Create a box and a corresponding rigid in the dynamics world
|
||||||
mFixedJointBox2 = new Box(boxDimension, positionBox2 , BOX_MASS, mDynamicsWorld);
|
mFixedJointBox2 = new Box(boxDimension, positionBox2 , BOX_MASS, mDynamicsWorld);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mFixedJointBox2->setColor(mBlueColorDemo);
|
||||||
|
mFixedJointBox2->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Change the material properties of the rigid body
|
// Change the material properties of the rigid body
|
||||||
rp3d::Material& material2 = mFixedJointBox2->getRigidBody()->getMaterial();
|
rp3d::Material& material2 = mFixedJointBox2->getRigidBody()->getMaterial();
|
||||||
material2.setBounciness(rp3d::decimal(0.4));
|
material2.setBounciness(rp3d::decimal(0.4));
|
||||||
|
@ -447,7 +471,10 @@ void JointsScene::createFloor() {
|
||||||
// Create the floor
|
// Create the floor
|
||||||
openglframework::Vector3 floorPosition(0, 0, 0);
|
openglframework::Vector3 floorPosition(0, 0, 0);
|
||||||
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
|
||||||
mFloor->setColor(Color(0.70f, 0.70f, 0.7f, 1.0f));
|
|
||||||
|
// Set the box color
|
||||||
|
mFloor->setColor(mGreyColorDemo);
|
||||||
|
mFloor->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// The floor must be a static rigid body
|
// The floor must be a static rigid body
|
||||||
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
mFloor->getRigidBody()->setType(rp3d::STATIC);
|
||||||
|
|
|
@ -51,6 +51,10 @@ RaycastScene::RaycastScene(const std::string& name)
|
||||||
// Create a convex mesh and a corresponding collision body in the dynamics world
|
// Create a convex mesh and a corresponding collision body in the dynamics world
|
||||||
mDumbbell = new Dumbbell(position1, mCollisionWorld, mMeshFolderPath);
|
mDumbbell = new Dumbbell(position1, mCollisionWorld, mMeshFolderPath);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mDumbbell->setColor(mGreyColorDemo);
|
||||||
|
mDumbbell->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// ---------- Box ---------- //
|
// ---------- Box ---------- //
|
||||||
openglframework::Vector3 position2(0, 0, 0);
|
openglframework::Vector3 position2(0, 0, 0);
|
||||||
|
|
||||||
|
@ -58,6 +62,10 @@ RaycastScene::RaycastScene(const std::string& name)
|
||||||
mBox = new Box(BOX_SIZE, position2, mCollisionWorld);
|
mBox = new Box(BOX_SIZE, position2, mCollisionWorld);
|
||||||
mBox->getCollisionBody()->setIsActive(false);
|
mBox->getCollisionBody()->setIsActive(false);
|
||||||
|
|
||||||
|
// Set the box color
|
||||||
|
mBox->setColor(mGreyColorDemo);
|
||||||
|
mBox->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// ---------- Sphere ---------- //
|
// ---------- Sphere ---------- //
|
||||||
openglframework::Vector3 position3(0, 0, 0);
|
openglframework::Vector3 position3(0, 0, 0);
|
||||||
|
|
||||||
|
@ -65,6 +73,10 @@ RaycastScene::RaycastScene(const std::string& name)
|
||||||
mSphere = new Sphere(SPHERE_RADIUS, position3, mCollisionWorld,
|
mSphere = new Sphere(SPHERE_RADIUS, position3, mCollisionWorld,
|
||||||
mMeshFolderPath);
|
mMeshFolderPath);
|
||||||
|
|
||||||
|
// Set the color
|
||||||
|
mSphere->setColor(mGreyColorDemo);
|
||||||
|
mSphere->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// ---------- Cone ---------- //
|
// ---------- Cone ---------- //
|
||||||
openglframework::Vector3 position4(0, 0, 0);
|
openglframework::Vector3 position4(0, 0, 0);
|
||||||
|
|
||||||
|
@ -72,6 +84,10 @@ RaycastScene::RaycastScene(const std::string& name)
|
||||||
mCone = new Cone(CONE_RADIUS, CONE_HEIGHT, position4, mCollisionWorld,
|
mCone = new Cone(CONE_RADIUS, CONE_HEIGHT, position4, mCollisionWorld,
|
||||||
mMeshFolderPath);
|
mMeshFolderPath);
|
||||||
|
|
||||||
|
// Set the color
|
||||||
|
mCone->setColor(mGreyColorDemo);
|
||||||
|
mCone->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// ---------- Cylinder ---------- //
|
// ---------- Cylinder ---------- //
|
||||||
openglframework::Vector3 position5(0, 0, 0);
|
openglframework::Vector3 position5(0, 0, 0);
|
||||||
|
|
||||||
|
@ -79,6 +95,10 @@ RaycastScene::RaycastScene(const std::string& name)
|
||||||
mCylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position5,
|
mCylinder = new Cylinder(CYLINDER_RADIUS, CYLINDER_HEIGHT, position5,
|
||||||
mCollisionWorld, mMeshFolderPath);
|
mCollisionWorld, mMeshFolderPath);
|
||||||
|
|
||||||
|
// Set the color
|
||||||
|
mCylinder->setColor(mGreyColorDemo);
|
||||||
|
mCylinder->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// ---------- Capsule ---------- //
|
// ---------- Capsule ---------- //
|
||||||
openglframework::Vector3 position6(0, 0, 0);
|
openglframework::Vector3 position6(0, 0, 0);
|
||||||
|
|
||||||
|
@ -86,12 +106,20 @@ RaycastScene::RaycastScene(const std::string& name)
|
||||||
mCapsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position6 ,
|
mCapsule = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, position6 ,
|
||||||
mCollisionWorld, mMeshFolderPath);
|
mCollisionWorld, mMeshFolderPath);
|
||||||
|
|
||||||
|
// Set the color
|
||||||
|
mCapsule->setColor(mGreyColorDemo);
|
||||||
|
mCapsule->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// ---------- Convex Mesh ---------- //
|
// ---------- Convex Mesh ---------- //
|
||||||
openglframework::Vector3 position7(0, 0, 0);
|
openglframework::Vector3 position7(0, 0, 0);
|
||||||
|
|
||||||
// Create a convex mesh and a corresponding collision body in the dynamics world
|
// Create a convex mesh and a corresponding collision body in the dynamics world
|
||||||
mConvexMesh = new ConvexMesh(position7, mCollisionWorld, mMeshFolderPath);
|
mConvexMesh = new ConvexMesh(position7, mCollisionWorld, mMeshFolderPath);
|
||||||
|
|
||||||
|
// Set the color
|
||||||
|
mConvexMesh->setColor(mGreyColorDemo);
|
||||||
|
mConvexMesh->setSleepingColor(mRedColorDemo);
|
||||||
|
|
||||||
// Create the lines that will be used for raycasting
|
// Create the lines that will be used for raycasting
|
||||||
createLines();
|
createLines();
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,6 @@
|
||||||
uniform vec3 lightAmbientColor; // Lights ambient color
|
uniform vec3 lightAmbientColor; // Lights ambient color
|
||||||
uniform vec3 light0PosCameraSpace; // Camera-space position of the light
|
uniform vec3 light0PosCameraSpace; // Camera-space position of the light
|
||||||
uniform vec3 light0DiffuseColor; // Light 0 diffuse color
|
uniform vec3 light0DiffuseColor; // Light 0 diffuse color
|
||||||
uniform vec3 light0SpecularColor; // Light 0 specular color
|
|
||||||
uniform float shininess; // Shininess
|
|
||||||
uniform sampler2D textureSampler; // Texture
|
uniform sampler2D textureSampler; // Texture
|
||||||
uniform sampler2D shadowMapSampler; // Shadow map texture sampler
|
uniform sampler2D shadowMapSampler; // Shadow map texture sampler
|
||||||
uniform bool isTexture; // True if we need to use the texture
|
uniform bool isTexture; // True if we need to use the texture
|
||||||
|
@ -63,17 +61,10 @@ void main() {
|
||||||
float diffuseFactor = max(dot(N, L0), 0.0);
|
float diffuseFactor = max(dot(N, L0), 0.0);
|
||||||
vec3 diffuse = light0DiffuseColor * diffuseFactor * textureColor;
|
vec3 diffuse = light0DiffuseColor * diffuseFactor * textureColor;
|
||||||
|
|
||||||
// Compute the specular term of light 0
|
|
||||||
vec3 V = normalize(-vertexPosCameraSpace);
|
|
||||||
vec3 H0 = normalize(V + L0);
|
|
||||||
float specularFactor = pow(max(dot(N, H0), 0.0), shininess);
|
|
||||||
if (diffuseFactor < 0.0) specularFactor = 0.0;
|
|
||||||
vec3 specular = light0SpecularColor * specularFactor;
|
|
||||||
|
|
||||||
// Compute shadow factor
|
// Compute shadow factor
|
||||||
float shadow = 1.0;
|
float shadow = 1.0;
|
||||||
if (isShadowEnabled) {
|
if (isShadowEnabled) {
|
||||||
float bias = 0.0001;
|
float bias = 0.0003;
|
||||||
float shadowBias = -0.000;
|
float shadowBias = -0.000;
|
||||||
vec4 shadowMapUV = shadowMapCoords;
|
vec4 shadowMapUV = shadowMapCoords;
|
||||||
shadowMapUV.z -= shadowBias;
|
shadowMapUV.z -= shadowBias;
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
const float GUI_SCALING = 2.0f;
|
const float GUI_SCALING = 2.0f;
|
||||||
const int LEFT_PANE_WIDTH = 300;
|
const int LEFT_PANE_WIDTH = 300;
|
||||||
const int LEFT_PANE_HEADER_HEIGHT = 90;
|
const int LEFT_PANE_HEADER_HEIGHT = 90;
|
||||||
const double TIME_INTERVAL_DISPLAY_PROFILING_INFO = 1.0;
|
const double TIME_INTERVAL_DISPLAY_PROFILING_INFO = 0.2;
|
||||||
|
|
||||||
using namespace openglframework;
|
using namespace openglframework;
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,15 @@
|
||||||
using namespace openglframework;
|
using namespace openglframework;
|
||||||
|
|
||||||
int SceneDemo::shadowMapTextureLevel = 0;
|
int SceneDemo::shadowMapTextureLevel = 0;
|
||||||
|
openglframework::Color SceneDemo::mGreyColorDemo = Color(0.70f, 0.70f, 0.7f, 1.0);
|
||||||
|
openglframework::Color SceneDemo::mYellowColorDemo = Color(0.9, 0.88, 0.145, 1.0);
|
||||||
|
openglframework::Color SceneDemo::mBlueColorDemo = Color(0, 0.66, 0.95, 1.0);
|
||||||
|
openglframework::Color SceneDemo::mOrangeColorDemo = Color(0.9, 0.35, 0, 1.0);
|
||||||
|
openglframework::Color SceneDemo::mPinkColorDemo = Color(0.83, 0.48, 0.64, 1.0);
|
||||||
|
openglframework::Color SceneDemo::mRedColorDemo = Color(0.95, 0, 0, 1.0);
|
||||||
|
int SceneDemo::mNbDemoColors = 4;
|
||||||
|
openglframework::Color SceneDemo::mDemoColors[] = {SceneDemo::mYellowColorDemo, SceneDemo::mBlueColorDemo,
|
||||||
|
SceneDemo::mOrangeColorDemo, SceneDemo::mPinkColorDemo};
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
SceneDemo::SceneDemo(const std::string& name, float sceneRadius, bool isShadowMappingEnabled)
|
SceneDemo::SceneDemo(const std::string& name, float sceneRadius, bool isShadowMappingEnabled)
|
||||||
|
@ -43,15 +52,16 @@ SceneDemo::SceneDemo(const std::string& name, float sceneRadius, bool isShadowMa
|
||||||
shadowMapTextureLevel++;
|
shadowMapTextureLevel++;
|
||||||
|
|
||||||
// Move the light0
|
// Move the light0
|
||||||
mLight0.translateWorld(Vector3(0, 40, 40));
|
mLight0.translateWorld(Vector3(-2, 35, 40));
|
||||||
|
|
||||||
// Camera at light0 postion for the shadow map
|
// Camera at light0 postion for the shadow map
|
||||||
mShadowMapLightCamera.translateWorld(mLight0.getOrigin());
|
mShadowMapLightCamera.translateWorld(mLight0.getOrigin());
|
||||||
mShadowMapLightCamera.rotateLocal(Vector3(1, 0, 0), -PI / 4.0f);
|
mShadowMapLightCamera.rotateLocal(Vector3(1, 0, 0), -PI / 4.0f);
|
||||||
|
mShadowMapLightCamera.rotateWorld(Vector3(0, 1, 0), PI / 8.0f);
|
||||||
|
|
||||||
mShadowMapLightCamera.setDimensions(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
|
mShadowMapLightCamera.setDimensions(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
|
||||||
mShadowMapLightCamera.setFieldOfView(70.0f);
|
mShadowMapLightCamera.setFieldOfView(80.0f);
|
||||||
mShadowMapLightCamera.setSceneRadius(100);
|
mShadowMapLightCamera.setSceneRadius(100);
|
||||||
//mShadowMapLightCamera.setZoom(1.0);
|
|
||||||
|
|
||||||
mShadowMapBiasMatrix.setAllValues(0.5, 0.0, 0.0, 0.5,
|
mShadowMapBiasMatrix.setAllValues(0.5, 0.0, 0.0, 0.5,
|
||||||
0.0, 0.5, 0.0, 0.5,
|
0.0, 0.5, 0.0, 0.5,
|
||||||
|
@ -142,8 +152,6 @@ void SceneDemo::render() {
|
||||||
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
|
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
|
||||||
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.4f, 0.4f, 0.4f));
|
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.4f, 0.4f, 0.4f));
|
||||||
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
|
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
|
||||||
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specCol.r, specCol.g, specCol.b));
|
|
||||||
mPhongShader.setFloatUniform("shininess", 60.0f);
|
|
||||||
mPhongShader.setIntUniform("shadowMapSampler", textureUnit);
|
mPhongShader.setIntUniform("shadowMapSampler", textureUnit);
|
||||||
mPhongShader.setIntUniform("isShadowEnabled", mIsShadowMappingEnabled);
|
mPhongShader.setIntUniform("isShadowEnabled", mIsShadowMappingEnabled);
|
||||||
|
|
||||||
|
@ -186,7 +194,6 @@ void SceneDemo::createShadowMapFBOAndTexture() {
|
||||||
mIsShadowMappingInitialized = true;
|
mIsShadowMappingInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : Delete this
|
|
||||||
void SceneDemo::createQuadVBO() {
|
void SceneDemo::createQuadVBO() {
|
||||||
|
|
||||||
mVAOQuad.create();
|
mVAOQuad.create();
|
||||||
|
@ -217,10 +224,12 @@ void SceneDemo::drawTextureQuad() {
|
||||||
// Clear previous frame values
|
// Clear previous frame values
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
const GLuint textureUnit = 0;
|
||||||
|
|
||||||
mVAOQuad.bind();
|
mVAOQuad.bind();
|
||||||
mQuadShader.bind();
|
mQuadShader.bind();
|
||||||
mShadowMapTexture.bind();
|
mShadowMapTexture.bind();
|
||||||
mQuadShader.setIntUniform("textureSampler", mShadowMapTexture.getID());
|
mQuadShader.setIntUniform("textureSampler", textureUnit);
|
||||||
mVBOQuad.bind();
|
mVBOQuad.bind();
|
||||||
|
|
||||||
GLint vertexPositionLoc = mQuadShader.getAttribLocation("vertexPosition");
|
GLint vertexPositionLoc = mQuadShader.getAttribLocation("vertexPosition");
|
||||||
|
|
|
@ -30,8 +30,8 @@
|
||||||
#include "Scene.h"
|
#include "Scene.h"
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const int SHADOWMAP_WIDTH = 1024;
|
const int SHADOWMAP_WIDTH = 2048;
|
||||||
const int SHADOWMAP_HEIGHT = 1024;
|
const int SHADOWMAP_HEIGHT = 2048;
|
||||||
|
|
||||||
// Class SceneDemo
|
// Class SceneDemo
|
||||||
// Abstract class that represents a 3D scene for the ReactPhysics3D examples.
|
// Abstract class that represents a 3D scene for the ReactPhysics3D examples.
|
||||||
|
@ -76,6 +76,14 @@ class SceneDemo : public Scene {
|
||||||
|
|
||||||
openglframework::VertexBufferObject mVBOQuad;
|
openglframework::VertexBufferObject mVBOQuad;
|
||||||
|
|
||||||
|
static openglframework::Color mGreyColorDemo;
|
||||||
|
static openglframework::Color mYellowColorDemo;
|
||||||
|
static openglframework::Color mBlueColorDemo;
|
||||||
|
static openglframework::Color mOrangeColorDemo;
|
||||||
|
static openglframework::Color mPinkColorDemo;
|
||||||
|
static openglframework::Color mRedColorDemo;
|
||||||
|
static openglframework::Color mDemoColors[];
|
||||||
|
static int mNbDemoColors;
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user