-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderCamera.cpp
More file actions
218 lines (190 loc) · 5 KB
/
Copy pathRenderCamera.cpp
File metadata and controls
218 lines (190 loc) · 5 KB
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
//
// This is the camera for the rendering of the scene ONLY.
//
// (c) Georg Umlauf, 2021
//
#include "RenderCamera.h"
#include "GLConvenience.h"
#include "QtConvenience.h"
RenderCamera::RenderCamera(QObject* parent) :
QObject(parent),
xRotation(0),
yRotation(0),
zRotation(0)
{
projectionMatrix.setToIdentity();
cameraMatrix. setToIdentity();
worldMatrix. setToIdentity();
renderMatrix = getRenderMatrix();
}
void RenderCamera::setup()
{
// position and angles
QMatrix4x4 cm;
cm.setToIdentity();
cm.translate(position.x(),position.y(),position.z());
cm.rotate (xRotation, 1, 0, 0);
cm.rotate (yRotation, 0, 1, 0);
cm.rotate (zRotation, 0, 0, 1);
setCameraMatrix(cm);
// the world is still for now, thus nothing to do for renderCamera->setWorldMatrix
}
void RenderCamera::reset()
{
xRotation=0;
yRotation=0;
zRotation=0;
setPosition(QVector3D(-0.05f, -0.0f, -0.1f));
rotate(0, 130, 30);
renderMatrix = getRenderMatrix();
}
void RenderCamera::forward()
{
position[2] += TranslationSTEP;
emit changed();
}
void RenderCamera::backward()
{
position[2] -= TranslationSTEP;
emit changed();
}
void RenderCamera::left()
{
position[0] += TranslationSTEP;
emit changed();
}
void RenderCamera::right()
{
position[0] -= TranslationSTEP;
emit changed();
}
void RenderCamera::up()
{
position[1] -= TranslationSTEP;
emit changed();
}
void RenderCamera::down()
{
position[1] += TranslationSTEP;
emit changed();
}
void RenderCamera::setPosition(const QVector3D& _position)
{
position = _position;
}
void RenderCamera::setProjectionMatrix(const QMatrix4x4& P)
{
projectionMatrix = P;
renderMatrix = getRenderMatrix();
}
void RenderCamera::setCameraMatrix(const QMatrix4x4& C)
{
cameraMatrix = C;
renderMatrix = getRenderMatrix();
}
void RenderCamera::setWorldMatrix(const QMatrix4x4& W)
{
worldMatrix = W;
renderMatrix = getRenderMatrix();
}
void RenderCamera::setXRotation(int angle)
{
angle = angle % (RotationBASE * RotationSTEP);
if (angle != xRotation) {
xRotation = angle;
emit changed();
}
}
void RenderCamera::setYRotation(int angle)
{
angle = angle % (RotationBASE * RotationSTEP);
if (angle != yRotation) {
yRotation = angle;
emit changed();
}
}
void RenderCamera::setZRotation(int angle)
{
angle = angle % (RotationBASE * RotationSTEP);
if (angle != zRotation) {
zRotation = angle;
emit changed();
}
}
void RenderCamera::rotate(int dx, int dy, int dz)
{
setXRotation(xRotation + dx);
setYRotation(yRotation + dy);
setZRotation(zRotation + dz);
}
QMatrix4x4 RenderCamera::getRenderMatrix() const
{
QMatrix4x4 mvMatrix = cameraMatrix * worldMatrix;
mvMatrix.scale(0.05f); // make it small
mvMatrix = projectionMatrix * mvMatrix;
return mvMatrix;
}
QMatrix4x4 RenderCamera::getViewMatrix() const
{
return projectionMatrix * cameraMatrix * worldMatrix;
}
void RenderCamera::renderPoint(const QVector3D& p,
const QColor& color,
float pointSize) const
{
glPointSize(fmaxf(1.0f,pointSize));
glBegin(GL_POINTS);
glColor3f(color);
glVertex3f(renderMatrix ^ p);
glEnd();
}
void RenderCamera::renderPoint(const QVector4D& p,
const QColor& color,
float pointSize) const
{
renderPoint(QVector3D(p[0],p[1],p[2]),color,pointSize);
}
void RenderCamera::renderLine (const QVector3D& a,
const QVector3D& b,
const QColor& color,
float lineWidth) const
{
glLineWidth(fmaxf(1.0f,lineWidth));
glBegin(GL_LINES);
glColor4f(color);
glVertex3f(renderMatrix ^ a);
glVertex3f(renderMatrix ^ b);
glEnd();
}
void RenderCamera::renderLine (const QVector4D& a,
const QVector4D& b,
const QColor& color,
float lineWidth) const
{
renderLine(QVector3D(a[0],a[1],a[2]),QVector3D(b[0],b[1],b[2]),color,lineWidth);
}
void RenderCamera::renderPlane(const QVector3D& a,
const QVector3D& b,
const QVector3D& c,
const QVector3D& d,
const QColor& color,
float alpha) const
{
glBegin(GL_QUADS);
glColor4f(color,fminf(fmaxf(0.0f,alpha),1.0f));
glVertex3f(renderMatrix ^ a);
glVertex3f(renderMatrix ^ b);
glVertex3f(renderMatrix ^ c);
glVertex3f(renderMatrix ^ d);
glEnd();
}
void RenderCamera::renderPCL (const QVector<QVector4D>& pcl,
const QColor& color,
float pointSize) const
{
glPointSize(fmaxf(1.0f,pointSize));
glBegin(GL_POINTS);
glColor3f(color);
for (const auto & p: pcl) glVertex3f(renderMatrix ^ p);
glEnd();
}