it works!

Thu, 23 Jan 2020 00:29:10 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 23 Jan 2020 00:29:10 +0200
changeset 30
1536f23cfab7
parent 29
4cc6b582fde8
child 31
b6df269a2c6b

it works!

locale/fi.ts file | annotate | diff | comparison | revisions
locale/sv.ts 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/locale/fi.ts	Wed Jan 22 22:43:28 2020 +0200
+++ b/locale/fi.ts	Thu Jan 23 00:29:10 2020 +0200
@@ -189,7 +189,7 @@
 <context>
     <name>PartRenderer</name>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="182"/>
+        <location filename="../src/gl/partrenderer.cpp" line="160"/>
         <source>Rendering error</source>
         <translation type="unfinished"></translation>
     </message>
@@ -233,4 +233,27 @@
         <translation type="unfinished"></translation>
     </message>
 </context>
+<context>
+    <name>gl::Compiler</name>
+    <message>
+        <location filename="../src/gl/compiler.cpp" line="79"/>
+        <source>Vertex shader:</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../src/gl/compiler.cpp" line="84"/>
+        <source>Fragment shader:</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../src/gl/compiler.cpp" line="88"/>
+        <source>Shader compile error</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../src/gl/compiler.cpp" line="88"/>
+        <source>Could not compile shaders.</source>
+        <translation type="unfinished"></translation>
+    </message>
+</context>
 </TS>
--- a/locale/sv.ts	Wed Jan 22 22:43:28 2020 +0200
+++ b/locale/sv.ts	Thu Jan 23 00:29:10 2020 +0200
@@ -251,4 +251,23 @@
         <translation type="unfinished"></translation>
     </message>
 </context>
+<context>
+    <name>gl::Compiler</name>
+    <message>
+        <source>Vertex shader:</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <source>Fragment shader:</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <source>Shader compile error</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <source>Could not compile shaders.</source>
+        <translation type="unfinished"></translation>
+    </message>
+</context>
 </TS>
--- a/src/gl/compiler.cpp	Wed Jan 22 22:43:28 2020 +0200
+++ b/src/gl/compiler.cpp	Thu Jan 23 00:29:10 2020 +0200
@@ -97,8 +97,9 @@
 			object.vertexArray.bind();
 			object.program->enableAttributeArray(0);
 			object.program->enableAttributeArray(1);
-			object.program->setAttributeBuffer(0, GL_FLOAT, 0, 3);
-			object.program->setAttributeBuffer(1, GL_FLOAT, 3, 4);
+			constexpr int stride = 7 * sizeof(GLfloat);
+			object.program->setAttributeBuffer(0, GL_FLOAT, 0 * sizeof(GLfloat), 3, stride);
+			object.program->setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(GLfloat), 4, stride);
 			object.vertexArray.release();
 			object.buffer.release();
 			object.program->release();
@@ -179,8 +180,6 @@
 	for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
 	{
 		Point3D& point = polygon.vertices[i];
-		polygon.vertices[i].y = -polygon.vertices[i].y;
-		polygon.vertices[i].z = -polygon.vertices[i].z;
 		this->boundingBox.consider(polygon.vertices[i]);
 		vertexBuffer.push_back(static_cast<GLfloat>(point.x));
 		vertexBuffer.push_back(static_cast<GLfloat>(point.y));
--- a/src/gl/compiler.h	Wed Jan 22 22:43:28 2020 +0200
+++ b/src/gl/compiler.h	Thu Jan 23 00:29:10 2020 +0200
@@ -21,6 +21,7 @@
 #include "gl/common.h"
 #include "types/boundingbox.h"
 #include <glm/glm.hpp>
+#include <glm/gtc/type_ptr.hpp>
 #include <QMap>
 #include <QSet>
 #include <QOpenGLVertexArrayObject>
@@ -59,6 +60,7 @@
 	void bindVertexArray(gl::ArrayClass arrayClass);
 	void releaseVertexArray(gl::ArrayClass arrayClass);
 	void buildShaders(int arrayId);
+
 	template<typename T>
 	void setUniform(const char* uniformName, T&& value)
 	{
@@ -72,6 +74,13 @@
 			object.program->release();
 		}
 	}
+
+	template<typename Float, glm::qualifier Prec>
+	void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value)
+	{
+		const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value));
+		this->setUniform(uniformName, *array);
+	}
 private:
 	bool m_vboChanged[gl::numVbos] = {true};
 	std::size_t storedVboSizes[gl::numVbos] = {0_z};
--- a/src/gl/partrenderer.cpp	Wed Jan 22 22:43:28 2020 +0200
+++ b/src/gl/partrenderer.cpp	Thu Jan 23 00:29:10 2020 +0200
@@ -44,8 +44,6 @@
 	{
 		abort();
 	}
-	glEnableClientState(GL_NORMAL_ARRAY);
-	glEnableClientState(GL_VERTEX_ARRAY);
 	this->compiler->initialize();
 	this->compiler->build(this->model, this->documents);
 	this->initializeLighting();
@@ -53,22 +51,9 @@
 	this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
 	this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
 	glLineWidth(2.0);
+	this->update();
 }
 
-/*
- * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix.
- */
-static QMatrix4x4 padMatrix(const QMatrix3x3& stub)
-{
-	return {
-		stub(0, 0), stub(0, 1), stub(0, 2), 0,
-		stub(1, 0), stub(1, 1), stub(1, 2), 0,
-		stub(2, 0), stub(2, 1), stub(2, 2), 0,
-		0, 0, 0, 1
-	};
-}
-
-
 void PartRenderer::initializeLighting()
 {
 	GLfloat materialShininess[] = {5.0};
@@ -128,16 +113,8 @@
 {
 	glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	glMatrixMode(GL_MODELVIEW);
 	glEnable(GL_DEPTH_TEST);
 	glEnable(GL_LIGHTING);
-	glLoadIdentity();
-	//glTranslated(0.0, 0.0, -4.5 * this->compiler->modelDistance());
-	//glTranslated(0.0, 0.0, -4.5);
-	//glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
-	//xyz(glTranslatef, -this->compiler->modelCenter());
-	auto rotationMatrix = padMatrix(this->rotation.toRotationMatrix());
-	rotationMatrix(2, 3) = 0;
 	glEnable(GL_POLYGON_OFFSET_FILL);
 	glPolygonOffset(1.0f, 1.0f);
 	switch (this->renderStyle)
@@ -150,7 +127,7 @@
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		break;
 	}
-	this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix));
+	this->compiler->setUniformMatrix("CameraTransformation", 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);
@@ -191,12 +168,13 @@
 	const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
 	if (left and not move.isNull())
 	{
+		// FIXME: find a more elegant way to do this
 		const QQuaternion versor = QQuaternion::fromAxisAndAngle(
-			QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f},
+			QVector3D{static_cast<float>(-move.y()), static_cast<float>(move.x()), 0.0f},
 			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});
+		QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, static_cast<float>(2 + 2 * this->compiler->modelDistance())});
 		glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()};
 		this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0});
 		this->update();
@@ -204,11 +182,6 @@
 	this->lastMousePosition = pointToPointF(event->pos());
 }
 
-void PartRenderer::setCompiler(gl::Compiler* compiler)
-{
-	this->compiler = compiler;
-}
-
 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
 {
 	this->renderStyle = newStyle;
--- a/src/gl/partrenderer.h	Wed Jan 22 22:43:28 2020 +0200
+++ b/src/gl/partrenderer.h	Thu Jan 23 00:29:10 2020 +0200
@@ -22,7 +22,6 @@
 	void resizeGL(int width, int height) override;
 	void paintGL() override;
 	void mouseMoveEvent(QMouseEvent* event) override;
-	void setCompiler(gl::Compiler* compiler);
 private slots:
 	void setRenderStyle(const gl::RenderStyle newStyle);
 private:

mercurial