added selection highlighting

Fri, 07 Feb 2020 01:58:34 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 07 Feb 2020 01:58:34 +0200
changeset 48
3c10f0e2fbe0
parent 47
cd6704009eb9
child 49
d56cc7387dad

added selection highlighting

src/configurationoptions.txt file | annotate | diff | comparison | revisions
src/gl/common.h file | annotate | diff | comparison | revisions
src/gl/compiler.cpp file | annotate | diff | comparison | revisions
src/gl/compiler.h file | annotate | diff | comparison | revisions
src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.cpp file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.ui file | annotate | diff | comparison | revisions
src/ui/canvas.cpp file | annotate | diff | comparison | revisions
--- a/src/configurationoptions.txt	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/configurationoptions.txt	Fri Feb 07 01:58:34 2020 +0200
@@ -11,6 +11,7 @@
 option Locale = "system"
 option BackgroundColor = QColor{48, 48, 48}
 option MainColor = QColor{255, 255, 64}
+option SelectedColor = QColor{32, 32, 224}
 option LineThickness = 2.0f
 option LineAntiAliasing = true
 option RenderStyle = 0
--- a/src/gl/common.h	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/gl/common.h	Fri Feb 07 01:58:34 2020 +0200
@@ -158,6 +158,7 @@
 		gl::RenderStyle style = gl::RenderStyle::Normal;
 		QColor mainColor{255, 255, 64};
 		QColor backgroundColor{48, 48, 48};
+		QColor selectedColor{32, 32, 255};
 		GLfloat lineThickness = 2.0f;
 		bool lineAntiAliasing = true;
 	};
--- a/src/gl/compiler.cpp	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/gl/compiler.cpp	Fri Feb 07 01:58:34 2020 +0200
@@ -31,7 +31,8 @@
 layout(location=0) in vec3 position;
 layout(location=1) in vec4 color;
 layout(location=2) in vec3 normal;
-layout(location=3) in vec3 id;
+layout(location=3) in vec3 idColor;
+layout(location=4) in int id;
 out vec4 vColor;
 out vec3 vFragPos;
 out vec3 vNormal;
@@ -39,6 +40,8 @@
 uniform mat4 viewMatrix;
 uniform mat4 projectionMatrix;
 uniform int fragmentStyle;
+uniform vec3 selectedColor;
+uniform int highlighted;
 
 const int FRAGSTYLE_Normal = 0;
 const int FRAGSTYLE_BfcGreen = 1;
@@ -50,22 +53,30 @@
 {
 	mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
 	vNormal = normalize(normalMatrix * normal);
-	if (fragmentStyle == FRAGSTYLE_BfcGreen)
-	{
-		vColor = vec4(0.2, 0.9, 0.2, 1.0);
-	}
-	else if (fragmentStyle == FRAGSTYLE_BfcRed)
+	if (fragmentStyle == FRAGSTYLE_Id)
 	{
-		vColor = vec4(0.9, 0.2, 0.2, 1.0);
-	}
-	else if (fragmentStyle == FRAGSTYLE_Id)
-	{
-		vColor = vec4(id, 1.0);
+		vColor = vec4(idColor, 1.0);
 	}
 	else
 	{
-		vColor = color;
+		if (fragmentStyle == FRAGSTYLE_BfcGreen)
+		{
+			vColor = vec4(0.2, 0.9, 0.2, 1.0);
+		}
+		else if (fragmentStyle == FRAGSTYLE_BfcRed)
+		{
+			vColor = vec4(0.9, 0.2, 0.2, 1.0);
+		}
+		else
+		{
+			vColor = color;
+		}
+		if (highlighted == id)
+		{
+			vColor = (vColor + vec4(selectedColor, 1.0) * 0.6) / 1.6;
+		}
 	}
+
 	vFragPos = vec3(modelMatrix * vec4(position, 1.0));
 	gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0);
 }
@@ -142,15 +153,16 @@
 			object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
 			object.vertexArray.create();
 			object.vertexArray.bind();
-			object.program->enableAttributeArray(0);
-			object.program->enableAttributeArray(1);
-			object.program->enableAttributeArray(2);
-			object.program->enableAttributeArray(3);
+			for (int k : {0, 1, 2, 3, 4})
+			{
+				object.program->enableAttributeArray(k);
+			}
 			constexpr int stride = sizeof(gl::Vertex);
 			object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride);
 			object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride);
 			object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride);
-			object.program->setAttributeBuffer(3, GL_FLOAT, offsetof(gl::Vertex, id), 3, stride);
+			object.program->setAttributeBuffer(3, GL_FLOAT, offsetof(gl::Vertex, idColor), 3, stride);
+			glVertexAttribIPointer(4, 1, GL_INT, stride, (void*)offsetof(gl::Vertex, id));
 			object.vertexArray.release();
 			object.buffer.release();
 			object.program->release();
@@ -230,7 +242,8 @@
 		vertex.position = polygon.vertices[i];
 		vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2));
 		vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
-		vertex.id = colorFromId(polygon.id);
+		vertex.idColor = colorFromId(polygon.id);
+		vertex.id = polygon.id.value;
 	}
 }
 
--- a/src/gl/compiler.h	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/gl/compiler.h	Fri Feb 07 01:58:34 2020 +0200
@@ -26,6 +26,7 @@
 #include <QOpenGLVertexArrayObject>
 #include <QOpenGLBuffer>
 #include <QOpenGLShaderProgram>
+#include <QOpenGLExtraFunctions>
 
 class Model;
 class DocumentManager;
@@ -39,11 +40,12 @@
 		glm::vec3 position;
 		glm::vec4 color;
 		glm::vec3 normal;
-		glm::vec3 id;
+		glm::vec3 idColor;
+		glm::int32 id;
 	};
 }
 
-class gl::Compiler : public QObject, protected QOpenGLFunctions
+class gl::Compiler : public QObject, protected QOpenGLExtraFunctions
 {
 	Q_OBJECT
 public:
@@ -87,6 +89,7 @@
 	bool initialized = false;
 	BoundingBox boundingBox;
 	const ldraw::ColorTable& colorTable;
+	ldraw::Id hovered = ldraw::NULL_ID;
 	struct
 	{
 		QOpenGLShaderProgram* program = nullptr;
--- a/src/gl/partrenderer.cpp	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/gl/partrenderer.cpp	Fri Feb 07 01:58:34 2020 +0200
@@ -92,6 +92,11 @@
 	this->renderScene();
 }
 
+static QVector3D vec3FromQColor(const QColor& color)
+{
+	return {(float)color.redF(), (float)color.greenF(), (float)color.blueF()};
+}
+
 void PartRenderer::renderScene()
 {
 	if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
@@ -117,6 +122,8 @@
 		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 		this->compiler->setUniform("useLighting", false);
 	}
+	this->compiler->setUniform("selectedColor", vec3FromQColor(this->renderPreferences.selectedColor));
+	this->compiler->setUniform("highlighted", this->highlighted.value);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	glEnable(GL_DEPTH_TEST);
 	glEnable(GL_POLYGON_OFFSET_FILL);
@@ -274,3 +281,8 @@
 	this->update();
 }
 
+void PartRenderer::setHighlight(ldraw::Id highlightedId)
+{
+	this->highlighted = highlightedId;
+}
+
--- a/src/gl/partrenderer.h	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/gl/partrenderer.h	Fri Feb 07 01:58:34 2020 +0200
@@ -22,6 +22,7 @@
 		QWidget* parent = nullptr);
 	~PartRenderer() override;
 	void setRenderPreferences(const gl::RenderPreferences& newPreferences);
+	void setHighlight(ldraw::Id highlightedId);
 protected:
 	ldraw::Id pick(const QPoint& where);
 	void initializeGL() override;
@@ -43,6 +44,7 @@
 	glm::mat4 projectionMatrix;
 	glm::mat4 viewMatrix;
 	glm::quat modelQuaternion;
+	ldraw::Id highlighted = ldraw::NULL_ID;
 	static constexpr double MIN_ZOOM = 0.0;
 	static constexpr double MAX_ZOOM = 3.0;
 	double zoom = 1.0;
--- a/src/mainwindow.cpp	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/mainwindow.cpp	Fri Feb 07 01:58:34 2020 +0200
@@ -323,6 +323,7 @@
 	this->renderPreferences.backgroundColor = this->settings.backgroundColor();
 	this->renderPreferences.lineThickness = this->settings.lineThickness();
 	this->renderPreferences.lineAntiAliasing = this->settings.lineAntiAliasing();
+	this->renderPreferences.selectedColor = this->settings.selectedColor();
 	const QString systemLocale = QLocale::system().name();
 	const QVariant defaultLocale = this->settings.locale();
 	this->changeLanguage(defaultLocale.toString());
--- a/src/settingseditor/settingseditor.cpp	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/settingseditor/settingseditor.cpp	Fri Feb 07 01:58:34 2020 +0200
@@ -40,6 +40,7 @@
 	this->settings->setLocale(this->ui.language->currentData().toString());
 	this->settings->setMainColor(this->ui.mainColorButton->selectedColor());
 	this->settings->setBackgroundColor(this->ui.backgroundColorButton->selectedColor());
+	this->settings->setSelectedColor(this->ui.selectedColorButton->selectedColor());
 	this->settings->setLineThickness(static_cast<GLfloat>(this->ui.lineThickness->value()));
 	this->settings->setLineAntiAliasing(this->ui.lineAntiAliasing->isChecked());
 	this->librariesEditor.saveSettings(this->settings);
@@ -72,6 +73,7 @@
 	this->setCurrentLanguage(this->settings->locale());
 	this->ui.mainColorButton->setSelectedColor(this->settings->mainColor());
 	this->ui.backgroundColorButton->setSelectedColor(this->settings->backgroundColor());
+	this->ui.selectedColorButton->setSelectedColor(this->settings->selectedColor());
 	this->ui.lineThickness->setValue(static_cast<double>(this->settings->lineThickness()));
 	this->ui.lineAntiAliasing->setChecked(this->settings->lineAntiAliasing());
 }
--- a/src/settingseditor/settingseditor.ui	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/settingseditor/settingseditor.ui	Fri Feb 07 01:58:34 2020 +0200
@@ -93,37 +93,51 @@
               </property>
              </widget>
             </item>
-            <item row="1" column="0">
+            <item row="2" column="0">
              <widget class="QLabel" name="label_3">
               <property name="text">
                <string>Background colour:</string>
               </property>
              </widget>
             </item>
-            <item row="1" column="1">
+            <item row="2" column="1">
              <widget class="ColorButton" name="backgroundColorButton">
               <property name="text">
                <string/>
               </property>
              </widget>
             </item>
-            <item row="2" column="0">
+            <item row="4" column="0">
              <widget class="QLabel" name="label_4">
               <property name="text">
                <string>Line thickness:</string>
               </property>
              </widget>
             </item>
-            <item row="2" column="1">
+            <item row="4" column="1">
              <widget class="QDoubleSpinBox" name="lineThickness"/>
             </item>
-            <item row="3" column="0">
+            <item row="5" column="0">
              <widget class="QCheckBox" name="lineAntiAliasing">
               <property name="text">
                <string>Line anti-aliasing</string>
               </property>
              </widget>
             </item>
+            <item row="1" column="1">
+             <widget class="ColorButton" name="selectedColorButton">
+              <property name="text">
+               <string/>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="0">
+             <widget class="QLabel" name="label_5">
+              <property name="text">
+               <string>Selected colour:</string>
+              </property>
+             </widget>
+            </item>
            </layout>
           </item>
           <item>
--- a/src/ui/canvas.cpp	Thu Feb 06 23:41:20 2020 +0200
+++ b/src/ui/canvas.cpp	Fri Feb 07 01:58:34 2020 +0200
@@ -15,5 +15,6 @@
 {
 	const ldraw::Id id = this->pick(event->pos());
 	this->newStatusText("Selected: %1"_q.arg(id.value));
+	this->setHighlight(id);
 	PartRenderer::mouseMoveEvent(event);
 }

mercurial