Permalink
Browse files

Added methods to the Camera interface for calculating good camera pos…

…itions, with implementation in BaseCamera.

This is taken pretty much straight from my OBJViewer project.
  • Loading branch information...
1 parent 0305d0e commit 49c715f9f41aa177d0debfd6f23bac07621a1392 @vilya committed Aug 26, 2010
Showing with 105 additions and 1 deletion.
  1. +4 −0 TODO.txt
  2. +80 −1 src/vgl_basecamera.cpp
  3. +11 −0 src/vgl_basecamera.h
  4. +10 −0 src/vgl_camera.h
View
@@ -14,3 +14,7 @@ TODO
- Make sure we're only exporting the public symbols from the library.
- Get rid of the BaseCamera class (or gut it, so that it's better for subclassing).
+
+- Get the ArcballCamera working *better* than the hacked-together camera in the
+ OBJViewer.
+
View
@@ -115,10 +115,89 @@ void BaseCamera::zoom(int prevX, int prevY, int currX, int currY)
}
+void BaseCamera::centerView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.z - low.z) / 10.0f;
+ _pos = _target + Vec3f(0, 0, distance);
+ _up = Vec3f(0, 1, 0);
+}
+
+
+void BaseCamera::frontView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.z - low.z) / 2.0f +
+ distanceFrom(high.x, low.x, high.y, low.y);
+ _pos = _target + Vec3f(0, 0, distance);
+ _up = Vec3f(0, 1, 0);
+}
+
+
+void BaseCamera::backView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.z - low.z) / 2.0f +
+ distanceFrom(high.x, low.x, high.y, low.y);
+ _pos = _target + Vec3f(0, 0, -distance);
+ _up = Vec3f(0, 1, 0);
+}
+
+
+void BaseCamera::leftView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.x - low.x) / 2.0f +
+ distanceFrom(high.z, low.z, high.y, low.y);
+ _pos = _target + Vec3f(-distance, 0, 0);
+ _up = Vec3f(0, 1, 0);
+}
+
+
+void BaseCamera::rightView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.x - low.x) / 2.0f +
+ distanceFrom(high.z, low.z, high.y, low.y);
+ _pos = _target + Vec3f(distance, 0, 0);
+ _up = Vec3f(0, 1, 0);
+}
+
+
+void BaseCamera::topView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.y - low.y) +
+ distanceFrom(high.x, low.x, high.z, low.z);
+ _pos = _target + Vec3f(0, distance, 0);
+ _up = Vec3f(0, 0, -1);
+}
+
+
+void BaseCamera::bottomView(const Vec3f& low, const Vec3f& high)
+{
+ _target = (high + low) / 2.0f;
+ float distance = (high.y - low.y) +
+ distanceFrom(high.x, low.x, high.z, low.z);
+ _pos = _target + Vec3f(0, -distance, 0);
+ _up = Vec3f(0, 0, 1);
+}
+
+
+float BaseCamera::distanceFrom(float highU, float lowU, float highV, float lowV) const
+{
+ float opposite = std::max(highU - lowU, highV - lowV) / 2.0;
+ float angle = (_aperture / 2.0) * M_PI / 180.0;
+ float adjacent = (opposite / tanf(angle));
+ return adjacent;
+}
+
+
void BaseCamera::setupProjectionMatrix()
{
float distance = length(_target - _pos);
- gluPerspective(_aperture, float(_pixelWidth) / float(_pixelHeight), distance * 0.1, distance * 2.0);
+ gluPerspective(_aperture, float(_pixelWidth) / float(_pixelHeight),
+ distance * 0.1, distance * 2.0);
}
View
@@ -21,10 +21,21 @@ class BaseCamera : public Camera {
virtual void dolly(int prevX, int prevY, int currX, int currY);
virtual void zoom(int prevX, int prevY, int currX, int currY);
+ virtual void centerView(const Vec3f& low, const Vec3f& high);
+ virtual void frontView(const Vec3f& low, const Vec3f& high);
+ virtual void backView(const Vec3f& low, const Vec3f& high);
+ virtual void leftView(const Vec3f& low, const Vec3f& high);
+ virtual void rightView(const Vec3f& low, const Vec3f& high);
+ virtual void topView(const Vec3f& low, const Vec3f& high);
+ virtual void bottomView(const Vec3f& low, const Vec3f& high);
+
virtual void setupProjectionMatrix();
virtual void setupModelViewMatrix();
protected:
+ float distanceFrom(float highU, float lowU, float highV, float lowV) const;
+
+protected:
Vec3f _pos, _target, _up;
float _left, _right, _bottom, _top;
float _aperture;
View
@@ -1,6 +1,8 @@
#ifndef vgl_camera_h
#define vgl_camera_h
+#include "vgl_vec3.h"
+
namespace vgl {
@@ -12,6 +14,14 @@ class Camera {
virtual void dolly(int prevX, int prevY, int currX, int currY) = 0;
virtual void zoom(int prevX, int prevY, int currX, int currY) = 0;
+ virtual void centerView(const Vec3f& low, const Vec3f& high) = 0;
+ virtual void frontView(const Vec3f& low, const Vec3f& high) = 0;
+ virtual void backView(const Vec3f& low, const Vec3f& high) = 0;
+ virtual void leftView(const Vec3f& low, const Vec3f& high) = 0;
+ virtual void rightView(const Vec3f& low, const Vec3f& high) = 0;
+ virtual void topView(const Vec3f& low, const Vec3f& high) = 0;
+ virtual void bottomView(const Vec3f& low, const Vec3f& high) = 0;
+
virtual void setupProjectionMatrix() = 0;
virtual void setupModelViewMatrix() = 0;
};

0 comments on commit 49c715f

Please sign in to comment.