Continue to implement the examples using the openg-framework
This commit is contained in:
parent
a1bd7f7be9
commit
91908c1bbc
0
documentation/API/ReactPhysics3DLogo.png
Executable file → Normal file
0
documentation/API/ReactPhysics3DLogo.png
Executable file → Normal file
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
0
documentation/UserManual/ReactPhysics3D-UserManual.tex
Executable file → Normal file
0
documentation/UserManual/ReactPhysics3D-UserManual.tex
Executable file → Normal file
0
documentation/UserManual/images/ReactPhysics3DLogo.png
Executable file → Normal file
0
documentation/UserManual/images/ReactPhysics3DLogo.png
Executable file → Normal file
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
0
documentation/UserManual/title.tex
Executable file → Normal file
0
documentation/UserManual/title.tex
Executable file → Normal file
|
@ -1,5 +1,5 @@
|
||||||
# Minimum cmake version required
|
# Minimum cmake version required
|
||||||
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
|
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
|
||||||
|
|
||||||
add_subdirectory(opengl-framework/)
|
add_subdirectory(common/)
|
||||||
add_subdirectory(fallingcubes/)
|
add_subdirectory(fallingcubes/)
|
180
examples/common/Box.cpp
Normal file
180
examples/common/Box.cpp
Normal file
|
@ -0,0 +1,180 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||||
|
* Copyright (c) 2010-2013 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 "Box.h"
|
||||||
|
|
||||||
|
// Macros
|
||||||
|
#define MEMBER_OFFSET(s,m) ((char *)NULL + (offsetof(s,m)))
|
||||||
|
|
||||||
|
// Initialize static variables
|
||||||
|
openglframework::VertexBufferObject Box::mVBOVertices(GL_ARRAY_BUFFER);
|
||||||
|
openglframework::VertexBufferObject Box::mVBOIndices(GL_ELEMENT_ARRAY_BUFFER);
|
||||||
|
bool Box::areVBOsCreated = false;
|
||||||
|
VertexData Box::mCubeVertices[8] = {
|
||||||
|
{openglframework::Vector3(1,1,1),openglframework::Vector3(1,1,1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(-1,1,1),openglframework::Vector3(-1,1,1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(-1,-1,1),openglframework::Vector3(-1,-1,1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(1,-1,1),openglframework::Vector3(1,-1,1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(1,-1,-1),openglframework::Vector3(1,-1,-1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(-1,-1,-1),openglframework::Vector3(-1,-1,-1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(-1,1,-1),openglframework::Vector3(-1,1,-1),openglframework::Color(0,0,1,1)},
|
||||||
|
{openglframework::Vector3(1,1,-1),openglframework::Vector3(1,1,-1),openglframework::Color(0,0,1,1)}
|
||||||
|
};
|
||||||
|
GLuint Box::mCubeIndices[36] = { 0, 1, 2,
|
||||||
|
2, 3, 0,
|
||||||
|
7, 4, 5,
|
||||||
|
5, 6, 7,
|
||||||
|
6, 5, 2,
|
||||||
|
2, 1, 6,
|
||||||
|
7, 0, 3,
|
||||||
|
3, 4, 7,
|
||||||
|
7, 6, 1,
|
||||||
|
1, 0, 7,
|
||||||
|
3, 2, 5,
|
||||||
|
5, 4, 3};
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position,
|
||||||
|
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld)
|
||||||
|
: openglframework::Object3D() {
|
||||||
|
|
||||||
|
// Initialize the size of the box
|
||||||
|
mSize[0] = size.x * 0.5f;
|
||||||
|
mSize[1] = size.y * 0.5f;
|
||||||
|
mSize[2] = size.z * 0.5f;
|
||||||
|
|
||||||
|
// Compute the scaling matrix
|
||||||
|
mScalingMatrix = openglframework::Matrix4(mSize[0], 0, 0, 0,
|
||||||
|
0, mSize[1], 0, 0,
|
||||||
|
0, 0, mSize[2], 0,
|
||||||
|
0, 0, 0, 1);
|
||||||
|
|
||||||
|
// Initialize the position where the cube will be rendered
|
||||||
|
translateWorld(position);
|
||||||
|
|
||||||
|
// Create the collision shape for the rigid body (box shape)
|
||||||
|
mCollisionShape = new rp3d::BoxShape(rp3d::Vector3(mSize[0], mSize[1], mSize[2]));
|
||||||
|
|
||||||
|
// Compute the inertia tensor of the body using its collision shape
|
||||||
|
rp3d::Matrix3x3 inertiaTensor;
|
||||||
|
mCollisionShape->computeLocalInertiaTensor(inertiaTensor, mass);
|
||||||
|
|
||||||
|
// Initial position and orientation of the rigid body
|
||||||
|
rp3d::Vector3 initPosition(position.x, position.y, position.z);
|
||||||
|
rp3d::Quaternion initOrientation = rp3d::Quaternion::identity();
|
||||||
|
rp3d::Transform transform(initPosition, initOrientation);
|
||||||
|
|
||||||
|
// Create a rigid body corresponding to the cube in the dynamics world
|
||||||
|
mRigidBody = dynamicsWorld->createRigidBody(transform, mass, inertiaTensor, mCollisionShape);
|
||||||
|
|
||||||
|
// If the Vertex Buffer object has not been created yet
|
||||||
|
if (!areVBOsCreated) {
|
||||||
|
// Create the Vertex Buffer
|
||||||
|
createVBO();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Box::~Box() {
|
||||||
|
|
||||||
|
// Destroy the collision shape
|
||||||
|
delete mCollisionShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the cube at the correct position and with the correct orientation
|
||||||
|
void Box::render(openglframework::Shader& shader) {
|
||||||
|
|
||||||
|
// Bind the shader
|
||||||
|
shader.bind();
|
||||||
|
|
||||||
|
// Set the model to World matrix
|
||||||
|
shader.setMatrix4x4Uniform("modelToWorldMatrix", mTransformMatrix);
|
||||||
|
|
||||||
|
// Bind the vertices VBO
|
||||||
|
mVBOVertices.bind();
|
||||||
|
|
||||||
|
// Enable the vertex, normal and color arrays
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
glEnableClientState(GL_NORMAL_ARRAY);
|
||||||
|
|
||||||
|
// Set the arrays pointers
|
||||||
|
glVertexPointer(3, GL_FLOAT, sizeof(VertexData), MEMBER_OFFSET(VertexData, position));
|
||||||
|
glNormalPointer(GL_FLOAT, sizeof(VertexData), MEMBER_OFFSET(VertexData, normal));
|
||||||
|
glColorPointer(3, GL_FLOAT, sizeof(VertexData), MEMBER_OFFSET(VertexData, color));
|
||||||
|
|
||||||
|
// Bind the indices VBO
|
||||||
|
mVBOIndices.bind();
|
||||||
|
|
||||||
|
// Draw the geometry of the box
|
||||||
|
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (char*)NULL);
|
||||||
|
|
||||||
|
// Unbind the VBOs
|
||||||
|
mVBOVertices.unbind();
|
||||||
|
mVBOIndices.unbind();
|
||||||
|
|
||||||
|
// Disable the arrays
|
||||||
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
glDisableClientState(GL_NORMAL_ARRAY);
|
||||||
|
|
||||||
|
// Unbind the shader
|
||||||
|
shader.unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the transform matrix of the box
|
||||||
|
void Box::updateTransform() {
|
||||||
|
|
||||||
|
// Get the interpolated transform of the rigid body
|
||||||
|
rp3d::Transform transform = mRigidBody->getInterpolatedTransform();
|
||||||
|
|
||||||
|
// Compute the transform used for rendering the box
|
||||||
|
float matrix[16];
|
||||||
|
transform.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.
|
||||||
|
/// We create two VBOs (one for vertices and one for indices) to render all the boxes
|
||||||
|
/// in the simulation.
|
||||||
|
void Box::createVBO() {
|
||||||
|
|
||||||
|
// Create the VBOs
|
||||||
|
mVBOVertices.create();
|
||||||
|
mVBOIndices.create();
|
||||||
|
|
||||||
|
// Copy the data into the VBOs
|
||||||
|
mVBOVertices.copyDataIntoVBO(sizeof(mCubeVertices), mCubeVertices, GL_STATIC_DRAW);
|
||||||
|
mVBOIndices.copyDataIntoVBO(sizeof(mCubeIndices), mCubeIndices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
areVBOsCreated = true;
|
||||||
|
}
|
111
examples/common/Box.h
Normal file
111
examples/common/Box.h
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||||
|
* Copyright (c) 2010-2013 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 BOX_H
|
||||||
|
#define BOX_H
|
||||||
|
|
||||||
|
// Libraries
|
||||||
|
#include "openglframework.h"
|
||||||
|
#include "reactphysics3d.h"
|
||||||
|
|
||||||
|
// Structure VertexData
|
||||||
|
struct VertexData {
|
||||||
|
|
||||||
|
/// Vertex position
|
||||||
|
openglframework::Vector3 position;
|
||||||
|
|
||||||
|
/// Vertex normal
|
||||||
|
openglframework::Vector3 normal;
|
||||||
|
|
||||||
|
// Vertex color
|
||||||
|
openglframework::Color color;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Class Box
|
||||||
|
class Box : public openglframework::Object3D {
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
|
/// Size of each side of the box
|
||||||
|
float mSize[3];
|
||||||
|
|
||||||
|
/// Rigid body used to simulate the dynamics of the box
|
||||||
|
rp3d::RigidBody* mRigidBody;
|
||||||
|
|
||||||
|
/// Collision shape of the rigid body
|
||||||
|
rp3d::BoxShape* mCollisionShape;
|
||||||
|
|
||||||
|
/// Scaling matrix (applied to a cube to obtain the correct box dimensions)
|
||||||
|
openglframework::Matrix4 mScalingMatrix;
|
||||||
|
|
||||||
|
/// Vertex Buffer Object for the vertices data used to render the box with OpenGL
|
||||||
|
static openglframework::VertexBufferObject mVBOVertices;
|
||||||
|
|
||||||
|
/// Vertex Buffer Object for the indices used to render the box with OpenGL
|
||||||
|
static openglframework::VertexBufferObject mVBOIndices;
|
||||||
|
|
||||||
|
/// Vertex data for each vertex of the cube (used to render the box)
|
||||||
|
static VertexData mCubeVertices[8];
|
||||||
|
|
||||||
|
/// Indices of the cube (used to render the box)
|
||||||
|
static GLuint mCubeIndices[36];
|
||||||
|
|
||||||
|
/// True if the VBOs have already been created
|
||||||
|
static bool areVBOsCreated;
|
||||||
|
|
||||||
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
|
/// Create a Vertex Buffer Object to render to box with OpenGL
|
||||||
|
static void createVBO();
|
||||||
|
|
||||||
|
public :
|
||||||
|
|
||||||
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
|
/// Constructor
|
||||||
|
Box(const openglframework::Vector3& size, const openglframework::Vector3& position,
|
||||||
|
float mass, rp3d::DynamicsWorld* dynamicsWorld);
|
||||||
|
|
||||||
|
/// Destructor
|
||||||
|
~Box();
|
||||||
|
|
||||||
|
/// Return a pointer to the rigid body of the box
|
||||||
|
rp3d::RigidBody* getRigidBody();
|
||||||
|
|
||||||
|
/// Update the transform matrix of the box
|
||||||
|
void updateTransform();
|
||||||
|
|
||||||
|
/// Render the cube at the correct position and with the correct orientation
|
||||||
|
void render(openglframework::Shader& shader);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return a pointer to the rigid body of the box
|
||||||
|
inline rp3d::RigidBody* Box::getRigidBody() {
|
||||||
|
return mRigidBody;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
4
examples/common/CMakeLists.txt
Normal file
4
examples/common/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# Minimum cmake version required
|
||||||
|
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
|
||||||
|
|
||||||
|
add_subdirectory(opengl-framework/)
|
83
examples/common/Viewer.cpp
Normal file
83
examples/common/Viewer.cpp
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||||
|
* Copyright (c) 2010-2013 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 "Viewer.h"
|
||||||
|
#include "openglframework.h"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Viewer::Viewer() : openglframework::GlutViewer(), fps(0), nbFrames(0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the FPS
|
||||||
|
void Viewer::computeFPS() {
|
||||||
|
|
||||||
|
nbFrames++;
|
||||||
|
|
||||||
|
// Get the number of milliseconds since glutInit called
|
||||||
|
currentTime = glutGet(GLUT_ELAPSED_TIME);
|
||||||
|
|
||||||
|
// Calculate time passed
|
||||||
|
int timeInterval = currentTime - previousTime;
|
||||||
|
|
||||||
|
// Update the FPS counter each second
|
||||||
|
if(timeInterval > 1000){
|
||||||
|
|
||||||
|
// calculate the number of frames per second
|
||||||
|
fps = nbFrames / (timeInterval / 1000.0f);
|
||||||
|
|
||||||
|
// Set time
|
||||||
|
previousTime = currentTime;
|
||||||
|
|
||||||
|
// Reset frame count
|
||||||
|
nbFrames = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display the GUI
|
||||||
|
void Viewer::displayGUI() {
|
||||||
|
|
||||||
|
// Display the FPS
|
||||||
|
displayFPS();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display the FPS
|
||||||
|
void Viewer::displayFPS() {
|
||||||
|
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(0, mCamera.getWidth(), mCamera.getHeight(), 0, -1, 1);
|
||||||
|
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glRasterPos2i(10, 20);
|
||||||
|
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "FPS : " << fps;
|
||||||
|
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)ss.str().c_str());
|
||||||
|
}
|
71
examples/common/Viewer.h
Normal file
71
examples/common/Viewer.h
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||||
|
* Copyright (c) 2010-2013 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 VIEWER_H
|
||||||
|
#define VIEWER_H
|
||||||
|
|
||||||
|
// Libraries
|
||||||
|
#include "openglframework.h"
|
||||||
|
|
||||||
|
// Class Viewer
|
||||||
|
class Viewer : public openglframework::GlutViewer {
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
|
/// Current number of frames per seconds
|
||||||
|
int fps;
|
||||||
|
|
||||||
|
/// Number of frames during the last second
|
||||||
|
int nbFrames;
|
||||||
|
|
||||||
|
/// Current time for fps computation
|
||||||
|
int currentTime;
|
||||||
|
|
||||||
|
/// Previous time for fps computation
|
||||||
|
int previousTime;
|
||||||
|
|
||||||
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
|
/// Display the FPS
|
||||||
|
void displayFPS();
|
||||||
|
|
||||||
|
public :
|
||||||
|
|
||||||
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
|
/// Constructor
|
||||||
|
Viewer();
|
||||||
|
|
||||||
|
/// Compute the FPS
|
||||||
|
void computeFPS();
|
||||||
|
|
||||||
|
/// Display the GUI
|
||||||
|
void displayGUI();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -168,6 +168,16 @@ void GlutViewer::mouseMotionEvent(int xMouse, int yMouse) {
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Called when a GLUT keyboard event occurs
|
||||||
|
void GlutViewer::keyboardEvent(int key, int xMouse, int yMouse) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called when a GLUT special keyboard event occurs
|
||||||
|
void GlutViewer::keyboardSpecialEvent(int key, int xMouse, int yMouse) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Map the mouse x,y coordinates to a point on a sphere
|
// Map the mouse x,y coordinates to a point on a sphere
|
||||||
bool GlutViewer::mapMouseCoordinatesToSphere(int xMouse, int yMouse, Vector3& spherePoint) const {
|
bool GlutViewer::mapMouseCoordinatesToSphere(int xMouse, int yMouse, Vector3& spherePoint) const {
|
||||||
|
|
|
@ -39,7 +39,7 @@ namespace openglframework {
|
||||||
// Class Renderer
|
// Class Renderer
|
||||||
class GlutViewer {
|
class GlutViewer {
|
||||||
|
|
||||||
private:
|
protected :
|
||||||
|
|
||||||
// -------------------- Attributes -------------------- //
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ class GlutViewer {
|
||||||
GlutViewer();
|
GlutViewer();
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~GlutViewer();
|
virtual ~GlutViewer();
|
||||||
|
|
||||||
// Initialize the viewer
|
// Initialize the viewer
|
||||||
bool init(int argc, char** argv, const std::string& windowsTitle,
|
bool init(int argc, char** argv, const std::string& windowsTitle,
|
||||||
|
@ -112,16 +112,17 @@ class GlutViewer {
|
||||||
// Get the camera
|
// Get the camera
|
||||||
Camera& getCamera();
|
Camera& getCamera();
|
||||||
|
|
||||||
void motion(int x, int y);
|
|
||||||
|
|
||||||
// Called when a GLUT mouse button event occurs
|
// Called when a GLUT mouse button event occurs
|
||||||
void mouseButtonEvent(int button, int state, int x, int y);
|
void mouseButtonEvent(int button, int state, int x, int y);
|
||||||
|
|
||||||
// Called when a GLUT mouse motion event occurs
|
// Called when a GLUT mouse motion event occurs
|
||||||
void mouseMotionEvent(int xMouse, int yMouse);
|
void mouseMotionEvent(int xMouse, int yMouse);
|
||||||
|
|
||||||
void keyboard(int key, int x, int y);
|
// Called when a GLUT keyboard event occurs
|
||||||
void special(int key, int x, int y);
|
void keyboardEvent(int key, int xMouse, int yMouse);
|
||||||
|
|
||||||
|
// Called when a GLUT special keyboard event occurs
|
||||||
|
void keyboardSpecialEvent(int key, int xMouse, int yMouse);
|
||||||
|
|
||||||
// Check the OpenGL errors
|
// Check the OpenGL errors
|
||||||
static void checkOpenGLErrors();
|
static void checkOpenGLErrors();
|
|
@ -5,13 +5,13 @@ cmake_minimum_required(VERSION 2.6)
|
||||||
PROJECT(FallingCubes)
|
PROJECT(FallingCubes)
|
||||||
|
|
||||||
# Copy the shaders used for the demo into the build directory
|
# Copy the shaders used for the demo into the build directory
|
||||||
FILE(COPY "../opengl-framework/src/shaders/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/shaders/")
|
FILE(COPY "../common/opengl-framework/src/shaders/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/shaders/")
|
||||||
|
|
||||||
# Headers
|
# Headers
|
||||||
INCLUDE_DIRECTORIES("../opengl-framework/src/")
|
INCLUDE_DIRECTORIES("../common/opengl-framework/src/" "../common/")
|
||||||
|
|
||||||
# Create the example executable using the
|
# Create the example executable using the
|
||||||
# compiled reactphysics3d static library
|
# compiled reactphysics3d static library
|
||||||
ADD_EXECUTABLE(fallingcubes main.cpp Scene.cpp Scene.h Box.cpp Box.h)
|
ADD_EXECUTABLE(fallingcubes FallingCubes.cpp Scene.cpp Scene.h "../common/Box.cpp" "../common/Box.h" "../common/Viewer.cpp" "../common/Viewer.h")
|
||||||
|
|
||||||
TARGET_LINK_LIBRARIES(fallingcubes reactphysics3d openglframework)
|
TARGET_LINK_LIBRARIES(fallingcubes reactphysics3d openglframework)
|
||||||
|
|
|
@ -25,40 +25,32 @@
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "Scene.h"
|
#include "Scene.h"
|
||||||
#include <sstream>
|
#include "Viewer.h"
|
||||||
|
|
||||||
// Declarations
|
// Declarations
|
||||||
void simulate();
|
void simulate();
|
||||||
void display();
|
void display();
|
||||||
void displayFPS();
|
void finish();
|
||||||
void computeFPS();
|
|
||||||
void reshape(int width, int height);
|
void reshape(int width, int height);
|
||||||
void mouseButton(int button, int state, int x, int y);
|
void mouseButton(int button, int state, int x, int y);
|
||||||
void mouseMotion(int x, int y);
|
void mouseMotion(int x, int y);
|
||||||
void keyboardSpecial(int key, int x, int y);
|
void keyboard(unsigned char key, int x, int y);
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
// Namespaces
|
// Namespaces
|
||||||
using namespace openglframework;
|
using namespace openglframework;
|
||||||
|
|
||||||
// Global variables
|
// Global variables
|
||||||
GlutViewer* viewer;
|
Viewer* viewer;
|
||||||
Scene* scene;
|
Scene* scene;
|
||||||
int fps;
|
|
||||||
int nbFrames;
|
|
||||||
int currentTime;
|
|
||||||
int previousTime;
|
|
||||||
int width, height;
|
|
||||||
|
|
||||||
// Main function
|
// Main function
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
// Create and initialize the Viewer
|
// Create and initialize the Viewer
|
||||||
viewer = new GlutViewer();
|
viewer = new Viewer();
|
||||||
Vector2 windowsSize = Vector2(800, 600);
|
Vector2 windowsSize = Vector2(800, 600);
|
||||||
Vector2 windowsPosition = Vector2(100, 100);
|
Vector2 windowsPosition = Vector2(100, 100);
|
||||||
width = windowsSize.x;
|
|
||||||
height = windowsSize.y;
|
|
||||||
bool initOK = viewer->init(argc, argv, "ReactPhysics3D Examples - Falling Cubes", windowsSize, windowsPosition);
|
bool initOK = viewer->init(argc, argv, "ReactPhysics3D Examples - Falling Cubes", windowsSize, windowsPosition);
|
||||||
if (!initOK) return 1;
|
if (!initOK) return 1;
|
||||||
|
|
||||||
|
@ -67,21 +59,18 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
nbFrames = 0;
|
|
||||||
|
|
||||||
// Glut Idle function that is continuously called
|
// Glut Idle function that is continuously called
|
||||||
glutIdleFunc(simulate);
|
glutIdleFunc(simulate);
|
||||||
glutDisplayFunc(display);
|
glutDisplayFunc(display);
|
||||||
glutReshapeFunc(reshape);
|
glutReshapeFunc(reshape);
|
||||||
glutMouseFunc(mouseButton);
|
glutMouseFunc(mouseButton);
|
||||||
glutMotionFunc(mouseMotion);
|
glutMotionFunc(mouseMotion);
|
||||||
glutSpecialFunc(keyboardSpecial);
|
glutKeyboardFunc(keyboard);
|
||||||
|
|
||||||
// Glut main looop
|
// Glut main looop
|
||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
|
|
||||||
delete viewer;
|
finish();
|
||||||
delete scene;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +81,7 @@ void simulate() {
|
||||||
// Physics simulation
|
// Physics simulation
|
||||||
scene->simulate();
|
scene->simulate();
|
||||||
|
|
||||||
computeFPS();
|
viewer->computeFPS();
|
||||||
|
|
||||||
// Ask GLUT to render the scene
|
// Ask GLUT to render the scene
|
||||||
glutPostRedisplay ();
|
glutPostRedisplay ();
|
||||||
|
@ -108,8 +97,6 @@ void init() {
|
||||||
// Reshape function
|
// Reshape function
|
||||||
void reshape(int newWidth, int newHeight) {
|
void reshape(int newWidth, int newHeight) {
|
||||||
viewer->reshape(newWidth, newHeight);
|
viewer->reshape(newWidth, newHeight);
|
||||||
width = newWidth;
|
|
||||||
height = newHeight;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called when a mouse button event occurs
|
// Called when a mouse button event occurs
|
||||||
|
@ -123,12 +110,28 @@ void mouseMotion(int x, int y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called when the user hits a special key on the keyboard
|
// Called when the user hits a special key on the keyboard
|
||||||
void keyboardSpecial(int key, int x, int y) {
|
void keyboard(unsigned char key, int x, int y) {
|
||||||
/*
|
switch(key) {
|
||||||
if(key=='0')
|
|
||||||
|
// Escape key
|
||||||
|
case 27:
|
||||||
|
finish();
|
||||||
exit(0);
|
exit(0);
|
||||||
if(key== GLUT_KEY_RIGHT) {
|
break;
|
||||||
*/
|
|
||||||
|
// Space bar
|
||||||
|
case 32:
|
||||||
|
scene->pauseContinueSimulation();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of the application
|
||||||
|
void finish() {
|
||||||
|
|
||||||
|
// Destroy the viewer and the scene
|
||||||
|
delete viewer;
|
||||||
|
delete scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display the scene
|
// Display the scene
|
||||||
|
@ -138,7 +141,7 @@ void display() {
|
||||||
scene->render();
|
scene->render();
|
||||||
|
|
||||||
// Display the FPS
|
// Display the FPS
|
||||||
displayFPS();
|
viewer->displayGUI();
|
||||||
|
|
||||||
// Swap the buffers
|
// Swap the buffers
|
||||||
glutSwapBuffers();
|
glutSwapBuffers();
|
||||||
|
@ -147,43 +150,4 @@ void display() {
|
||||||
GlutViewer::checkOpenGLErrors();
|
GlutViewer::checkOpenGLErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute the FPS
|
|
||||||
void computeFPS() {
|
|
||||||
nbFrames++;
|
|
||||||
|
|
||||||
// Get the number of milliseconds since glutInit called
|
|
||||||
currentTime = glutGet(GLUT_ELAPSED_TIME);
|
|
||||||
|
|
||||||
// Calculate time passed
|
|
||||||
int timeInterval = currentTime - previousTime;
|
|
||||||
|
|
||||||
// Update the FPS counter each second
|
|
||||||
if(timeInterval > 1000){
|
|
||||||
|
|
||||||
// calculate the number of frames per second
|
|
||||||
fps = nbFrames / (timeInterval / 1000.0f);
|
|
||||||
|
|
||||||
// Set time
|
|
||||||
previousTime = currentTime;
|
|
||||||
|
|
||||||
// Reset frame count
|
|
||||||
nbFrames = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display the FPS
|
|
||||||
void displayFPS() {
|
|
||||||
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
|
||||||
glLoadIdentity();
|
|
||||||
glOrtho(0, width, height, 0, -1, 1);
|
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
|
||||||
glLoadIdentity();
|
|
||||||
|
|
||||||
glRasterPos2i(10, 20);
|
|
||||||
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "FPS : " << fps;
|
|
||||||
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)ss.str().c_str());
|
|
||||||
}
|
|
|
@ -32,32 +32,40 @@ using namespace openglframework;
|
||||||
// Constructor
|
// Constructor
|
||||||
Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
|
Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
|
||||||
mPhongShader("shaders/phong.vert",
|
mPhongShader("shaders/phong.vert",
|
||||||
"shaders/phong.frag"){
|
"shaders/phong.frag"), mIsRunning(false) {
|
||||||
|
|
||||||
// Move the light 0
|
// Move the light 0
|
||||||
mLight0.translateWorld(Vector3(7, 15, 15));
|
mLight0.translateWorld(Vector3(7, 15, 15));
|
||||||
|
|
||||||
// Compute the radius and the center of the scene
|
// Compute the radius and the center of the scene
|
||||||
float radius = 10.0f;
|
float radiusScene = 10.0f;
|
||||||
openglframework::Vector3 center(0, 5, 0);
|
openglframework::Vector3 center(0, 5, 0);
|
||||||
|
|
||||||
// Set the center of the scene
|
// Set the center of the scene
|
||||||
mViewer->setScenePosition(center, radius);
|
mViewer->setScenePosition(center, radiusScene);
|
||||||
|
|
||||||
// Gravity vector in the dynamics world
|
// Gravity vector in the dynamics world
|
||||||
rp3d::Vector3 gravity(0, -9.81, 0);
|
rp3d::Vector3 gravity(0, -9.81, 0);
|
||||||
|
|
||||||
// Time step for the physics simulation
|
// Time step for the physics simulation
|
||||||
rp3d::decimal timeStep = 1.0f / 80.0f;
|
rp3d::decimal timeStep = 1.0f / 60.0f;
|
||||||
|
|
||||||
// Create the dynamics world for the physics simulation
|
// Create the dynamics world for the physics simulation
|
||||||
mDynamicsWorld = new rp3d::DynamicsWorld(gravity, timeStep);
|
mDynamicsWorld = new rp3d::DynamicsWorld(gravity, timeStep);
|
||||||
|
|
||||||
|
// Set the number of iterations of the constraint solver
|
||||||
|
mDynamicsWorld->setNbIterationsSolver(15);
|
||||||
|
|
||||||
|
float radius = 2.0f;
|
||||||
|
|
||||||
// Create all the cubes of the scene
|
// Create all the cubes of the scene
|
||||||
for (int i=0; i<NB_BOXES; i++) {
|
for (int i=0; i<NB_BOXES; i++) {
|
||||||
|
|
||||||
// Position of the cubes
|
// Position of the cubes
|
||||||
openglframework::Vector3 position(0, 5 + i * (BOX_SIZE.y + 0.5f), 0);
|
float angle = i * 30.0f;
|
||||||
|
openglframework::Vector3 position(radius * cos(angle),
|
||||||
|
1 + i * (BOX_SIZE.y + 0.3f),
|
||||||
|
radius * sin(angle));
|
||||||
|
|
||||||
// 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);
|
||||||
|
@ -83,14 +91,14 @@ Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
|
||||||
mFloor->getRigidBody()->setRestitution(0.3);
|
mFloor->getRigidBody()->setRestitution(0.3);
|
||||||
|
|
||||||
// Start the simulation
|
// Start the simulation
|
||||||
mDynamicsWorld->start();
|
startSimulation();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
Scene::~Scene() {
|
Scene::~Scene() {
|
||||||
|
|
||||||
// Stop the physics simulation
|
// Stop the physics simulation
|
||||||
mDynamicsWorld->stop();
|
stopSimulation();
|
||||||
|
|
||||||
// Destroy the shader
|
// Destroy the shader
|
||||||
mPhongShader.destroy();
|
mPhongShader.destroy();
|
||||||
|
@ -118,17 +126,22 @@ Scene::~Scene() {
|
||||||
// Take a step for the simulation
|
// Take a step for the simulation
|
||||||
void Scene::simulate() {
|
void Scene::simulate() {
|
||||||
|
|
||||||
// Take a simulation step
|
// If the physics simulation is running
|
||||||
mDynamicsWorld->update();
|
if (mIsRunning) {
|
||||||
|
|
||||||
// Update the position and orientation of the boxes
|
// Take a simulation step
|
||||||
for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) {
|
mDynamicsWorld->update();
|
||||||
|
|
||||||
|
// Update the position and orientation of the boxes
|
||||||
|
for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) {
|
||||||
|
|
||||||
|
// Update the transform used for the rendering
|
||||||
|
(*it)->updateTransform();
|
||||||
|
}
|
||||||
|
|
||||||
|
mFloor->updateTransform();
|
||||||
|
|
||||||
// Update the transform used for the rendering
|
|
||||||
(*it)->updateTransform();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mFloor->updateTransform();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the scene
|
// Render the scene
|
||||||
|
@ -136,7 +149,7 @@ void Scene::render() {
|
||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glDisable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
// Bind the shader
|
// Bind the shader
|
||||||
mPhongShader.bind();
|
mPhongShader.bind();
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
#include "Box.h"
|
#include "Box.h"
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const int NB_BOXES = 10; // Number of boxes in the scene
|
const int NB_BOXES = 20; // 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(20, 0.5f, 20); // Floor dimensions in meters
|
const openglframework::Vector3 FLOOR_SIZE(20, 0.5f, 20); // Floor dimensions in meters
|
||||||
const float BOX_MASS = 1.0f; // Box mass in kilograms
|
const float BOX_MASS = 1.0f; // Box mass in kilograms
|
||||||
|
@ -63,6 +63,9 @@ class Scene {
|
||||||
/// Dynamics world used for the physics simulation
|
/// Dynamics world used for the physics simulation
|
||||||
rp3d::DynamicsWorld* mDynamicsWorld;
|
rp3d::DynamicsWorld* mDynamicsWorld;
|
||||||
|
|
||||||
|
/// True if the physics simulation is running
|
||||||
|
bool mIsRunning;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
@ -76,8 +79,39 @@ class Scene {
|
||||||
/// Take a step for the simulation
|
/// Take a step for the simulation
|
||||||
void simulate();
|
void simulate();
|
||||||
|
|
||||||
|
/// Stop the simulation
|
||||||
|
void stopSimulation();
|
||||||
|
|
||||||
|
/// Start the simulation
|
||||||
|
void startSimulation();
|
||||||
|
|
||||||
|
/// Pause or continue simulation
|
||||||
|
void pauseContinueSimulation();
|
||||||
|
|
||||||
/// Render the scene
|
/// Render the scene
|
||||||
void render();
|
void render();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Stop the simulation
|
||||||
|
inline void Scene::stopSimulation() {
|
||||||
|
mDynamicsWorld->stop();
|
||||||
|
mIsRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the simulation
|
||||||
|
inline void Scene::startSimulation() {
|
||||||
|
mDynamicsWorld->start();
|
||||||
|
mIsRunning = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pause or continue simulation
|
||||||
|
inline void Scene::pauseContinueSimulation() {
|
||||||
|
if (mIsRunning) {
|
||||||
|
stopSimulation();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
startSimulation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -215,7 +215,6 @@ inline void DynamicsWorld::start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void DynamicsWorld::stop() {
|
inline void DynamicsWorld::stop() {
|
||||||
std::cout << "Stop Simulation" << std::endl;
|
|
||||||
mTimer.stop();
|
mTimer.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user