added MoveCamera but isn't working well

updated about text,
This commit is contained in:
Anakin 2017-02-04 15:48:10 +01:00
parent 648b805daf
commit 94a2fa59ec
6 changed files with 79 additions and 8 deletions

View File

@ -19,7 +19,7 @@ public:
virtual void rotateAction(QVector2D diff) = 0; virtual void rotateAction(QVector2D diff) = 0;
virtual void moveAction(QVector2D diff) = 0; virtual void moveAction(QVector2D diff) = 0;
virtual void wheelAction(double value) = 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 void recalculateMatrix() = 0;
virtual QMatrix4x4 getMatrix() { recalculateMatrix(); return m_matrix; }; virtual QMatrix4x4 getMatrix() { recalculateMatrix(); return m_matrix; };

View File

@ -10,7 +10,9 @@ public:
// attributes // attributes
private: private:
QVector4D m_direction;
QVector4D m_position;
QVector3D m_up;
// functions // functions
public: public:

View File

@ -4,9 +4,22 @@ source code: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/QtMeshViewer
=============================================================== ===============================================================
Controll: Controll:
Free Camera: static view position and you rotate and move the object
left mouse - rotate left mouse - rotate
right mouse - move right mouse - move
scroll - zoom 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 space - reset view
L - set light to current position L - set light to current position
esc - close esc - close

View File

@ -2,6 +2,16 @@
#include <QVector2D> #include <QVector2D>
int sgn(double value)
{
if (value > 0)
return 1;
else if (value < 0)
return -1;
else
return 0;
}
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// constructor/destructor // constructor/destructor
@ -21,26 +31,41 @@ MoveCamera::~MoveCamera()
void MoveCamera::rotateAction(QVector2D diff) 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) 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) void MoveCamera::wheelAction(double value)
{ {
m_position -= sgn(value) * 0.1 * m_zSpeed * m_direction;
} }
void MoveCamera::recalculateMatrix() void MoveCamera::recalculateMatrix()
{ {
m_matrix = QMatrix4x4();
m_matrix.lookAt(m_position.toVector3D(), m_position.toVector3D() - m_direction.toVector3D(), m_up);
} }
void MoveCamera::resetView() void MoveCamera::resetView()
{ {
m_position = { 0,0,4,1 };
m_direction = { 0,0,1,0 };
m_up = { 0,1,0 };
CameraInterface::resetView(); CameraInterface::resetView();
} }

View File

@ -235,6 +235,38 @@ void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{ {
resetView(); 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) else if (e->key() == Qt::Key_Escape)
{ {
parentWidget()->close(); parentWidget()->close();

View File

@ -27,7 +27,6 @@ void OrbitCamera::rotateAction(QVector2D diff)
m_theta -= diff.y() * 0.01; m_theta -= diff.y() * 0.01;
m_theta = qMax(qMin(M_PI - 0.0001, m_theta), 0.0001); m_theta = qMax(qMin(M_PI - 0.0001, m_theta), 0.0001);
} }
void OrbitCamera::moveAction(QVector2D diff) void OrbitCamera::moveAction(QVector2D diff)
@ -51,14 +50,14 @@ void OrbitCamera::recalculateMatrix()
tmpPosition.setZ(qCos(m_theta)); tmpPosition.setZ(qCos(m_theta));
m_matrix.lookAt(m_roh * tmpPosition, QVector3D(0,0,0), QVector3D(0, 0, 1)); 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() void OrbitCamera::resetView()
{ {
m_roh = 4; m_roh = 4;
m_phi = -M_PI_2; m_phi = 0; //-M_PI_2;
m_theta = M_PI_2; m_theta = M_PI_2;
CameraInterface::resetView(); CameraInterface::resetView();
} }