src/gl/partrenderer.cpp

Wed, 26 Feb 2020 02:21:07 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Feb 2020 02:21:07 +0200
changeset 55
cb81ecb5fb23
parent 53
3af627f7a40f
child 56
fad4a5dd8dee
permissions
-rw-r--r--

grid stuff

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <GL/glut.h>
#include <glm/ext/matrix_transform.hpp>
#include <glm/ext/matrix_clip_space.hpp>
#include <QMouseEvent>
#include <QMessageBox>
#include <QAbstractButton>
#include "geometry.h"
#include "partrenderer.h"

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

PartRenderer::~PartRenderer()
{
}

static QVector3D vec3FromQColor(const QColor& color)
{
	return {(float)color.redF(), (float)color.greenF(), (float)color.blueF()};
}

void PartRenderer::initializeGL()
{
	this->initializeOpenGLFunctions();
	if (glGetError() != GL_NO_ERROR)
	{
		abort();
	}
	this->gridProgram.emplace(this);
	this->gridProgram->initialize();
	this->compiler->initialize();
	this->compiler->build(this->model, this->documents, this->renderPreferences);
	this->initialized = true;
	this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0});
	this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0});
	this->setupBackgroundColor();
	this->updateModelMatrix();
	this->updateViewMatrix();
	this->update();
}

void PartRenderer::resizeGL(int width, int height)
{
	glViewport(0, 0, width, height);
	this->viewportMatrix = {
		{1, 0, 0},
		{0, 1, 0},
		{width * 0.5f, height * 0.5f, 1}
	};
	this->projectionMatrix = glm::perspective(
		glm::radians(45.0f),
		static_cast<float>(width) / static_cast<float>(height),
		0.1f,
		10000.f);
	this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix);
	this->gridProgram->setProjectionMatrix(this->projectionMatrix);
}

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

void PartRenderer::paintGL()
{
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	this->renderScene();
}

void PartRenderer::renderScene()
{
	this->checkForGLErrors();
	if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
	{
		glEnable(GL_LINE_SMOOTH);
		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	}
	else {
		glDisable(GL_LINE_SMOOTH);
	}
	if (this->renderPreferences.style != gl::RenderStyle::PickScene)
	{
		const QColor& backgroundColor = this->renderPreferences.backgroundColor;
		glClearColor(
			static_cast<float>(backgroundColor.redF()),
			static_cast<float>(backgroundColor.greenF()),
			static_cast<float>(backgroundColor.blueF()),
			1.0f);
		this->compiler->setUniform("useLighting", GL_TRUE);
	}
	else
	{
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		this->compiler->setUniform("useLighting", GL_FALSE);
	}
	this->checkForGLErrors();
	this->compiler->setUniform("selectedColor", vec3FromQColor(this->renderPreferences.selectedColor));
	this->compiler->setUniform("highlighted", this->highlighted.value);
	this->checkForGLErrors();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
	glLineWidth(this->renderPreferences.lineThickness);
	switch (this->renderPreferences.style)
	{
	case gl::RenderStyle::Normal:
		this->setFragmentStyle(gl::FragmentStyle::Normal);
		this->renderAllArrays();
		break;
	case gl::RenderStyle::BfcRedGreen:
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);
		this->setFragmentStyle(gl::FragmentStyle::BfcGreen);
		renderVao(gl::ArrayClass::Triangles);
		renderVao(gl::ArrayClass::Quads);
		glCullFace(GL_FRONT);
		this->setFragmentStyle(gl::FragmentStyle::BfcRed);
		renderVao(gl::ArrayClass::Triangles);
		renderVao(gl::ArrayClass::Quads);
		glDisable(GL_CULL_FACE);
		this->setFragmentStyle(gl::FragmentStyle::Normal);
		renderVao(gl::ArrayClass::Lines);
	case gl::RenderStyle::RandomColors:
		this->setFragmentStyle(gl::FragmentStyle::RandomColors);
		this->renderAllArrays();
		break;
	case gl::RenderStyle::PickScene:
		glLineWidth(3.0f);
		this->setFragmentStyle(gl::FragmentStyle::Id);
		this->renderAllArrays();
		break;
	case gl::RenderStyle::Wireframe:
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		this->setFragmentStyle(gl::FragmentStyle::Normal);
		this->renderAllArrays();
		break;
	}
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	this->gridProgram->draw();
	glDisable(GL_BLEND);
	glDisable(GL_POLYGON_OFFSET_FILL);
}

void PartRenderer::renderAllArrays()
{
	// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
	renderVao(gl::ArrayClass::Triangles);
	renderVao(gl::ArrayClass::Quads);
	renderVao(gl::ArrayClass::Lines);
}


void PartRenderer::updateViewMatrix()
{
	// I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior
	const double z  = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance());
	this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0});
	this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix);
	this->gridProgram->setViewMatrix(this->viewMatrix);
}

void PartRenderer::updateModelMatrix()
{
	const glm::mat4 modelMatrix = glm::mat4_cast(this->modelQuaternion);
	this->compiler->setUniformMatrix("modelMatrix", modelMatrix);
	this->gridProgram->setModelMatrix(modelMatrix);
	this->update();
}

void PartRenderer::setupBackgroundColor()
{
	if (this->gridProgram.has_value())
	{
		const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
		this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black);
	}
}

void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
{
	this->compiler->bindVertexArray(arrayClass);
	const std::size_t vertexCount = this->compiler->vertexCount(arrayClass);
	this->checkForGLErrors();
	glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
	this->checkForGLErrors();
	this->compiler->releaseVertexArray(arrayClass);
	this->checkForGLErrors();
}

void PartRenderer::checkForGLErrors()
{
	gl::checkForGLErrors(this);
}

void gl::checkForGLErrors(QWidget* parent)
{
	GLenum glError;
	QStringList errors;
	while ((glError = glGetError()) != GL_NO_ERROR)
	{
		const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError)));
		errors.append(glErrorString);
	}
	if (not errors.isEmpty())
	{
		QMessageBox box{parent};
		box.setIcon(QMessageBox::Critical);
		box.setText(QObject::tr("OpenGL error: %1").arg(errors.join("\n")));
		box.setWindowTitle(QObject::tr("OpenGL error"));
		box.setStandardButtons(QMessageBox::Close);
		box.button(QMessageBox::Close)->setText(QObject::tr("Damn it"));
		box.exec();
	}
}

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())
	{
		// q_x is the rotation of the brick along the vertical y-axis, because turning the
		// vertical axis causes horizontal (=x) rotation. Likewise q_y is the rotation of the
		// brick along the horizontal x-axis, which causes vertical rotation.
		const auto scalar = 0.006f;
		const float move_x = static_cast<float>(move.x());
		const float move_y = static_cast<float>(move.y());
		const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0});
		const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0});
		this->modelQuaternion = q_x * q_y * this->modelQuaternion;
		this->updateModelMatrix();
	}
	this->lastMousePosition = pointToPointF(event->pos());
}

void PartRenderer::wheelEvent(QWheelEvent* event)
{
	static constexpr double WHEEL_STEP = 1 / 1000.0;
	const double move = (-event->angleDelta().y()) * WHEEL_STEP;
	this->zoom = std::clamp(this->zoom + move, MIN_ZOOM, MAX_ZOOM);
	this->updateViewMatrix();
	this->update();
}

std::optional<glm::vec3> PartRenderer::cameraToGrid(const QPoint& point)
{
	glm::vec3 pp = {point.x(), this->height() - point.y(), 1};
	pp = glm::inverse(this->viewportMatrix) * pp;
	glm::mat4 matrix;
	matrix = this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion);
	matrix = glm::transpose(glm::inverse(matrix));
	auto p1 = matrix * glm::vec4{pp.x, pp.y, 0, 1};
	auto p2 = matrix * glm::vec4{pp.x, pp.y, 1, 1};
	geom::Line line = geom::lineFromPoints(p1, p2);
	return geom::linePlaneIntersection(line, geom::XY);
}

QPointF PartRenderer::worldToCamera(const glm::vec3& point)
{
	glm::vec4 p = {point, 1};
	p = glm::mat4_cast(this->modelQuaternion) * p;
	p = this->viewMatrix * p;
	p = this->projectionMatrix * p;
	glm::vec2 pp = this->viewportMatrix * glm::vec3{p.x, p.y, 1};
	return toQPointF(pp);
	/*
	const glm::mat4 matrix = this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion);
	return toQPointF(matrix * glm::vec4{point, 1});
	*/
}

ldraw::Id PartRenderer::pick(const QPoint& where)
{
	const gl::RenderStyle oldRenderStyle = this->renderPreferences.style;
	this->renderPreferences.style = gl::RenderStyle::PickScene;
	this->makeCurrent();
	this->renderScene();
	std::array<GLbyte, 3> data;
	this->checkForGLErrors();
	glReadPixels(where.x(), this->height() - where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]);
	this->checkForGLErrors();
	this->renderPreferences.style = oldRenderStyle;
	this->update();
	return gl::Compiler::idFromColor(data);
}

/**
 * @brief Changes the color of rendered fragments
 * @param newFragmentStyle new fragment style to use
 */
void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle)
{
	this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle));
}

/**
 * @brief Changes the way the scene is rendered
 * @param newStyle new render style to use
 */
void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences)
{
	bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor;
	bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor;
	this->renderPreferences = newPreferences;
	if (mainColorChanged or backgroundColorChanged)
	{
		this->compiler->build(this->model, this->documents, this->renderPreferences);
		this->setupBackgroundColor();

	}
	this->update();
}

mercurial