src/gl/partrenderer.cpp

Wed, 01 Jan 2020 17:45:56 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 01 Jan 2020 17:45:56 +0200
changeset 21
0133e565e072
parent 18
918b6c0f8b5b
child 22
6da867fa5429
permissions
-rw-r--r--

things

#include <QMouseEvent>
#include <GL/glut.h> // teapot
#include "partrenderer.h"

PartRenderer::PartRenderer(Model* model, DocumentManager* documents, QWidget* parent) :
	QOpenGLWidget{parent},
	model{model},
	documents{documents},
	compiler{new gl::Compiler{this}}
{
	this->setMouseTracking(true);
}

void PartRenderer::initializeGL()
{
	this->initializeOpenGLFunctions();
	if (this->glGetError() != GL_NO_ERROR)
	{
		abort();
	}
	this->initializeLighting();
	this->initialized = true;
	this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
	this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
	this->compiler->build(this->model, this->documents);
}

/*
 * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix.
 */
static QMatrix4x4 padMatrix(const QMatrix3x3& stub)
{
	return {
		stub(0, 0), stub(0, 1), stub(0, 2), 0,
		stub(1, 0), stub(1, 1), stub(1, 2), 0,
		stub(2, 0), stub(2, 1), stub(2, 2), 0,
		0, 0, 0, 1
	};
}


void PartRenderer::initializeLighting()
{
	GLfloat materialShininess[] = {5.0};
	GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
	GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0};
	glShadeModel(GL_SMOOTH);
	glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_DEPTH_TEST);
}

void PartRenderer::resizeGL(int width, int height)
{
	constexpr GLfloat near = 1.0f;
	constexpr GLfloat far = 1e+05f;
	glViewport (0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far);
	glMatrixMode(GL_MODELVIEW);
}

static int getGlTypeForVboClass(const gl::VboClass vboClass)
{
	switch (vboClass)
	{
	case gl::VboClass::Lines:
	case gl::VboClass::ConditionalLines:
		return GL_LINES;
	case gl::VboClass::Triangles:
		return GL_TRIANGLES;
	case gl::VboClass::Quads:
		return GL_QUADS;
	}
	throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
}

// https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on
#include <QMessageBox>
void PartRenderer::paintGL()
{
	switch (this->renderStyle)
	{
	case gl::RenderStyle::Normal:
	case gl::RenderStyle::BfcRedGreen:
	case gl::RenderStyle::RandomColors:
		break;
	case gl::RenderStyle::Wireframe:
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		break;
	}
	glMatrixMode(GL_MODELVIEW);
	// clear the drawing buffer.
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	// clear the identity matrix.
	glLoadIdentity();
	// traslate the draw by z = -4.0
	// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
	glTranslatef(0.0,0.0,-4.5);
	// Red color used to draw.
	glColor3f(0.8, 0.2, 0.1);
	glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
	//glutSolidTeapot(1.0);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads})
	{
		const int vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces});
		const int vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors});
		const int vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals});
		const int count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3;
		glBindBuffer(GL_ARRAY_BUFFER, vboSurfaces);
		glVertexPointer(3, GL_FLOAT, 0, nullptr);
		glBindBuffer(GL_ARRAY_BUFFER, vboColors);
		glColorPointer(4, GL_FLOAT, 0, nullptr);
		//glBindBuffer(GL_ARRAY_BUFFER, vboNormals);
		//glNormalPointer(GL_FLOAT, 0, nullptr);
		glDrawArrays(getGlTypeForVboClass(vboClass), 0, count);
	}
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

	//glFlush();
	const int glError = this->glGetError();
	if (glError != GL_NO_ERROR)
	{
		const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError)));
		QMessageBox::critical(
			this,
			tr("Rendering error"),
			QString{"Failed to render: %1"}.arg(glErrorString));
	}
}

static QPointF pointToPointF(const QPoint& point)
{
	return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
}

/*
static QPoint pointFToPoint(const QPointF& point)
{
	return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
}
*/

void PartRenderer::mouseMoveEvent(QMouseEvent* event)
{
	const bool left = event->buttons() & Qt::LeftButton;
	const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
	if (left and not move.isNull())
	{
		const QQuaternion versor = QQuaternion::fromAxisAndAngle(
			QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f},
			0.6 * std::hypot(move.x(), move.y())
		);
		this->rotation = versor * this->rotation;
		this->update();
	}
	this->lastMousePosition = pointToPointF(event->pos());
}

void PartRenderer::setCompiler(gl::Compiler* compiler)
{
	this->compiler = compiler;
}

void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
{
	this->renderStyle = newStyle;
	this->update();
}

mercurial