added movement and zoom

This commit is contained in:
Anakin 2016-12-30 13:28:07 +01:00
parent 353d932147
commit e863c68a77
3 changed files with 88 additions and 60 deletions

View File

@ -32,12 +32,14 @@ private:
GeometryEngine *m_dataEngine; GeometryEngine *m_dataEngine;
QMatrix4x4 m_projection; QMatrix4x4 m_projection;
QVector3D m_translation;
QQuaternion m_rotation; QQuaternion m_rotation;
protected: protected:
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *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 initializeGL() Q_DECL_OVERRIDE; void initializeGL() Q_DECL_OVERRIDE;

View File

@ -50,75 +50,75 @@ void GeometryEngine::initCubeGeometry()
auto msg = e.what(); auto msg = e.what();
} }
// Transfer vertex data to VBO 0 QVector<VertexData> vertices = {
m_arrayBuf.bind(); // Vertex data for face 0
m_arrayBuf.allocate(m_models->first()->segmList.front()->vertices.data(), m_models->first()->segmList.front()->vertices.size() * sizeof(VertexData)); {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)}, // v0
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)}, // v1
{QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.0f, 0.5f)}, // v2
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v3
// Transfer index data to VBO 1 // Vertex data for face 1
m_indexBuf.bind(); {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D( 0.0f, 0.5f)}, // v4
m_indexBuf.allocate(m_models->first()->segmList.front()->indices.data(), m_models->first()->segmList.front()->indices.size() * sizeof(GLushort)); {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.5f)}, // v5
{QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.0f, 1.0f)}, // v6
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v7
// Vertex data for face 2
{QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v8
{QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(1.0f, 0.5f)}, // v9
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)}, // v10
{QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(1.0f, 1.0f)}, // v11
//QVector<VertexData> vertices = { // Vertex data for face 3
// // Vertex data for face 0 {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v12
// {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)}, // v0 {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(1.0f, 0.0f)}, // v13
// {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)}, // v1 {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v14
// {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.0f, 0.5f)}, // v2 {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(1.0f, 0.5f)}, // v15
// {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v3
// // Vertex data for face 1 // Vertex data for face 4
// {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D( 0.0f, 0.5f)}, // v4 {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.0f)}, // v16
// {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.5f)}, // v5 {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v17
// {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.0f, 1.0f)}, // v6 {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v18
// {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v7 {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v19
// // Vertex data for face 2 // Vertex data for face 5
// {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v8 {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v20
// {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(1.0f, 0.5f)}, // v9 {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v21
// {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)}, // v10 {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v22
// {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(1.0f, 1.0f)}, // v11 {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)} // v23
};
// // Vertex data for face 3 QVector<GLushort> indices = {
// {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v12 0,1,2, //vorne (4)
// {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(1.0f, 0.0f)}, // v13 3,2,1,
// {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v14 4,5,7, //rechts (1)
// {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(1.0f, 0.5f)}, // v15 6,4,7,
8,9,11, //hinten (3)*
// // Vertex data for face 4 8,11,10,
// {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.0f)}, // v16 14,12,13, //links (6)*
// {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v17 14,13,15,
// {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v18 18,16,17, //unten (5)
// {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v19 19,18,17,
23,22,20, //oben (2)*
// // Vertex data for face 5 23,20,21
// {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v20 };
// {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v21
// {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v22
// {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)} // v23
// };
//QVector<GLushort> indices = {
// 0,1,2, //vorne (4)
// 3,2,1,
// 4,5,7, //rechts (1)
// 6,4,7,
// 8,9,11, //hinten (3)*
// 8,11,10,
// 14,12,13, //links (6)*
// 14,13,15,
// 18,16,17, //unten (5)
// 19,18,17,
// 23,22,20, //oben (2)*
// 23,20,21
//};
//// Transfer vertex data to VBO 0 //// Transfer vertex data to VBO 0
//m_arrayBuf.bind(); //m_arrayBuf.bind();
//m_arrayBuf.allocate(vertices.data(), vertices.size() * sizeof(VertexData)); //m_arrayBuf.allocate(m_models->first()->segmList.front()->vertices.data(), m_models->first()->segmList.front()->vertices.size() * sizeof(VertexData));
//// Transfer index data to VBO 1 //// Transfer index data to VBO 1
//m_indexBuf.bind(); //m_indexBuf.bind();
//m_indexBuf.allocate(indices.data(), indices.size() * sizeof(GLushort)); //m_indexBuf.allocate(m_models->first()->segmList.front()->indices.data(), m_models->first()->segmList.front()->indices.size() * sizeof(GLushort));
// Transfer vertex data to VBO 0
m_arrayBuf.bind();
m_arrayBuf.allocate(vertices.data(), vertices.size() * sizeof(VertexData));
// Transfer index data to VBO 1
m_indexBuf.bind();
m_indexBuf.allocate(indices.data(), indices.size() * sizeof(GLushort));
// load the texture // load the texture
initTexture(); initTexture();

View File

@ -2,6 +2,9 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <math.h> #include <math.h>
#include <iostream>
#define DEFAULT_Z_DISTANCE -5.0
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
@ -12,6 +15,7 @@ OglViewerWidget::OglViewerWidget(QWidget *parent) :
m_dataEngine(0) m_dataEngine(0)
{ {
setFocus(); setFocus();
m_translation.setZ(DEFAULT_Z_DISTANCE);
} }
OglViewerWidget::~OglViewerWidget() OglViewerWidget::~OglViewerWidget()
@ -63,13 +67,35 @@ void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
// request an update // request an update
update(); update();
} }
else if (m_mouse.right)
{
// get the difference between last press and now
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
// update the new position
m_mouse.position = QVector2D(e->localPos());
// calculate the translation
m_translation += {(float)(diff.x() * 0.01), (float)(diff.y() * -0.01), 0.0};
// request an update
update();
}
}
void OglViewerWidget::wheelEvent(QWheelEvent *e)
{
m_translation += {0.0, 0.0, (float)e->angleDelta().y() / 240};
update();
} }
void OglViewerWidget::keyPressEvent(QKeyEvent *e) void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{ {
if (e->key() == Qt::Key_Space) if (e->key() == Qt::Key_Space)
{
m_rotation = QQuaternion(); m_rotation = QQuaternion();
m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
}
update(); update();
} }
@ -97,7 +123,7 @@ void OglViewerWidget::resizeGL(int w, int h)
qreal aspect = qreal(w) / qreal(h ? h : 1); qreal aspect = qreal(w) / qreal(h ? h : 1);
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0; const qreal zNear = 0.1, zFar = 100.0, fov = 45.0;
// Reset projection // Reset projection
m_projection.setToIdentity(); m_projection.setToIdentity();
@ -113,7 +139,7 @@ void OglViewerWidget::paintGL()
// Calculate model view transformation // Calculate model view transformation
QMatrix4x4 view; QMatrix4x4 view;
view.translate(0.0, 0.0, -5.0); view.translate(m_translation);
view.rotate(m_rotation); view.rotate(m_rotation);
// Set modelview-projection matrix // Set modelview-projection matrix