From 9c16aa32f1ac799f5fec233057e7bd7aa3100665 Mon Sep 17 00:00:00 2001
From: Anakin <Battlefrontler@t-online.de>
Date: Mon, 30 Jan 2017 11:31:37 +0100
Subject: [PATCH] headlight, bug fixes, code improvement,

---
 QtMeshViewer/Header/OglViewerWidget.h   |  3 +++
 QtMeshViewer/Header/SettingsWindow.h    |  2 ++
 QtMeshViewer/Source/MainWindow.cpp      |  1 +
 QtMeshViewer/Source/OglViewerWidget.cpp | 35 +++++++++++++++++--------
 QtMeshViewer/Source/SettingsWindow.cpp  |  1 +
 5 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/QtMeshViewer/Header/OglViewerWidget.h b/QtMeshViewer/Header/OglViewerWidget.h
index 0a61d1a..e0ab083 100644
--- a/QtMeshViewer/Header/OglViewerWidget.h
+++ b/QtMeshViewer/Header/OglViewerWidget.h
@@ -34,6 +34,7 @@ private:
 		QVector3D intensities = { 1.0,1.0,1.0 };
 		float attenuationFactor = 0.0f;
 		float ambientCoefficient = 0.005f;
+		bool headlight = false;
 	} m_light;
 
 	SettingsWindow* m_settings;
@@ -79,6 +80,7 @@ protected:
 
 // slots
 public slots:
+	void loadFile(QString name);
 	void toggleAxis(int axis);
 	void toggleWireframe();
 	void toggleLight();
@@ -88,6 +90,7 @@ public slots:
 	void setLightColor(QVector3D value);
 	void setAttFac(double value);
 	void setAmbCoef(double value);
+	void setHeadlight(bool value);
 	void setBackfaceCulling(bool value);
 	void setZoomSpeed(int percent);
 
diff --git a/QtMeshViewer/Header/SettingsWindow.h b/QtMeshViewer/Header/SettingsWindow.h
index ac3f8ba..e7fe472 100644
--- a/QtMeshViewer/Header/SettingsWindow.h
+++ b/QtMeshViewer/Header/SettingsWindow.h
@@ -30,6 +30,8 @@ signals:
 	void updateLightColor(QVector3D value);
 	void updateAttFac(double value);
 	void updateAmbCoef(double value);
+	void sendHeadlight(bool value);
 	void sendBackfaceCulling(bool value);
 	void sendZommSpeed(int percent);
+	
 };
\ No newline at end of file
diff --git a/QtMeshViewer/Source/MainWindow.cpp b/QtMeshViewer/Source/MainWindow.cpp
index 05c12b6..d5d5404 100644
--- a/QtMeshViewer/Source/MainWindow.cpp
+++ b/QtMeshViewer/Source/MainWindow.cpp
@@ -71,6 +71,7 @@ void MainWindow::setupWidgets()
 	// Ogl Viewer
 	OglViewerWidget* viewer = new OglViewerWidget(this);
 	setCentralWidget(viewer);
+	connect(this, &MainWindow::loadFile, viewer, &OglViewerWidget::loadFile);
 
 	// open file
 	QToolButton *openFile = new QToolButton(this);
diff --git a/QtMeshViewer/Source/OglViewerWidget.cpp b/QtMeshViewer/Source/OglViewerWidget.cpp
index 7e2f3d7..ea35f65 100644
--- a/QtMeshViewer/Source/OglViewerWidget.cpp
+++ b/QtMeshViewer/Source/OglViewerWidget.cpp
@@ -25,6 +25,7 @@ OglViewerWidget::OglViewerWidget(QWidget *parent)
 	connect(m_settings, &SettingsWindow::updateLightColor, this, &OglViewerWidget::setLightColor);
 	connect(m_settings, &SettingsWindow::updateAttFac, this, &OglViewerWidget::setAttFac);
 	connect(m_settings, &SettingsWindow::updateAmbCoef, this, &OglViewerWidget::setAmbCoef);
+	connect(m_settings, &SettingsWindow::sendHeadlight, this, &OglViewerWidget::setHeadlight);
 	connect(m_settings, &SettingsWindow::sendBackfaceCulling, this, &OglViewerWidget::setBackfaceCulling);
 	connect(m_settings, &SettingsWindow::sendZommSpeed, this, &OglViewerWidget::setZoomSpeed);
 }
@@ -68,6 +69,9 @@ void OglViewerWidget::resetView()
 	m_rotation = QQuaternion();
 	m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
 	m_zSpeed = 1;
+
+	if (m_light.headlight)
+		updateLightPosition();
 	update();
 }
 
@@ -92,10 +96,6 @@ void OglViewerWidget::initializeGL()
 	// Enable depth buffer
 	glEnable(GL_DEPTH_TEST);
 
-	//TODO: does not work
-	// Enable back face culling
-	//glEnable(GL_CULL_FACE);
-
 	// Enable transparency
 	glEnable(GL_BLEND);
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -103,12 +103,6 @@ void OglViewerWidget::initializeGL()
 	m_dataEngine = new GeometryEngine(this);
 	connect(m_dataEngine, &GeometryEngine::requestResetView, this, &OglViewerWidget::resetView);
 	connect(m_dataEngine, &GeometryEngine::requestUpdate, this, static_cast<void(OglViewerWidget::*)(void)>(&OglViewerWidget::update));
-
-	//TODO: better solution
-	MainWindow* parent = dynamic_cast<MainWindow*>(parentWidget());
-	if (parent != NULL)
-		connect(parent, &MainWindow::loadFile, [this](QString value) {m_dataEngine->loadFile(value); });
-
 }
 
 void OglViewerWidget::resizeGL(int w, int h)
@@ -274,6 +268,8 @@ void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
 		}
 
 		// request an update
+		if (m_light.headlight)
+			updateLightPosition();
 		update();
 	}
 	else if (m_mouse.right)
@@ -288,6 +284,8 @@ void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
 		m_translation += {(float)(diff.x() * 0.01), (float)(diff.y() * -0.01), 0.0};
 
 		// request an update
+		if (m_light.headlight)
+			updateLightPosition();
 		update();
 	}
 }
@@ -295,6 +293,9 @@ void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
 void OglViewerWidget::wheelEvent(QWheelEvent *e)
 {
 	m_translation += {0.0, 0.0, (float)m_zSpeed * e->angleDelta().y() / 240};
+	
+	if (m_light.headlight)
+		updateLightPosition();
 	update();
 }
 
@@ -333,6 +334,11 @@ void OglViewerWidget::dropEvent(QDropEvent * e)
 /////////////////////////////////////////////////////////////////////////
 // public slots
 
+void OglViewerWidget::loadFile(QString name)
+{
+	m_dataEngine->loadFile(name);
+}
+
 void OglViewerWidget::toggleAxis(int axis)
 {
 	switch (axis)
@@ -417,6 +423,13 @@ void OglViewerWidget::setAmbCoef(double value)
 		update();
 }
 
+void OglViewerWidget::setHeadlight(bool value)
+{
+	m_light.headlight = value;
+	if (m_lightOn)
+		update();
+}
+
 void OglViewerWidget::setBackfaceCulling(bool value)
 {
 	m_backfaceCulling = value;
@@ -425,5 +438,5 @@ void OglViewerWidget::setBackfaceCulling(bool value)
 
 void OglViewerWidget::setZoomSpeed(int percent)
 {
-	m_zSpeed = percent / 100;
+	m_zSpeed = (double) percent / 100;
 }
diff --git a/QtMeshViewer/Source/SettingsWindow.cpp b/QtMeshViewer/Source/SettingsWindow.cpp
index f751be4..763e88d 100644
--- a/QtMeshViewer/Source/SettingsWindow.cpp
+++ b/QtMeshViewer/Source/SettingsWindow.cpp
@@ -94,6 +94,7 @@ void SettingsWindow::setupConnections()
 
 	connect(ui->checkBackfaceCulling, &QCheckBox::toggled, [this]() {emit sendBackfaceCulling(ui->checkBackfaceCulling->isChecked()); });
 	connect(ui->spinZSpeed, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this](int value) {emit sendZommSpeed(value); });
+	connect(ui->checkHeadlight, &QCheckBox::toggled, [this]() {emit sendHeadlight(ui->checkHeadlight->isChecked()); });
 }
 
 
-- 
GitLab