OglViewerWidget.cpp 2.88 KB
Newer Older
1 2 3 4 5 6 7
#include "..\Header\OglViewerWidget.h"

#include <QMouseEvent>
#include <math.h>

OglViewerWidget::OglViewerWidget(QWidget *parent) :
	QOpenGLWidget(parent),
Anakin's avatar
Anakin committed
8
	m_dataEngine(0)
9
{
10
	setFocus();
11 12 13 14 15 16 17
}

OglViewerWidget::~OglViewerWidget()
{
	// Make sure the context is current when deleting the texture
	// and the buffers.
	makeCurrent();
Anakin's avatar
Anakin committed
18
	delete m_dataEngine;
19 20 21 22 23 24
	doneCurrent();
}

void OglViewerWidget::mousePressEvent(QMouseEvent *e)
{
	// Save mouse press position
25 26 27 28 29 30 31
	m_mouse.position = QVector2D(e->localPos());

	// Which button has been pressed?
	if (e->button() == Qt::LeftButton)
		m_mouse.left = true;
	else if (e->button() == Qt::RightButton)
		m_mouse.right = true;
32 33 34 35
}

void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e)
{
36 37 38 39 40
	if (e->button() == Qt::LeftButton)
		m_mouse.left = false;
	else if (e->button() == Qt::RightButton)
		m_mouse.right = false;
}
41

42 43 44 45 46 47
void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
{
	if (m_mouse.left)
	{
		// get the difference between last press and now
		QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
48

49 50
		// update the new position
		m_mouse.position = QVector2D(e->localPos());
51

52 53
		// calculate the rotation axis and rotate
		m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
54

55 56 57
		// request an update
		update();
	}
58 59
}

60
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
61
{
62 63
	if (e->key() == Qt::Key_Space)
		m_rotation = QQuaternion();
64

65
	update();
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

void OglViewerWidget::initializeGL()
{
	initializeOpenGLFunctions();

	glClearColor(0, 0, 0, 1);

	initShaders();

	// Enable depth buffer
	glEnable(GL_DEPTH_TEST);

	// Enable back face culling
	glEnable(GL_CULL_FACE);

Anakin's avatar
Anakin committed
82
	m_dataEngine = new GeometryEngine;
83 84 85 86 87 88

}

void OglViewerWidget::initShaders()
{
	// Compile vertex shader
Anakin's avatar
Anakin committed
89
	if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
90 91 92
		close();

	// Compile fragment shader
Anakin's avatar
Anakin committed
93
	if (!m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
94 95 96
		close();

	// Link shader pipeline
Anakin's avatar
Anakin committed
97
	if (!m_program.link())
98 99 100
		close();

	// Bind shader pipeline for use
Anakin's avatar
Anakin committed
101
	if (!m_program.bind())
102 103 104 105 106 107 108 109 110 111 112 113
		close();
}

void OglViewerWidget::resizeGL(int w, int h)
{
	// Calculate aspect ratio
	qreal aspect = qreal(w) / qreal(h ? h : 1);

	// 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;

	// Reset projection
114
	m_projection.setToIdentity();
115 116

	// Set perspective projection
117
	m_projection.perspective(fov, aspect, zNear, zFar);
118 119 120 121 122 123 124 125
}

void OglViewerWidget::paintGL()
{
	// Clear color and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Calculate model view transformation
Anakin's avatar
Anakin committed
126 127 128
	QMatrix4x4 view;
	view.translate(0.0, 0.0, -5.0);
	view.rotate(m_rotation);
129 130

	// Set modelview-projection matrix
Anakin's avatar
Anakin committed
131
	m_program.setUniformValue("mvp_matrix", m_projection * view);
132 133

	// Draw cube geometry
Anakin's avatar
Anakin committed
134
	m_dataEngine->drawGeometry(&m_program);
135
}