src/gl/partrenderer.cpp

changeset 189
815fbaae9cb2
parent 150
b6cbba6e29a1
child 199
6988973515d2
--- a/src/gl/partrenderer.cpp	Tue May 24 16:11:10 2022 +0300
+++ b/src/gl/partrenderer.cpp	Wed May 25 12:01:58 2022 +0300
@@ -28,6 +28,7 @@
 
 static constexpr double MIN_ZOOM = -3.0;
 static constexpr double MAX_ZOOM = 3.0;
+QOpenGLFunctions glfunc;
 
 PartRenderer::PartRenderer(
 	Model* model,
@@ -37,8 +38,7 @@
 	QOpenGLWidget{parent},
 	model{model},
 	documents{documents},
-	colorTable{colorTable},
-	compiler{new gl::Compiler{model, this->colorTable, this}}
+	colorTable{colorTable}
 {
 	this->setMouseTracking(true);
 	connect(model, &Model::rowsInserted, [&]{
@@ -51,7 +51,7 @@
 {
 }
 
-static QVector3D vec3FromQColor(const QColor& color)
+static QVector3D calcQVector3DFromQColor(const QColor& color)
 {
 	return {
 		toFloat(color.redF()),
@@ -62,17 +62,16 @@
 
 void PartRenderer::initializeGL()
 {
-	this->initializeOpenGLFunctions();
+	::glfunc.initializeOpenGLFunctions();
 	if (glGetError() != GL_NO_ERROR)
 	{
 		abort();
 	}
-	this->compiler->initialize();
+	gl::initializeModelShaders(&this->shaders);
 	connect(this->model, &Model::dataChanged, this, &PartRenderer::build);
 	this->initialized = true;
 	this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0});
 	this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0});
-	this->setupBackgroundColor();
 	this->updateModelMatrix();
 	this->updateViewMatrix();
 	this->update();
@@ -87,11 +86,11 @@
 		static_cast<float>(width) / static_cast<float>(height),
 		0.1f,
 		10000.f);
-	this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix);
+	gl::setShaderUniformMatrix(&this->shaders, "projectionMatrix", this->projectionMatrix);
 	Q_EMIT projectionMatrixChanged(this->projectionMatrix);
 }
 
-static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
+static constexpr GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
 {
 	switch (vboClass)
 	{
@@ -103,7 +102,7 @@
 	case gl::ArrayClass::Quads:
 		return GL_QUADS;
 	}
-	throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
+	throw std::runtime_error{"bad value for vboClass"};
 }
 
 void PartRenderer::paintGL()
@@ -117,12 +116,15 @@
 {
 	if (this->needBuild)
 	{
-		this->compiler->build(this->documents, this->renderPreferences);
+		gl::build(&this->shaders, this->model, this->colorTable, this->documents, this->renderPreferences);
+		this->boundingBox = gl::boundingBoxForModel(this->model, this->documents);
 		this->needBuild = false;
 	}
 	this->checkForGLErrors();
-	if (this->renderPreferences.lineAntiAliasing && this->renderPreferences.style != gl::RenderStyle::PickScene)
-	{
+	if (true
+		and this->renderPreferences.lineAntiAliasing
+		and this->renderPreferences.style != gl::RenderStyle::PickScene
+	) {
 		glEnable(GL_LINE_SMOOTH);
 		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
 	}
@@ -137,27 +139,34 @@
 			static_cast<float>(backgroundColor.greenF()),
 			static_cast<float>(backgroundColor.blueF()),
 			1.0f);
-		this->compiler->setUniform("useLighting", GL_TRUE);
+		gl::setShaderUniform(&this->shaders, "useLighting", GL_TRUE);
 	}
 	else
 	{
 		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-		this->compiler->setUniform("useLighting", GL_FALSE);
+		gl::setShaderUniform(&this->shaders, "useLighting", GL_FALSE);
 	}
 	this->checkForGLErrors();
-	this->compiler->setUniform("selectedColor", vec3FromQColor(this->renderPreferences.selectedColor));
-	this->compiler->setUniform("highlighted", this->highlighted.value);
+	const QVector3D color = calcQVector3DFromQColor(this->renderPreferences.selectedColor);
+	gl::setShaderUniform(&this->shaders, "selectedColor", color);
+	gl::setShaderUniform(&this->shaders, "highlighted", this->highlighted.value);
 	this->checkForGLErrors();
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	glEnable(GL_DEPTH_TEST);
 	glEnable(GL_POLYGON_OFFSET_FILL);
 	glPolygonOffset(1.0f, 1.0f);
 	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);
+	};
 	switch (this->renderPreferences.style)
 	{
 	case gl::RenderStyle::Normal:
 		this->setFragmentStyle(gl::FragmentStyle::Normal);
-		this->renderAllArrays();
+		renderAllArrays();
 		break;
 	case gl::RenderStyle::BfcRedGreen:
 		glEnable(GL_CULL_FACE);
@@ -172,59 +181,49 @@
 		glDisable(GL_CULL_FACE);
 		this->setFragmentStyle(gl::FragmentStyle::Normal);
 		renderVao(gl::ArrayClass::Lines);
+		break;
 	case gl::RenderStyle::RandomColors:
 		this->setFragmentStyle(gl::FragmentStyle::RandomColors);
-		this->renderAllArrays();
+		renderAllArrays();
 		break;
 	case gl::RenderStyle::PickScene:
 		glLineWidth(3.0f);
 		this->setFragmentStyle(gl::FragmentStyle::Id);
-		this->renderAllArrays();
+		renderAllArrays();
 		break;
 	case gl::RenderStyle::VertexPickScene:
 		glLineWidth(1.0f);
 		this->setFragmentStyle(gl::FragmentStyle::Black);
-		this->renderAllArrays();
+		renderAllArrays();
 		break;
 	case gl::RenderStyle::Wireframe:
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		this->setFragmentStyle(gl::FragmentStyle::Normal);
-		this->renderAllArrays();
+		renderAllArrays();
 		break;
 	}
 	glDisable(GL_POLYGON_OFFSET_FILL);
 }
 
-void PartRenderer::renderAllArrays()
-{
-	// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
-	renderVao(gl::ArrayClass::Triangles);
-	renderVao(gl::ArrayClass::Quads);
-	renderVao(gl::ArrayClass::Lines);
-}
-
 
 void PartRenderer::updateViewMatrix()
 {
 	// I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior
-	const double z  = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance());
+	const float modelDistance = longestMeasure(this->boundingBox);
+	const double z  = 2.0 * std::exp(this->zoom) * (1 + static_cast<double>(modelDistance));
 	this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0});
-	this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix);
+	gl::setShaderUniformMatrix(&this->shaders, "viewMatrix", this->viewMatrix);
 	Q_EMIT this->viewMatrixChanged(this->viewMatrix);
 }
 
 void PartRenderer::updateModelMatrix()
 {
 	this->modelMatrix = glm::mat4_cast(this->modelQuaternion);
-	this->compiler->setUniformMatrix("modelMatrix", modelMatrix);
+	gl::setShaderUniformMatrix(&this->shaders, "modelMatrix", modelMatrix);
 	Q_EMIT this->modelMatrixChanged(this->modelMatrix);
 	this->update();
 }
 
-void PartRenderer::setupBackgroundColor()
-{
-}
-
 void PartRenderer::build()
 {
 	this->needBuild = true;
@@ -232,12 +231,12 @@
 
 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
 {
-	this->compiler->bindVertexArray(arrayClass);
-	const std::size_t vertexCount = this->compiler->vertexCount(arrayClass);
+	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));
 	this->checkForGLErrors();
-	this->compiler->releaseVertexArray(arrayClass);
+	gl::releaseModelShaderVertexArray(&this->shaders, arrayClass);
 	this->checkForGLErrors();
 }
 
@@ -365,7 +364,7 @@
 	this->checkForGLErrors();
 	this->renderPreferences.style = oldRenderStyle;
 	this->update();
-	return gl::Compiler::idFromColor(data);
+	return gl::idFromColor(data);
 }
 
 /**
@@ -374,7 +373,7 @@
  */
 void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle)
 {
-	this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle));
+	gl::setShaderUniform(&this->shaders, "fragmentStyle", static_cast<int>(newFragmentStyle));
 }
 
 /**
@@ -389,7 +388,6 @@
 	if (mainColorChanged or backgroundColorChanged)
 	{
 		this->build();
-		this->setupBackgroundColor();
 	}
 	Q_EMIT this->renderPreferencesChanged();
 	this->update();

mercurial