Modifications in concave shape scene in testbed application

This commit is contained in:
Daniel Chappuis 2016-04-07 08:39:17 +02:00
parent 4a5c57c37e
commit 357e0792ea
12 changed files with 25896 additions and 50 deletions

View File

@ -29,13 +29,13 @@
// Constructor // Constructor
ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position, ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath) const std::string& meshPath)
: 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) { mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
// Load the mesh from a file // Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "concavemesh.obj", *this); openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh // Calculate the normals of the mesh
calculateNormals(); calculateNormals();
@ -86,13 +86,13 @@ ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position,
// Constructor // Constructor
ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position, float mass, ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position, float mass,
reactphysics3d::DynamicsWorld* dynamicsWorld, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath) const std::string& meshPath)
: 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) { mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
// Load the mesh from a file // Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "concavemesh.obj", *this); openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh // Calculate the normals of the mesh
calculateNormals(); calculateNormals();

View File

@ -77,11 +77,11 @@ class ConcaveMesh : public openglframework::Mesh, public PhysicsObject {
/// Constructor /// Constructor
ConcaveMesh(const openglframework::Vector3& position, ConcaveMesh(const openglframework::Vector3& position,
rp3d::CollisionWorld* world, const std::string& meshFolderPath); rp3d::CollisionWorld* world, const std::string& meshPath);
/// Constructor /// Constructor
ConcaveMesh(const openglframework::Vector3& position, float mass, ConcaveMesh(const openglframework::Vector3& position, float mass,
rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath); rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshPath);
/// Destructor /// Destructor
~ConcaveMesh(); ~ConcaveMesh();

View File

@ -29,13 +29,13 @@
// Constructor // Constructor
ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath) const std::string& meshPath)
: 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) { 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(meshPath, *this);
// Calculate the normals of the mesh // Calculate the normals of the mesh
calculateNormals(); calculateNormals();
@ -80,13 +80,13 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
// Constructor // Constructor
ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass, ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass,
reactphysics3d::DynamicsWorld* dynamicsWorld, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath) const std::string& meshPath)
: 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) { 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(meshPath, *this);
// Calculate the normals of the mesh // Calculate the normals of the mesh
calculateNormals(); calculateNormals();

View File

@ -76,11 +76,11 @@ class ConvexMesh : public openglframework::Mesh, public PhysicsObject {
/// Constructor /// Constructor
ConvexMesh(const openglframework::Vector3& position, ConvexMesh(const openglframework::Vector3& position,
rp3d::CollisionWorld* world, const std::string& meshFolderPath); rp3d::CollisionWorld* world, const std::string& meshPath);
/// Constructor /// Constructor
ConvexMesh(const openglframework::Vector3& position, float mass, ConvexMesh(const openglframework::Vector3& position, float mass,
rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath); rp3d::DynamicsWorld* dynamicsWorld, const std::string& meshPath);
/// Destructor /// Destructor
~ConvexMesh(); ~ConvexMesh();

View File

@ -1,4 +1,4 @@
# Blender MTL File: 'concave_terrain.blend' # Blender MTL File: 'None'
# Material Count: 1 # Material Count: 1
newmtl None newmtl None

25823
testbed/meshes/city.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -164,7 +164,7 @@ class Vector3 {
Vector3 normalize() { Vector3 normalize() {
float l = length(); float l = length();
if(l < std::numeric_limits<float>::epsilon() ) { if(l < std::numeric_limits<float>::epsilon() ) {
assert(false); return *this;
} }
x /= l; x /= l;
y /= l; y /= l;

View File

@ -221,7 +221,7 @@ CollisionShapesScene::CollisionShapesScene(const std::string& name)
radius * sin(angle)); radius * sin(angle));
// 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 + "convexmesh.obj");
// Set the box color // Set the box color
mesh->setColor(mDemoColors[i % mNbDemoColors]); mesh->setColor(mDemoColors[i % mNbDemoColors]);

View File

@ -52,21 +52,27 @@ ConcaveMeshScene::ConcaveMeshScene(const std::string& name)
// Set the number of iterations of the constraint solver // Set the number of iterations of the constraint solver
mDynamicsWorld->setNbIterationsVelocitySolver(15); mDynamicsWorld->setNbIterationsVelocitySolver(15);
// ---------- Create the cube ----------- // // ---------- Create the boxes ----------- //
for (int i=0; i<NB_BOXES_X; i++) {
for (int j=0; j<NB_BOXES_Z; j++) {
// Position // Position
openglframework::Vector3 spherePos(15, 10, 0); openglframework::Vector3 boxPosition(-NB_BOXES_X * BOX_SIZE * BOXES_SPACE / 2 + i * BOX_SIZE * BOXES_SPACE, 30, -NB_BOXES_Z * BOX_SIZE * BOXES_SPACE / 2 + j * BOX_SIZE * BOXES_SPACE);
// Create a sphere and a corresponding rigid in the dynamics world // Create a sphere and a corresponding rigid in the dynamics world
mBox = new Box(Vector3(3, 3, 3), spherePos, 80.1, mDynamicsWorld); mBoxes[i * NB_BOXES_Z + j] = new Box(Vector3(BOX_SIZE, BOX_SIZE, BOX_SIZE) * 0.5f, boxPosition, 80.1, mDynamicsWorld);
// Set the sphere color // Set the sphere color
mBox->setColor(mDemoColors[0]); mBoxes[i * NB_BOXES_Z + j]->setColor(mDemoColors[0]);
mBox->setSleepingColor(mRedColorDemo); mBoxes[i * NB_BOXES_Z + j]->setSleepingColor(mRedColorDemo);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& sphereMat = mBox->getRigidBody()->getMaterial(); rp3d::Material& boxMaterial = mBoxes[i * NB_BOXES_Z + j]->getRigidBody()->getMaterial();
sphereMat.setBounciness(rp3d::decimal(0.2)); boxMaterial.setBounciness(rp3d::decimal(0.2));
}
}
// ---------- Create the triangular mesh ---------- // // ---------- Create the triangular mesh ---------- //
@ -75,7 +81,7 @@ ConcaveMeshScene::ConcaveMeshScene(const std::string& name)
rp3d::decimal mass = 1.0; rp3d::decimal mass = 1.0;
// Create a convex mesh and a corresponding rigid in the dynamics world // Create a convex mesh and a corresponding rigid in the dynamics world
mConcaveMesh = new ConcaveMesh(position, mass, mDynamicsWorld, meshFolderPath); mConcaveMesh = new ConcaveMesh(position, mass, mDynamicsWorld, meshFolderPath + "city.obj");
// Set the mesh as beeing static // Set the mesh as beeing static
mConcaveMesh->getRigidBody()->setType(rp3d::STATIC); mConcaveMesh->getRigidBody()->setType(rp3d::STATIC);
@ -104,11 +110,14 @@ ConcaveMeshScene::ConcaveMeshScene(const std::string& name)
// Destructor // Destructor
ConcaveMeshScene::~ConcaveMeshScene() { ConcaveMeshScene::~ConcaveMeshScene() {
mDynamicsWorld->destroyRigidBody(mBox->getRigidBody());
// Destroy the corresponding rigid body from the dynamics world // Destroy the corresponding rigid body from the dynamics world
mDynamicsWorld->destroyRigidBody(mConcaveMesh->getRigidBody()); mDynamicsWorld->destroyRigidBody(mConcaveMesh->getRigidBody());
delete mBox; // Destroy the boxes
for (int i=0; i<NB_BOXES_X * NB_BOXES_Z; i++) {
mDynamicsWorld->destroyRigidBody(mBoxes[i]->getRigidBody());
delete mBoxes[i];
}
// Destroy the convex mesh // Destroy the convex mesh
delete mConcaveMesh; delete mConcaveMesh;
@ -143,7 +152,10 @@ void ConcaveMeshScene::update() {
// Update the transform used for the rendering // Update the transform used for the rendering
mConcaveMesh->updateTransform(mInterpolationFactor); mConcaveMesh->updateTransform(mInterpolationFactor);
mBox->updateTransform(mInterpolationFactor);
for (int i=0; i<NB_BOXES_X * NB_BOXES_Z; i++) {
mBoxes[i]->updateTransform(mInterpolationFactor);
}
} }
// Render the scene in a single pass // Render the scene in a single pass
@ -153,7 +165,10 @@ void ConcaveMeshScene::renderSinglePass(Shader& shader, const openglframework::M
shader.bind(); shader.bind();
mConcaveMesh->render(shader, worldToCameraMatrix); mConcaveMesh->render(shader, worldToCameraMatrix);
mBox->render(shader, worldToCameraMatrix);
for (int i=0; i<NB_BOXES_X * NB_BOXES_Z; i++) {
mBoxes[i]->render(shader, worldToCameraMatrix);
}
// Unbind the shader // Unbind the shader
shader.unbind(); shader.unbind();
@ -163,13 +178,18 @@ void ConcaveMeshScene::renderSinglePass(Shader& shader, const openglframework::M
void ConcaveMeshScene::reset() { void ConcaveMeshScene::reset() {
// Reset the transform // Reset the transform
rp3d::Quaternion initOrientation(0, 0, 3.141/12); rp3d::Transform transform(rp3d::Vector3::zero(), rp3d::Quaternion::identity());
rp3d::Quaternion initOrientation2(0, 0, 3.141/13);
//rp3d::Transform transform(rp3d::Vector3(0, 0, 0), initOrientation);
rp3d::Transform transform(rp3d::Vector3(0, 0, 0), initOrientation2);
mConcaveMesh->resetTransform(transform); mConcaveMesh->resetTransform(transform);
rp3d::Vector3 spherePos(2, 15, 0); for (int i=0; i<NB_BOXES_X; i++) {
rp3d::Transform sphereTransform(spherePos, initOrientation2); for (int j=0; j<NB_BOXES_Z; j++) {
mBox->resetTransform(sphereTransform);
// Position
rp3d::Vector3 boxPosition(-NB_BOXES_X * BOX_SIZE * BOXES_SPACE / 2 + i * BOX_SIZE * BOXES_SPACE, 30, -NB_BOXES_Z * BOX_SIZE * BOXES_SPACE / 2 + j * BOX_SIZE * BOXES_SPACE);
rp3d::Transform boxTransform(boxPosition, rp3d::Quaternion::identity());
mBoxes[i * NB_BOXES_Z + j]->resetTransform(boxTransform);
}
}
} }

View File

@ -38,6 +38,10 @@ namespace trianglemeshscene {
// 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_BOXES_X = 8;
const int NB_BOXES_Z = 8;
const float BOX_SIZE = 3.0f;
const float BOXES_SPACE = 2.0f;
// Class TriangleMeshScene // Class TriangleMeshScene
class ConcaveMeshScene : public SceneDemo { class ConcaveMeshScene : public SceneDemo {
@ -46,7 +50,7 @@ class ConcaveMeshScene : public SceneDemo {
// -------------------- Attributes -------------------- // // -------------------- Attributes -------------------- //
Box* mBox; Box* mBoxes[NB_BOXES_X * NB_BOXES_Z];
/// Concave triangles mesh /// Concave triangles mesh
ConcaveMesh* mConcaveMesh; ConcaveMesh* mConcaveMesh;

View File

@ -57,16 +57,16 @@ HeightFieldScene::HeightFieldScene(const std::string& name) : SceneDemo(name, SC
// Position // Position
openglframework::Vector3 position(15, 10 + 6 * i, 0); openglframework::Vector3 position(15, 10 + 6 * i, 0);
// Create a sphere and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mBoxes[i] = new Box(Vector3(3, 3, 3), position, 80.1, mDynamicsWorld); mBoxes[i] = new Box(Vector3(3, 3, 3), position, 80.1, mDynamicsWorld);
// Set the sphere color // Set the box color
mBoxes[i]->setColor(mDemoColors[2]); mBoxes[i]->setColor(mDemoColors[2]);
mBoxes[i]->setSleepingColor(mRedColorDemo); mBoxes[i]->setSleepingColor(mRedColorDemo);
// Change the material properties of the rigid body // Change the material properties of the rigid body
rp3d::Material& sphereMat = mBoxes[i]->getRigidBody()->getMaterial(); rp3d::Material& boxMaterial = mBoxes[i]->getRigidBody()->getMaterial();
sphereMat.setBounciness(rp3d::decimal(0.2)); boxMaterial.setBounciness(rp3d::decimal(0.2));
} }
// ---------- Create the height field ---------- // // ---------- Create the height field ---------- //
@ -174,15 +174,14 @@ void HeightFieldScene::renderSinglePass(Shader& shader, const openglframework::M
void HeightFieldScene::reset() { void HeightFieldScene::reset() {
// Reset the transform // Reset the transform
rp3d::Quaternion initOrientation = rp3d::Quaternion::identity(); rp3d::Transform transform(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity());
rp3d::Transform transform(rp3d::Vector3(0, 0, 0), initOrientation);
mHeightField->resetTransform(transform); mHeightField->resetTransform(transform);
float heightFieldWidth = 10.0f; float heightFieldWidth = 10.0f;
float stepDist = heightFieldWidth / (NB_BOXES + 1); float stepDist = heightFieldWidth / (NB_BOXES + 1);
for (int i=0; i<NB_BOXES; i++) { for (int i=0; i<NB_BOXES; i++) {
rp3d::Vector3 spherePos(-heightFieldWidth * 0.5f + i * stepDist , 14 + 6.0f * i, -heightFieldWidth * 0.5f + i * stepDist); rp3d::Vector3 boxPosition(-heightFieldWidth * 0.5f + i * stepDist , 14 + 6.0f * i, -heightFieldWidth * 0.5f + i * stepDist);
rp3d::Transform sphereTransform(spherePos, initOrientation); rp3d::Transform boxTransform(boxPosition, rp3d::Quaternion::identity());
mBoxes[i]->resetTransform(sphereTransform); mBoxes[i]->resetTransform(boxTransform);
} }
} }

View File

@ -116,7 +116,7 @@ RaycastScene::RaycastScene(const std::string& name)
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 + "convexmesh.obj");
// Set the color // Set the color
mConvexMesh->setColor(mGreyColorDemo); mConvexMesh->setColor(mGreyColorDemo);
@ -126,7 +126,7 @@ RaycastScene::RaycastScene(const std::string& name)
openglframework::Vector3 position8(0, 0, 0); openglframework::Vector3 position8(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
mConcaveMesh = new ConcaveMesh(position8, mCollisionWorld, mMeshFolderPath); mConcaveMesh = new ConcaveMesh(position8, mCollisionWorld, mMeshFolderPath + "city.obj");
// Set the color // Set the color
mConcaveMesh->setColor(mGreyColorDemo); mConcaveMesh->setColor(mGreyColorDemo);