modelview matrix set up

Wed, 22 Jan 2020 22:41:17 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 22:41:17 +0200
changeset 28
c92c1daf735f
parent 27
c57fb7a5ffa3
child 29
4cc6b582fde8

modelview matrix set up

CMakeLists.txt file | annotate | diff | comparison | revisions
src/gl/compiler.cpp file | annotate | diff | comparison | revisions
src/gl/compiler.h file | annotate | diff | comparison | revisions
src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Wed Jan 22 01:17:11 2020 +0200
+++ b/CMakeLists.txt	Wed Jan 22 22:41:17 2020 +0200
@@ -12,10 +12,14 @@
 find_package(Qt5Network REQUIRED)
 find_package(Qt5LinguistTools REQUIRED)
 find_package(GLUT REQUIRED)
-set (CMAKE_AUTOMOC ON)
+find_package(GLM REQUIRED)
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_AUTOUIC ON)
+set(CMAKE_AUTORCC ON)
 find_package(OpenGL REQUIRED)
 include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
 include_directories(${GLUT_INCLUDE_DIR})
+include_directories(${GLM_INCLUDE_DIR})
 
 set (LDFORGE_SOURCES
 	src/colors.cpp
@@ -103,7 +107,6 @@
 qt5_create_translation(QM_FILES ${LDFORGE_SOURCES} ${LDFORGE_HEADERS} ${LDFORGE_FORMS} ${LDFORGE_LOCALES})
 add_custom_target(translations ALL DEPENDS ${QM_FILES})
 add_custom_target(resources ALL DEPENDS ${LDFORGE_RESOURCES})
-add_dependencies(resources translations)
 
 set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -129,6 +132,7 @@
 set(LANGUAGES_QRC "languages.qrc")
 configure_file(${LANGUAGES_QRC} ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
 qt5_add_resources(LDFORGE_QM_RC_FILE ${CMAKE_CURRENT_BINARY_DIR}/${LANGUAGES_QRC})
+set_source_files_properties(${LDFORGE_LOCALES} PROPERTIES HEADER_FILE_ONLY TRUE)
 
 add_executable(ldforge WIN32
 	${LDFORGE_SOURCES}
@@ -136,6 +140,7 @@
 	${LDFORGE_RESOURCES}
 	${LDFORGE_QRC}
 	${LDFORGE_QM_RC_FILE}
+	${QM_FILES}
 	${LDFORGE_FORMS_HEADERS}
 )
 
@@ -144,6 +149,7 @@
 set_target_properties(ldforge PROPERTIES AUTOMOC 1)
 target_link_libraries(ldforge Qt5::Widgets Qt5::Network Qt5::OpenGL ${OPENGL_LIBRARIES})
 target_link_libraries(ldforge ${GLUT_LIBRARIES})
+add_dependencies(ldforge resources)
 cotire(ldforge)
 
 # Collect the current hg revision into hginfo.h
--- a/src/gl/compiler.cpp	Wed Jan 22 01:17:11 2020 +0200
+++ b/src/gl/compiler.cpp	Wed Jan 22 22:41:17 2020 +0200
@@ -36,7 +36,7 @@
 void main()
 {
 	vColor = color;
-	gl_Position = projection * modelview * CameraTransformation * vec4(position, 1.0);
+	gl_Position = CameraTransformation * vec4(position, 1.0);
 }
 )";
 
@@ -72,8 +72,22 @@
 			auto& object = this->glObjects[i];
 			object.program = new QOpenGLShaderProgram;
 			object.program->create();
-			object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
-			object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
+			const bool vertexShaderCompiled = object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
+			QString log;
+			if (not vertexShaderCompiled)
+			{
+				log += "\n" + tr("Vertex shader:") + "\n" + object.program->log();
+			}
+			const bool fragmentShaderCompiled = object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
+			if (not fragmentShaderCompiled)
+			{
+				log += "\n" + tr("Fragment shader:") + "\n" + object.program->log();
+			}
+			if (not vertexShaderCompiled or not fragmentShaderCompiled)
+			{
+				QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Could not compile shaders.") + "\n" + log);
+				std::exit(-1);
+			}
 			object.program->link();
 			object.program->bind();
 			object.buffer.create();
@@ -233,3 +247,25 @@
 	return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses
 		+ static_cast<std::uint8_t>(vboAddress.vboSubclass);
 }
+
+QMatrix4x4 gl::toQMatrix(const glm::mat4& matrix)
+{
+	return  {
+		matrix[0][0],
+		matrix[0][1],
+		matrix[0][2],
+		matrix[0][3],
+		matrix[1][0],
+		matrix[1][1],
+		matrix[1][2],
+		matrix[1][3],
+		matrix[2][0],
+		matrix[2][1],
+		matrix[2][2],
+		matrix[2][3],
+		matrix[3][0],
+		matrix[3][1],
+		matrix[3][2],
+		matrix[3][3],
+	};
+}
--- a/src/gl/compiler.h	Wed Jan 22 01:17:11 2020 +0200
+++ b/src/gl/compiler.h	Wed Jan 22 22:41:17 2020 +0200
@@ -20,6 +20,7 @@
 #include "main.h"
 #include "gl/common.h"
 #include "types/boundingbox.h"
+#include <glm/glm.hpp>
 #include <QMap>
 #include <QSet>
 #include <QOpenGLVertexArrayObject>
@@ -39,6 +40,7 @@
 		VboSubclass vboSubclass;
 	};
 	int vboIndex(const VboAddress vboAddress);
+	QMatrix4x4 toQMatrix(const glm::mat4& matrix);
 }
 
 class gl::Compiler : public QObject, protected QOpenGLFunctions
--- a/src/gl/partrenderer.cpp	Wed Jan 22 01:17:11 2020 +0200
+++ b/src/gl/partrenderer.cpp	Wed Jan 22 22:41:17 2020 +0200
@@ -17,6 +17,8 @@
  */
 
 #include <GL/glut.h>
+#include <glm/ext/matrix_transform.hpp>
+#include <glm/ext/matrix_clip_space.hpp>
 #include <QMouseEvent>
 #include <QMessageBox>
 #include "partrenderer.h"
@@ -85,13 +87,12 @@
 
 void PartRenderer::resizeGL(int width, int height)
 {
-	constexpr GLdouble near = 1.0;
-	constexpr GLdouble far = 1e+05;
-	glViewport (0, 0, width, height);
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far);
-	glMatrixMode(GL_MODELVIEW);
+	glViewport(0, 0, width, height);
+	this->projectionMatrix = glm::perspective(
+		glm::radians(90.0f),
+		static_cast<float>(width) / static_cast<float>(height),
+		0.1f,
+		100.f);
 }
 
 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
@@ -149,7 +150,7 @@
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		break;
 	}
-	this->compiler->setUniform("CameraTransformation", rotationMatrix);
+	this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix));
 	// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
 	renderVao(gl::ArrayClass::Triangles);
 	renderVao(gl::ArrayClass::Quads);
@@ -195,6 +196,9 @@
 			0.6f * static_cast<float>(std::hypot(move.x(), move.y()))
 		);
 		this->rotation = versor * this->rotation;
+		QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, 4.5});
+		glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()};
+		this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0});
 		this->update();
 	}
 	this->lastMousePosition = pointToPointF(event->pos());
--- a/src/gl/partrenderer.h	Wed Jan 22 01:17:11 2020 +0200
+++ b/src/gl/partrenderer.h	Wed Jan 22 22:41:17 2020 +0200
@@ -6,6 +6,7 @@
 #include <QOpenGLBuffer>
 #include <QOpenGLShader>
 #include <QOpenGLShaderProgram>
+#include <glm/glm.hpp>
 #include "main.h"
 #include "gl/common.h"
 #include "gl/compiler.h"
@@ -33,6 +34,8 @@
 	QQuaternion rotation;
 	gl::Compiler* compiler;
 	gl::RenderStyle renderStyle = gl::RenderStyle::Normal;
+	glm::mat4 projectionMatrix;
+	glm::mat4 viewMatrix;
 	bool initialized = false;
 	void initializeLighting();
 	void renderVao(const gl::ArrayClass arrayClass);

mercurial