xref: /aosp_15_r20/external/eigen/demos/opengl/camera.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #ifndef EIGEN_CAMERA_H
11*bf2c3715SXin Li #define EIGEN_CAMERA_H
12*bf2c3715SXin Li 
13*bf2c3715SXin Li #include <Eigen/Geometry>
14*bf2c3715SXin Li #include <QObject>
15*bf2c3715SXin Li // #include <frame.h>
16*bf2c3715SXin Li 
17*bf2c3715SXin Li class Frame
18*bf2c3715SXin Li {
19*bf2c3715SXin Li   public:
20*bf2c3715SXin Li     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21*bf2c3715SXin Li 
22*bf2c3715SXin Li     inline Frame(const Eigen::Vector3f& pos = Eigen::Vector3f::Zero(),
23*bf2c3715SXin Li                  const Eigen::Quaternionf& o = Eigen::Quaternionf())
orientation(o)24*bf2c3715SXin Li       : orientation(o), position(pos)
25*bf2c3715SXin Li     {}
lerp(float alpha,const Frame & other)26*bf2c3715SXin Li     Frame lerp(float alpha, const Frame& other) const
27*bf2c3715SXin Li     {
28*bf2c3715SXin Li       return Frame((1.f-alpha)*position + alpha * other.position,
29*bf2c3715SXin Li                    orientation.slerp(alpha,other.orientation));
30*bf2c3715SXin Li     }
31*bf2c3715SXin Li 
32*bf2c3715SXin Li     Eigen::Quaternionf orientation;
33*bf2c3715SXin Li     Eigen::Vector3f position;
34*bf2c3715SXin Li };
35*bf2c3715SXin Li 
36*bf2c3715SXin Li class Camera
37*bf2c3715SXin Li {
38*bf2c3715SXin Li   public:
39*bf2c3715SXin Li     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
40*bf2c3715SXin Li 
41*bf2c3715SXin Li     Camera(void);
42*bf2c3715SXin Li 
43*bf2c3715SXin Li     Camera(const Camera& other);
44*bf2c3715SXin Li 
45*bf2c3715SXin Li     virtual ~Camera();
46*bf2c3715SXin Li 
47*bf2c3715SXin Li     Camera& operator=(const Camera& other);
48*bf2c3715SXin Li 
49*bf2c3715SXin Li     void setViewport(uint offsetx, uint offsety, uint width, uint height);
50*bf2c3715SXin Li     void setViewport(uint width, uint height);
51*bf2c3715SXin Li 
vpX(void)52*bf2c3715SXin Li     inline uint vpX(void) const { return mVpX; }
vpY(void)53*bf2c3715SXin Li     inline uint vpY(void) const { return mVpY; }
vpWidth(void)54*bf2c3715SXin Li     inline uint vpWidth(void) const { return mVpWidth; }
vpHeight(void)55*bf2c3715SXin Li     inline uint vpHeight(void) const { return mVpHeight; }
56*bf2c3715SXin Li 
fovY(void)57*bf2c3715SXin Li     inline float fovY(void) const { return mFovY; }
58*bf2c3715SXin Li     void setFovY(float value);
59*bf2c3715SXin Li 
60*bf2c3715SXin Li     void setPosition(const Eigen::Vector3f& pos);
position(void)61*bf2c3715SXin Li     inline const Eigen::Vector3f& position(void) const { return mFrame.position; }
62*bf2c3715SXin Li 
63*bf2c3715SXin Li     void setOrientation(const Eigen::Quaternionf& q);
orientation(void)64*bf2c3715SXin Li     inline const Eigen::Quaternionf& orientation(void) const { return mFrame.orientation; }
65*bf2c3715SXin Li 
66*bf2c3715SXin Li     void setFrame(const Frame& f);
frame(void)67*bf2c3715SXin Li     const Frame& frame(void) const { return mFrame; }
68*bf2c3715SXin Li 
69*bf2c3715SXin Li     void setDirection(const Eigen::Vector3f& newDirection);
70*bf2c3715SXin Li     Eigen::Vector3f direction(void) const;
71*bf2c3715SXin Li     void setUp(const Eigen::Vector3f& vectorUp);
72*bf2c3715SXin Li     Eigen::Vector3f up(void) const;
73*bf2c3715SXin Li     Eigen::Vector3f right(void) const;
74*bf2c3715SXin Li 
75*bf2c3715SXin Li     void setTarget(const Eigen::Vector3f& target);
target(void)76*bf2c3715SXin Li     inline const Eigen::Vector3f& target(void) { return mTarget; }
77*bf2c3715SXin Li 
78*bf2c3715SXin Li     const Eigen::Affine3f& viewMatrix(void) const;
79*bf2c3715SXin Li     const Eigen::Matrix4f& projectionMatrix(void) const;
80*bf2c3715SXin Li 
81*bf2c3715SXin Li     void rotateAroundTarget(const Eigen::Quaternionf& q);
82*bf2c3715SXin Li     void localRotate(const Eigen::Quaternionf& q);
83*bf2c3715SXin Li     void zoom(float d);
84*bf2c3715SXin Li 
85*bf2c3715SXin Li     void localTranslate(const Eigen::Vector3f& t);
86*bf2c3715SXin Li 
87*bf2c3715SXin Li     /** Setup OpenGL matrices and viewport */
88*bf2c3715SXin Li     void activateGL(void);
89*bf2c3715SXin Li 
90*bf2c3715SXin Li     Eigen::Vector3f unProject(const Eigen::Vector2f& uv, float depth, const Eigen::Matrix4f& invModelview) const;
91*bf2c3715SXin Li     Eigen::Vector3f unProject(const Eigen::Vector2f& uv, float depth) const;
92*bf2c3715SXin Li 
93*bf2c3715SXin Li   protected:
94*bf2c3715SXin Li     void updateViewMatrix(void) const;
95*bf2c3715SXin Li     void updateProjectionMatrix(void) const;
96*bf2c3715SXin Li 
97*bf2c3715SXin Li   protected:
98*bf2c3715SXin Li 
99*bf2c3715SXin Li     uint mVpX, mVpY;
100*bf2c3715SXin Li     uint mVpWidth, mVpHeight;
101*bf2c3715SXin Li 
102*bf2c3715SXin Li     Frame mFrame;
103*bf2c3715SXin Li 
104*bf2c3715SXin Li     mutable Eigen::Affine3f mViewMatrix;
105*bf2c3715SXin Li     mutable Eigen::Matrix4f mProjectionMatrix;
106*bf2c3715SXin Li 
107*bf2c3715SXin Li     mutable bool mViewIsUptodate;
108*bf2c3715SXin Li     mutable bool mProjIsUptodate;
109*bf2c3715SXin Li 
110*bf2c3715SXin Li     // used by rotateAroundTarget
111*bf2c3715SXin Li     Eigen::Vector3f mTarget;
112*bf2c3715SXin Li 
113*bf2c3715SXin Li     float mFovY;
114*bf2c3715SXin Li     float mNearDist;
115*bf2c3715SXin Li     float mFarDist;
116*bf2c3715SXin Li };
117*bf2c3715SXin Li 
118*bf2c3715SXin Li #endif // EIGEN_CAMERA_H
119