added the pick scene

Thu, 06 Feb 2020 20:33:05 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 06 Feb 2020 20:33:05 +0200
changeset 46
98645c8e7704
parent 45
272c84c7c87e
child 47
cd6704009eb9

added the pick scene

locale/fi.ts 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/linetypes/object.cpp file | annotate | diff | comparison | revisions
src/main.h file | annotate | diff | comparison | revisions
--- a/locale/fi.ts	Sun Feb 02 00:58:59 2020 +0200
+++ b/locale/fi.ts	Thu Feb 06 20:33:05 2020 +0200
@@ -209,7 +209,7 @@
 <context>
     <name>PartRenderer</name>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="190"/>
+        <location filename="../src/gl/partrenderer.cpp" line="206"/>
         <source>Rendering error</source>
         <translation type="unfinished"></translation>
     </message>
@@ -286,22 +286,22 @@
 <context>
     <name>gl::Compiler</name>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="112"/>
+        <location filename="../src/gl/compiler.cpp" line="126"/>
         <source>Vertex shader:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="117"/>
+        <location filename="../src/gl/compiler.cpp" line="131"/>
         <source>Fragment shader:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="121"/>
+        <location filename="../src/gl/compiler.cpp" line="135"/>
         <source>Shader compile error</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="121"/>
+        <location filename="../src/gl/compiler.cpp" line="135"/>
         <source>Could not compile shaders.</source>
         <translation type="unfinished"></translation>
     </message>
--- a/src/gl/common.h	Sun Feb 02 00:58:59 2020 +0200
+++ b/src/gl/common.h	Thu Feb 06 20:33:05 2020 +0200
@@ -138,7 +138,8 @@
 		Normal,
 		Wireframe,
 		BfcRedGreen,
-		RandomColors
+		RandomColors,
+		PickScene
 	};
 
 	// These are also defined in shaders
@@ -148,6 +149,7 @@
 		BfcGreen = 1,
 		BfcRed = 2,
 		RandomColors = 3,
+		Id = 4,
 	};
 
 	// User options for rendering
--- a/src/gl/compiler.cpp	Sun Feb 02 00:58:59 2020 +0200
+++ b/src/gl/compiler.cpp	Thu Feb 06 20:33:05 2020 +0200
@@ -31,6 +31,7 @@
 layout(location=0) in vec3 position;
 layout(location=1) in vec4 color;
 layout(location=2) in vec3 normal;
+layout(location=3) in vec3 id;
 out vec4 vColor;
 out vec3 vFragPos;
 out vec3 vNormal;
@@ -43,6 +44,7 @@
 const int FRAGSTYLE_BfcGreen = 1;
 const int FRAGSTYLE_BfcRed = 2;
 const int FRAGSTYLE_Random = 3;
+const int FRAGSTYLE_Id = 4;
 
 void main()
 {
@@ -56,6 +58,10 @@
 	{
 		vColor = vec4(0.9, 0.2, 0.2, 1.0);
 	}
+	else if (fragmentStyle == FRAGSTYLE_Id)
+	{
+		vColor = vec4(id, 1.0);
+	}
 	else
 	{
 		vColor = color;
@@ -75,13 +81,21 @@
 const vec3 lightPos = vec3(0.5, 0.5, 0.5);
 const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
 const float ambientStrength = 0.7;
+uniform bool useLighting;
 
 void main()
 {
-	vec4 ambient = ambientStrength * lightColor;
-	vec3 lightDirection = normalize(lightPos - vFragPos);
-	vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor;
-	fColor = (ambient + diffuse) * vColor;
+	if (useLighting)
+	{
+		vec4 ambient = ambientStrength * lightColor;
+		vec3 lightDirection = normalize(lightPos - vFragPos);
+		vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor;
+		fColor = (ambient + diffuse) * vColor;
+	}
+	else
+	{
+		fColor = vColor;
+	}
 }
 )";
 
@@ -131,10 +145,12 @@
 			object.program->enableAttributeArray(0);
 			object.program->enableAttributeArray(1);
 			object.program->enableAttributeArray(2);
+			object.program->enableAttributeArray(3);
 			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.vertexArray.release();
 			object.buffer.release();
 			object.program->release();
@@ -179,15 +195,14 @@
 	return gl::ArrayClass::Lines;
 }
 
-[[maybe_unused]]
-static QColor colorFromId(ldraw::Id id)
+static glm::vec3 colorFromId(ldraw::Id id)
 {
 	// Calculate a color based from this index. This method caters for
 	// 16777216 objects. I don't think that will be exceeded anytime soon.
 	const int r = (id.value / 0x10000) % 0x100;
 	const int g = (id.value / 0x100) % 0x100;
 	const int b = id.value % 0x100;
-	return {r, g, b};
+	return {r / 255.0f, g / 255.0f, b / 255.0f};
 }
 
 void gl::Compiler::buildPolygon(
@@ -210,6 +225,7 @@
 		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);
 	}
 }
 
--- a/src/gl/compiler.h	Sun Feb 02 00:58:59 2020 +0200
+++ b/src/gl/compiler.h	Thu Feb 06 20:33:05 2020 +0200
@@ -39,6 +39,7 @@
 		glm::vec3 position;
 		glm::vec4 color;
 		glm::vec3 normal;
+		glm::vec3 id;
 	};
 }
 
@@ -87,6 +88,7 @@
 	struct
 	{
 		QOpenGLShaderProgram* program = nullptr;
+		QOpenGLShaderProgram* pickSceneProgram = nullptr;
 		QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer};
 		QOpenGLVertexArrayObject vertexArray;
 	} glObjects[gl::NUM_ARRAY_CLASSES];
--- a/src/gl/partrenderer.cpp	Sun Feb 02 00:58:59 2020 +0200
+++ b/src/gl/partrenderer.cpp	Thu Feb 06 20:33:05 2020 +0200
@@ -92,7 +92,7 @@
 	glEnable (GL_DEPTH_TEST);
 	glShadeModel (GL_SMOOTH);
 	glEnable (GL_MULTISAMPLE);
-	if (this->renderPreferences.lineAntiAliasing)
+	if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
 	{
 		glEnable(GL_LINE_SMOOTH);
 		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
@@ -106,11 +106,20 @@
 void PartRenderer::renderScene()
 {
 	const QColor& backgroundColor = this->renderPreferences.backgroundColor;
-	glClearColor(
-		static_cast<float>(backgroundColor.redF()),
-		static_cast<float>(backgroundColor.greenF()),
-		static_cast<float>(backgroundColor.blueF()),
-		1.0f);
+	if (this->renderPreferences.style != gl::RenderStyle::PickScene)
+	{
+		glClearColor(
+			static_cast<float>(backgroundColor.redF()),
+			static_cast<float>(backgroundColor.greenF()),
+			static_cast<float>(backgroundColor.blueF()),
+			1.0f);
+		this->compiler->setUniform("useLighting", true);
+	}
+	else
+	{
+		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+		this->compiler->setUniform("useLighting", false);
+	}
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	glEnable(GL_DEPTH_TEST);
 	glEnable(GL_POLYGON_OFFSET_FILL);
@@ -139,6 +148,11 @@
 		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);
--- a/src/linetypes/object.cpp	Sun Feb 02 00:58:59 2020 +0200
+++ b/src/linetypes/object.cpp	Thu Feb 06 20:33:05 2020 +0200
@@ -2,9 +2,9 @@
 #include <QFont>
 #include "object.h"
 
-static unsigned int getIdForNewObject()
+static std::int32_t getIdForNewObject()
 {
-	static unsigned int id = 0;
+	static std::int32_t id = 0;
 	id += 1;
 	return id;
 }
--- a/src/main.h	Sun Feb 02 00:58:59 2020 +0200
+++ b/src/main.h	Thu Feb 06 20:33:05 2020 +0200
@@ -36,12 +36,14 @@
 	// Uniquely identifies a model body object
 	struct Id
 	{
-		unsigned int value;
+		std::int32_t value;
 		constexpr bool operator<(ldraw::Id other) const
 		{
 			return this->value < other.value;
 		}
 	};
+	using id_t = Id;
+	constexpr id_t NULL_ID = id_t{0};
 }
 
 constexpr std::size_t operator""_z(const unsigned long long int x)

mercurial