// Camera class // celes@tecgraf.puc-rio.br // Jan 2003 #ifndef VGL_CAMERA_H #define VGL_CAMERA_H #include #include #include #include #include "defines.h" class UtlWriter; class VGL_API VglCamera { bool m_updated; bool m_ortho; bool m_autofit; bool m_box_set; bool m_stdist; int m_curreye; int m_headlight; float m_angle; float m_aspect; float m_znear, m_zfar; float m_fitfactor; float m_shiftx, m_shifty; float m_zcenter; float m_pmin, m_pmax; float m_width, m_distance; float m_zps, m_vzps; float m_tc, m_hit; AlgVector m_pos; AlgVector m_target; AlgVector m_up; AlgVector m_dop; AlgVector m_center; AlgVector m_box[8]; AlgStackMatrix m_stack; AlgMatrix m_currmatrix; AlgMatrix m_lasttransf; AlgMatrix m_rotation; AlgVector m_vmin, m_vmax; // view volume float m_tx, m_ty; int (*m_validate)(void*); void* m_vdata; class ZoomStack { public: struct ZoomData { float factor; float tx; float ty; ZoomData(float s, float x, float y) : factor(s), tx(x), ty(y) { } }; private: int m_top; DsArray m_array; public: ZoomStack (); void Clear (); bool Undo (); bool Redo (); void Zoom (ZoomData& d); bool IsEmpty (); ZoomData* Top (); }; ZoomStack m_zoomstack; public: static char* CLASSNAME () { return "Camera"; } virtual char* ClassName () const { return CLASSNAME(); } /** * Constructs a new VglCamera object. */ VglCamera (); /** * Destroys a VglCamera object. */ virtual ~VglCamera (); /** * Sets orthographic or perspective projection. * \param value true for orthographic, false for perspective projection. */ void SetOrtho (bool flag); /** * Gets projection type. * \return true for orthographic, false for perspective projection. */ bool GetOrtho (); /** * Sets the camera position in world coordinates. * \param x x coordinate. * \param y y coordinate. * \param z z coordinate. */ void SetPosition (float x, float y, float z); /** * Sets the camera target in world coordinates (where to look at). * \param x x coordinate. * \param y y coordinate. * \param z z coordinate. */ void SetTarget (float x, float y, float z); /** * Sets the camera up vector. * \param nx x coord. * \param ny y coord. * \param nz z coord. */ void SetUp (float nx, float ny, float nz); /** * Sets the angle of the field of view in the x-z plane. * \param fovy Angle in degrees. Must be between 0.0 and 180.0. */ void SetAngle (float fovy); float GetAngle () { return m_angle; } /** * Sets the camera aspect ratio (width/height). */ void SetAspect (float aspect); /** * Gets the camera aspect radio (width/height). */ float GetAspect () { return m_aspect; } /** * Sets the near and far clipping planes. * \param znear Near plane. * \param zfar Far plane. */ void SetZPlanes (float znear, float zfar); /** * Sets the aimed camera rotation. * \param rotation Camera rotation matrix. */ void SetRotation (const AlgMatrix& rotation); /** * Sets the aimed camera rotation. * \param angle Camera rotation angle. * \param x Camera rotation axis x. * \param y Camera rotation axis y. * \param z Camera rotation axis z. */ void SetRotation (float angle, float x, float y, float z); /** * Sets the camera shift. * shiftx and shifty are in normalized coordinates. */ void SetShift (float shiftx, float shifty); /** * Gets the camera shift. */ void GetShift (float* shiftx, float* shifty) { *shiftx = m_shiftx; *shifty = m_shifty; } float GetZCenter (); virtual AlgMatrix GetModelview (); AlgMatrix GetProjection (); /** * Sets the current view volume. Only used in orthographic projection when autofit is disabled. * The view volume is calculated when autofit is enabled and when a perspective projection is used. */ void SetViewVolume (float xmin, float ymin, float zmin, float xmax, float ymax, float zmax); /** * Obtains the current view volume. */ void GetViewVolume (float* xmin, float* ymin, float* zmin, float* xmax, float* ymax, float* zmax); //* Set model box to be viewed void SetBox (float xmin, float xmax, float ymin, float ymax, float zmin, float zmax); void GetBox (float* xmin, float* xmax, float* ymin, float* ymax, float* zmin, float* zmax); const AlgVector* GetBox (); const AlgVector* GetViewBox () { return GetBox(); } /** * Enables/disables automatic model fitting. */ void SetAutoFit (bool flag); /** * Specifies the direction of projection (for model autofitting). */ void SetDOP (float x, float y, float z); void SetDOP (AlgVector dop) { SetDOP(dop.x,dop.y,dop.z); } /** * Specifies the automatic model fitting fit factor (a scale factor applied to the fitting). */ void SetFitFactor (float factor); /** * Enables automatic model fitting (computes camera parameters to fit the entire model in the direction of projection) * x,y,z are the direction of projection (DOP) * factor specifies a scale factor applied to the fitting. */ void EnableAutoFit (float x=0.0f, float y=0.0f, float z=0.0f, float factor=0.0f) { SetAutoFit(true); SetDOP(x,y,z); SetFitFactor(factor); } void DisableAutoFit () { SetAutoFit(false); } //* Stereo parameters // Set the current eye (-1=stereo left, 0=mon, 1=stereo right) void SetCurrEye (int eye); int GetCurrEye (); // Set parallax min and max (in degrees) void SetParallax (float pmin, float pmax); // Set screen void SetPhysicalScreen (float w, float d); // Set zero parallax setting (from 0.0 to 1.0) void SetZeroParallax (float zps); // Avoid stereo distortion in depth? void SetStereoDistortion(bool avoid); float GetComputedZPS (); //* Utility functions //* Center view // Computes camera parameter to center the view based on the given box void CenterView (); /** * Computes the camera parameters to fit the box inside the frustum. * Maintains the rotations to the model. * A fitting factor, specified in SetFitFactor will be used. */ void FitView (); //* Load functions //* Set head light void SetHeadlight (int lightid); // Loads the corresponding modelview and projection matrices. void Load (); //* Transform functions //* Push transformation void PushMatrix (); //* Pop transformation void PopMatrix (); //* Load identity void LoadIdentity (); //* Translate void Translate (float x, float y, float z); //* Translate camera in world coordinates void TranslateW (float x, float y, float z); //* Rotate void Rotate (float angle, float x, float y, float z); //* Translate view void TranslateView (float x, float y, float z); //* Translate zoom void TranslateZoom (float x, float y); //* Rotate view void RotateView (float angle, float x, float y, float z); //* Scale view void ScaleView (float sx, float sy, float sz); //* Scale zoom void ScaleZoom (float s); //* Undo last transformation void UndoTransform (); //* Redo last transformation void RedoTransform (); //* Set validation callback // It should return 0 if transform is to be invalidated. void SetValidateFunc (int (*func)(void*), void* data=0); //* Zoom void Zoom (float factor, float x=0.5f, float y=0.5f); //* Undo Zoom int UndoZoom (); //* Redo Zoom int RedoZoom (); //* Reset Zoom void ResetZoom (); //* I/O functions bool Write (char* filename); bool Write (FILE* fp); void Write (UtlWriter* writer); bool Read (char* filename); bool Read (FILE* fp); bool Skip (FILE* fp); //* Reset zoom and matrix void Reset (); void ApplyTransform (AlgMatrix& t); private: void ComputeFrustum (); void FindMinMax (AlgVector& axis, float* min, float* max); AlgVector ComputeBoxSize (); void ComputeStereo (); float AvoidStereoDistortion(float tc, float pmin, float pmax); }; #endif