Render vertices as spheres

Wed, 09 Mar 2022 12:42:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 09 Mar 2022 12:42:45 +0200
changeset 172
50f055543ff6
parent 171
b497f5e66749
child 173
8a3047468994

Render vertices as spheres

src/gl/vertexprogram.cpp file | annotate | diff | comparison | revisions
src/ui/canvas.cpp file | annotate | diff | comparison | revisions
--- a/src/gl/vertexprogram.cpp	Sun Mar 06 10:32:54 2022 +0200
+++ b/src/gl/vertexprogram.cpp	Wed Mar 09 12:42:45 2022 +0200
@@ -31,18 +31,38 @@
 }
 )";
 
-static constexpr glm::vec3 o = {0, 0, 0};
-static constexpr glm::vec3 c1 = {1, -1, 1};
-static constexpr glm::vec3 c2 = {1, -1, -1};
-static constexpr glm::vec3 c3 = {-1, -1, -1};
-static constexpr glm::vec3 c4 = {-1, -1, 1};
-
-static constexpr glm::vec3 markerGeometry[] = {
-	o, c1, c2,
-	o, c2, c3,
-	o, c3, c4,
-	o, c4, c1,
-};
+std::vector<glm::vec3> sphere(const int d2)
+{
+	std::vector<glm::vec3> result;
+	result.reserve(12 * d2 * d2);
+	for (int i = 0; i < d2; ++i)
+	{
+		const float alpha = i * math::pi / d2;
+		const float alpha_2 = (i + 1) * math::pi / d2;
+		for (int j = -d2; j < d2; ++j)
+		{
+			const float beta = j * math::pi / d2;
+			const float beta_2 = (j + 1) * math::pi / d2;
+			const float x1 = cos(beta) * sin(alpha);
+			const float x2 = cos(beta) * sin(alpha_2);
+			const float x3 = cos(beta_2) * sin(alpha_2);
+			const float x4 = cos(beta_2) * sin(alpha);
+			const float z1 = sin(beta) * sin(alpha);
+			const float z2 = sin(beta) * sin(alpha_2);
+			const float z3 = sin(beta_2) * sin(alpha_2);
+			const float z4 = sin(beta_2) * sin(alpha);
+			const float y1 = cos(alpha);
+			const float y2 = cos(alpha_2);
+			result.push_back({x1, y1, z1});
+			result.push_back({x2, y2, z2});
+			result.push_back({x3, y2, z3});
+			result.push_back({x1, y1, z1});
+			result.push_back({x3, y2, z3});
+			result.push_back({x4, y1, z4});
+		}
+	}
+	return result;
+}
 
 VertexProgram::VertexProgram(QObject *parent) :
 	AbstractBasicShaderProgram{parent}
@@ -104,12 +124,13 @@
 {
 	constexpr glm::vec3 color = {0.0, 1.0, 1.0};
 	this->data.clear();
+	const std::vector<glm::vec3> sphere = ::sphere(8 / 2);
 	document->applyToVertices([&](const glm::vec3&, const VertexMap::VertexInfo& info)
 	{
-		reserveMore(this->data, ::countof(::markerGeometry));
-		for (const glm::vec3& point : ::markerGeometry)
+		reserveMore(this->data, sphere.size());
+		for (const glm::vec3& point : sphere)
 		{
-			const glm::vec3 transformed = info.transform * glm::vec4{point, 1};
+			const glm::vec3 transformed = glm::scale(info.transform, glm::vec3{0.5, 0.5, 0.5}) * glm::vec4{point, 1};
 			this->data.push_back({transformed, color});
 		}
 	});
--- a/src/ui/canvas.cpp	Sun Mar 06 10:32:54 2022 +0200
+++ b/src/ui/canvas.cpp	Wed Mar 09 12:42:45 2022 +0200
@@ -130,13 +130,8 @@
 		}
 		// Render vertices
 		{
-			glLineWidth(1.5);
-			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-			glEnable(GL_LINE_SMOOTH);
-			glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
+			glCullFace(GL_FRONT);
 			this->vertexProgram->draw();
-			glDisable(GL_LINE_SMOOTH);
-			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 		}
 		// Render grid
 		{

mercurial