added cull depth support

Mon, 02 Apr 2018 10:33:17 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 02 Apr 2018 10:33:17 +0300
changeset 1369
1e2391b78d17
parent 1368
36105978da93
child 1370
c6d5ba08c62c

added cull depth support

src/canvas.cpp file | annotate | diff | comparison | revisions
src/canvas.h file | annotate | diff | comparison | revisions
src/glrenderer.cpp file | annotate | diff | comparison | revisions
src/glrenderer.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.h file | annotate | diff | comparison | revisions
--- a/src/canvas.cpp	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/canvas.cpp	Mon Apr 02 10:33:17 2018 +0300
@@ -86,15 +86,15 @@
 	Vertex topLeft = currentCamera().idealize(currentCamera().convert2dTo3d({0, 0}));
 	Vertex bottomRight = currentCamera().idealize(currentCamera().convert2dTo3d({width(), height()}));
 	qreal gridSize = grid()->coordinateSnap();
-	// glEnable(GL_LINE_STIPPLE);
+	glEnable(GL_LINE_STIPPLE);
 	glBegin(GL_LINES);
 
 	switch (grid()->type())
 	{
 	case Grid::Cartesian:
 		{
-			qreal x0 = sign(topLeft.x) * (fabs(topLeft.x) - fmod(fabs(topLeft.x), gridSize));
-			qreal y0 = sign(topLeft.y) * (fabs(topLeft.y) - fmod(fabs(topLeft.y), gridSize));
+			qreal x0 = sign(topLeft.x) * (abs(topLeft.x) - fmod(abs(topLeft.x), gridSize));
+			qreal y0 = sign(topLeft.y) * (abs(topLeft.y) - fmod(abs(topLeft.y), gridSize));
 
 			static const auto prepareGridLine = [](qreal value) -> bool
 			{
@@ -198,6 +198,18 @@
 
 	glEnd();
 	glDisable(GL_LINE_STIPPLE);
+
+	if (this->camera() < Camera::Free)
+	{
+		GLfloat cullz = this->cullValues[static_cast<int>(this->camera())];
+		GLRotationMatrix matrix = {
+			1, 0, 0, cullz,
+			0, 1, 0, 0,
+			0, 0, 1, 0,
+			0, 0, 0, 1,
+		};
+		glMultMatrixf(matrix);
+	}
 }
 
 bool Canvas::freeCameraAllowed() const
@@ -376,3 +388,23 @@
 		event->acceptProposedAction();
 	*/
 }
+
+double Canvas::currentCullValue() const
+{
+	if (this->camera() < Camera::Free)
+		return far - this->cullValues[static_cast<int>(this->camera())];
+	else
+		return 0.0;
+}
+
+void Canvas::setCullValue(double value)
+{
+	if (this->camera() < Camera::Free)
+		this->cullValues[static_cast<int>(this->camera())] = far - value;
+}
+
+void Canvas::clearCurrentCullValue()
+{
+	if (this->camera() < Camera::Free)
+		this->cullValues[static_cast<int>(this->camera())] = 0.0;
+}
--- a/src/canvas.h	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/canvas.h	Mon Apr 02 10:33:17 2018 +0300
@@ -32,12 +32,15 @@
 	void drawPoint(QPainter& painter, QPointF pos, QColor color = QColor (64, 192, 0)) const;
 	void drawBlipCoordinates(QPainter& painter, const Vertex& pos3d) const;
 	void drawBlipCoordinates(QPainter& painter, const Vertex& pos3d, QPointF pos) const;
+	void clearCurrentCullValue();
+	double currentCullValue() const;
 	double getDepthValue() const;
 	void getRelativeAxes(Axis& relX, Axis& relY) const;
 	Axis getRelativeZ() const;
 	QPen linePen() const;
 	const Vertex& position3D() const;
 	void setDepthValue(double depth);
+	void setCullValue(double value);
 	void setEditMode(EditModeType type);
 
 protected:
@@ -58,4 +61,5 @@
 	AbstractEditMode* m_currentEditMode = nullptr;
 	Vertex m_position3D;
 	double m_depthValues[6] = {0};
+	double cullValues[6] = {0};
 };
--- a/src/glrenderer.cpp	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/glrenderer.cpp	Mon Apr 02 10:33:17 2018 +0300
@@ -341,7 +341,7 @@
 	glViewport (0, 0, width, height);
 	glMatrixMode (GL_PROJECTION);
 	glLoadIdentity();
-	gluPerspective (45.0f, (double) width / (double) height, 1.0f, 10000.0f);
+	gluPerspective (45.0f, (double) width / (double) height, near, far);
 	glMatrixMode (GL_MODELVIEW);
 
 	// Unfortunately Qt does not provide a resized() signal so we have to manually feed the information.
--- a/src/glrenderer.h	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/glrenderer.h	Mon Apr 02 10:33:17 2018 +0300
@@ -54,6 +54,8 @@
 
 public:
 	enum { BlackRgb = 0xff000000 };
+	static constexpr GLfloat near = 1.0f;
+	static constexpr GLfloat far = 10000.0f;
 
 	GLRenderer(const Model* model, QWidget* parent = nullptr);
 	~GLRenderer();
--- a/src/mainwindow.cpp	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/mainwindow.cpp	Mon Apr 02 10:33:17 2018 +0300
@@ -497,6 +497,8 @@
 	{
 		contextMenu->addSeparator();
 		contextMenu->addAction (ui.actionSetDrawDepth);
+		contextMenu->addAction(ui.actionSetCullDepth);
+		contextMenu->addAction(ui.actionClearCullDepth);
 	}
 
 	contextMenu->exec(position);
--- a/src/mainwindow.ui	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/mainwindow.ui	Mon Apr 02 10:33:17 2018 +0300
@@ -1758,6 +1758,16 @@
     <string>Fi&amp;x rounding errors</string>
    </property>
   </action>
+  <action name="actionSetCullDepth">
+   <property name="text">
+    <string>Set cull depth</string>
+   </property>
+  </action>
+  <action name="actionClearCullDepth">
+   <property name="text">
+    <string>Clear cull depth</string>
+   </property>
+  </action>
  </widget>
  <customwidgets>
   <customwidget>
--- a/src/model.cpp	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/model.cpp	Mon Apr 02 10:33:17 2018 +0300
@@ -175,9 +175,10 @@
 	if (index.isValid())
 	{
 		removeAt(index.row());
+		int position = index.row();
 
-		for (signed int i = countof(model.objects()) - 1; i >= 0; i -= 1)
-			insertCopy(index.row() + i, model.objects()[i]);
+		for (signed int i = 0; i < countof(model); i += 1)
+			insertCopy(position + i, model.objects()[i]);
 	}
 }
 
--- a/src/toolsets/viewtoolset.cpp	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/toolsets/viewtoolset.cpp	Mon Apr 02 10:33:17 2018 +0300
@@ -174,12 +174,51 @@
 		return;
 
 	bool ok;
-	double depth = QInputDialog::getDouble(m_window, "Set Draw Depth",
-	                                       format("Depth value for %1:", m_window->renderer()->currentCamera().name()),
-	                                       m_window->renderer()->getDepthValue(), -10000.0f, 10000.0f, 3, &ok);
+	double depth = QInputDialog::getDouble(
+		m_window,
+		tr("Set draw depth"),
+		format(
+			tr("Depth value for %1:"),
+			m_window->renderer()->currentCamera().name()
+		),
+		m_window->renderer()->getDepthValue(),
+		-10000.0f,
+		10000.0f,
+		4,
+		&ok
+	);
 
 	if (ok)
-		m_window->renderer()->setDepthValue (depth);
+		m_window->renderer()->setDepthValue(depth);
+}
+
+void ViewToolset::setCullDepth()
+{
+	if (m_window->renderer()->camera() == Camera::Free)
+		return;
+
+	bool ok;
+	double depth = QInputDialog::getDouble(
+		m_window,
+		tr("Set cull value"),
+		format(
+			tr("Cull depth for %1:\nPolygons closer than at this depth are not shown."),
+			m_window->renderer()->currentCamera().name()
+		),
+		m_window->renderer()->currentCullValue(),
+		-GLRenderer::far,
+		GLRenderer::far,
+		4,
+		&ok
+	);
+
+	if (ok)
+		m_window->renderer()->setCullValue(depth);
+}
+
+void ViewToolset::clearCullDepth()
+{
+	m_window->renderer()->clearCurrentCullValue();
 }
 
 #if 0
--- a/src/toolsets/viewtoolset.h	Sun Apr 01 15:32:30 2018 +0300
+++ b/src/toolsets/viewtoolset.h	Mon Apr 02 10:33:17 2018 +0300
@@ -28,6 +28,7 @@
 
 	Q_INVOKABLE void axes();
 	Q_INVOKABLE void bfcView();
+	Q_INVOKABLE void clearCullDepth();
 	Q_INVOKABLE void drawAngles();
 	Q_INVOKABLE void drawConditionalLines();
 	Q_INVOKABLE void drawEdgeLines();
@@ -40,6 +41,7 @@
 	Q_INVOKABLE void selectAll();
 	Q_INVOKABLE void selectByColor();
 	Q_INVOKABLE void selectByType();
+	Q_INVOKABLE void setCullDepth();
 	Q_INVOKABLE void setDrawDepth();
 	Q_INVOKABLE void visibilityHide();
 	Q_INVOKABLE void visibilityReveal();

mercurial