`PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time

Sun, 09 Apr 2023 12:23:32 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 12:23:32 +0300
changeset 360
41b38b9e05a2
parent 359
73b6c478378e
child 361
c5e8b68e34f8

`PartRenderer::renderVao` no longer throws if bad array class is given, this is now checked on compile time

src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.h file | annotate | diff | comparison | revisions
--- a/src/gl/partrenderer.cpp	Sun Apr 09 01:12:00 2023 +0300
+++ b/src/gl/partrenderer.cpp	Sun Apr 09 12:23:32 2023 +0300
@@ -101,21 +101,6 @@
 	Q_EMIT projectionMatrixChanged(this->projectionMatrix);
 }
 
-static constexpr GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
-{
-	switch (vboClass)
-	{
-	case gl::ArrayClass::Lines:
-	case gl::ArrayClass::ConditionalLines:
-		return GL_LINES;
-	case gl::ArrayClass::Triangles:
-		return GL_TRIANGLES;
-	case gl::ArrayClass::Quads:
-		return GL_QUADS;
-	}
-	throw std::runtime_error{"bad value for vboClass"};
-}
-
 void PartRenderer::paintGL()
 {
 	glEnable(GL_DEPTH_TEST);
@@ -178,9 +163,9 @@
 	glLineWidth(this->renderPreferences.lineThickness);
 	const auto renderAllArrays = [this](){
 		// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
-		this->renderVao(gl::ArrayClass::Triangles);
-		this->renderVao(gl::ArrayClass::Quads);
-		this->renderVao(gl::ArrayClass::Lines);
+		this->renderVao<gl::ArrayClass::Triangles>();
+		this->renderVao<gl::ArrayClass::Quads>();
+		this->renderVao<gl::ArrayClass::Lines>();
 	};
 	if (this->renderPreferences.wireframe and this->renderPreferences.style != gl::RenderStyle::PickScene) {
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@@ -195,15 +180,15 @@
 		glEnable(GL_CULL_FACE);
 		glCullFace(GL_BACK);
 		this->setFragmentStyle(gl::FragmentStyle::BfcGreen);
-		renderVao(gl::ArrayClass::Triangles);
-		renderVao(gl::ArrayClass::Quads);
+		renderVao<gl::ArrayClass::Triangles>();
+		renderVao<gl::ArrayClass::Quads>();
 		glCullFace(GL_FRONT);
 		this->setFragmentStyle(gl::FragmentStyle::BfcRed);
-		renderVao(gl::ArrayClass::Triangles);
-		renderVao(gl::ArrayClass::Quads);
+		renderVao<gl::ArrayClass::Triangles>();
+		renderVao<gl::ArrayClass::Quads>();
 		glDisable(GL_CULL_FACE);
 		this->setFragmentStyle(gl::FragmentStyle::Normal);
-		renderVao(gl::ArrayClass::Lines);
+		renderVao<gl::ArrayClass::Lines>();
 		break;
 	case gl::RenderStyle::RandomColors:
 		this->setFragmentStyle(gl::FragmentStyle::RandomColors);
@@ -248,12 +233,12 @@
 	this->needBuild = true;
 }
 
-void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
+void PartRenderer::renderVao(const gl::ArrayClass arrayClass, const GLenum glType)
 {
 	gl::bindModelShaderVertexArray(&this->shaders, arrayClass);
 	const std::size_t vertexCount = gl::vertexCount(&this->shaders, arrayClass);
 	this->checkForGLErrors();
-	glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
+	glDrawArrays(glType, 0, static_cast<GLsizei>(vertexCount));
 	this->checkForGLErrors();
 	gl::releaseModelShaderVertexArray(&this->shaders, arrayClass);
 	this->checkForGLErrors();
--- a/src/gl/partrenderer.h	Sun Apr 09 01:12:00 2023 +0300
+++ b/src/gl/partrenderer.h	Sun Apr 09 12:23:32 2023 +0300
@@ -71,10 +71,35 @@
 	void updateViewMatrix();
 	void updateModelMatrix();
 	Q_SLOT void build();
-	void renderVao(const gl::ArrayClass arrayClass);
+	void renderVao(const gl::ArrayClass arrayClass, const GLenum glType);
+	template<gl::ArrayClass arrayClass>
+	void renderVao();
 	void checkForGLErrors();
 };
 
+inline constexpr opt<GLenum> getGlTypeForArrayClass(const gl::ArrayClass vboClass)
+{
+	switch (vboClass)
+	{
+	case gl::ArrayClass::Lines:
+	case gl::ArrayClass::ConditionalLines:
+		return GL_LINES;
+	case gl::ArrayClass::Triangles:
+		return GL_TRIANGLES;
+	case gl::ArrayClass::Quads:
+		return GL_QUADS;
+	}
+	return {};
+}
+
+template<gl::ArrayClass arrayClass>
+void PartRenderer::renderVao()
+{
+	constexpr opt<GLenum> glType = getGlTypeForArrayClass(arrayClass);
+	static_assert(glType.has_value());
+	this->renderVao(arrayClass, *glType);
+}
+
 inline QVector<QPointF> convertWorldPointsToScreenPoints(
 	const std::vector<glm::vec3> &worldPoints,
 	const PartRenderer* renderer)

mercurial