src/gl/partrenderer.cpp

changeset 21
0133e565e072
parent 18
918b6c0f8b5b
child 22
6da867fa5429
--- a/src/gl/partrenderer.cpp	Sat Dec 14 23:00:01 2019 +0200
+++ b/src/gl/partrenderer.cpp	Wed Jan 01 17:45:56 2020 +0200
@@ -2,8 +2,11 @@
 #include <GL/glut.h> // teapot
 #include "partrenderer.h"
 
-PartRenderer::PartRenderer(QWidget* parent) :
-	QOpenGLWidget{parent}
+PartRenderer::PartRenderer(Model* model, DocumentManager* documents, QWidget* parent) :
+	QOpenGLWidget{parent},
+	model{model},
+	documents{documents},
+	compiler{new gl::Compiler{this}}
 {
 	this->setMouseTracking(true);
 }
@@ -19,6 +22,7 @@
 	this->initialized = true;
 	this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
 	this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
+	this->compiler->build(this->model, this->documents);
 }
 
 /*
@@ -62,7 +66,23 @@
 	glMatrixMode(GL_MODELVIEW);
 }
 
+static int getGlTypeForVboClass(const gl::VboClass vboClass)
+{
+	switch (vboClass)
+	{
+	case gl::VboClass::Lines:
+	case gl::VboClass::ConditionalLines:
+		return GL_LINES;
+	case gl::VboClass::Triangles:
+		return GL_TRIANGLES;
+	case gl::VboClass::Quads:
+		return GL_QUADS;
+	}
+	throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
+}
+
 // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on
+#include <QMessageBox>
 void PartRenderer::paintGL()
 {
 	switch (this->renderStyle)
@@ -77,7 +97,10 @@
 	}
 	glMatrixMode(GL_MODELVIEW);
 	// clear the drawing buffer.
-	glClear(GL_COLOR_BUFFER_BIT);
+	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	glEnable(GL_DEPTH_TEST);
+	glEnable(GL_LIGHTING);
 	// clear the identity matrix.
 	glLoadIdentity();
 	// traslate the draw by z = -4.0
@@ -86,8 +109,37 @@
 	// Red color used to draw.
 	glColor3f(0.8, 0.2, 0.1);
 	glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
-	glutSolidTeapot(1.0);
-	glFlush();
+	//glutSolidTeapot(1.0);
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glEnableClientState(GL_COLOR_ARRAY);
+	for (const gl::VboClass vboClass : {gl::VboClass::Lines, gl::VboClass::Triangles, gl::VboClass::Quads})
+	{
+		const int vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces});
+		const int vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors});
+		const int vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals});
+		const int count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3;
+		glBindBuffer(GL_ARRAY_BUFFER, vboSurfaces);
+		glVertexPointer(3, GL_FLOAT, 0, nullptr);
+		glBindBuffer(GL_ARRAY_BUFFER, vboColors);
+		glColorPointer(4, GL_FLOAT, 0, nullptr);
+		//glBindBuffer(GL_ARRAY_BUFFER, vboNormals);
+		//glNormalPointer(GL_FLOAT, 0, nullptr);
+		glDrawArrays(getGlTypeForVboClass(vboClass), 0, count);
+	}
+	glBindBuffer(GL_ARRAY_BUFFER, 0);
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_COLOR_ARRAY);
+
+	//glFlush();
+	const int glError = this->glGetError();
+	if (glError != GL_NO_ERROR)
+	{
+		const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError)));
+		QMessageBox::critical(
+			this,
+			tr("Rendering error"),
+			QString{"Failed to render: %1"}.arg(glErrorString));
+	}
 }
 
 static QPointF pointToPointF(const QPoint& point)
@@ -118,6 +170,11 @@
 	this->lastMousePosition = pointToPointF(event->pos());
 }
 
+void PartRenderer::setCompiler(gl::Compiler* compiler)
+{
+	this->compiler = compiler;
+}
+
 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
 {
 	this->renderStyle = newStyle;

mercurial