fixed camera control,

removed test exe
This commit is contained in:
Anakin 2017-02-05 12:34:24 +01:00
parent a07d8acbec
commit 2d335474bf
6 changed files with 79 additions and 39 deletions

View File

@ -10,9 +10,11 @@ public:
// attributes
private:
QVector4D m_direction;
QVector4D m_position;
QVector3D m_up;
QVector3D m_position;
double m_phi;
double m_theta;
int m_forwardSpeed;
int m_sidewardSpeed;
// functions
public:

View File

@ -66,6 +66,7 @@ protected:
void wheelEvent(QWheelEvent *e) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
void keyReleaseEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
void dragEnterEvent(QDragEnterEvent *e) Q_DECL_OVERRIDE;
void dropEvent(QDropEvent * event) Q_DECL_OVERRIDE;

View File

@ -1,17 +1,8 @@
#include "..\Header\MoveCamera.h"
#include <QVector2D>
#include <qmath.h>
int sgn(double value)
{
if (value > 0)
return 1;
else if (value < 0)
return -1;
else
return 0;
}
/////////////////////////////////////////////////////////////////////////
// constructor/destructor
@ -31,41 +22,61 @@ 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_phi -= diff.x() * 0.01;
m_theta += diff.y() * 0.01;
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();
m_theta = qMax(qMin(M_PI - 0.0001, m_theta), 0.0001);
}
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);
if (diff.y() > 0)
m_sidewardSpeed = 1;
else if (diff.y() < 0)
m_sidewardSpeed = -1;
else
m_sidewardSpeed = 0;
}
void MoveCamera::wheelAction(double value)
{
m_position -= sgn(value) * 0.1 * m_zSpeed * m_direction;
if (value > 0)
m_forwardSpeed = 1;
else if (value < 0)
m_forwardSpeed = -1;
else
m_forwardSpeed = 0;
}
void MoveCamera::recalculateMatrix()
{
m_matrix = QMatrix4x4();
m_matrix.lookAt(m_position.toVector3D(), m_position.toVector3D() - m_direction.toVector3D(), m_up);
// different coordinate (spherical -> world) X->Z | Y->X | Z->Y
QVector3D tmpdirection(
qSin(m_theta) * qSin(m_phi),
qCos(m_theta),
qSin(m_theta) * qCos(m_phi)
);
QVector3D tmpRight(
qSin(m_phi - M_PI_2),
0,
qCos(m_phi - M_PI_2)
);
m_position += m_forwardSpeed * m_zSpeed * 0.1 * tmpdirection;
m_position += m_sidewardSpeed * m_zSpeed * 0.1 * tmpRight;
m_matrix.lookAt(m_position, m_position + tmpdirection, QVector3D::crossProduct(tmpRight, tmpdirection));
}
void MoveCamera::resetView()
{
m_position = { 0,0,4,1 };
m_direction = { 0,0,1,0 };
m_up = { 0,1,0 };
m_position = { 0,0,4 };
m_phi = M_PI;
m_theta = M_PI_2;
m_forwardSpeed = 0;
m_sidewardSpeed = 0;
CameraInterface::resetView();
}

View File

@ -278,6 +278,26 @@ void OglViewerWidget::keyPressEvent(QKeyEvent *e)
}
}
void OglViewerWidget::keyReleaseEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_W || e->key() == Qt::Key_S)
{
emit m_camera->wheelAction(0);
if (m_light.headlight)
updateLightPosition();
update();
}
else if (e->key() == Qt::Key_A || e->key() == Qt::Key_D)
{
emit m_camera->moveAction(QVector2D(0, 0));
if (m_light.headlight)
updateLightPosition();
update();
}
}
void OglViewerWidget::dragEnterEvent(QDragEnterEvent *e)
{
if (e->mimeData()->hasUrls())

View File

@ -26,7 +26,7 @@ void OrbitCamera::rotateAction(QVector2D diff)
m_phi -= diff.x() * 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)
@ -44,20 +44,26 @@ void OrbitCamera::recalculateMatrix()
{
m_matrix = QMatrix4x4();
QVector3D tmpPosition;
tmpPosition.setX(qSin(m_theta) * qCos(m_phi));
tmpPosition.setY(qSin(m_theta) * qSin(m_phi));
tmpPosition.setZ(qCos(m_theta));
// different coordinate (spherical -> world) X->Z | Y->X | Z->Y
QVector3D tmpPosition(
qSin(m_theta) * qSin(m_phi),
qCos(m_theta),
qSin(m_theta) * qCos(m_phi)
);
m_matrix.lookAt(m_roh * tmpPosition, QVector3D(0,0,0), QVector3D(0, 0, 1));
m_matrix.rotate(90, 1, 0, 0);
m_matrix.rotate(90, 0, 1, 0);
QVector3D tmpRight(
qSin(m_phi - M_PI_2),
0,
qCos(m_phi - M_PI_2)
);
m_matrix.lookAt(m_roh * tmpPosition, QVector3D(0,0,0), QVector3D::crossProduct(tmpRight, tmpPosition));
}
void OrbitCamera::resetView()
{
m_roh = 4;
m_phi = 0; //-M_PI_2;
m_phi = 0;
m_theta = M_PI_2;
CameraInterface::resetView();
}

Binary file not shown.