src/gl/partrenderer.cpp

changeset 376
3cef3b016330
parent 360
41b38b9e05a2
child 382
94d5587bb0c4
--- a/src/gl/partrenderer.cpp	Tue Apr 11 20:27:04 2023 +0300
+++ b/src/gl/partrenderer.cpp	Tue Apr 11 22:39:18 2023 +0300
@@ -33,6 +33,8 @@
 
 static constexpr double MIN_ZOOM = -3.0;
 static constexpr double MAX_ZOOM = 3.0;
+const gl::RenderPreferences PartRenderer::default_render_preferences{};
+const gl::build_preferences PartRenderer::default_build_preferences{};
 
 PartRenderer::PartRenderer(
 	QTextDocument* model,
@@ -120,24 +122,22 @@
 {
 	if (this->needBuild)
 	{
-		gl::build(&this->shaders, this->model, this->colorTable, this->documents, this->renderPreferences);
+		gl::build(&this->shaders, this->model, this->colorTable, this->documents, this->build_preferences);
 		this->boundingBox = gl::boundingBoxForModel(this->model, this->documents);
 		this->needBuild = false;
 	}
 	this->checkForGLErrors();
-	if (true
-		and this->renderPreferences.lineAntiAliasing
-		and this->renderPreferences.style != gl::RenderStyle::PickScene
-	) {
+	if (this->render_preferences->lineAntiAliasing)
+	{
 		glEnable(GL_LINE_SMOOTH);
 		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
 	}
 	else {
 		glDisable(GL_LINE_SMOOTH);
 	}
-	if (this->renderPreferences.style != gl::RenderStyle::PickScene)
+	if (this->render_preferences->style != gl::RenderStyle::PickScene)
 	{
-		const QColor& backgroundColor = this->renderPreferences.backgroundColor;
+		const QColor& backgroundColor = this->build_preferences->backgroundColor;
 		glClearColor(
 			static_cast<float>(backgroundColor.redF()),
 			static_cast<float>(backgroundColor.greenF()),
@@ -151,7 +151,7 @@
 		gl::setShaderUniform(&this->shaders, "useLighting", GL_FALSE);
 	}
 	this->checkForGLErrors();
-	const QColor qs = this->renderPreferences.selectedColor;
+	const QColor qs = this->render_preferences->selectedColor;
 	const glm::vec4 selectedColor{qs.redF(), qs.greenF(), qs.blueF(), 1.0f};
 	gl::setShaderUniformVector(&this->shaders, "selectedColor", selectedColor);
 	gl::setShaderUniform(&this->shaders, "highlighted", this->highlighted.value);
@@ -160,17 +160,18 @@
 	glEnable(GL_DEPTH_TEST);
 	glEnable(GL_POLYGON_OFFSET_FILL);
 	glPolygonOffset(1.0f, 1.0f);
-	glLineWidth(this->renderPreferences.lineThickness);
+	glLineWidth(this->render_preferences->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>();
 	};
-	if (this->renderPreferences.wireframe and this->renderPreferences.style != gl::RenderStyle::PickScene) {
+	if (this->render_preferences->wireframe)
+	{
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 	}
-	switch (this->renderPreferences.style)
+	switch (this->render_preferences->style)
 	{
 	case gl::RenderStyle::Normal:
 		this->setFragmentStyle(gl::FragmentStyle::Normal);
@@ -195,7 +196,6 @@
 		renderAllArrays();
 		break;
 	case gl::RenderStyle::PickScene:
-		glLineWidth(3.0f);
 		this->setFragmentStyle(gl::FragmentStyle::Id);
 		renderAllArrays();
 		break;
@@ -395,7 +395,7 @@
 
 bool PartRenderer::isDark() const
 {
-	return luma(this->renderPreferences.backgroundColor) < 0.25;
+	return luma(this->build_preferences->backgroundColor) < 0.25;
 }
 
 Line<3> PartRenderer::cameraLine(const QPointF& point) const
@@ -433,8 +433,16 @@
 	// will be affected by High DPI scaling. We need to take this into account
 	// and multiply the pixel positions by the screen pixel scaling factor.
 	where *= this->devicePixelRatioF();
-	const gl::RenderStyle oldRenderStyle = this->renderPreferences.style;
-	this->renderPreferences.style = gl::RenderStyle::PickScene;
+	static const gl::RenderPreferences pick_scene_preferences {
+		.style = gl::RenderStyle::PickScene,
+		.selectedColor = {},
+		.lineThickness = 3.0f,
+		.lineAntiAliasing = false,
+		.drawAxes = false,
+		.wireframe = false,
+	};
+	const gl::RenderPreferences* old_render_preferences = this->render_preferences;
+	this->render_preferences = &pick_scene_preferences;
 	this->makeCurrent();
 	QOpenGLFramebufferObject fbo{this->width(), this->height(), QOpenGLFramebufferObject::CombinedDepthStencil};
 	fbo.bind();
@@ -444,7 +452,7 @@
 	glReadPixels(where.x(), where.y(), 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &data[0]);
 	this->checkForGLErrors();
 	fbo.release();
-	this->renderPreferences.style = oldRenderStyle;
+	this->render_preferences = old_render_preferences;
 	return gl::idFromUcharColor(data);
 }
 
@@ -458,23 +466,6 @@
 }
 
 /**
- * @brief Changes the way the scene is rendered
- * @param newStyle new render style to use
- */
-void PartRenderer::setRenderPreferences(const gl::RenderPreferences& newPreferences)
-{
-	bool mainColorChanged = this->renderPreferences.mainColor != newPreferences.mainColor;
-	bool backgroundColorChanged = this->renderPreferences.backgroundColor != newPreferences.backgroundColor;
-	this->renderPreferences = newPreferences;
-	if (mainColorChanged or backgroundColorChanged)
-	{
-		this->build();
-	}
-	Q_EMIT this->renderPreferencesChanged();
-	this->update();
-}
-
-/**
  * @return the currently highlighted object
  */
 ModelId PartRenderer::getHighlightedObject() const

mercurial