File size: 5,981 Bytes
90f0b29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "camera.h"
#include "gpuhelper.h"
#include <GL/glu.h>
#include "Eigen/LU"
using namespace Eigen;
Camera::Camera()
: mViewIsUptodate(false), mProjIsUptodate(false)
{
mViewMatrix.setIdentity();
mFovY = M_PI/3.;
mNearDist = 1.;
mFarDist = 50000.;
mVpX = 0;
mVpY = 0;
setPosition(Vector3f::Constant(100.));
setTarget(Vector3f::Zero());
}
Camera& Camera::operator=(const Camera& other)
{
mViewIsUptodate = false;
mProjIsUptodate = false;
mVpX = other.mVpX;
mVpY = other.mVpY;
mVpWidth = other.mVpWidth;
mVpHeight = other.mVpHeight;
mTarget = other.mTarget;
mFovY = other.mFovY;
mNearDist = other.mNearDist;
mFarDist = other.mFarDist;
mViewMatrix = other.mViewMatrix;
mProjectionMatrix = other.mProjectionMatrix;
return *this;
}
Camera::Camera(const Camera& other)
{
*this = other;
}
Camera::~Camera()
{
}
void Camera::setViewport(uint offsetx, uint offsety, uint width, uint height)
{
mVpX = offsetx;
mVpY = offsety;
mVpWidth = width;
mVpHeight = height;
mProjIsUptodate = false;
}
void Camera::setViewport(uint width, uint height)
{
mVpWidth = width;
mVpHeight = height;
mProjIsUptodate = false;
}
void Camera::setFovY(float value)
{
mFovY = value;
mProjIsUptodate = false;
}
Vector3f Camera::direction(void) const
{
return - (orientation() * Vector3f::UnitZ());
}
Vector3f Camera::up(void) const
{
return orientation() * Vector3f::UnitY();
}
Vector3f Camera::right(void) const
{
return orientation() * Vector3f::UnitX();
}
void Camera::setDirection(const Vector3f& newDirection)
{
// TODO implement it computing the rotation between newDirection and current dir ?
Vector3f up = this->up();
Matrix3f camAxes;
camAxes.col(2) = (-newDirection).normalized();
camAxes.col(0) = up.cross( camAxes.col(2) ).normalized();
camAxes.col(1) = camAxes.col(2).cross( camAxes.col(0) ).normalized();
setOrientation(Quaternionf(camAxes));
mViewIsUptodate = false;
}
void Camera::setTarget(const Vector3f& target)
{
mTarget = target;
if (!mTarget.isApprox(position()))
{
Vector3f newDirection = mTarget - position();
setDirection(newDirection.normalized());
}
}
void Camera::setPosition(const Vector3f& p)
{
mFrame.position = p;
mViewIsUptodate = false;
}
void Camera::setOrientation(const Quaternionf& q)
{
mFrame.orientation = q;
mViewIsUptodate = false;
}
void Camera::setFrame(const Frame& f)
{
mFrame = f;
mViewIsUptodate = false;
}
void Camera::rotateAroundTarget(const Quaternionf& q)
{
Matrix4f mrot, mt, mtm;
// update the transform matrix
updateViewMatrix();
Vector3f t = mViewMatrix * mTarget;
mViewMatrix = Translation3f(t)
* q
* Translation3f(-t)
* mViewMatrix;
Quaternionf qa(mViewMatrix.linear());
qa = qa.conjugate();
setOrientation(qa);
setPosition(- (qa * mViewMatrix.translation()) );
mViewIsUptodate = true;
}
void Camera::localRotate(const Quaternionf& q)
{
float dist = (position() - mTarget).norm();
setOrientation(orientation() * q);
mTarget = position() + dist * direction();
mViewIsUptodate = false;
}
void Camera::zoom(float d)
{
float dist = (position() - mTarget).norm();
if(dist > d)
{
setPosition(position() + direction() * d);
mViewIsUptodate = false;
}
}
void Camera::localTranslate(const Vector3f& t)
{
Vector3f trans = orientation() * t;
setPosition( position() + trans );
setTarget( mTarget + trans );
mViewIsUptodate = false;
}
void Camera::updateViewMatrix(void) const
{
if(!mViewIsUptodate)
{
Quaternionf q = orientation().conjugate();
mViewMatrix.linear() = q.toRotationMatrix();
mViewMatrix.translation() = - (mViewMatrix.linear() * position());
mViewIsUptodate = true;
}
}
const Affine3f& Camera::viewMatrix(void) const
{
updateViewMatrix();
return mViewMatrix;
}
void Camera::updateProjectionMatrix(void) const
{
if(!mProjIsUptodate)
{
mProjectionMatrix.setIdentity();
float aspect = float(mVpWidth)/float(mVpHeight);
float theta = mFovY*0.5;
float range = mFarDist - mNearDist;
float invtan = 1./tan(theta);
mProjectionMatrix(0,0) = invtan / aspect;
mProjectionMatrix(1,1) = invtan;
mProjectionMatrix(2,2) = -(mNearDist + mFarDist) / range;
mProjectionMatrix(3,2) = -1;
mProjectionMatrix(2,3) = -2 * mNearDist * mFarDist / range;
mProjectionMatrix(3,3) = 0;
mProjIsUptodate = true;
}
}
const Matrix4f& Camera::projectionMatrix(void) const
{
updateProjectionMatrix();
return mProjectionMatrix;
}
void Camera::activateGL(void)
{
glViewport(vpX(), vpY(), vpWidth(), vpHeight());
gpu.loadMatrix(projectionMatrix(),GL_PROJECTION);
gpu.loadMatrix(viewMatrix().matrix(),GL_MODELVIEW);
}
Vector3f Camera::unProject(const Vector2f& uv, float depth) const
{
Matrix4f inv = mViewMatrix.inverse().matrix();
return unProject(uv, depth, inv);
}
Vector3f Camera::unProject(const Vector2f& uv, float depth, const Matrix4f& invModelview) const
{
updateViewMatrix();
updateProjectionMatrix();
Vector3f a(2.*uv.x()/float(mVpWidth)-1., 2.*uv.y()/float(mVpHeight)-1., 1.);
a.x() *= depth/mProjectionMatrix(0,0);
a.y() *= depth/mProjectionMatrix(1,1);
a.z() = -depth;
// FIXME /\/|
Vector4f b = invModelview * Vector4f(a.x(), a.y(), a.z(), 1.);
return Vector3f(b.x(), b.y(), b.z());
}
|