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-04-14 19:32:40 +00:00
|
|
|
void display();
|
2013-04-18 20:22:45 +00:00
|
|
|
void finish();
|
2013-04-14 19:32:40 +00:00
|
|
|
void reshape(int width, int height);
|
|
|
|
void mouseButton(int button, int state, int x, int y);
|
|
|
|
void mouseMotion(int x, int y);
|
2013-04-18 20:22:45 +00:00
|
|
|
void keyboard(unsigned char key, int x, int y);
|
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-09-10 19:53:34 +00:00
|
|
|
bool initOK = viewer->init(argc, argv, "ReactPhysics3D Examples - Cubes", windowsSize, windowsPosition);
|
2013-04-14 19:32:40 +00:00
|
|
|
if (!initOK) return 1;
|
|
|
|
|
|
|
|
// Create the scene
|
|
|
|
scene = new Scene(viewer);
|
2013-03-17 15:45:48 +00:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Glut Idle function that is continuously called
|
2013-03-17 15:45:48 +00:00
|
|
|
glutIdleFunc(simulate);
|
|
|
|
glutDisplayFunc(display);
|
|
|
|
glutReshapeFunc(reshape);
|
2013-04-14 19:32:40 +00:00
|
|
|
glutMouseFunc(mouseButton);
|
|
|
|
glutMotionFunc(mouseMotion);
|
2013-04-18 20:22:45 +00:00
|
|
|
glutKeyboardFunc(keyboard);
|
2013-09-15 21:32:42 +00:00
|
|
|
|
|
|
|
#ifdef USE_FREEGLUT
|
2013-04-25 20:34:20 +00:00
|
|
|
glutCloseFunc(finish);
|
2013-09-15 21:32:42 +00:00
|
|
|
#else
|
|
|
|
atexit(finish);
|
|
|
|
#endif
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Glut main looop
|
2013-03-17 15:45:48 +00:00
|
|
|
glutMainLoop();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-04-18 20:22:45 +00:00
|
|
|
viewer->computeFPS();
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Ask GLUT to render the scene
|
|
|
|
glutPostRedisplay ();
|
|
|
|
}
|
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-04-14 19:32:40 +00:00
|
|
|
// Reshape function
|
|
|
|
void reshape(int newWidth, int newHeight) {
|
|
|
|
viewer->reshape(newWidth, newHeight);
|
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Called when a mouse button event occurs
|
|
|
|
void mouseButton(int button, int state, int x, int y) {
|
|
|
|
viewer->mouseButtonEvent(button, state, x, y);
|
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Called when a mouse motion event occurs
|
|
|
|
void mouseMotion(int x, int y) {
|
|
|
|
viewer->mouseMotionEvent(x, y);
|
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Called when the user hits a special key on the keyboard
|
2013-04-18 20:22:45 +00:00
|
|
|
void keyboard(unsigned char key, int x, int y) {
|
|
|
|
switch(key) {
|
|
|
|
|
|
|
|
// Escape key
|
|
|
|
case 27:
|
2013-09-15 21:32:42 +00:00
|
|
|
#ifdef USE_FREEGLUT
|
|
|
|
glutLeaveMainLoop();
|
|
|
|
#endif
|
2013-04-18 20:22:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Space bar
|
|
|
|
case 32:
|
|
|
|
scene->pauseContinueSimulation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of the application
|
|
|
|
void finish() {
|
|
|
|
|
|
|
|
// Destroy the viewer and the scene
|
|
|
|
delete viewer;
|
|
|
|
delete scene;
|
2013-04-14 19:32:40 +00:00
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Display the scene
|
|
|
|
void display() {
|
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
|
|
|
// Swap the buffers
|
|
|
|
glutSwapBuffers();
|
2013-03-17 15:45:48 +00:00
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
// Check the OpenGL errors
|
|
|
|
GlutViewer::checkOpenGLErrors();
|
|
|
|
}
|
2013-03-17 15:45:48 +00:00
|
|
|
|
|
|
|
|