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

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

Anakin's avatar
Anakin committed
6 7 8 9

/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

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

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

Anakin's avatar
Anakin committed
26 27 28 29

/////////////////////////////////////////////////////////////////////////
// protected functions

30 31 32
void OglViewerWidget::mousePressEvent(QMouseEvent *e)
{
	// Save mouse press position
33 34 35 36 37 38 39
	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;
40 41 42 43
}

void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e)
{
44 45 46 47 48
	if (e->button() == Qt::LeftButton)
		m_mouse.left = false;
	else if (e->button() == Qt::RightButton)
		m_mouse.right = false;
}
49

50 51 52 53 54 55
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;
56

57 58
		// update the new position
		m_mouse.position = QVector2D(e->localPos());
59

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

63 64 65
		// request an update
		update();
	}
66 67
}

68
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
69
{
70 71
	if (e->key() == Qt::Key_Space)
		m_rotation = QQuaternion();
72

73
	update();
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

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
90
	m_dataEngine = new GeometryEngine;
91 92 93 94 95 96 97 98 99 100 101 102

}

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
103
	m_projection.setToIdentity();
104 105

	// Set perspective projection
106
	m_projection.perspective(fov, aspect, zNear, zFar);
107 108 109 110 111 112 113 114
}

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
115 116 117
	QMatrix4x4 view;
	view.translate(0.0, 0.0, -5.0);
	view.rotate(m_rotation);
118 119

	// Set modelview-projection matrix
Anakin's avatar
Anakin committed
120
	m_program.setUniformValue("mvp_matrix", m_projection * view);
121 122

	// Draw cube geometry
Anakin's avatar
Anakin committed
123
	m_dataEngine->drawGeometry(&m_program);
124
}
Anakin's avatar
Anakin committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151


/////////////////////////////////////////////////////////////////////////
// private functions

void OglViewerWidget::initShaders()
{
	// Compile vertex shader
	if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
		close();

	// Compile fragment shader
	if (!m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
		close();

	// Link shader pipeline
	if (!m_program.link())
		close();

	// Bind shader pipeline for use
	if (!m_program.bind())
		close();
}


/////////////////////////////////////////////////////////////////////////
// public functions