diff --git a/documentation/API/ReactPhysics3DLogo.png b/documentation/API/ReactPhysics3DLogo.png old mode 100755 new mode 100644 diff --git a/documentation/UserManual/ReactPhysics3D-UserManual.tex b/documentation/UserManual/ReactPhysics3D-UserManual.tex old mode 100755 new mode 100644 diff --git a/documentation/UserManual/images/ReactPhysics3DLogo.png b/documentation/UserManual/images/ReactPhysics3DLogo.png old mode 100755 new mode 100644 diff --git a/documentation/UserManual/title.tex b/documentation/UserManual/title.tex old mode 100755 new mode 100644 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4f0cab67..568479fb 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,5 @@ # Minimum cmake version required CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -add_subdirectory(opengl-framework/) -add_subdirectory(fallingcubes/) \ No newline at end of file +add_subdirectory(common/) +add_subdirectory(fallingcubes/) diff --git a/examples/common/Box.cpp b/examples/common/Box.cpp new file mode 100644 index 00000000..d4169c9a --- /dev/null +++ b/examples/common/Box.cpp @@ -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; +} diff --git a/examples/common/Box.h b/examples/common/Box.h new file mode 100644 index 00000000..d287c3cb --- /dev/null +++ b/examples/common/Box.h @@ -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 diff --git a/examples/common/CMakeLists.txt b/examples/common/CMakeLists.txt new file mode 100644 index 00000000..ba4484e0 --- /dev/null +++ b/examples/common/CMakeLists.txt @@ -0,0 +1,4 @@ +# Minimum cmake version required +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) + +add_subdirectory(opengl-framework/) diff --git a/examples/common/Viewer.cpp b/examples/common/Viewer.cpp new file mode 100644 index 00000000..1f99d4dc --- /dev/null +++ b/examples/common/Viewer.cpp @@ -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()); +} diff --git a/examples/common/Viewer.h b/examples/common/Viewer.h new file mode 100644 index 00000000..572154fa --- /dev/null +++ b/examples/common/Viewer.h @@ -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 diff --git a/examples/opengl-framework/CMakeLists.txt b/examples/common/opengl-framework/CMakeLists.txt similarity index 100% rename from examples/opengl-framework/CMakeLists.txt rename to examples/common/opengl-framework/CMakeLists.txt diff --git a/examples/opengl-framework/freeglut/CMakeLists.txt b/examples/common/opengl-framework/freeglut/CMakeLists.txt similarity index 100% rename from examples/opengl-framework/freeglut/CMakeLists.txt rename to examples/common/opengl-framework/freeglut/CMakeLists.txt diff --git a/examples/opengl-framework/freeglut/COPYING.txt b/examples/common/opengl-framework/freeglut/COPYING.txt similarity index 100% rename from examples/opengl-framework/freeglut/COPYING.txt rename to examples/common/opengl-framework/freeglut/COPYING.txt diff --git a/examples/opengl-framework/freeglut/GL/freeglut.h b/examples/common/opengl-framework/freeglut/GL/freeglut.h similarity index 100% rename from examples/opengl-framework/freeglut/GL/freeglut.h rename to examples/common/opengl-framework/freeglut/GL/freeglut.h diff --git a/examples/opengl-framework/freeglut/GL/freeglut_ext.h b/examples/common/opengl-framework/freeglut/GL/freeglut_ext.h similarity index 100% rename from examples/opengl-framework/freeglut/GL/freeglut_ext.h rename to examples/common/opengl-framework/freeglut/GL/freeglut_ext.h diff --git a/examples/opengl-framework/freeglut/GL/freeglut_std.h b/examples/common/opengl-framework/freeglut/GL/freeglut_std.h similarity index 100% rename from examples/opengl-framework/freeglut/GL/freeglut_std.h rename to examples/common/opengl-framework/freeglut/GL/freeglut_std.h diff --git a/examples/opengl-framework/freeglut/GL/glut.h b/examples/common/opengl-framework/freeglut/GL/glut.h similarity index 100% rename from examples/opengl-framework/freeglut/GL/glut.h rename to examples/common/opengl-framework/freeglut/GL/glut.h diff --git a/examples/opengl-framework/freeglut/VERSION.txt b/examples/common/opengl-framework/freeglut/VERSION.txt similarity index 100% rename from examples/opengl-framework/freeglut/VERSION.txt rename to examples/common/opengl-framework/freeglut/VERSION.txt diff --git a/examples/opengl-framework/freeglut/freeglut_callbacks.c b/examples/common/opengl-framework/freeglut/freeglut_callbacks.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_callbacks.c rename to examples/common/opengl-framework/freeglut/freeglut_callbacks.c diff --git a/examples/opengl-framework/freeglut/freeglut_cursor.c b/examples/common/opengl-framework/freeglut/freeglut_cursor.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_cursor.c rename to examples/common/opengl-framework/freeglut/freeglut_cursor.c diff --git a/examples/opengl-framework/freeglut/freeglut_display.c b/examples/common/opengl-framework/freeglut/freeglut_display.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_display.c rename to examples/common/opengl-framework/freeglut/freeglut_display.c diff --git a/examples/opengl-framework/freeglut/freeglut_ext.c b/examples/common/opengl-framework/freeglut/freeglut_ext.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_ext.c rename to examples/common/opengl-framework/freeglut/freeglut_ext.c diff --git a/examples/opengl-framework/freeglut/freeglut_font.c b/examples/common/opengl-framework/freeglut/freeglut_font.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_font.c rename to examples/common/opengl-framework/freeglut/freeglut_font.c diff --git a/examples/opengl-framework/freeglut/freeglut_font_data.c b/examples/common/opengl-framework/freeglut/freeglut_font_data.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_font_data.c rename to examples/common/opengl-framework/freeglut/freeglut_font_data.c diff --git a/examples/opengl-framework/freeglut/freeglut_gamemode.c b/examples/common/opengl-framework/freeglut/freeglut_gamemode.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_gamemode.c rename to examples/common/opengl-framework/freeglut/freeglut_gamemode.c diff --git a/examples/opengl-framework/freeglut/freeglut_geometry.c b/examples/common/opengl-framework/freeglut/freeglut_geometry.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_geometry.c rename to examples/common/opengl-framework/freeglut/freeglut_geometry.c diff --git a/examples/opengl-framework/freeglut/freeglut_glutfont_definitions.c b/examples/common/opengl-framework/freeglut/freeglut_glutfont_definitions.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_glutfont_definitions.c rename to examples/common/opengl-framework/freeglut/freeglut_glutfont_definitions.c diff --git a/examples/opengl-framework/freeglut/freeglut_init.c b/examples/common/opengl-framework/freeglut/freeglut_init.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_init.c rename to examples/common/opengl-framework/freeglut/freeglut_init.c diff --git a/examples/opengl-framework/freeglut/freeglut_input_devices.c b/examples/common/opengl-framework/freeglut/freeglut_input_devices.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_input_devices.c rename to examples/common/opengl-framework/freeglut/freeglut_input_devices.c diff --git a/examples/opengl-framework/freeglut/freeglut_internal.h b/examples/common/opengl-framework/freeglut/freeglut_internal.h similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_internal.h rename to examples/common/opengl-framework/freeglut/freeglut_internal.h diff --git a/examples/opengl-framework/freeglut/freeglut_joystick.c b/examples/common/opengl-framework/freeglut/freeglut_joystick.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_joystick.c rename to examples/common/opengl-framework/freeglut/freeglut_joystick.c diff --git a/examples/opengl-framework/freeglut/freeglut_main.c b/examples/common/opengl-framework/freeglut/freeglut_main.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_main.c rename to examples/common/opengl-framework/freeglut/freeglut_main.c diff --git a/examples/opengl-framework/freeglut/freeglut_menu.c b/examples/common/opengl-framework/freeglut/freeglut_menu.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_menu.c rename to examples/common/opengl-framework/freeglut/freeglut_menu.c diff --git a/examples/opengl-framework/freeglut/freeglut_misc.c b/examples/common/opengl-framework/freeglut/freeglut_misc.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_misc.c rename to examples/common/opengl-framework/freeglut/freeglut_misc.c diff --git a/examples/opengl-framework/freeglut/freeglut_overlay.c b/examples/common/opengl-framework/freeglut/freeglut_overlay.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_overlay.c rename to examples/common/opengl-framework/freeglut/freeglut_overlay.c diff --git a/examples/opengl-framework/freeglut/freeglut_spaceball.c b/examples/common/opengl-framework/freeglut/freeglut_spaceball.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_spaceball.c rename to examples/common/opengl-framework/freeglut/freeglut_spaceball.c diff --git a/examples/opengl-framework/freeglut/freeglut_state.c b/examples/common/opengl-framework/freeglut/freeglut_state.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_state.c rename to examples/common/opengl-framework/freeglut/freeglut_state.c diff --git a/examples/opengl-framework/freeglut/freeglut_stroke_mono_roman.c b/examples/common/opengl-framework/freeglut/freeglut_stroke_mono_roman.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_stroke_mono_roman.c rename to examples/common/opengl-framework/freeglut/freeglut_stroke_mono_roman.c diff --git a/examples/opengl-framework/freeglut/freeglut_stroke_roman.c b/examples/common/opengl-framework/freeglut/freeglut_stroke_roman.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_stroke_roman.c rename to examples/common/opengl-framework/freeglut/freeglut_stroke_roman.c diff --git a/examples/opengl-framework/freeglut/freeglut_structure.c b/examples/common/opengl-framework/freeglut/freeglut_structure.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_structure.c rename to examples/common/opengl-framework/freeglut/freeglut_structure.c diff --git a/examples/opengl-framework/freeglut/freeglut_teapot.c b/examples/common/opengl-framework/freeglut/freeglut_teapot.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_teapot.c rename to examples/common/opengl-framework/freeglut/freeglut_teapot.c diff --git a/examples/opengl-framework/freeglut/freeglut_teapot_data.h b/examples/common/opengl-framework/freeglut/freeglut_teapot_data.h similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_teapot_data.h rename to examples/common/opengl-framework/freeglut/freeglut_teapot_data.h diff --git a/examples/opengl-framework/freeglut/freeglut_videoresize.c b/examples/common/opengl-framework/freeglut/freeglut_videoresize.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_videoresize.c rename to examples/common/opengl-framework/freeglut/freeglut_videoresize.c diff --git a/examples/opengl-framework/freeglut/freeglut_window.c b/examples/common/opengl-framework/freeglut/freeglut_window.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_window.c rename to examples/common/opengl-framework/freeglut/freeglut_window.c diff --git a/examples/opengl-framework/freeglut/freeglut_xinput.c b/examples/common/opengl-framework/freeglut/freeglut_xinput.c similarity index 100% rename from examples/opengl-framework/freeglut/freeglut_xinput.c rename to examples/common/opengl-framework/freeglut/freeglut_xinput.c diff --git a/examples/opengl-framework/freeglut/glut.h b/examples/common/opengl-framework/freeglut/glut.h similarity index 100% rename from examples/opengl-framework/freeglut/glut.h rename to examples/common/opengl-framework/freeglut/glut.h diff --git a/examples/opengl-framework/src/Camera.cpp b/examples/common/opengl-framework/src/Camera.cpp similarity index 100% rename from examples/opengl-framework/src/Camera.cpp rename to examples/common/opengl-framework/src/Camera.cpp diff --git a/examples/opengl-framework/src/Camera.h b/examples/common/opengl-framework/src/Camera.h similarity index 100% rename from examples/opengl-framework/src/Camera.h rename to examples/common/opengl-framework/src/Camera.h diff --git a/examples/opengl-framework/src/FrameBufferObject.cpp b/examples/common/opengl-framework/src/FrameBufferObject.cpp similarity index 100% rename from examples/opengl-framework/src/FrameBufferObject.cpp rename to examples/common/opengl-framework/src/FrameBufferObject.cpp diff --git a/examples/opengl-framework/src/FrameBufferObject.h b/examples/common/opengl-framework/src/FrameBufferObject.h similarity index 100% rename from examples/opengl-framework/src/FrameBufferObject.h rename to examples/common/opengl-framework/src/FrameBufferObject.h diff --git a/examples/opengl-framework/src/GlutViewer.cpp b/examples/common/opengl-framework/src/GlutViewer.cpp similarity index 97% rename from examples/opengl-framework/src/GlutViewer.cpp rename to examples/common/opengl-framework/src/GlutViewer.cpp index d0dc9bed..45cf639d 100644 --- a/examples/opengl-framework/src/GlutViewer.cpp +++ b/examples/common/opengl-framework/src/GlutViewer.cpp @@ -168,6 +168,16 @@ void GlutViewer::mouseMotionEvent(int xMouse, int yMouse) { 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 bool GlutViewer::mapMouseCoordinatesToSphere(int xMouse, int yMouse, Vector3& spherePoint) const { diff --git a/examples/opengl-framework/src/GlutViewer.h b/examples/common/opengl-framework/src/GlutViewer.h similarity index 95% rename from examples/opengl-framework/src/GlutViewer.h rename to examples/common/opengl-framework/src/GlutViewer.h index b5916179..7718a1af 100644 --- a/examples/opengl-framework/src/GlutViewer.h +++ b/examples/common/opengl-framework/src/GlutViewer.h @@ -39,7 +39,7 @@ namespace openglframework { // Class Renderer class GlutViewer { - private: + protected : // -------------------- Attributes -------------------- // @@ -81,7 +81,7 @@ class GlutViewer { GlutViewer(); // Destructor - ~GlutViewer(); + virtual ~GlutViewer(); // Initialize the viewer bool init(int argc, char** argv, const std::string& windowsTitle, @@ -112,16 +112,17 @@ class GlutViewer { // Get the camera Camera& getCamera(); - void motion(int x, int y); - // Called when a GLUT mouse button event occurs void mouseButtonEvent(int button, int state, int x, int y); // Called when a GLUT mouse motion event occurs void mouseMotionEvent(int xMouse, int yMouse); - void keyboard(int key, int x, int y); - void special(int key, int x, int y); + // Called when a GLUT keyboard event occurs + 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 static void checkOpenGLErrors(); diff --git a/examples/opengl-framework/src/Light.cpp b/examples/common/opengl-framework/src/Light.cpp similarity index 100% rename from examples/opengl-framework/src/Light.cpp rename to examples/common/opengl-framework/src/Light.cpp diff --git a/examples/opengl-framework/src/Light.h b/examples/common/opengl-framework/src/Light.h similarity index 100% rename from examples/opengl-framework/src/Light.h rename to examples/common/opengl-framework/src/Light.h diff --git a/examples/opengl-framework/src/Mesh.cpp b/examples/common/opengl-framework/src/Mesh.cpp similarity index 100% rename from examples/opengl-framework/src/Mesh.cpp rename to examples/common/opengl-framework/src/Mesh.cpp diff --git a/examples/opengl-framework/src/Mesh.h b/examples/common/opengl-framework/src/Mesh.h similarity index 100% rename from examples/opengl-framework/src/Mesh.h rename to examples/common/opengl-framework/src/Mesh.h diff --git a/examples/opengl-framework/src/MeshReaderWriter.cpp b/examples/common/opengl-framework/src/MeshReaderWriter.cpp similarity index 100% rename from examples/opengl-framework/src/MeshReaderWriter.cpp rename to examples/common/opengl-framework/src/MeshReaderWriter.cpp diff --git a/examples/opengl-framework/src/MeshReaderWriter.h b/examples/common/opengl-framework/src/MeshReaderWriter.h similarity index 100% rename from examples/opengl-framework/src/MeshReaderWriter.h rename to examples/common/opengl-framework/src/MeshReaderWriter.h diff --git a/examples/opengl-framework/src/Object3D.cpp b/examples/common/opengl-framework/src/Object3D.cpp similarity index 100% rename from examples/opengl-framework/src/Object3D.cpp rename to examples/common/opengl-framework/src/Object3D.cpp diff --git a/examples/opengl-framework/src/Object3D.h b/examples/common/opengl-framework/src/Object3D.h similarity index 100% rename from examples/opengl-framework/src/Object3D.h rename to examples/common/opengl-framework/src/Object3D.h diff --git a/examples/opengl-framework/src/Shader.cpp b/examples/common/opengl-framework/src/Shader.cpp similarity index 100% rename from examples/opengl-framework/src/Shader.cpp rename to examples/common/opengl-framework/src/Shader.cpp diff --git a/examples/opengl-framework/src/Shader.h b/examples/common/opengl-framework/src/Shader.h similarity index 100% rename from examples/opengl-framework/src/Shader.h rename to examples/common/opengl-framework/src/Shader.h diff --git a/examples/opengl-framework/src/Texture2D.cpp b/examples/common/opengl-framework/src/Texture2D.cpp similarity index 100% rename from examples/opengl-framework/src/Texture2D.cpp rename to examples/common/opengl-framework/src/Texture2D.cpp diff --git a/examples/opengl-framework/src/Texture2D.h b/examples/common/opengl-framework/src/Texture2D.h similarity index 100% rename from examples/opengl-framework/src/Texture2D.h rename to examples/common/opengl-framework/src/Texture2D.h diff --git a/examples/opengl-framework/src/TextureReaderWriter.cpp b/examples/common/opengl-framework/src/TextureReaderWriter.cpp similarity index 100% rename from examples/opengl-framework/src/TextureReaderWriter.cpp rename to examples/common/opengl-framework/src/TextureReaderWriter.cpp diff --git a/examples/opengl-framework/src/TextureReaderWriter.h b/examples/common/opengl-framework/src/TextureReaderWriter.h similarity index 100% rename from examples/opengl-framework/src/TextureReaderWriter.h rename to examples/common/opengl-framework/src/TextureReaderWriter.h diff --git a/examples/opengl-framework/src/VertexBufferObject.cpp b/examples/common/opengl-framework/src/VertexBufferObject.cpp similarity index 100% rename from examples/opengl-framework/src/VertexBufferObject.cpp rename to examples/common/opengl-framework/src/VertexBufferObject.cpp diff --git a/examples/opengl-framework/src/VertexBufferObject.h b/examples/common/opengl-framework/src/VertexBufferObject.h similarity index 100% rename from examples/opengl-framework/src/VertexBufferObject.h rename to examples/common/opengl-framework/src/VertexBufferObject.h diff --git a/examples/opengl-framework/src/definitions.h b/examples/common/opengl-framework/src/definitions.h similarity index 100% rename from examples/opengl-framework/src/definitions.h rename to examples/common/opengl-framework/src/definitions.h diff --git a/examples/opengl-framework/src/maths/Color.h b/examples/common/opengl-framework/src/maths/Color.h similarity index 100% rename from examples/opengl-framework/src/maths/Color.h rename to examples/common/opengl-framework/src/maths/Color.h diff --git a/examples/opengl-framework/src/maths/Matrix3.h b/examples/common/opengl-framework/src/maths/Matrix3.h similarity index 100% rename from examples/opengl-framework/src/maths/Matrix3.h rename to examples/common/opengl-framework/src/maths/Matrix3.h diff --git a/examples/opengl-framework/src/maths/Matrix4.h b/examples/common/opengl-framework/src/maths/Matrix4.h similarity index 100% rename from examples/opengl-framework/src/maths/Matrix4.h rename to examples/common/opengl-framework/src/maths/Matrix4.h diff --git a/examples/opengl-framework/src/maths/Vector2.h b/examples/common/opengl-framework/src/maths/Vector2.h similarity index 100% rename from examples/opengl-framework/src/maths/Vector2.h rename to examples/common/opengl-framework/src/maths/Vector2.h diff --git a/examples/opengl-framework/src/maths/Vector3.h b/examples/common/opengl-framework/src/maths/Vector3.h similarity index 100% rename from examples/opengl-framework/src/maths/Vector3.h rename to examples/common/opengl-framework/src/maths/Vector3.h diff --git a/examples/opengl-framework/src/maths/Vector4.h b/examples/common/opengl-framework/src/maths/Vector4.h similarity index 100% rename from examples/opengl-framework/src/maths/Vector4.h rename to examples/common/opengl-framework/src/maths/Vector4.h diff --git a/examples/opengl-framework/src/openglframework.h b/examples/common/opengl-framework/src/openglframework.h similarity index 100% rename from examples/opengl-framework/src/openglframework.h rename to examples/common/opengl-framework/src/openglframework.h diff --git a/examples/opengl-framework/src/shaders/depth.frag b/examples/common/opengl-framework/src/shaders/depth.frag similarity index 100% rename from examples/opengl-framework/src/shaders/depth.frag rename to examples/common/opengl-framework/src/shaders/depth.frag diff --git a/examples/opengl-framework/src/shaders/depth.vert b/examples/common/opengl-framework/src/shaders/depth.vert similarity index 100% rename from examples/opengl-framework/src/shaders/depth.vert rename to examples/common/opengl-framework/src/shaders/depth.vert diff --git a/examples/opengl-framework/src/shaders/phong.frag b/examples/common/opengl-framework/src/shaders/phong.frag similarity index 100% rename from examples/opengl-framework/src/shaders/phong.frag rename to examples/common/opengl-framework/src/shaders/phong.frag diff --git a/examples/opengl-framework/src/shaders/phong.vert b/examples/common/opengl-framework/src/shaders/phong.vert similarity index 100% rename from examples/opengl-framework/src/shaders/phong.vert rename to examples/common/opengl-framework/src/shaders/phong.vert diff --git a/examples/fallingcubes/CMakeLists.txt b/examples/fallingcubes/CMakeLists.txt index 4dcb291f..a7b5ee34 100644 --- a/examples/fallingcubes/CMakeLists.txt +++ b/examples/fallingcubes/CMakeLists.txt @@ -5,13 +5,13 @@ cmake_minimum_required(VERSION 2.6) PROJECT(FallingCubes) # 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 -INCLUDE_DIRECTORIES("../opengl-framework/src/") +INCLUDE_DIRECTORIES("../common/opengl-framework/src/" "../common/") # Create the example executable using the # 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) diff --git a/examples/fallingcubes/main.cpp b/examples/fallingcubes/FallingCubes.cpp similarity index 72% rename from examples/fallingcubes/main.cpp rename to examples/fallingcubes/FallingCubes.cpp index 6dbfc7f4..65f8f485 100644 --- a/examples/fallingcubes/main.cpp +++ b/examples/fallingcubes/FallingCubes.cpp @@ -25,40 +25,32 @@ // Libraries #include "Scene.h" -#include <sstream> +#include "Viewer.h" // Declarations void simulate(); void display(); -void displayFPS(); -void computeFPS(); +void finish(); void reshape(int width, int height); void mouseButton(int button, int state, 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(); // Namespaces using namespace openglframework; // Global variables -GlutViewer* viewer; +Viewer* viewer; Scene* scene; -int fps; -int nbFrames; -int currentTime; -int previousTime; -int width, height; // Main function int main(int argc, char** argv) { // Create and initialize the Viewer - viewer = new GlutViewer(); + viewer = new Viewer(); Vector2 windowsSize = Vector2(800, 600); Vector2 windowsPosition = Vector2(100, 100); - width = windowsSize.x; - height = windowsSize.y; bool initOK = viewer->init(argc, argv, "ReactPhysics3D Examples - Falling Cubes", windowsSize, windowsPosition); if (!initOK) return 1; @@ -67,21 +59,18 @@ int main(int argc, char** argv) { init(); - nbFrames = 0; - // Glut Idle function that is continuously called glutIdleFunc(simulate); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouseButton); glutMotionFunc(mouseMotion); - glutSpecialFunc(keyboardSpecial); + glutKeyboardFunc(keyboard); // Glut main looop glutMainLoop(); - delete viewer; - delete scene; + finish(); return 0; } @@ -92,7 +81,7 @@ void simulate() { // Physics simulation scene->simulate(); - computeFPS(); + viewer->computeFPS(); // Ask GLUT to render the scene glutPostRedisplay (); @@ -108,8 +97,6 @@ void init() { // Reshape function void reshape(int newWidth, int newHeight) { viewer->reshape(newWidth, newHeight); - width = newWidth; - height = newHeight; } // 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 -void keyboardSpecial(int key, int x, int y) { - /* - if(key=='0') +void keyboard(unsigned char key, int x, int y) { + switch(key) { + + // Escape key + case 27: + finish(); 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 @@ -138,7 +141,7 @@ void display() { scene->render(); // Display the FPS - displayFPS(); + viewer->displayGUI(); // Swap the buffers glutSwapBuffers(); @@ -147,43 +150,4 @@ void display() { 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()); -} diff --git a/examples/fallingcubes/Scene.cpp b/examples/fallingcubes/Scene.cpp index 71a84821..693a0fa5 100644 --- a/examples/fallingcubes/Scene.cpp +++ b/examples/fallingcubes/Scene.cpp @@ -32,32 +32,40 @@ using namespace openglframework; // Constructor Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0), mPhongShader("shaders/phong.vert", - "shaders/phong.frag"){ + "shaders/phong.frag"), mIsRunning(false) { // Move the light 0 mLight0.translateWorld(Vector3(7, 15, 15)); // Compute the radius and the center of the scene - float radius = 10.0f; + float radiusScene = 10.0f; openglframework::Vector3 center(0, 5, 0); // Set the center of the scene - mViewer->setScenePosition(center, radius); + mViewer->setScenePosition(center, radiusScene); // Gravity vector in the dynamics world rp3d::Vector3 gravity(0, -9.81, 0); // 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 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 for (int i=0; i<NB_BOXES; i++) { // 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 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); // Start the simulation - mDynamicsWorld->start(); + startSimulation(); } // Destructor Scene::~Scene() { // Stop the physics simulation - mDynamicsWorld->stop(); + stopSimulation(); // Destroy the shader mPhongShader.destroy(); @@ -118,17 +126,22 @@ Scene::~Scene() { // Take a step for the simulation void Scene::simulate() { - // Take a simulation step - mDynamicsWorld->update(); + // If the physics simulation is running + if (mIsRunning) { - // Update the position and orientation of the boxes - for (std::vector<Box*>::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it) { + // Take a simulation step + 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 @@ -136,7 +149,7 @@ void Scene::render() { glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_CULL_FACE); + glEnable(GL_CULL_FACE); // Bind the shader mPhongShader.bind(); diff --git a/examples/fallingcubes/Scene.h b/examples/fallingcubes/Scene.h index a865d8e0..3741962e 100644 --- a/examples/fallingcubes/Scene.h +++ b/examples/fallingcubes/Scene.h @@ -32,7 +32,7 @@ #include "Box.h" // 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 FLOOR_SIZE(20, 0.5f, 20); // Floor dimensions in meters const float BOX_MASS = 1.0f; // Box mass in kilograms @@ -63,6 +63,9 @@ class Scene { /// Dynamics world used for the physics simulation rp3d::DynamicsWorld* mDynamicsWorld; + /// True if the physics simulation is running + bool mIsRunning; + public: // -------------------- Methods -------------------- // @@ -76,8 +79,39 @@ class Scene { /// Take a step for the simulation void simulate(); + /// Stop the simulation + void stopSimulation(); + + /// Start the simulation + void startSimulation(); + + /// Pause or continue simulation + void pauseContinueSimulation(); + /// Render the scene 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 diff --git a/src/engine/DynamicsWorld.h b/src/engine/DynamicsWorld.h index fb66aa7b..1519b369 100644 --- a/src/engine/DynamicsWorld.h +++ b/src/engine/DynamicsWorld.h @@ -215,7 +215,6 @@ inline void DynamicsWorld::start() { } inline void DynamicsWorld::stop() { - std::cout << "Stop Simulation" << std::endl; mTimer.stop(); }