Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
OpenGL
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
C-Fu
OpenGL
Commits
faea3b07
Commit
faea3b07
authored
Dec 27, 2016
by
Anakin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removed timer rotation, now the user has full control,
added keyboard support, reset rotation with space
parent
a30f1b12
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
41 deletions
+45
-41
QtMeshViewer/Header/OglViewerWidget.h
QtMeshViewer/Header/OglViewerWidget.h
+9
-8
QtMeshViewer/Source/OglViewerWidget.cpp
QtMeshViewer/Source/OglViewerWidget.cpp
+36
-33
No files found.
QtMeshViewer/Header/OglViewerWidget.h
View file @
faea3b07
...
...
@@ -24,7 +24,8 @@ public:
protected:
void
mousePressEvent
(
QMouseEvent
*
e
)
Q_DECL_OVERRIDE
;
void
mouseReleaseEvent
(
QMouseEvent
*
e
)
Q_DECL_OVERRIDE
;
void
timerEvent
(
QTimerEvent
*
e
)
Q_DECL_OVERRIDE
;
void
mouseMoveEvent
(
QMouseEvent
*
e
)
Q_DECL_OVERRIDE
;
void
keyPressEvent
(
QKeyEvent
*
e
)
Q_DECL_OVERRIDE
;
void
initializeGL
()
Q_DECL_OVERRIDE
;
void
resizeGL
(
int
w
,
int
h
)
Q_DECL_OVERRIDE
;
...
...
@@ -34,17 +35,17 @@ protected:
void
initTextures
();
private:
QBasicTimer
timer
;
struct
{
bool
left
=
false
;
bool
right
=
false
;
QVector2D
position
;
}
m_mouse
;
QOpenGLShaderProgram
program
;
GeometryEngine
*
geometries
;
QOpenGLTexture
*
texture
;
QMatrix4x4
projection
;
QVector2D
mousePressPosition
;
QVector3D
rotationAxis
;
qreal
angularSpeed
;
QQuaternion
rotation
;
QMatrix4x4
m_projection
;
QQuaternion
m_rotation
;
};
QtMeshViewer/Source/OglViewerWidget.cpp
View file @
faea3b07
...
...
@@ -2,13 +2,14 @@
#include <QMouseEvent>
#include <math.h>
#include <iostream>
OglViewerWidget
::
OglViewerWidget
(
QWidget
*
parent
)
:
QOpenGLWidget
(
parent
),
geometries
(
0
),
texture
(
0
),
angularSpeed
(
0
)
texture
(
0
)
{
setFocus
();
}
OglViewerWidget
::~
OglViewerWidget
()
...
...
@@ -24,43 +25,47 @@ OglViewerWidget::~OglViewerWidget()
void
OglViewerWidget
::
mousePressEvent
(
QMouseEvent
*
e
)
{
// Save mouse press position
mousePressPosition
=
QVector2D
(
e
->
localPos
());
m_mouse
.
position
=
QVector2D
(
e
->
localPos
());
// Which button has been pressed?
if
(
e
->
button
()
==
Qt
::
LeftButton
)
m_mouse
.
left
=
true
;
else
if
(
e
->
button
()
==
Qt
::
RightButton
)
m_mouse
.
right
=
true
;
}
void
OglViewerWidget
::
mouseReleaseEvent
(
QMouseEvent
*
e
)
{
// Mouse release position - mouse press position
QVector2D
diff
=
QVector2D
(
e
->
localPos
())
-
mousePressPosition
;
if
(
e
->
button
()
==
Qt
::
LeftButton
)
m_mouse
.
left
=
false
;
else
if
(
e
->
button
()
==
Qt
::
RightButton
)
m_mouse
.
right
=
false
;
}
// Rotation axis is perpendicular to the mouse position difference
// vector
QVector3D
n
=
QVector3D
(
diff
.
y
(),
diff
.
x
(),
0.0
).
normalized
();
void
OglViewerWidget
::
mouseMoveEvent
(
QMouseEvent
*
e
)
{
if
(
m_mouse
.
left
)
{
// get the difference between last press and now
QVector2D
diff
=
QVector2D
(
e
->
localPos
())
-
m_mouse
.
position
;
// Accelerate angular speed relative to the length of the mouse sweep
qreal
acc
=
diff
.
length
()
/
100.0
;
// update the new position
m_mouse
.
position
=
QVector2D
(
e
->
localPos
())
;
// Calculate new rotation axis as weighted sum
rotationAxis
=
(
rotationAxis
*
angularSpeed
+
n
*
acc
).
normalized
()
;
// calculate the rotation axis and rotate
m_rotation
=
QQuaternion
::
fromAxisAndAngle
(
QVector3D
(
diff
.
y
(),
diff
.
x
(),
0.0
).
normalized
(),
diff
.
length
()
*
0.5
)
*
m_rotation
;
// Increase angular speed
angularSpeed
+=
acc
;
// request an update
update
();
}
}
void
OglViewerWidget
::
timerEvent
(
QTimerEvent
*
)
void
OglViewerWidget
::
keyPressEvent
(
QKeyEvent
*
e
)
{
// Decrease angular speed (friction)
angularSpeed
*=
0.99
;
// Stop rotation when speed goes below threshold
if
(
angularSpeed
<
0.01
)
{
angularSpeed
=
0.0
;
}
else
{
// Update rotation
rotation
=
QQuaternion
::
fromAxisAndAngle
(
rotationAxis
,
angularSpeed
)
*
rotation
;
if
(
e
->
key
()
==
Qt
::
Key_Space
)
m_rotation
=
QQuaternion
();
// Request an update
update
();
}
update
();
}
void
OglViewerWidget
::
initializeGL
()
...
...
@@ -80,8 +85,6 @@ void OglViewerWidget::initializeGL()
geometries
=
new
GeometryEngine
;
// Use QBasicTimer because its faster than QTimer
timer
.
start
(
12
,
this
);
}
void
OglViewerWidget
::
initShaders
()
...
...
@@ -128,10 +131,10 @@ void OglViewerWidget::resizeGL(int w, int h)
const
qreal
zNear
=
3.0
,
zFar
=
7.0
,
fov
=
45.0
;
// Reset projection
projection
.
setToIdentity
();
m_
projection
.
setToIdentity
();
// Set perspective projection
projection
.
perspective
(
fov
,
aspect
,
zNear
,
zFar
);
m_
projection
.
perspective
(
fov
,
aspect
,
zNear
,
zFar
);
}
void
OglViewerWidget
::
paintGL
()
...
...
@@ -144,10 +147,10 @@ void OglViewerWidget::paintGL()
// Calculate model view transformation
QMatrix4x4
matrix
;
matrix
.
translate
(
0.0
,
0.0
,
-
5.0
);
matrix
.
rotate
(
rotation
);
matrix
.
rotate
(
m_
rotation
);
// Set modelview-projection matrix
program
.
setUniformValue
(
"mvp_matrix"
,
projection
*
matrix
);
program
.
setUniformValue
(
"mvp_matrix"
,
m_
projection
*
matrix
);
// Use texture unit 0 which contains cube.png
program
.
setUniformValue
(
"texture"
,
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment