Readd axis labels, fix antialiasing

Mon, 20 Jun 2022 16:59:09 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 16:59:09 +0300
changeset 234
87ee9824210b
parent 233
5509bec02c81
child 235
7ef03c2b46ab

Readd axis labels, fix antialiasing

src/gl/axesprogram.cpp file | annotate | diff | comparison | revisions
src/gl/axesprogram.h file | annotate | diff | comparison | revisions
src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
--- a/src/gl/axesprogram.cpp	Mon Jun 20 16:43:56 2022 +0300
+++ b/src/gl/axesprogram.cpp	Mon Jun 20 16:59:09 2022 +0300
@@ -16,7 +16,10 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <QPainter>
+#include <QPainterPath>
 #include "axesprogram.h"
+#include "gl/partrenderer.h"
 
 static constexpr char vertexShaderSource[] = R"(
 #version 330 core
@@ -87,6 +90,72 @@
 	this->shader.bufferData(&data[0], countof(data), sizeof data[0]);
 }
 
+void drawBorderedText(QPainter* painter, const QPointF& point, const QFont& font, const QString& text)
+{
+	QPainterPath path;
+	path.addText(point, font, text);
+	painter->save();
+	painter->setBrush(Qt::white);
+	painter->setPen({Qt::black, 0.1 * font.pointSizeF()});
+	painter->drawPath(path);
+	painter->restore();
+}
+
+void AxesLayer::overpaint(QPainter* painter)
+{
+	QFont font;
+	font.setStyle(QFont::StyleItalic);
+	font.setBold(true);
+	painter->setFont(font);
+	QFontMetrics fontMetrics{font};
+	const auto renderText = [&](const QString& text, const PointOnRectagle& intersection)
+	{
+		QPointF position = toQPointF(intersection.position);
+		const RectangleSide side = intersection.side;
+		switch (side)
+		{
+		case RectangleSide::Top:
+			position += QPointF{0, static_cast<qreal>(fontMetrics.ascent())};
+			break;
+		case RectangleSide::Left:
+			break;
+		case RectangleSide::Bottom:
+			position += QPointF{0, static_cast<qreal>(-fontMetrics.descent())};
+			break;
+		case RectangleSide::Right:
+			position += QPointF{static_cast<qreal>(-fontMetrics.horizontalAdvance(text)), 0};
+			break;
+		}
+		drawBorderedText(painter, position, font, text);
+	};
+	const QRectF box {QPointF{0, 0}, sizeToSizeF(this->renderer->size())};
+	const QPointF p1 = this->renderer->modelToScreenCoordinates(glm::vec3{0, 0, 0});
+	static const struct
+	{
+		QString text;
+		glm::vec3 direction;
+	} directions[] =
+	{
+		{"+x", {1, 0, 0}},
+		{"-x", {-1, 0, 0}},
+		{"+y", {0, 1, 0}},
+		{"-y", {0, -1, 0}},
+		{"+z", {0, 0, 1}},
+		{"-z", {0, 0, -1}},
+	};
+	for (const auto& axis : directions)
+	{
+		const QPointF x_p = this->renderer->modelToScreenCoordinates(axis.direction);
+		const auto intersection = rayRectangleIntersection(
+			rayFromPoints(toVec2(p1), toVec2(x_p)),
+			box);
+		if (intersection.has_value())
+		{
+			renderText(axis.text, *intersection);
+		}
+	}
+}
+
 void AxesLayer::paintGL()
 {
 	glLineWidth(5);
--- a/src/gl/axesprogram.h	Mon Jun 20 16:43:56 2022 +0300
+++ b/src/gl/axesprogram.h	Mon Jun 20 16:59:09 2022 +0300
@@ -7,6 +7,7 @@
 	BasicShader shader;
 public:
 	void initializeGL() override;
+	void overpaint(QPainter* painter) override;
 	void paintGL() override;
 	void mvpMatrixChanged(const glm::mat4& mvpMatrix) override;
 };
--- a/src/gl/partrenderer.cpp	Mon Jun 20 16:43:56 2022 +0300
+++ b/src/gl/partrenderer.cpp	Mon Jun 20 16:59:09 2022 +0300
@@ -44,6 +44,9 @@
 {
 	this->setMouseTracking(true);
 	this->setFocusPolicy(Qt::WheelFocus);
+	QSurfaceFormat surfaceFormat;
+	surfaceFormat.setSamples(8);
+	this->setFormat(surfaceFormat);
 	connect(model, &Model::rowsInserted, [&]{
 		this->needBuild = true;
 	});
@@ -135,6 +138,7 @@
 		layer->paintGL();
 	}
 	QPainter painter{this};
+	painter.setRenderHint(QPainter::Antialiasing);
 	for (RenderLayer* layer : this->activeRenderLayers) {
 		layer->overpaint(&painter);
 	}

mercurial