src/gl/partrenderer.cpp

Sun, 19 Jan 2020 13:53:07 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 13:53:07 +0200
changeset 23
3387a84ddaba
parent 22
6da867fa5429
child 24
1a0faaaceb84
permissions
-rw-r--r--

fixed a pile of nonsense that caused subfiles to go haywire

#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 (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);
	glLineWidth(2.0);
}

/*
 * 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 GLdouble near = 1.0;
	constexpr GLdouble far = 1e+05;
	glViewport (0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far);
	glMatrixMode(GL_MODELVIEW);
}

static GLenum 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()
{
	glEnable (GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable (GL_POLYGON_OFFSET_FILL);
	glPolygonOffset (1.0f, 1.0f);
	glEnable (GL_DEPTH_TEST);
	glShadeModel (GL_SMOOTH);
	glEnable (GL_MULTISAMPLE);
	glEnable (GL_LINE_SMOOTH);
	glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
	this->renderScene();
}

void PartRenderer::renderScene()
{
	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);
	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);
	glLoadIdentity();
	glTranslatef(0.0, 0.0, -4.5 * this->compiler->modelDistance());
	glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
	xyz(glTranslatef, -this->compiler->modelCenter());
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads})
	{
		const GLuint vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces});
		const GLuint vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors});
		const GLuint vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals});
		const std::size_t count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3_z;
		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, static_cast<GLsizei>(count));
	}
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	const GLenum 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));
	}
	glDisable(GL_CULL_FACE);
}

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.6f * static_cast<float>(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