#include #include #include #include #include using namespace osg; Camera::Camera(DisplaySettings* ds) { _adjustAspectRatioMode = ADJUST_HORIZONTAL; // projection details. float fovy = 45.0f; if (ds) { fovy = 2.0f*RadiansToDegrees(atan(ds->getScreenHeight()*0.5/ds->getScreenDistance())); } setPerspective(fovy,1.0,1.0,1000.0); // look at details. _lookAtType =USE_HOME_POSITON; _eye.set(0.0f,0.0f,0.0f); _center.set(0.0f,0.0f,-1.0f); _up.set(0.0f,1.0f,0.0f); _attachedTransformMode = NO_ATTACHED_TRANSFORM; if (ds) _screenDistance = ds->getScreenDistance(); else _screenDistance = 0.33f; _fusionDistanceMode = PROPORTIONAL_TO_LOOK_DISTANCE; _fusionDistanceRatio = 1.0f; } Camera::Camera(const Camera& camera):Referenced() { copy(camera); } Camera& Camera::operator=(const Camera& camera) { if (&camera==this) return *this; copy(camera); return *this; } void Camera::copy(const Camera& camera) { _projectionType = camera._projectionType; // how the window dimensions should be altered during a window resize. _adjustAspectRatioMode = camera._adjustAspectRatioMode; // note, in Frustum/Perspective mode these values are scaled // by the zNear from when they were initialised to ensure that // subsequent changes in zNear do not affect them. _left = camera._left; _right = camera._right; _bottom = camera._bottom; _top = camera._top; _zNear = camera._zNear; _zFar = camera._zFar; // look at details. _lookAtType = camera._lookAtType; _eye = camera._eye; _center = camera._center; _up = camera._up; _attachedTransformMode = camera._attachedTransformMode; _eyeToModelTransform = camera._eyeToModelTransform; _modelToEyeTransform = camera._modelToEyeTransform; _screenDistance = camera._screenDistance; _fusionDistanceMode = camera._fusionDistanceMode; _fusionDistanceRatio = camera._fusionDistanceRatio; } Camera::~Camera() { } /** Set a orthographics projection. See glOrtho for further details.*/ void Camera::setOrtho(const double left, const double right, const double bottom, const double top, const double zNear, const double zFar) { _projectionType = ORTHO; _left = left; _right = right; _bottom = bottom; _top = top; _zNear = zNear; _zFar = zFar; } /** Set a 2D orthographics projection. See gluOrtho2D for further details.*/ void Camera::setOrtho2D(const double left, const double right, const double bottom, const double top) { _projectionType = ORTHO2D; _left = left; _right = right; _bottom = bottom; _top = top; _zNear = -1.0; _zFar = 1.0; } /** Set a perspective projection. See glFrustum for further details.*/ void Camera::setFrustum(const double left, const double right, const double bottom, const double top, const double zNear, const double zFar) { _projectionType = FRUSTUM; // note, in Frustum/Perspective mode these values are scaled // by the zNear from when they were initialised to ensure that // subsequent changes in zNear do not affect them. _left = left; _right = right; _bottom = bottom; _top = top; _zNear = zNear; _zFar = zFar; } /** Set a sysmetical perspective projection, See gluPerspective for further details.*/ void Camera::setPerspective(const double fovy,const double aspectRatio, const double zNear, const double zFar) { _projectionType = PERSPECTIVE; // note, in Frustum/Perspective mode these values are scaled // by the zNear from when they were initialised to ensure that // subsequent changes in zNear do not affect them. // calculate the appropriate left, right etc. double tan_fovy = tan(DegreesToRadians(fovy*0.5)); _right = tan_fovy * aspectRatio * zNear; _left = -_right; _top = tan_fovy * zNear; _bottom = -_top; _zNear = zNear; _zFar = zFar; } /** Set a sysmetical perspective projection using field of view.*/ void Camera::setFOV(const double fovx,const double fovy, const double zNear, const double zFar) { _projectionType = PERSPECTIVE; // note, in Frustum/Perspective mode these values are scaled // by the zNear from when they were initialised to ensure that // subsequent changes in zNear do not affect them. // calculate the appropriate left, right etc. double tan_fovx = tan(DegreesToRadians(fovx*0.5)); double tan_fovy = tan(DegreesToRadians(fovy*0.5)); _right = tan_fovx * zNear; _left = -_right; _top = tan_fovy * zNear; _bottom = -_top; _zNear = zNear; _zFar = zFar; } /** Set the near and far clipping planes.*/ void Camera::setNearFar(const double zNear, const double zFar) { if (_projectionType==FRUSTUM || _projectionType==PERSPECTIVE) { double adjustRatio = zNear/_zNear; _left *= adjustRatio; _right *= adjustRatio; _bottom *= adjustRatio; _top *= adjustRatio; } _zNear = zNear; _zFar = zFar; if (_projectionType==ORTHO2D) { if (_zNear!=-1.0 || _zFar!=1.0) _projectionType = ORTHO; } } /** Adjust the clipping planes to account for a new window aspcect ratio. * Typicall used after resizeing a window.*/ void Camera::adjustAspectRatio(const double newAspectRatio, const AdjustAspectRatioMode aa) { if (newAspectRatio<0.01f || newAspectRatio>100.0f) { notify(NOTICE)<<"Warning: aspect ratio out of range (0.01..100) in Camera::adjustAspectRatio("<invert(*_eyeToModelTransform)) { notify(WARN)<<"Warning: Camera::attachTransform() failed to invert _modelToEyeTransform"<invert(*_modelToEyeTransform)) { notify(WARN)<<"Warning: Camera::attachTransform() failed to invert _modelToEyeTransform"<