PCB Environment: /home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/UI/Camera.hpp Source File

PCB Environment 2

Loading...
Searching...
No Matches
Camera.hpp
1
2#ifndef GYM_PCB_UI_CAMERA_H
3#define GYM_PCB_UI_CAMERA_H
4
5#include "Py.hpp"
6#include "Math/Mat4.hpp"
7#include "Math/Misc.hpp"
8#include <QOpenGLFunctions_4_3_Core>
9#include <QPointF>
10
14 class Camera
15{
16public:
17 Camera();
18 ~Camera();
19
21 QPointF worldPosToNormalizedPos(const Point_2&) const;
22 Point_2 normalizedPosToWorldPos(const QPointF&) const;
23
24 bool isVisible(const Bbox_2 &bbox) const { return CGAL::do_overlap(mBbox, bbox); }
25
26 void loadUniforms();
27
28 float adjustLineWidth(float) const;
29
30 void setBaseDimensions(uint w, uint h, double unitsPerNanoMeter);
31 void setZoom(float, QPointF anchor = QPointF(0.5, 0.5));
32 void setOrigin(const Point_2&);
33 void setOriginRelative(const Vector_2&);
34 void setCenter(const Point_2&);
35 void setView(const Point_2 &origin, const Vector_2 &size);
36 void setZNear(float z);
37 void setZFar(float z);
38 void setZRange(float near, float far);
39
40 void moveBy(const Vector_2 &v) { setCenter(mCenter + v); }
41
42 const Bbox_2 getBbox() const { return mBbox; }
43 const float getZoom() const { return mZoom; }
44 const Vector_2& getDims() const { return mDims; }
45 const Point_2& getCornerMin() const { return mCornerBL; }
46 const Point_2& getCornerMax() const { return mCornerTR; }
47 const Point_2& getOrigin() const { return mCornerBL; }
48
49 const math::Mat4& getMVP() const { return mMVP; }
50
51 const float *getFloatMVP() const { return &mUniforms.MVP[0]; }
52 struct {
53 GLfloat MVP[16];
54 } mUniforms;
55
56 GLuint getUBO() const { return mUBO; }
57
58private:
59 math::Mat4 mMVP;
60 float mZoom{1.0f};
61 Vector_2 mBaseDims;
62 Vector_2 mDims;
63 Bbox_2 mBbox;
64 Point_2 mCornerBL{0,0};
65 Point_2 mCornerTR;
66 Point_2 mCenter;
67 float mZNear{-1.0f};
68 float mZFar{1.0f};
69 //float mAspectRatio{1.0f};
70 GLuint mUBO{0};
71
72 void update();
73};
74
75inline void Camera::setZNear(float z)
76{
77 setZRange(z, mZFar);
78}
79inline void Camera::setZFar(float z)
80{
81 setZRange(mZNear, z);
82}
83
84inline Point_2 Camera::normalizedPosToWorldPos(const QPointF &v) const
85{
86 return mCornerBL + Vector_2(mDims.x() * v.x(), mDims.y() * v.y());
87}
88 inline QPointF Camera::worldPosToNormalizedPos(const Point_2 &v) const
89{
90 auto dv = v - mCornerBL;
91 return QPointF(float(dv.x()) / mDims.x(), 1.0f - float(dv.y()) / mDims.y());
92}
93
94inline float Camera::adjustLineWidth(float lw) const
95{
96 return lw * math::approx::rsqrt(mZoom);
97}
98
99#endif // GYM_PCB_UI_CAMERA_H
Camera::worldPosToNormalizedPos
QPointF worldPosToNormalizedPos(const Point_2 &) const
normalizedPos is 0 to 1 within the GL widget (= camera view area)
Definition Camera.hpp:88
math::Mat4
Definition Mat4.hpp:12