parent
a07d8acbec
commit
2d335474bf
|
@ -10,9 +10,11 @@ public:
|
||||||
|
|
||||||
// attributes
|
// attributes
|
||||||
private:
|
private:
|
||||||
QVector4D m_direction;
|
QVector3D m_position;
|
||||||
QVector4D m_position;
|
double m_phi;
|
||||||
QVector3D m_up;
|
double m_theta;
|
||||||
|
int m_forwardSpeed;
|
||||||
|
int m_sidewardSpeed;
|
||||||
|
|
||||||
// functions
|
// functions
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -66,6 +66,7 @@ protected:
|
||||||
void wheelEvent(QWheelEvent *e) Q_DECL_OVERRIDE;
|
void wheelEvent(QWheelEvent *e) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
void keyPressEvent(QKeyEvent *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 dragEnterEvent(QDragEnterEvent *e) Q_DECL_OVERRIDE;
|
||||||
void dropEvent(QDropEvent * event) Q_DECL_OVERRIDE;
|
void dropEvent(QDropEvent * event) Q_DECL_OVERRIDE;
|
||||||
|
|
|
@ -1,17 +1,8 @@
|
||||||
#include "..\Header\MoveCamera.h"
|
#include "..\Header\MoveCamera.h"
|
||||||
#include <QVector2D>
|
#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
|
// constructor/destructor
|
||||||
|
|
||||||
|
@ -31,41 +22,61 @@ MoveCamera::~MoveCamera()
|
||||||
|
|
||||||
void MoveCamera::rotateAction(QVector2D diff)
|
void MoveCamera::rotateAction(QVector2D diff)
|
||||||
{
|
{
|
||||||
QMatrix4x4 rot;
|
m_phi -= diff.x() * 0.01;
|
||||||
rot.rotate(-diff.x() * 0.5f, 0, 1, 0);
|
m_theta += diff.y() * 0.01;
|
||||||
rot.rotate(-diff.y() * 0.5f, 1, 0, 0);
|
|
||||||
|
|
||||||
m_direction = rot * m_direction;
|
m_theta = qMax(qMin(M_PI - 0.0001, m_theta), 0.0001);
|
||||||
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());
|
if (diff.y() > 0)
|
||||||
|
m_sidewardSpeed = 1;
|
||||||
m_position += sgn(diff.y()) * 0.1 * m_zSpeed * QVector4D(sideDirection, 0);
|
else if (diff.y() < 0)
|
||||||
|
m_sidewardSpeed = -1;
|
||||||
|
else
|
||||||
|
m_sidewardSpeed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveCamera::wheelAction(double value)
|
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()
|
void MoveCamera::recalculateMatrix()
|
||||||
{
|
{
|
||||||
m_matrix = QMatrix4x4();
|
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()
|
void MoveCamera::resetView()
|
||||||
{
|
{
|
||||||
m_position = { 0,0,4,1 };
|
m_position = { 0,0,4 };
|
||||||
m_direction = { 0,0,1,0 };
|
m_phi = M_PI;
|
||||||
m_up = { 0,1,0 };
|
m_theta = M_PI_2;
|
||||||
|
m_forwardSpeed = 0;
|
||||||
|
m_sidewardSpeed = 0;
|
||||||
CameraInterface::resetView();
|
CameraInterface::resetView();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
void OglViewerWidget::dragEnterEvent(QDragEnterEvent *e)
|
||||||
{
|
{
|
||||||
if (e->mimeData()->hasUrls())
|
if (e->mimeData()->hasUrls())
|
||||||
|
|
|
@ -26,7 +26,7 @@ void OrbitCamera::rotateAction(QVector2D diff)
|
||||||
m_phi -= diff.x() * 0.01;
|
m_phi -= diff.x() * 0.01;
|
||||||
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)
|
||||||
|
@ -44,20 +44,26 @@ void OrbitCamera::recalculateMatrix()
|
||||||
{
|
{
|
||||||
m_matrix = QMatrix4x4();
|
m_matrix = QMatrix4x4();
|
||||||
|
|
||||||
QVector3D tmpPosition;
|
// different coordinate (spherical -> world) X->Z | Y->X | Z->Y
|
||||||
tmpPosition.setX(qSin(m_theta) * qCos(m_phi));
|
QVector3D tmpPosition(
|
||||||
tmpPosition.setY(qSin(m_theta) * qSin(m_phi));
|
qSin(m_theta) * qSin(m_phi),
|
||||||
tmpPosition.setZ(qCos(m_theta));
|
qCos(m_theta),
|
||||||
|
qSin(m_theta) * qCos(m_phi)
|
||||||
|
);
|
||||||
|
|
||||||
m_matrix.lookAt(m_roh * tmpPosition, QVector3D(0,0,0), QVector3D(0, 0, 1));
|
QVector3D tmpRight(
|
||||||
m_matrix.rotate(90, 1, 0, 0);
|
qSin(m_phi - M_PI_2),
|
||||||
m_matrix.rotate(90, 0, 1, 0);
|
0,
|
||||||
|
qCos(m_phi - M_PI_2)
|
||||||
|
);
|
||||||
|
|
||||||
|
m_matrix.lookAt(m_roh * tmpPosition, QVector3D(0,0,0), QVector3D::crossProduct(tmpRight, tmpPosition));
|
||||||
}
|
}
|
||||||
|
|
||||||
void OrbitCamera::resetView()
|
void OrbitCamera::resetView()
|
||||||
{
|
{
|
||||||
m_roh = 4;
|
m_roh = 4;
|
||||||
m_phi = 0; //-M_PI_2;
|
m_phi = 0;
|
||||||
m_theta = M_PI_2;
|
m_theta = M_PI_2;
|
||||||
CameraInterface::resetView();
|
CameraInterface::resetView();
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue