2013-03-17 15:45:48 +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
|
2013-04-14 19:32:40 +00:00
|
|
|
#include "Scene.h"
|
2013-04-18 20:22:45 +00:00
|
|
|
#include "Viewer.h"
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Declarations
|
2013-03-17 15:45:48 +00:00
|
|
|
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-04-14 19:32:40 +00:00
|
|
|
void init();
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Namespaces
|
|
|
|
using namespace openglframework;
|
2013-03-17 15:45:48 +00:00
|
|
|
|
|
|
|
// Global variables
|
2013-04-18 20:22:45 +00:00
|
|
|
Viewer* viewer;
|
2013-04-14 19:32:40 +00:00
|
|
|
Scene* scene;
|
2013-03-17 15:45:48 +00:00
|
|
|
|
|
|
|
// Main function
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Create and initialize the Viewer
|
2013-04-18 20:22:45 +00:00
|
|
|
viewer = new Viewer();
|
2013-04-14 19:32:40 +00:00
|
|
|
Vector2 windowsSize = Vector2(800, 600);
|
|
|
|
Vector2 windowsPosition = Vector2(100, 100);
|
2013-12-30 00:51:46 +00:00
|
|
|
viewer->init(argc, argv, "ReactPhysics3D Examples - Cubes", windowsSize, windowsPosition, true);
|
|
|
|
|
2014-01-18 16:57:42 +00:00
|
|
|
// 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]);
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
// Register callback methods
|
|
|
|
viewer->registerUpdateFunction(update);
|
|
|
|
viewer->registerKeyboardCallback(keyboard);
|
|
|
|
viewer->registerMouseButtonCallback(mouseButton);
|
|
|
|
viewer->registerMouseCursorCallback(mouseMotion);
|
|
|
|
viewer->registerScrollingCallback(scroll);
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Create the scene
|
2014-01-18 16:57:42 +00:00
|
|
|
scene = new Scene(viewer, shaderFolderPath);
|
2013-03-17 15:45:48 +00:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
viewer->startMainLoop();
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
delete viewer;
|
|
|
|
delete scene;
|
2013-03-17 15:45:48 +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-04-14 19:32:40 +00:00
|
|
|
// Simulate function
|
|
|
|
void simulate() {
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Physics simulation
|
|
|
|
scene->simulate();
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
// Compute the current framerate
|
2013-04-18 20:22:45 +00:00
|
|
|
viewer->computeFPS();
|
2013-04-14 19:32:40 +00:00
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Initialization
|
|
|
|
void init() {
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Define the background color (black)
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
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-04-14 19:32:40 +00:00
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +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-04-14 19:32:40 +00:00
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +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-04-14 19:32:40 +00:00
|
|
|
viewer->mouseMotionEvent(x, y);
|
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Display the scene
|
2013-12-30 00:51:46 +00:00
|
|
|
void render() {
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Render the scene
|
|
|
|
scene->render();
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Display the FPS
|
2013-04-18 20:22:45 +00:00
|
|
|
viewer->displayGUI();
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Check the OpenGL errors
|
2013-12-30 00:51:46 +00:00
|
|
|
Viewer::checkOpenGLErrors();
|
2013-04-14 19:32:40 +00:00
|
|
|
}
|