src/gl/partrenderer.cpp

changeset 47
cd6704009eb9
parent 46
98645c8e7704
child 48
3c10f0e2fbe0
--- a/src/gl/partrenderer.cpp	Thu Feb 06 20:33:05 2020 +0200
+++ b/src/gl/partrenderer.cpp	Thu Feb 06 23:41:20 2020 +0200
@@ -21,6 +21,7 @@
 #include <glm/ext/matrix_clip_space.hpp>
 #include <QMouseEvent>
 #include <QMessageBox>
+#include <QAbstractButton>
 #include "partrenderer.h"
 
 PartRenderer::PartRenderer(
@@ -85,13 +86,14 @@
 
 void PartRenderer::paintGL()
 {
-	/*
-	glEnable (GL_BLEND);
-	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-	*/
-	glEnable (GL_DEPTH_TEST);
-	glShadeModel (GL_SMOOTH);
-	glEnable (GL_MULTISAMPLE);
+	glEnable(GL_DEPTH_TEST);
+	glShadeModel(GL_SMOOTH);
+	glEnable(GL_MULTISAMPLE);
+	this->renderScene();
+}
+
+void PartRenderer::renderScene()
+{
 	if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
 	{
 		glEnable(GL_LINE_SMOOTH);
@@ -100,14 +102,9 @@
 	else {
 		glDisable(GL_LINE_SMOOTH);
 	}
-	this->renderScene();
-}
-
-void PartRenderer::renderScene()
-{
-	const QColor& backgroundColor = this->renderPreferences.backgroundColor;
 	if (this->renderPreferences.style != gl::RenderStyle::PickScene)
 	{
+		const QColor& backgroundColor = this->renderPreferences.backgroundColor;
 		glClearColor(
 			static_cast<float>(backgroundColor.redF()),
 			static_cast<float>(backgroundColor.greenF()),
@@ -199,10 +196,13 @@
 	}
 	if (not errors.isEmpty())
 	{
-		QMessageBox::critical(
-			this,
-			tr("Rendering error"),
-			QString{"Failed to render.\n%1"}.arg(errors.join("\n")));
+		QMessageBox box{this};
+		box.setIcon(QMessageBox::Critical);
+		box.setText(tr("Failed to render: %1").arg(errors.join("\n")));
+		box.setWindowTitle(tr("Rendering error"));
+		box.setStandardButtons(QMessageBox::Close);
+		box.button(QMessageBox::Close)->setText(tr("Damn it"));
+		box.exec();
 	}
 }
 
@@ -236,6 +236,19 @@
 	this->update();
 }
 
+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;
+	glReadPixels(where.x(), this->height() - where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]);
+	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

mercurial