Add the example scenes to the testbed application

This commit is contained in:
Daniel Chappuis 2015-04-15 23:11:27 +02:00
parent c264356cc6
commit e31c7abffc
24 changed files with 407 additions and 1122 deletions

View File

@ -16,23 +16,59 @@ ADD_SUBDIRECTORY(glfw/)
# Copy the shaders used for the demo into the build directory
FILE(COPY "shaders/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/shaders/")
# Headers
MESSAGE("Test" "${OPENGLFRAMEWORK_DIR}")
INCLUDE_DIRECTORIES("opengl-framework/src/" "glfw/include/" "common/")
# Copy the meshes used for the demo into the build directory
FILE(COPY "meshes/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/meshes/")
# Source files
# Headers
INCLUDE_DIRECTORIES("src" "opengl-framework/src/" "glfw/include/" "common/" "scenes/")
# Testbed source files
SET(TESTBED_SOURCES
Main.cpp
TestbedApplication.h
TestbedApplication.cpp
Gui.h
Gui.cpp
Scene.h
Scene.cpp
src/Main.cpp
src/TestbedApplication.h
src/TestbedApplication.cpp
src/Gui.h
src/Gui.cpp
src/Scene.h
src/Scene.cpp
)
# Common source files
SET(COMMON_SOURCES
common/Box.h
common/Box.cpp
common/Cone.h
common/Cone.cpp
common/Sphere.h
common/Sphere.cpp
common/Line.h
common/Line.cpp
common/Capsule.h
common/Capsule.cpp
common/ConvexMesh.h
common/ConvexMesh.cpp
common/Cylinder.h
common/Cylinder.cpp
common/Dumbbell.h
common/Dumbbell.cpp
common/VisualContactPoint.h
common/VisualContactPoint.cpp
)
# Examples scenes source files
SET(SCENES_SOURCES
scenes/cubes/CubesScene.h
scenes/cubes/CubesScene.cpp
scenes/joints/JointsScene.h
scenes/joints/JointsScene.cpp
scenes/raycast/RaycastScene.h
scenes/raycast/RaycastScene.cpp
scenes/collisionshapes/CollisionShapesScene.h
scenes/collisionshapes/CollisionShapesScene.cpp
)
# Create the executable
ADD_EXECUTABLE(testbed ${TESTBED_SOURCES})
ADD_EXECUTABLE(testbed ${TESTBED_SOURCES} ${SCENES_SOURCES} ${COMMON_SOURCES})
# Link with libraries
TARGET_LINK_LIBRARIES(testbed reactphysics3d openglframework glfw ${GLFW_LIBRARIES})

View File

@ -1,42 +0,0 @@
# Minimum cmake version required
cmake_minimum_required(VERSION 2.6)
# Project configuration
PROJECT(CollisionShapes)
# Where to build the executables
SET(EXECUTABLE_OUTPUT_PATH "${OUR_EXECUTABLE_OUTPUT_PATH}/collisionshapes")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXECUTABLE_OUTPUT_PATH})
# Copy the shaders used for the demo into the build directory
FILE(COPY "${OPENGLFRAMEWORK_DIR}/src/shaders/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/shaders/")
# Copy the meshes used for the demo into the build directory
FILE(COPY "../common/meshes/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/meshes/")
# Headers
INCLUDE_DIRECTORIES("${OPENGLFRAMEWORK_DIR}/src/" "../common/glfw/include/" "../common/")
# Source files
SET(COLLISION_SHAPES_SOURCES
CollisionShapes.cpp
Scene.cpp
Scene.h
"../common/VisualContactPoint.cpp"
"../common/ConvexMesh.cpp"
"../common/Capsule.cpp"
"../common/Sphere.cpp"
"../common/Cylinder.cpp"
"../common/Cone.cpp"
"../common/Dumbbell.cpp"
"../common/Box.cpp"
"../common/Viewer.cpp"
)
# Create the executable
ADD_EXECUTABLE(collisionshapes ${COLLISION_SHAPES_SOURCES})
# Link with libraries
TARGET_LINK_LIBRARIES(collisionshapes reactphysics3d openglframework glfw ${GLFW_LIBRARIES})

View File

@ -1,152 +0,0 @@
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2015 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "Scene.h"
#include "../common/Viewer.h"
// Declarations
void simulate();
void render();
void update();
void mouseButton(GLFWwindow* window, int button, int action, int mods);
void mouseMotion(GLFWwindow* window, double x, double y);
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
void scroll(GLFWwindow* window, double xAxis, double yAxis);
void init();
// Namespaces
using namespace openglframework;
// Global variables
Viewer* viewer;
Scene* scene;
// Main function
int main(int argc, char** argv) {
// Create and initialize the Viewer
viewer = new Viewer();
Vector2 windowsSize = Vector2(800, 600);
Vector2 windowsPosition = Vector2(100, 100);
viewer->init(argc, argv, "ReactPhysics3D Examples - Collision Shapes",
windowsSize, windowsPosition, true);
// If the shaders and meshes folders are not specified as an argument
if (argc < 3) {
std::cerr << "Error : You need to specify the shaders folder as the first argument"
<< " and the meshes folder as the second argument" << std::endl;
return 1;
}
// Get the path of the shaders folder
std::string shaderFolderPath(argv[1]);
std::string meshFolderPath(argv[2]);
// Register callback methods
viewer->registerUpdateFunction(update);
viewer->registerKeyboardCallback(keyboard);
viewer->registerMouseButtonCallback(mouseButton);
viewer->registerMouseCursorCallback(mouseMotion);
viewer->registerScrollingCallback(scroll);
// Create the scene
scene = new Scene(viewer, shaderFolderPath, meshFolderPath);
init();
viewer->startMainLoop();
delete viewer;
delete scene;
return 0;
}
// Update function that is called each frame
void update() {
// Take a simulation step
simulate();
// Render
render();
}
// Simulate function
void simulate() {
// Physics simulation
scene->simulate();
viewer->computeFPS();
}
// Initialization
void init() {
// Define the background color (black)
glClearColor(0.0, 0.0, 0.0, 1.0);
}
// Callback method to receive keyboard events
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
scene->pauseContinueSimulation();
}
}
// Callback method to receive scrolling events
void scroll(GLFWwindow* window, double xAxis, double yAxis) {
viewer->scrollingEvent(static_cast<float>(yAxis));
}
// Called when a mouse button event occurs
void mouseButton(GLFWwindow* window, int button, int action, int mods) {
viewer->mouseButtonEvent(button, action);
}
// Called when a mouse motion event occurs
void mouseMotion(GLFWwindow* window, double x, double y) {
viewer->mouseMotionEvent(x, y);
}
// Display the scene
void render() {
// Render the scene
scene->render();
// Display the FPS
viewer->displayGUI();
// Check the OpenGL errors
Viewer::checkOpenGLErrors();
}

View File

@ -1,34 +0,0 @@
# Minimum cmake version required
cmake_minimum_required(VERSION 2.6)
# Project configuration
PROJECT(Cubes)
# Where to build the executables
SET(EXECUTABLE_OUTPUT_PATH "${OUR_EXECUTABLE_OUTPUT_PATH}/cubes")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXECUTABLE_OUTPUT_PATH})
# Copy the shaders used for the demo into the build directory
FILE(COPY "${OPENGLFRAMEWORK_DIR}/src/shaders/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/shaders/")
# Headers
INCLUDE_DIRECTORIES("${OPENGLFRAMEWORK_DIR}/src/" "../common/glfw/include/" "../common/")
# Source files
SET(CUBES_SOURCES
Cubes.cpp
Scene.cpp
Scene.h
"../common/Box.cpp"
"../common/Box.h"
"../common/Viewer.cpp"
"../common/Viewer.h"
)
# Create the executable
ADD_EXECUTABLE(cubes ${CUBES_SOURCES})
# Link with libraries
TARGET_LINK_LIBRARIES(cubes reactphysics3d openglframework glfw ${GLFW_LIBRARIES})

View File

@ -1,148 +0,0 @@
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2015 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "Scene.h"
#include "Viewer.h"
// Declarations
void simulate();
void render();
void update();
void mouseButton(GLFWwindow* window, int button, int action, int mods);
void mouseMotion(GLFWwindow* window, double x, double y);
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
void scroll(GLFWwindow* window, double xAxis, double yAxis);
void init();
// Namespaces
using namespace openglframework;
// Global variables
Viewer* viewer;
Scene* scene;
// Main function
int main(int argc, char** argv) {
// Create and initialize the Viewer
viewer = new Viewer();
Vector2 windowsSize = Vector2(800, 600);
Vector2 windowsPosition = Vector2(100, 100);
viewer->init(argc, argv, "ReactPhysics3D Examples - Cubes", windowsSize, windowsPosition, true);
// If the shaders folder is not specified as an argument
if (argc < 2) {
std::cerr << "Error : You need to specify the shaders folder as argument !" << std::endl;
return 1;
}
// Get the path of the shaders folder
std::string shaderFolderPath(argv[1]);
// Register callback methods
viewer->registerUpdateFunction(update);
viewer->registerKeyboardCallback(keyboard);
viewer->registerMouseButtonCallback(mouseButton);
viewer->registerMouseCursorCallback(mouseMotion);
viewer->registerScrollingCallback(scroll);
// Create the scene
scene = new Scene(viewer, shaderFolderPath);
init();
viewer->startMainLoop();
delete viewer;
delete scene;
return 0;
}
// Update function that is called each frame
void update() {
// Take a simulation step
simulate();
// Render
render();
}
// Simulate function
void simulate() {
// Physics simulation
scene->simulate();
// Compute the current framerate
viewer->computeFPS();
}
// Initialization
void init() {
// Define the background color (black)
glClearColor(0.0, 0.0, 0.0, 1.0);
}
// Callback method to receive keyboard events
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
scene->pauseContinueSimulation();
}
}
// Callback method to receive scrolling events
void scroll(GLFWwindow* window, double xAxis, double yAxis) {
viewer->scrollingEvent(static_cast<float>(yAxis));
}
// Called when a mouse button event occurs
void mouseButton(GLFWwindow* window, int button, int action, int mods) {
viewer->mouseButtonEvent(button, action);
}
// Called when a mouse motion event occurs
void mouseMotion(GLFWwindow* window, double x, double y) {
viewer->mouseMotionEvent(x, y);
}
// Display the scene
void render() {
// Render the scene
scene->render();
// Display the FPS
viewer->displayGUI();
// Check the OpenGL errors
Viewer::checkOpenGLErrors();
}

View File

@ -1,34 +0,0 @@
# Minimum cmake version required
cmake_minimum_required(VERSION 2.6)
# Project configuration
PROJECT(Joints)
# Where to build the executables
SET(EXECUTABLE_OUTPUT_PATH "${OUR_EXECUTABLE_OUTPUT_PATH}/joints")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXECUTABLE_OUTPUT_PATH})
# Copy the shaders used for the demo into the build directory
FILE(COPY "${OPENGLFRAMEWORK_DIR}/src/shaders/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/shaders/")
# Headers
INCLUDE_DIRECTORIES("${OPENGLFRAMEWORK_DIR}/src/" "../common/glfw/include/" "../common/")
# Source files
SET(JOINTS_SOURCES
Joints.cpp
Scene.cpp
Scene.h
"../common/Box.cpp"
"../common/Box.h"
"../common/Viewer.cpp"
"../common/Viewer.h"
)
# Create the executable
ADD_EXECUTABLE(joints ${JOINTS_SOURCES})
# Link with libraries
TARGET_LINK_LIBRARIES(joints reactphysics3d openglframework glfw ${GLFW_LIBRARIES})

View File

@ -1,149 +0,0 @@
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2015 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "Scene.h"
#include "../common/Viewer.h"
// Declarations
void simulate();
void update();
void render();
void mouseButton(GLFWwindow* window, int button, int action, int mods);
void mouseMotion(GLFWwindow* window, double x, double y);
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
void scroll(GLFWwindow* window, double xAxis, double yAxis);
void init();
// Namespaces
using namespace openglframework;
// Global variables
Viewer* viewer;
Scene* scene;
// Main function
int main(int argc, char** argv) {
// Create and initialize the Viewer
viewer = new Viewer();
Vector2 windowsSize = Vector2(800, 600);
Vector2 windowsPosition = Vector2(100, 100);
viewer->init(argc, argv, "ReactPhysics3D Examples - Joints", windowsSize, windowsPosition, true);
// If the shaders folder is not specified as an argument
if (argc < 2) {
std::cerr << "Error : You need to specify the shaders folder as argument !" << std::endl;
return 1;
}
// Get the path of the shaders folder
std::string shaderFolderPath(argv[1]);
// Register callback methods
viewer->registerUpdateFunction(update);
viewer->registerKeyboardCallback(keyboard);
viewer->registerMouseButtonCallback(mouseButton);
viewer->registerMouseCursorCallback(mouseMotion);
viewer->registerScrollingCallback(scroll);
// Create the scene
scene = new Scene(viewer, shaderFolderPath);
init();
viewer->startMainLoop();
delete viewer;
delete scene;
return 0;
}
// Update function that is called each frame
void update() {
// Take a simulation step
simulate();
// Render
render();
}
// Simulate function
void simulate() {
// Physics simulation
scene->simulate();
viewer->computeFPS();
}
// Initialization
void init() {
// Define the background color (black)
glClearColor(0.0, 0.0, 0.0, 1.0);
}
// Callback method to receive keyboard events
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
scene->pauseContinueSimulation();
}
}
// Callback method to receive scrolling events
void scroll(GLFWwindow* window, double xAxis, double yAxis) {
viewer->scrollingEvent(static_cast<float>(yAxis));
}
// Called when a mouse button event occurs
void mouseButton(GLFWwindow* window, int button, int action, int mods) {
viewer->mouseButtonEvent(button, action);
}
// Called when a mouse motion event occurs
void mouseMotion(GLFWwindow* window, double x, double y) {
viewer->mouseMotionEvent(x, y);
}
// Display the scene
void render() {
// Render the scene
scene->render();
// Display the FPS
viewer->displayGUI();
// Check the OpenGL errors
Viewer::checkOpenGLErrors();
}

View File

@ -1,43 +0,0 @@
# Minimum cmake version required
cmake_minimum_required(VERSION 2.6)
# Project configuration
PROJECT(Raycast)
# Where to build the executables
SET(EXECUTABLE_OUTPUT_PATH "${OUR_EXECUTABLE_OUTPUT_PATH}/raycast")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXECUTABLE_OUTPUT_PATH})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXECUTABLE_OUTPUT_PATH})
# Copy the shaders used for the demo into the build directory
FILE(COPY "${OPENGLFRAMEWORK_DIR}/src/shaders/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/shaders/")
# Copy the meshes used for the demo into the build directory
FILE(COPY "../common/meshes/" DESTINATION "${EXECUTABLE_OUTPUT_PATH}/meshes/")
# Headers
INCLUDE_DIRECTORIES("${OPENGLFRAMEWORK_DIR}/src/" "../common/glfw/include/" "../common/")
# Source files
SET(RAYCAST_SOURCES
Raycast.cpp
Scene.cpp
Scene.h
"../common/VisualContactPoint.cpp"
"../common/ConvexMesh.cpp"
"../common/Capsule.cpp"
"../common/Sphere.cpp"
"../common/Line.cpp"
"../common/Cylinder.cpp"
"../common/Cone.cpp"
"../common/Dumbbell.cpp"
"../common/Box.cpp"
"../common/Viewer.cpp"
)
# Create the executable
ADD_EXECUTABLE(raycast ${RAYCAST_SOURCES})
# Link with libraries
TARGET_LINK_LIBRARIES(raycast reactphysics3d openglframework glfw ${GLFW_LIBRARIES})

View File

@ -1,155 +0,0 @@
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2015 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "Scene.h"
#include "../common/Viewer.h"
// Declarations
void simulate();
void render();
void update();
void mouseButton(GLFWwindow* window, int button, int action, int mods);
void mouseMotion(GLFWwindow* window, double x, double y);
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
void scroll(GLFWwindow* window, double xAxis, double yAxis);
void init();
// Namespaces
using namespace openglframework;
// Global variables
Viewer* viewer;
Scene* scene;
// Main function
int main(int argc, char** argv) {
// Create and initialize the Viewer
viewer = new Viewer();
Vector2 windowsSize = Vector2(800, 600);
Vector2 windowsPosition = Vector2(100, 100);
viewer->init(argc, argv, "ReactPhysics3D Examples - Raycast",
windowsSize, windowsPosition, true);
// If the shaders and meshes folders are not specified as an argument
if (argc < 3) {
std::cerr << "Error : You need to specify the shaders folder as the first argument"
<< " and the meshes folder as the second argument" << std::endl;
return 1;
}
// Get the path of the shaders folder
std::string shaderFolderPath(argv[1]);
std::string meshFolderPath(argv[2]);
// Register callback methods
viewer->registerUpdateFunction(update);
viewer->registerKeyboardCallback(keyboard);
viewer->registerMouseButtonCallback(mouseButton);
viewer->registerMouseCursorCallback(mouseMotion);
viewer->registerScrollingCallback(scroll);
// Create the scene
scene = new Scene(viewer, shaderFolderPath, meshFolderPath);
init();
viewer->startMainLoop();
delete viewer;
delete scene;
return 0;
}
// Update function that is called each frame
void update() {
// Take a simulation step
simulate();
// Render
render();
}
// Simulate function
void simulate() {
// Physics simulation
scene->simulate();
viewer->computeFPS();
}
// Initialization
void init() {
// Define the background color (black)
glClearColor(0.0, 0.0, 0.0, 1.0);
}
// Callback method to receive keyboard events
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
scene->changeBody();
}
if (key == GLFW_KEY_N && action == GLFW_PRESS) {
scene->showHideNormals();
}
}
// Callback method to receive scrolling events
void scroll(GLFWwindow* window, double xAxis, double yAxis) {
viewer->scrollingEvent(static_cast<float>(yAxis));
}
// Called when a mouse button event occurs
void mouseButton(GLFWwindow* window, int button, int action, int mods) {
viewer->mouseButtonEvent(button, action);
}
// Called when a mouse motion event occurs
void mouseMotion(GLFWwindow* window, double x, double y) {
viewer->mouseMotionEvent(x, y);
}
// Display the scene
void render() {
// Render the scene
scene->render();
// Display the FPS
viewer->displayGUI();
// Check the OpenGL errors
Viewer::checkOpenGLErrors();
}

View File

@ -24,16 +24,17 @@
********************************************************************************/
// Libraries
#include "Scene.h"
#include "CollisionShapesScene.h"
// Namespaces
using namespace openglframework;
using namespace collisionshapesscene;
// Constructor
Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::string& meshFolderPath)
: mViewer(viewer), mLight0(0),
mPhongShader(shaderFolderPath + "phong.vert",
shaderFolderPath +"phong.frag"), mIsRunning(false) {
CollisionShapesScene::CollisionShapesScene(const std::string& name)
: Scene(name), mLight0(0), mPhongShader("shaders/phong.vert", "shaders/phong.frag") {
std::string meshFolderPath("meshes/");
// Move the light 0
mLight0.translateWorld(Vector3(50, 50, 50));
@ -43,7 +44,7 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::str
openglframework::Vector3 center(0, 5, 0);
// Set the center of the scene
mViewer->setScenePosition(center, radiusScene);
setScenePosition(center, radiusScene);
// Gravity vector in the dynamics world
rp3d::Vector3 gravity(0, -9.81, 0);
@ -217,14 +218,11 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::str
material.setBounciness(rp3d::decimal(0.2));
// Start the simulation
startSimulation();
mDynamicsWorld->start();
}
// Destructor
Scene::~Scene() {
// Stop the physics simulation
stopSimulation();
CollisionShapesScene::~CollisionShapesScene() {
// Destroy the shader
mPhongShader.destroy();
@ -312,85 +310,81 @@ Scene::~Scene() {
}
// Take a step for the simulation
void Scene::simulate() {
void CollisionShapesScene::update() {
// If the physics simulation is running
if (mIsRunning) {
// Take a simulation step
mDynamicsWorld->update();
// 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 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();
}
// Update the position and orientation of the sphere
for (std::vector<Sphere*>::iterator it = mSpheres.begin(); it != mSpheres.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the cones
for (std::vector<Cone*>::iterator it = mCones.begin(); it != mCones.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the cylinders
for (std::vector<Cylinder*>::iterator it = mCylinders.begin(); it != mCylinders.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the capsules
for (std::vector<Capsule*>::iterator it = mCapsules.begin(); it != mCapsules.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the convex meshes
for (std::vector<ConvexMesh*>::iterator it = mConvexMeshes.begin();
it != mConvexMeshes.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the dumbbells
for (std::vector<Dumbbell*>::iterator it = mDumbbells.begin();
it != mDumbbells.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
mFloor->updateTransform();
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the sphere
for (std::vector<Sphere*>::iterator it = mSpheres.begin(); it != mSpheres.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the cones
for (std::vector<Cone*>::iterator it = mCones.begin(); it != mCones.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the cylinders
for (std::vector<Cylinder*>::iterator it = mCylinders.begin(); it != mCylinders.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the capsules
for (std::vector<Capsule*>::iterator it = mCapsules.begin(); it != mCapsules.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the convex meshes
for (std::vector<ConvexMesh*>::iterator it = mConvexMeshes.begin();
it != mConvexMeshes.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
// Update the position and orientation of the dumbbells
for (std::vector<Dumbbell*>::iterator it = mDumbbells.begin();
it != mDumbbells.end(); ++it) {
// Update the transform used for the rendering
(*it)->updateTransform();
}
mFloor->updateTransform();
}
// Render the scene
void Scene::render() {
void CollisionShapesScene::render() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Get the world-space to camera-space matrix
const Camera& camera = mViewer->getCamera();
const openglframework::Matrix4 worldToCameraMatrix = camera.getTransformMatrix().getInverse();
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Bind the shader
mPhongShader.bind();
// Set the variables of the shader
mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix());
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffColLight0 = mLight0.getDiffuseColor();
@ -442,3 +436,8 @@ void Scene::render() {
// Unbind the shader
mPhongShader.unbind();
}
/// Reset the scene
void CollisionShapesScene::reset() {
}

View File

@ -23,12 +23,13 @@
* *
********************************************************************************/
#ifndef SCENE_H
#define SCENE_H
#ifndef COLLISION_SHAPES_SCENE_H
#define COLLISION_SHAPES_SCENE_H
// Libraries
#include "openglframework.h"
#include "reactphysics3d.h"
#include "Scene.h"
#include "Sphere.h"
#include "Box.h"
#include "Cone.h"
@ -39,6 +40,9 @@
#include "VisualContactPoint.h"
#include "../common/Viewer.h"
namespace collisionshapesscene {
// Constants
const int NB_BOXES = 2;
const int NB_CUBES = 1;
@ -64,16 +68,13 @@ const float CAPSULE_MASS = 1.0f;
const float MESH_MASS = 1.0f;
const float FLOOR_MASS = 100.0f; // Floor mass in kilograms
// Class Scene
class Scene {
// Class CollisionShapesScene
class CollisionShapesScene : public Scene{
private :
// -------------------- Attributes -------------------- //
/// Pointer to the viewer
Viewer* mViewer;
/// Light 0
openglframework::Light mLight0;
@ -103,56 +104,26 @@ class Scene {
/// Dynamics world used for the physics simulation
rp3d::DynamicsWorld* mDynamicsWorld;
/// True if the physics simulation is running
bool mIsRunning;
public:
// -------------------- Methods -------------------- //
/// Constructor
Scene(Viewer* viewer, const std::string& shaderFolderPath,
const std::string& meshFolderPath);
CollisionShapesScene(const std::string& name);
/// Destructor
~Scene();
virtual ~CollisionShapesScene();
/// Take a step for the simulation
void simulate();
/// Stop the simulation
void stopSimulation();
/// Start the simulation
void startSimulation();
/// Pause or continue simulation
void pauseContinueSimulation();
virtual void update();
/// Render the scene
void render();
virtual void render();
/// Reset the scene
virtual void reset();
};
// 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

View File

@ -24,16 +24,16 @@
********************************************************************************/
// Libraries
#include "Scene.h"
#include "CubesScene.h"
// Namespaces
using namespace openglframework;
using namespace cubesscene;
// Constructor
Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath)
: mViewer(viewer), mLight0(0),
mPhongShader(shaderFolderPath + "phong.vert", shaderFolderPath + "phong.frag"),
mIsRunning(false) {
CubesScene::CubesScene(const std::string& name)
: Scene(name), mLight0(0),
mPhongShader("shaders/phong.vert", "shaders/phong.frag") {
// Move the light 0
mLight0.translateWorld(Vector3(7, 15, 15));
@ -43,7 +43,7 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath)
openglframework::Vector3 center(0, 5, 0);
// Set the center of the scene
mViewer->setScenePosition(center, radiusScene);
setScenePosition(center, radiusScene);
// Gravity vector in the dynamics world
rp3d::Vector3 gravity(0, rp3d::decimal(-5.81), 0);
@ -91,16 +91,15 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath)
material.setBounciness(rp3d::decimal(0.3));
// Start the simulation
startSimulation();
mDynamicsWorld->start();
counter=0;
}
// Destructor
Scene::~Scene() {
CubesScene::~CubesScene() {
// Stop the physics simulation
stopSimulation();
mDynamicsWorld->stop();
// Destroy the shader
mPhongShader.destroy();
@ -126,56 +125,51 @@ Scene::~Scene() {
}
// Take a step for the simulation
void Scene::simulate() {
void CubesScene::update() {
// If the physics simulation is running
if (mIsRunning) {
counter++;
if (counter == 400) {
//mIsRunning = false;
}
counter++;
if (counter == 400) {
//mIsRunning = false;
// 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();
// Set the color of the awake/sleeping bodies
for (uint i=0; i<mBoxes.size(); i++) {
if (mBoxes[i]->getRigidBody()->isSleeping()) {
mBoxes[i]->setColor(Color(1, 0, 0, 1));
}
// 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();
// Set the color of the awake/sleeping bodies
for (uint i=0; i<mBoxes.size(); i++) {
if (mBoxes[i]->getRigidBody()->isSleeping()) {
mBoxes[i]->setColor(Color(1, 0, 0, 1));
}
else {
mBoxes[i]->setColor(Color(0, 1, 0, 1));
}
else {
mBoxes[i]->setColor(Color(0, 1, 0, 1));
}
}
}
// Render the scene
void Scene::render() {
void CubesScene::render() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Get the world-space to camera-space matrix
const Camera& camera = mViewer->getCamera();
const openglframework::Matrix4 worldToCameraMatrix = camera.getTransformMatrix().getInverse();
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Bind the shader
mPhongShader.bind();
// Set the variables of the shader
mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix());
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("light0PosCameraSpace",worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffCol = mLight0.getDiffuseColor();
@ -195,3 +189,8 @@ void Scene::render() {
// Unbind the shader
mPhongShader.unbind();
}
// Reset the scene
void CubesScene::reset() {
}

View File

@ -23,14 +23,16 @@
* *
********************************************************************************/
#ifndef SCENE_H
#define SCENE_H
#ifndef CUBES_SCENE_H
#define CUBES_SCENE_H
// Libraries
#include "openglframework.h"
#include "reactphysics3d.h"
#include "Box.h"
#include "Viewer.h"
#include "Scene.h"
namespace cubesscene {
// Constants
const int NB_CUBES = 200; // Number of boxes in the scene
@ -39,18 +41,15 @@ const openglframework::Vector3 FLOOR_SIZE(50, 0.5f, 50); // Floor dimensions i
const float BOX_MASS = 1.0f; // Box mass in kilograms
const float FLOOR_MASS = 100.0f; // Floor mass in kilograms
// Class Scene
class Scene {
// Class CubesScene
class CubesScene : public Scene {
private :
protected :
// -------------------- Attributes -------------------- //
int counter;
/// Pointer to the viewer
Viewer* mViewer;
/// Light 0
openglframework::Light mLight0;
@ -66,55 +65,26 @@ class Scene {
/// Dynamics world used for the physics simulation
rp3d::DynamicsWorld* mDynamicsWorld;
/// True if the physics simulation is running
bool mIsRunning;
public:
// -------------------- Methods -------------------- //
/// Constructor
Scene(Viewer* viewer, const std::string& shaderFolderPath);
CubesScene(const std::string& name);
/// Destructor
~Scene();
virtual ~CubesScene();
/// Take a step for the simulation
void simulate();
/// Stop the simulation
void stopSimulation();
/// Start the simulation
void startSimulation();
/// Pause or continue simulation
void pauseContinueSimulation();
/// Update the scene (take a simulation step)
virtual void update();
/// Render the scene
void render();
virtual void render();
/// Reset the scene
virtual void reset();
};
// 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

View File

@ -24,17 +24,17 @@
********************************************************************************/
// Libraries
#include "Scene.h"
#include "JointsScene.h"
#include <cmath>
// Namespaces
using namespace openglframework;
using namespace jointsscene;
// Constructor
Scene::Scene(Viewer *viewer, const std::string& shaderFolderPath)
: mViewer(viewer), mLight0(0), mPhongShader(shaderFolderPath + "phong.vert",
shaderFolderPath + "phong.frag"),
mIsRunning(false) {
JointsScene::JointsScene(const std::string& name)
: Scene(name), mLight0(0), mPhongShader("shaders/phong.vert",
"shaders/phong.frag") {
// Move the light 0
mLight0.translateWorld(Vector3(7, 15, 15));
@ -44,7 +44,7 @@ Scene::Scene(Viewer *viewer, const std::string& shaderFolderPath)
openglframework::Vector3 center(0, 5, 0);
// Set the center of the scene
mViewer->setScenePosition(center, radiusScene);
setScenePosition(center, radiusScene);
// Gravity vector in the dynamics world
rp3d::Vector3 gravity(0, rp3d::decimal(-9.81), 0);
@ -74,14 +74,14 @@ Scene::Scene(Viewer *viewer, const std::string& shaderFolderPath)
createFloor();
// Start the simulation
startSimulation();
mDynamicsWorld->start();
}
// Destructor
Scene::~Scene() {
JointsScene::~JointsScene() {
// Stop the physics simulation
stopSimulation();
mDynamicsWorld->stop();
// Destroy the shader
mPhongShader.destroy();
@ -123,50 +123,45 @@ Scene::~Scene() {
}
// Take a step for the simulation
void Scene::simulate() {
void JointsScene::update() {
// If the physics simulation is running
if (mIsRunning) {
// Update the motor speed of the Slider Joint (to move up and down)
long double motorSpeed = 2 * cos(mDynamicsWorld->getPhysicsTime() * 1.5);
mSliderJoint->setMotorSpeed(rp3d::decimal(motorSpeed));
// Update the motor speed of the Slider Joint (to move up and down)
long double motorSpeed = 2 * cos(mDynamicsWorld->getPhysicsTime() * 1.5);
mSliderJoint->setMotorSpeed(rp3d::decimal(motorSpeed));
// Take a simulation step
mDynamicsWorld->update();
// Take a simulation step
mDynamicsWorld->update();
// Update the position and orientation of the boxes
mSliderJointBottomBox->updateTransform();
mSliderJointTopBox->updateTransform();
mPropellerBox->updateTransform();
mFixedJointBox1->updateTransform();
mFixedJointBox2->updateTransform();
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
mBallAndSocketJointChainBoxes[i]->updateTransform();
}
// Update the position and orientation of the floor
mFloor->updateTransform();
// Update the position and orientation of the boxes
mSliderJointBottomBox->updateTransform();
mSliderJointTopBox->updateTransform();
mPropellerBox->updateTransform();
mFixedJointBox1->updateTransform();
mFixedJointBox2->updateTransform();
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
mBallAndSocketJointChainBoxes[i]->updateTransform();
}
// Update the position and orientation of the floor
mFloor->updateTransform();
}
// Render the scene
void Scene::render() {
void JointsScene::render() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Get the world-space to camera-space matrix
const Camera& camera = mViewer->getCamera();
const openglframework::Matrix4 worldToCameraMatrix = camera.getTransformMatrix().getInverse();
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Bind the shader
mPhongShader.bind();
// Set the variables of the shader
mPhongShader.setVector3Uniform("light0PosCameraSpace",worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix());
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffCol = mLight0.getDiffuseColor();
const Color& specCol = mLight0.getSpecularColor();
@ -191,8 +186,13 @@ void Scene::render() {
mPhongShader.unbind();
}
// Reset the scene
void JointsScene::reset() {
}
// Create the boxes and joints for the Ball-and-Socket joint example
void Scene::createBallAndSocketJoints() {
void JointsScene::createBallAndSocketJoints() {
// --------------- Create the boxes --------------- //
@ -240,7 +240,7 @@ void Scene::createBallAndSocketJoints() {
}
/// Create the boxes and joint for the Slider joint example
void Scene::createSliderJoint() {
void JointsScene::createSliderJoint() {
// --------------- Create the first box --------------- //
@ -292,7 +292,7 @@ void Scene::createSliderJoint() {
}
/// Create the boxes and joint for the Hinge joint example
void Scene::createPropellerHingeJoint() {
void JointsScene::createPropellerHingeJoint() {
// --------------- Create the propeller box --------------- //
@ -327,7 +327,7 @@ void Scene::createPropellerHingeJoint() {
}
/// Create the boxes and joints for the fixed joints
void Scene::createFixedJoints() {
void JointsScene::createFixedJoints() {
// --------------- Create the first box --------------- //
@ -379,7 +379,7 @@ void Scene::createFixedJoints() {
}
// Create the floor
void Scene::createFloor() {
void JointsScene::createFloor() {
// Create the floor
openglframework::Vector3 floorPosition(0, 0, 0);

View File

@ -23,14 +23,16 @@
* *
********************************************************************************/
#ifndef SCENE_H
#define SCENE_H
#ifndef JOINTS_SCENE_H
#define JOINTS_SCENE_H
// Libraries
#include "openglframework.h"
#include "reactphysics3d.h"
#include "Box.h"
#include "../common/Viewer.h"
#include "Scene.h"
namespace jointsscene {
// Constants
const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters
@ -40,16 +42,13 @@ const float FLOOR_MASS = 100.0f; // Floor mass in kil
const int NB_BALLSOCKETJOINT_BOXES = 7; // Number of Ball-And-Socket chain boxes
const int NB_HINGE_BOXES = 7; // Number of Hinge chain boxes
// Class Scene
class Scene {
// Class JointsScene
class JointsScene : public Scene {
private :
protected :
// -------------------- Attributes -------------------- //
/// Pointer to the viewer
Viewer* mViewer;
/// Light 0
openglframework::Light mLight0;
@ -101,9 +100,6 @@ class Scene {
/// Dynamics world used for the physics simulation
rp3d::DynamicsWorld* mDynamicsWorld;
/// True if the physics simulation is running
bool mIsRunning;
// -------------------- Methods -------------------- //
/// Create the boxes and joints for the Ball-and-Socket joint example
@ -126,47 +122,21 @@ class Scene {
// -------------------- Methods -------------------- //
/// Constructor
Scene(Viewer* viewer, const std::string& shaderFolderPath);
JointsScene(const std::string& name);
/// Destructor
~Scene();
virtual ~JointsScene();
/// Take a step for the simulation
void simulate();
/// Stop the simulation
void stopSimulation();
/// Start the simulation
void startSimulation();
/// Pause or continue simulation
void pauseContinueSimulation();
virtual void update();
/// Render the scene
void render();
virtual void render();
/// Reset the scene
virtual void reset();
};
// 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

View File

@ -24,16 +24,18 @@
********************************************************************************/
// Libraries
#include "Scene.h"
#include "RaycastScene.h"
// Namespaces
using namespace openglframework;
using namespace raycastscene;
// Constructor
Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::string& meshFolderPath)
: mViewer(viewer), mLight0(0), mCurrentBodyIndex(-1), mAreNormalsDisplayed(false),
mPhongShader(shaderFolderPath + "phong.vert",
shaderFolderPath +"phong.frag") {
RaycastScene::RaycastScene(const std::string& name)
: Scene(name), mLight0(0), mCurrentBodyIndex(-1), mAreNormalsDisplayed(false),
mPhongShader("shaders/phong.vert", "shaders/phong.frag") {
std::string meshFolderPath("meshes/");
// Move the light 0
mLight0.translateWorld(Vector3(50, 50, 50));
@ -43,7 +45,7 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::str
openglframework::Vector3 center(0, 0, 0);
// Set the center of the scene
mViewer->setScenePosition(center, radiusScene);
setScenePosition(center, radiusScene);
// Create the dynamics world for the physics simulation
mCollisionWorld = new rp3d::CollisionWorld();
@ -105,7 +107,7 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::str
}
// Create the raycast lines
void Scene::createLines() {
void RaycastScene::createLines() {
int nbRaysOneDimension = std::sqrt(float(NB_RAYS));
@ -131,7 +133,7 @@ void Scene::createLines() {
}
// Change the body to raycast and to display
void Scene::changeBody() {
void RaycastScene::changeBody() {
mCurrentBodyIndex++;
if (mCurrentBodyIndex >= NB_BODIES) mCurrentBodyIndex = 0;
@ -163,8 +165,13 @@ void Scene::changeBody() {
}
}
// Reset the scene
void RaycastScene::reset() {
}
// Destructor
Scene::~Scene() {
RaycastScene::~RaycastScene() {
// Destroy the shader
mPhongShader.destroy();
@ -221,7 +228,7 @@ Scene::~Scene() {
}
// Take a step for the simulation
void Scene::simulate() {
void RaycastScene::update() {
mRaycastManager.resetPoints();
@ -246,15 +253,14 @@ void Scene::simulate() {
}
// Render the scene
void Scene::render() {
void RaycastScene::render() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
// Get the world-space to camera-space matrix
const Camera& camera = mViewer->getCamera();
const openglframework::Matrix4 worldToCameraMatrix = camera.getTransformMatrix().getInverse();
const openglframework::Matrix4 worldToCameraMatrix = mCamera.getTransformMatrix().getInverse();
// Bind the shader
mPhongShader.bind();
@ -263,7 +269,7 @@ void Scene::render() {
mPhongShader.setVector4Uniform("vertexColor", grey);
// Set the variables of the shader
mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix());
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
const Color& diffColLight0 = mLight0.getDiffuseColor();

View File

@ -23,14 +23,15 @@
* *
********************************************************************************/
#ifndef SCENE_H
#define SCENE_H
#ifndef RAYCAST_SCENE_H
#define RAYCAST_SCENE_H
// Libraries
#define _USE_MATH_DEFINES
#include <cmath>
#include "openglframework.h"
#include "reactphysics3d.h"
#include "Scene.h"
#include "Sphere.h"
#include "Box.h"
#include "Cone.h"
@ -42,6 +43,8 @@
#include "VisualContactPoint.h"
#include "../common/Viewer.h"
namespace raycastscene {
// Constants
const openglframework::Vector3 BOX_SIZE(4, 2, 1);
const float SPHERE_RADIUS = 3.0f;
@ -122,16 +125,13 @@ class RaycastManager : public rp3d::RaycastCallback {
}
};
// Class Scene
class Scene {
// Class RaycastScene
class RaycastScene : public Scene {
private :
// -------------------- Attributes -------------------- //
/// Pointer to the viewer
Viewer* mViewer;
/// Raycast manager
RaycastManager mRaycastManager;
@ -172,17 +172,19 @@ class Scene {
// -------------------- Methods -------------------- //
/// Constructor
Scene(Viewer* viewer, const std::string& shaderFolderPath,
const std::string& meshFolderPath);
RaycastScene(const std::string& name);
/// Destructor
~Scene();
virtual ~RaycastScene();
/// Take a step for the simulation
void simulate();
virtual void update();
/// Render the scene
void render();
virtual void render();
/// Reset the scene
virtual void reset();
/// Change the body to raycast
void changeBody();
@ -192,9 +194,10 @@ class Scene {
};
// Display or not the surface normals at hit points
inline void Scene::showHideNormals() {
inline void RaycastScene::showHideNormals() {
mAreNormalsDisplayed = !mAreNormalsDisplayed;
}
}
#endif

View File

@ -26,6 +26,16 @@
// Libraries
#include "Gui.h"
// Constructor
Gui::Gui() {
}
// Destructor
Gui::~Gui() {
}
// Display the GUI
void Gui::display() {

View File

@ -25,6 +25,7 @@
// Libraries
#include "Scene.h"
#include <GLFW/glfw3.h>
using namespace openglframework;
@ -33,6 +34,11 @@ Scene::Scene(const std::string& name) : mName(name) {
}
// Destructor
Scene::~Scene() {
}
// Set the scene position (where the camera needs to look at)
void Scene::setScenePosition(const openglframework::Vector3& position, float sceneRadius) {
@ -59,15 +65,12 @@ void Scene::resetCameraToViewAll() {
}
// Map the mouse x,y coordinates to a point on a sphere
bool Scene::mapMouseCoordinatesToSphere(double xMouse, double yMouse, Vector3& spherePoint) const {
bool Scene::mapMouseCoordinatesToSphere(double xMouse, double yMouse,
Vector3& spherePoint) const {
// Get the window dimension
int width, height;
glfwGetWindowSize(mWindow, &width, &height);
if ((xMouse >= 0) && (xMouse <= width) && (yMouse >= 0) && (yMouse <= height)) {
float x = float(xMouse - 0.5f * width) / float(width);
float y = float(0.5f * height - yMouse) / float(height);
if ((xMouse >= 0) && (xMouse <= mWindowWidth) && (yMouse >= 0) && (yMouse <= mWindowHeight)) {
float x = float(xMouse - 0.5f * mWindowWidth) / float(mWindowWidth);
float y = float(0.5f * mWindowHeight - yMouse) / float(mWindowHeight);
float sinx = sin(PI * x * 0.5f);
float siny = sin(PI * y * 0.5f);
float sinx2siny2 = sinx * sinx + siny * siny;
@ -84,17 +87,14 @@ bool Scene::mapMouseCoordinatesToSphere(double xMouse, double yMouse, Vector3& s
}
// Called when a mouse button event occurs
void Scene::mouseButtonEvent(int button, int action, int mods) {
// Get the mouse cursor position
double x, y;
glfwGetCursorPos(mWindow, &x, &y);
void Scene::mouseButtonEvent(int button, int action, int mods,
double mousePosX, double mousePosY) {
// If the mouse button is pressed
if (action == GLFW_PRESS) {
mLastMouseX = x;
mLastMouseY = y;
mIsLastPointOnSphereValid = mapMouseCoordinatesToSphere(x, y, mLastPointOnSphere);
mLastMouseX = mousePosX;
mLastMouseY = mousePosY;
mIsLastPointOnSphereValid = mapMouseCoordinatesToSphere(mousePosX, mousePosY, mLastPointOnSphere);
}
else { // If the mouse button is released
mIsLastPointOnSphereValid = false;
@ -102,22 +102,15 @@ void Scene::mouseButtonEvent(int button, int action, int mods) {
}
// Called when a mouse motion event occurs
void Scene::mouseMotionEvent(double xMouse, double yMouse) {
int leftButtonState = glfwGetMouseButton(mWindow, GLFW_MOUSE_BUTTON_LEFT);
int rightButtonState = glfwGetMouseButton(mWindow, GLFW_MOUSE_BUTTON_RIGHT);
int middleButtonState = glfwGetMouseButton(mWindow, GLFW_MOUSE_BUTTON_MIDDLE);
int altKeyState = glfwGetKey(mWindow, GLFW_KEY_LEFT_ALT);
void Scene::mouseMotionEvent(double xMouse, double yMouse, int leftButtonState,
int rightButtonState, int middleButtonState,
int altKeyState) {
// Zoom
if (leftButtonState == GLFW_PRESS && altKeyState == GLFW_PRESS) {
// Get the window dimension
int width, height;
glfwGetWindowSize(mWindow, &width, &height);
float dy = static_cast<float>(yMouse - mLastMouseY);
float h = static_cast<float>(height);
float h = static_cast<float>(mWindowHeight);
// Zoom the camera
zoom(-dy / h);
@ -139,8 +132,8 @@ void Scene::mouseMotionEvent(double xMouse, double yMouse) {
}
// Called when a scrolling event occurs
void Scene::scrollingEvent(float xAxis, float yAxis) {
zoom(yAxis * SCROLL_SENSITIVITY);
void Scene::scrollingEvent(float xAxis, float yAxis, float scrollSensitivy) {
zoom(yAxis * scrollSensitivy);
}
// Zoom the camera

View File

@ -49,6 +49,9 @@ class Scene {
/// Last mouse coordinates on the windows
double mLastMouseX, mLastMouseY;
/// Window dimension
int mWindowWidth, mWindowHeight;
/// Last point computed on a sphere (for camera rotation)
openglframework::Vector3 mLastPointOnSphere;
@ -87,9 +90,6 @@ class Scene {
/// Destructor
virtual ~Scene();
/// Initialize the scene
virtual void init()=0;
/// Reshape the view
virtual void reshape(int width, int height);
@ -106,13 +106,18 @@ class Scene {
virtual void keyboardEvent(int key, int scancode, int action, int mods);
/// Called when a mouse button event occurs
virtual void mouseButtonEvent(int button, int action, int mods);
virtual void mouseButtonEvent(int button, int action, int mods,
double mousePosX, double mousePosY);
/// Called when a mouse motion event occurs
virtual void mouseMotionEvent(double xMouse, double yMouse);
virtual void mouseMotionEvent(double xMouse, double yMouse, int leftButtonState,
int rightButtonState, int middleButtonState, int altKeyState);
/// Called when a scrolling event occurs
virtual void scrollingEvent(float xAxis, float yAxis);
virtual void scrollingEvent(float xAxis, float yAxis, float scrollSensitivy);
/// Set the window dimension
void setWindowDimension(int width, int height);
/// Return a reference to the camera
const openglframework::Camera& getCamera() const;
@ -133,5 +138,11 @@ inline const openglframework::Camera& Scene::getCamera() const {
return mCamera;
}
// Set the window dimension
inline void Scene::setWindowDimension(int width, int height) {
mWindowWidth = width;
mWindowHeight = height;
}
#endif

View File

@ -29,8 +29,16 @@
#include <iostream>
#include <cstdlib>
#include <sstream>
#include "cubes/CubesScene.h"
#include "joints/JointsScene.h"
#include "collisionshapes/CollisionShapesScene.h"
#include "raycast/RaycastScene.h"
using namespace openglframework;
using namespace jointsscene;
using namespace cubesscene;
using namespace raycastscene;
using namespace collisionshapesscene;
// Initialization of static variables
const float TestbedApplication::SCROLL_SENSITIVITY = 0.02f;
@ -54,6 +62,9 @@ TestbedApplication::~TestbedApplication() {
// TODO : Check that this method is called at the end
// Destroy all the scenes
destroyScenes();
// Destroy the window
glfwDestroyWindow(mWindow);
@ -110,6 +121,42 @@ void TestbedApplication::init() {
// Define the background color (black)
glClearColor(0.0, 0.0, 0.0, 1.0);
// Create all the scenes
createScenes();
}
// Create all the scenes
void TestbedApplication::createScenes() {
// Cubes scene
CubesScene* cubeScene = new CubesScene("Cubes");
mScenes.push_back(cubeScene);
// Joints scene
JointsScene* jointsScene = new JointsScene("Joints");
mScenes.push_back(jointsScene);
// Collision shapes scene
CollisionShapesScene* collisionShapesScene = new CollisionShapesScene("Collision Shapes");
mScenes.push_back(collisionShapesScene);
// Raycast scene
RaycastScene* raycastScene = new RaycastScene("Raycast");
mScenes.push_back(raycastScene);
assert(mScenes.size() > 0);
mCurrentScene = mScenes[0];
}
// Remove all the scenes
void TestbedApplication::destroyScenes() {
for (int i=0; i<mScenes.size(); i++) {
delete mScenes[i];
}
mCurrentScene = NULL;
}
void TestbedApplication::update() {
@ -141,10 +188,16 @@ void TestbedApplication::reshape() {
int width, height;
glfwGetFramebufferSize(mWindow, &width, &height);
// Resize the camera viewport of the current scene
// Resize the camera viewport
mCurrentScene->reshape(width, height);
// Resize the OpenGL viewport
glViewport(0, 0, width, height);
// Update the window size of the scene
int windowWidth, windowHeight;
glfwGetWindowSize(mWindow, &windowWidth, &windowHeight);
mCurrentScene->setWindowDimension(windowWidth, windowHeight);
}
// Start the main loop where rendering occur
@ -159,6 +212,9 @@ void TestbedApplication::startMainLoop() {
// Call the update function
update();
// Render the application
render();
// Swap front and back buffers
glfwSwapBuffers(mWindow);
@ -249,15 +305,27 @@ void TestbedApplication::keyboard(GLFWwindow* window, int key, int scancode,
// Callback method to receive scrolling events
void TestbedApplication::scroll(GLFWwindow* window, double xAxis, double yAxis) {
getInstance().mCurrentScene->scrollingEvent(xAxis, yAxis);
getInstance().mCurrentScene->scrollingEvent(xAxis, yAxis, SCROLL_SENSITIVITY);
}
// Called when a mouse button event occurs
void TestbedApplication::mouseButton(GLFWwindow* window, int button, int action, int mods) {
getInstance().mCurrentScene->mouseButtonEvent(button, action, mods);
// Get the mouse cursor position
double x, y;
glfwGetCursorPos(window, &x, &y);
getInstance().mCurrentScene->mouseButtonEvent(button, action, mods, x, y);
}
// Called when a mouse motion event occurs
void TestbedApplication::mouseMotion(GLFWwindow* window, double x, double y) {
getInstance().mCurrentScene->mouseMotionEvent(x, y);
int leftButtonState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
int rightButtonState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
int middleButtonState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE);
int altKeyState = glfwGetKey(window, GLFW_KEY_LEFT_ALT);
getInstance().mCurrentScene->mouseMotionEvent(x, y, leftButtonState, rightButtonState,
middleButtonState, altKeyState);
}

View File

@ -121,6 +121,12 @@ class TestbedApplication {
/// Called when a mouse motion event occurs
static void mouseMotion(GLFWwindow* window, double x, double y);
/// Initialize all the scenes
void createScenes();
/// Remove all the scenes
void destroyScenes() ;
public :
// -------------------- Methods -------------------- //
@ -131,7 +137,7 @@ class TestbedApplication {
/// Destructor
~TestbedApplication();
/// Initialize the viewer
/// Initialize the application
void init();
/// Start the main loop where rendering occur