From 94a2fa59ec4c44a9ece02c4f8b992fe2db6ca0c9 Mon Sep 17 00:00:00 2001
From: Anakin <Battlefrontler@t-online.de>
Date: Sat, 4 Feb 2017 15:48:10 +0100
Subject: [PATCH] added MoveCamera but isn't working well updated about text,

---
 QtMeshViewer/Header/CameraInterface.h   |  2 +-
 QtMeshViewer/Header/MoveCamera.h        |  4 +++-
 QtMeshViewer/Resources/about.txt        | 13 ++++++++++
 QtMeshViewer/Source/MoveCamera.cpp      | 29 ++++++++++++++++++++--
 QtMeshViewer/Source/OglViewerWidget.cpp | 32 +++++++++++++++++++++++++
 QtMeshViewer/Source/OrbitCamera.cpp     |  7 +++---
 6 files changed, 79 insertions(+), 8 deletions(-)

diff --git a/QtMeshViewer/Header/CameraInterface.h b/QtMeshViewer/Header/CameraInterface.h
index 0be00db..03959dd 100644
--- a/QtMeshViewer/Header/CameraInterface.h
+++ b/QtMeshViewer/Header/CameraInterface.h
@@ -19,7 +19,7 @@ public:
 	virtual void rotateAction(QVector2D diff) = 0;
 	virtual void moveAction(QVector2D diff) = 0;
 	virtual void wheelAction(double value) = 0;
-	virtual void resetView() { m_matrix = QMatrix4x4(); m_zSpeed = 1.0; };
+	virtual void resetView() { m_matrix = QMatrix4x4(); };
 
 	virtual void recalculateMatrix() = 0;
 	virtual QMatrix4x4 getMatrix() { recalculateMatrix(); return m_matrix; };
diff --git a/QtMeshViewer/Header/MoveCamera.h b/QtMeshViewer/Header/MoveCamera.h
index 5a4d7d5..f138ddc 100644
--- a/QtMeshViewer/Header/MoveCamera.h
+++ b/QtMeshViewer/Header/MoveCamera.h
@@ -10,7 +10,9 @@ public:
 
 	// attributes
 private:
-
+	QVector4D m_direction;
+	QVector4D m_position;
+	QVector3D m_up;
 
 	// functions
 public:
diff --git a/QtMeshViewer/Resources/about.txt b/QtMeshViewer/Resources/about.txt
index 779ecdb..0a27201 100644
--- a/QtMeshViewer/Resources/about.txt
+++ b/QtMeshViewer/Resources/about.txt
@@ -4,9 +4,22 @@ source code: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/QtMeshViewer
 
 ===============================================================
 Controll:
+
+Free Camera: static view position and you rotate and move the object
 left mouse - rotate
 right mouse - move
 scroll - zoom
+
+Orbit Camera: static object and you move around it like a satalite
+left mouse - rotate
+scroll - zoom
+
+Move Camera: static object and you can walk through the scene
+w/s - forward/backward
+a/d - left/right
+left mouse - look around
+
+General:
 space - reset view
 L - set light to current position
 esc - close
diff --git a/QtMeshViewer/Source/MoveCamera.cpp b/QtMeshViewer/Source/MoveCamera.cpp
index fee8054..8b6da49 100644
--- a/QtMeshViewer/Source/MoveCamera.cpp
+++ b/QtMeshViewer/Source/MoveCamera.cpp
@@ -2,6 +2,16 @@
 #include <QVector2D>
 
 
+int sgn(double value)
+{
+	if (value > 0)
+		return 1;
+	else if (value < 0)
+		return -1;
+	else
+		return 0;
+}
+
 /////////////////////////////////////////////////////////////////////////
 // constructor/destructor
 
@@ -21,26 +31,41 @@ MoveCamera::~MoveCamera()
 
 void MoveCamera::rotateAction(QVector2D diff)
 {
+	QMatrix4x4 rot;
+	rot.rotate(-diff.x() * 0.5f, 0, 1, 0);
+	rot.rotate(-diff.y() * 0.5f, 1, 0, 0);
 
+	m_direction = rot * m_direction;
+	m_direction.normalize();
+
+	m_up = QVector3D::crossProduct(m_direction.toVector3D(), QVector3D::crossProduct(QVector3D(0,1,0), m_direction.toVector3D()));
+	m_up.normalize();
 }
 
 void MoveCamera::moveAction(QVector2D diff)
 {
-
+	QVector3D sideDirection = QVector3D::crossProduct(QVector3D(0, 1, 0), m_direction.toVector3D());
+	
+	m_position += sgn(diff.y()) * 0.1 * m_zSpeed * QVector4D(sideDirection, 0);
 }
 
 void MoveCamera::wheelAction(double value)
 {
-
+	m_position -= sgn(value) * 0.1 * m_zSpeed * m_direction;
 }
 
 void MoveCamera::recalculateMatrix()
 {
+	m_matrix = QMatrix4x4();
 
+	m_matrix.lookAt(m_position.toVector3D(), m_position.toVector3D() - m_direction.toVector3D(), m_up);
 }
 
 void MoveCamera::resetView()
 {
+	m_position = { 0,0,4,1 };
+	m_direction = { 0,0,1,0 };
+	m_up = { 0,1,0 };
 
 	CameraInterface::resetView();
 }
diff --git a/QtMeshViewer/Source/OglViewerWidget.cpp b/QtMeshViewer/Source/OglViewerWidget.cpp
index 11893fe..068bf4c 100644
--- a/QtMeshViewer/Source/OglViewerWidget.cpp
+++ b/QtMeshViewer/Source/OglViewerWidget.cpp
@@ -235,6 +235,38 @@ void OglViewerWidget::keyPressEvent(QKeyEvent *e)
 	{
 		resetView();
 	}
+	else if (e->key() == Qt::Key_W)
+	{
+		emit m_camera->wheelAction(1);
+
+		if (m_light.headlight)
+			updateLightPosition();
+		update();
+	}
+	else if (e->key() == Qt::Key_S)
+	{
+		emit m_camera->wheelAction(-1);
+
+		if (m_light.headlight)
+			updateLightPosition();
+		update();
+	}
+	else if (e->key() == Qt::Key_A)
+	{
+		emit m_camera->moveAction(QVector2D(0, -1));
+
+		if (m_light.headlight)
+			updateLightPosition();
+		update();
+	}
+	else if (e->key() == Qt::Key_D)
+	{
+		emit m_camera->moveAction(QVector2D(0, 1));
+
+		if (m_light.headlight)
+			updateLightPosition();
+		update();
+	}
 	else if (e->key() == Qt::Key_Escape)
 	{
 		parentWidget()->close();
diff --git a/QtMeshViewer/Source/OrbitCamera.cpp b/QtMeshViewer/Source/OrbitCamera.cpp
index bfb3055..b1c8585 100644
--- a/QtMeshViewer/Source/OrbitCamera.cpp
+++ b/QtMeshViewer/Source/OrbitCamera.cpp
@@ -27,7 +27,6 @@ void OrbitCamera::rotateAction(QVector2D diff)
 	m_theta -= diff.y() * 0.01;
 
 	m_theta = qMax(qMin(M_PI - 0.0001, m_theta), 0.0001);
-
 }
 
 void OrbitCamera::moveAction(QVector2D diff)
@@ -51,14 +50,14 @@ void OrbitCamera::recalculateMatrix()
 	tmpPosition.setZ(qCos(m_theta));
 
 	m_matrix.lookAt(m_roh * tmpPosition, QVector3D(0,0,0), QVector3D(0, 0, 1));
-	m_matrix.rotate(90, 90, 0);
-	
+	m_matrix.rotate(90, 1, 0, 0);
+	m_matrix.rotate(90, 0, 1, 0);
 }
 
 void OrbitCamera::resetView()
 {
 	m_roh = 4;
-	m_phi = -M_PI_2;
+	m_phi = 0; //-M_PI_2;
 	m_theta = M_PI_2;
 	CameraInterface::resetView();
 }
-- 
GitLab