2013-04-14 19:32:40 +00:00
|
|
|
/********************************************************************************
|
2013-04-18 20:22:45 +00:00
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010-2013 Daniel Chappuis *
|
2013-04-14 19:32:40 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
#ifndef VIEWER_H
|
|
|
|
#define VIEWER_H
|
2013-04-14 19:32:40 +00:00
|
|
|
|
|
|
|
// Libraries
|
2013-04-18 20:22:45 +00:00
|
|
|
#include "openglframework.h"
|
2013-12-30 00:51:46 +00:00
|
|
|
#include <GLFW/glfw3.h>
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
// Class Viewer
|
2013-12-30 00:51:46 +00:00
|
|
|
class Viewer {
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
private :
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
// -------------------- Constants -------------------- //
|
|
|
|
|
|
|
|
static const float SCROLL_SENSITIVITY;
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
/// GLFW window
|
|
|
|
GLFWwindow* mWindow;
|
|
|
|
|
|
|
|
/// Window title
|
|
|
|
std::string mWindowTitle;
|
|
|
|
|
|
|
|
/// Camera
|
|
|
|
openglframework::Camera mCamera;
|
|
|
|
|
|
|
|
/// Center of the scene
|
|
|
|
openglframework::Vector3 mCenterScene;
|
|
|
|
|
|
|
|
/// Last mouse coordinates on the windows
|
|
|
|
double mLastMouseX, mLastMouseY;
|
|
|
|
|
|
|
|
/// Last point computed on a sphere (for camera rotation)
|
|
|
|
openglframework::Vector3 mLastPointOnSphere;
|
|
|
|
|
|
|
|
/// True if the last point computed on a sphere (for camera rotation) is valid
|
|
|
|
bool mIsLastPointOnSphereValid;
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
/// Current number of frames per seconds
|
2013-12-30 00:51:46 +00:00
|
|
|
double mFPS;
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
/// Number of frames during the last second
|
2013-12-30 00:51:46 +00:00
|
|
|
int mNbFrames;
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
/// Current time for fps computation
|
2013-12-30 00:51:46 +00:00
|
|
|
double mCurrentTime;
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
/// Previous time for fps computation
|
2013-12-30 00:51:46 +00:00
|
|
|
double mPreviousTime;
|
|
|
|
|
|
|
|
/// Pointer to the update function
|
|
|
|
void (*mUpdateFunctionPointer)();
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
bool mapMouseCoordinatesToSphere(double xMouse, double yMouse, openglframework::Vector3& spherePoint) const;
|
2013-04-18 20:22:45 +00:00
|
|
|
|
|
|
|
public :
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
Viewer();
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
/// Destructor
|
|
|
|
~Viewer();
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Initialize the viewer
|
|
|
|
void init(int argc, char** argv, const std::string& windowsTitle,
|
|
|
|
const openglframework::Vector2& windowsSize,
|
|
|
|
const openglframework::Vector2& windowsPosition,
|
|
|
|
bool isMultisamplingActive = false);
|
|
|
|
|
|
|
|
/// Start the main loop where rendering occur
|
|
|
|
void startMainLoop();
|
|
|
|
|
|
|
|
/// Called when the windows is reshaped
|
|
|
|
void reshape();
|
|
|
|
|
|
|
|
/// Set the scene position (where the camera needs to look at)
|
|
|
|
void setScenePosition(const openglframework::Vector3& position, float sceneRadius);
|
|
|
|
|
|
|
|
/// Set the camera so that we can view the whole scene
|
|
|
|
void resetCameraToViewAll();
|
|
|
|
|
|
|
|
/// Zoom the camera
|
|
|
|
void zoom(float zoomDiff);
|
|
|
|
|
|
|
|
/// Translate the camera
|
|
|
|
void translate(int xMouse, int yMouse);
|
|
|
|
|
|
|
|
/// Rotate the camera
|
|
|
|
void rotate(int xMouse, int yMouse);
|
|
|
|
|
|
|
|
/// Get the camera
|
|
|
|
openglframework::Camera& getCamera();
|
|
|
|
|
|
|
|
/// Called when a GLUT mouse button event occurs
|
|
|
|
void mouseButtonEvent(int button, int action);
|
|
|
|
|
|
|
|
/// Called when a GLUT mouse motion event occurs
|
|
|
|
void mouseMotionEvent(double xMouse, double yMouse);
|
|
|
|
|
|
|
|
/// Called when a scrolling event occurs
|
|
|
|
void scrollingEvent(float scrollAxis);
|
|
|
|
|
|
|
|
/// Check the OpenGL errors
|
|
|
|
static void checkOpenGLErrors();
|
|
|
|
|
|
|
|
/// Display the FPS
|
|
|
|
void displayFPS();
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
/// Compute the FPS
|
|
|
|
void computeFPS();
|
|
|
|
|
|
|
|
/// Display the GUI
|
|
|
|
void displayGUI();
|
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
/// GLFW error callback method
|
|
|
|
static void error_callback(int error, const char* description);
|
|
|
|
|
|
|
|
/// Register the update function that has to be called each frame
|
|
|
|
void registerUpdateFunction(void (*updateFunctionPointer)());
|
|
|
|
|
|
|
|
/// Register a keyboard callback method
|
|
|
|
void registerKeyboardCallback(GLFWkeyfun method);
|
|
|
|
|
|
|
|
/// Register a mouse button callback method
|
|
|
|
void registerMouseButtonCallback(GLFWmousebuttonfun method);
|
|
|
|
|
|
|
|
/// Register a mouse cursor motion callback method
|
|
|
|
void registerMouseCursorCallback(GLFWcursorposfun method);
|
|
|
|
|
|
|
|
/// Register a scrolling cursor callback method
|
|
|
|
void registerScrollingCallback(GLFWscrollfun method);
|
|
|
|
|
2013-04-18 20:22:45 +00:00
|
|
|
};
|
2013-04-14 19:32:40 +00:00
|
|
|
|
2013-12-30 00:51:46 +00:00
|
|
|
// Set the scene position (where the camera needs to look at)
|
|
|
|
inline void Viewer::setScenePosition(const openglframework::Vector3& position, float sceneRadius) {
|
|
|
|
|
|
|
|
// Set the position and radius of the scene
|
|
|
|
mCenterScene = position;
|
|
|
|
mCamera.setSceneRadius(sceneRadius);
|
|
|
|
|
|
|
|
// Reset the camera position and zoom in order to view all the scene
|
|
|
|
resetCameraToViewAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the camera
|
|
|
|
inline openglframework::Camera& Viewer::getCamera() {
|
|
|
|
return mCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the update function that has to be called each frame
|
|
|
|
inline void Viewer::registerUpdateFunction(void (*updateFunctionPointer)()) {
|
|
|
|
mUpdateFunctionPointer = updateFunctionPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a keyboard callback method
|
|
|
|
inline void Viewer::registerKeyboardCallback(GLFWkeyfun method) {
|
|
|
|
glfwSetKeyCallback(mWindow, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a mouse button callback method
|
|
|
|
inline void Viewer::registerMouseButtonCallback(GLFWmousebuttonfun method) {
|
|
|
|
glfwSetMouseButtonCallback(mWindow, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a mouse cursor motion callback method
|
|
|
|
inline void Viewer::registerMouseCursorCallback(GLFWcursorposfun method) {
|
|
|
|
glfwSetCursorPosCallback(mWindow, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a scrolling cursor callback method
|
|
|
|
inline void Viewer::registerScrollingCallback(GLFWscrollfun method) {
|
|
|
|
glfwSetScrollCallback(mWindow, method);
|
|
|
|
}
|
|
|
|
|
2013-04-14 19:32:40 +00:00
|
|
|
#endif
|