2013-09-10 19:35:56 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* 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 "Scene.h"
|
2013-12-30 00:51:46 +00:00
|
|
|
#include "../common/Viewer.h"
|
2013-09-10 19:35:56 +00:00
|
|
|
|
|
|
|
// Declarations
|
|
|
|
void simulate();
|
2013-12-30 00:51:46 +00:00
|
|
|
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);
|
2013-09-10 19:35:56 +00:00
|
|
|
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);
|
2013-12-30 00:51:46 +00:00
|
|
|
viewer->init(argc, argv, "ReactPhysics3D Examples - Collision Shapes",
|
|
|
|
windowsSize, windowsPosition, true);
|
|
|
|
|
|
|
|
// Register callback methods
|
|
|
|
viewer->registerUpdateFunction(update);
|
|
|
|
viewer->registerKeyboardCallback(keyboard);
|
|
|
|
viewer->registerMouseButtonCallback(mouseButton);
|
|
|
|
viewer->registerMouseCursorCallback(mouseMotion);
|
|
|
|
viewer->registerScrollingCallback(scroll);
|
2013-09-10 19:35:56 +00:00
|
|
|
|
|
|
|
// Create the scene
|
|
|
|
scene = new Scene(viewer);
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
viewer->startMainLoop();
|
|
|
|
|
|
|
|
delete viewer;
|
|
|
|
delete scene;
|
2013-09-10 19:35:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
// Update function that is called each frame
|
|
|
|
void update() {
|
|
|
|
|
|
|
|
// Take a simulation step
|
|
|
|
simulate();
|
|
|
|
|
|
|
|
// Render
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:35:56 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
// 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));
|
2013-09-10 19:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when a mouse button event occurs
|
2013-12-30 00:51:46 +00:00
|
|
|
void mouseButton(GLFWwindow* window, int button, int action, int mods) {
|
|
|
|
viewer->mouseButtonEvent(button, action);
|
2013-09-10 19:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when a mouse motion event occurs
|
2013-12-30 00:51:46 +00:00
|
|
|
void mouseMotion(GLFWwindow* window, double x, double y) {
|
2013-09-10 19:35:56 +00:00
|
|
|
viewer->mouseMotionEvent(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the scene
|
2013-12-30 00:51:46 +00:00
|
|
|
void render() {
|
2013-09-10 19:35:56 +00:00
|
|
|
|
|
|
|
// Render the scene
|
|
|
|
scene->render();
|
|
|
|
|
|
|
|
// Display the FPS
|
|
|
|
viewer->displayGUI();
|
|
|
|
|
|
|
|
// Check the OpenGL errors
|
2013-12-30 00:51:46 +00:00
|
|
|
Viewer::checkOpenGLErrors();
|
2013-09-10 19:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|