Commit 10852b77 authored by Stefan Pfeifer's avatar Stefan Pfeifer

Make view buttons (profile, top, 3d, fit) work again

parent cfd9e455
Pipeline #40481179 passed with stage
in 10 minutes and 20 seconds
......@@ -7,12 +7,7 @@
#include <math.h>
LimbView::LimbView()
: rot_x(0.0f),
rot_y(0.0f),
zoom(1.0f),
shift_x(0.0f),
shift_y(0.0f),
m_program(nullptr)
: shader_program(nullptr)
{
auto button0 = new QToolButton();
QObject::connect(button0, &QPushButton::clicked, this, &LimbView::viewProfile);
......@@ -67,28 +62,33 @@ LimbView::~LimbView()
void LimbView::setData(const InputData& data)
{
m_mesh.setData(data);
m_meshVbo.create();
m_meshVbo.bind();
m_meshVbo.allocate(m_mesh.vertexData().data(), m_mesh.vertexData().size()*sizeof(GLfloat));
limb_mesh.setData(data);
limb_mesh_vbo.create();
limb_mesh_vbo.bind();
limb_mesh_vbo.allocate(limb_mesh.vertexData().data(), limb_mesh.vertexData().size()*sizeof(GLfloat));
update();
}
void LimbView::viewProfile()
{
rot_x = 0.0f;
rot_y = 0.0f;
viewFit();
}
void LimbView::viewTop()
{
rot_x = 90.0f;
rot_y = 0.0f;
viewFit();
}
void LimbView::view3D()
{
rot_x = DEFAULT_ROT_X;
rot_y = DEFAULT_ROT_Y;
viewFit();
}
void LimbView::viewSymmetric(bool checked)
......@@ -98,18 +98,21 @@ void LimbView::viewSymmetric(bool checked)
void LimbView::viewFit()
{
shift_x = 0.0f;
shift_y = 0.0f;
zoom = DEFAULT_ZOOM;
update();
}
void LimbView::cleanup()
{
if(m_program == nullptr)
if(shader_program == nullptr)
return;
makeCurrent();
m_meshVbo.destroy();
delete m_program;
m_program = nullptr;
limb_mesh_vbo.destroy();
delete shader_program;
shader_program = nullptr;
doneCurrent();
}
......@@ -158,38 +161,38 @@ void LimbView::initializeGL()
initializeOpenGLFunctions();
glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
m_program = new QOpenGLShaderProgram;
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->bindAttributeLocation("modelPosition", 0);
m_program->bindAttributeLocation("modelNormal", 1);
m_program->bindAttributeLocation("modelColor", 2);
m_program->link();
m_program->bind();
m_projectionMatrixLoc = m_program->uniformLocation("projectionMatrix");
m_modelViewMatrixLoc = m_program->uniformLocation("modelViewMatrix");
m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
m_lightPositionLoc = m_program->uniformLocation("lightPosition");
// Setup our vertex buffer object.
m_meshVbo.create();
m_meshVbo.bind();
m_meshVbo.allocate(m_mesh.vertexData().data(), m_mesh.vertexData().size()*sizeof(GLfloat));
shader_program = new QOpenGLShaderProgram;
shader_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
shader_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
shader_program->bindAttributeLocation("modelPosition", 0);
shader_program->bindAttributeLocation("modelNormal", 1);
shader_program->bindAttributeLocation("modelColor", 2);
shader_program->link();
shader_program->bind();
loc_projectionMatrix = shader_program->uniformLocation("projectionMatrix");
loc_modelViewMatrix = shader_program->uniformLocation("modelViewMatrix");
loc_normalMatrix = shader_program->uniformLocation("normalMatrix");
loc_lightPosition = shader_program->uniformLocation("lightPosition");
// Setup vertex buffer object.
limb_mesh_vbo.create();
limb_mesh_vbo.bind();
limb_mesh_vbo.allocate(limb_mesh.vertexData().data(), limb_mesh.vertexData().size()*sizeof(GLfloat));
// Store the vertex attribute bindings for the program.
m_meshVbo.bind();
limb_mesh_vbo.bind();
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9*sizeof(GLfloat), (void*)(0*sizeof(GLfloat)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9*sizeof(GLfloat), (void*)(6*sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
m_meshVbo.release();
limb_mesh_vbo.release();
// Light position is fixed.
m_program->setUniformValue(m_lightPositionLoc, QVector3D(0.0f, 0.0f, 50.0f));
m_program->release();
// Set fixed light position
shader_program->setUniformValue(loc_lightPosition, QVector3D(0.0f, 0.0f, 50.0f));
shader_program->release();
}
void LimbView::paintGL()
......@@ -201,8 +204,8 @@ void LimbView::paintGL()
m_world.setToIdentity();
m_world.rotate(180.0f - rot_x, 1.0f, 0.0f, 0.0f);
m_world.rotate(rot_y, 0.0f, 1.0f, 0.0f);
m_world.scale(1.0f/m_mesh.aabbDiagonal());
m_world.translate(-m_mesh.aabbCenter());
m_world.scale(1.0f/limb_mesh.aabbDiagonal());
m_world.translate(-limb_mesh.aabbCenter());
m_camera.setToIdentity();
m_camera.translate(0.0f, 0.0f, -1.0f);
......@@ -215,29 +218,32 @@ void LimbView::paintGL()
( 0.5f*zoom + shift_y)*aspect_ratio,
0.001f, 100.0f);
m_program->bind();
m_program->setUniformValue(m_projectionMatrixLoc, m_projection);
m_program->setUniformValue(m_modelViewMatrixLoc, m_camera*m_world);
m_program->setUniformValue(m_normalMatrixLoc, m_world.normalMatrix());
shader_program->bind();
shader_program->setUniformValue(loc_projectionMatrix, m_projection);
shader_program->setUniformValue(loc_modelViewMatrix, m_camera*m_world);
shader_program->setUniformValue(loc_normalMatrix, m_world.normalMatrix());
glDrawArrays(GL_TRIANGLES, 0, m_mesh.vertexCount());
m_program->release();
glDrawArrays(GL_TRIANGLES, 0, limb_mesh.vertexCount());
shader_program->release();
}
void LimbView::mousePressEvent(QMouseEvent *event)
{
last_mouse_pos = event->pos();
mouse_pos = event->pos();
}
void LimbView::mouseMoveEvent(QMouseEvent *event)
{
int delta_x = event->x() - last_mouse_pos.x();
int delta_y = event->y() - last_mouse_pos.y();
int delta_x = event->x() - mouse_pos.x();
int delta_y = event->y() - mouse_pos.y();
if(event->buttons() & Qt::LeftButton)
{
rot_x += ROT_SPEED*delta_y;
rot_y += ROT_SPEED*delta_x;
qInfo() << rot_x << ", " << rot_y;
update();
}
else if(event->buttons() & Qt::MiddleButton)
......@@ -247,12 +253,12 @@ void LimbView::mouseMoveEvent(QMouseEvent *event)
update();
}
last_mouse_pos = event->pos();
mouse_pos = event->pos();
}
void LimbView::wheelEvent(QWheelEvent* event)
{
float delta_zoom = -ZOOM_SPEED*event->angleDelta().y()/120.0f*zoom; // dividing by 120 gives the number of 15 degree steps on a standard mouse
float delta_zoom = -ZOOM_SPEED*event->angleDelta().y()/120.0f*zoom; // Dividing by 120 gives the number of 15 degree steps on a standard mouse
float mouse_ratio_x = float(event->x())/this->width();
float mouse_ratio_y = float(event->y())/this->height();
......
......@@ -11,9 +11,13 @@
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
// Based on Qt's "Hello GL2" example, http://doc.qt.io/qt-5/qtopengl-hellogl2-example.html
class LimbView: public QOpenGLWidget, protected QOpenGLFunctions
{
private:
const float DEFAULT_ROT_X = 20.0f; // Magic number
const float DEFAULT_ROT_Y = -30.0f; // Magic number
const float DEFAULT_ZOOM = 1.1f; // Magic number
const float ZOOM_SPEED = 0.2f; // Magic number
const float ROT_SPEED = 0.15f; // Magic number
......@@ -38,24 +42,23 @@ private:
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent* event) override;
QPoint last_mouse_pos;
LimbMesh limb_mesh;
QOpenGLBuffer limb_mesh_vbo;
float rot_x;
float rot_y;
float shift_x;
float shift_y;
float zoom;
LimbMesh m_mesh;
QOpenGLBuffer m_meshVbo;
QOpenGLShaderProgram* m_program;
int m_projectionMatrixLoc;
int m_modelViewMatrixLoc;
int m_normalMatrixLoc;
int m_lightPositionLoc;
QOpenGLShaderProgram* shader_program;
int loc_projectionMatrix;
int loc_modelViewMatrix;
int loc_normalMatrix;
int loc_lightPosition;
QMatrix4x4 m_projection;
QMatrix4x4 m_camera;
QMatrix4x4 m_world;
QPoint mouse_pos;
float shift_x;
float shift_y;
float rot_x;
float rot_y;
float zoom;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment