src/gl/partrenderer.cpp

changeset 22
6da867fa5429
parent 21
0133e565e072
child 23
3387a84ddaba
--- a/src/gl/partrenderer.cpp	Wed Jan 01 17:45:56 2020 +0200
+++ b/src/gl/partrenderer.cpp	Sun Jan 19 02:54:48 2020 +0200
@@ -14,7 +14,7 @@
 void PartRenderer::initializeGL()
 {
 	this->initializeOpenGLFunctions();
-	if (this->glGetError() != GL_NO_ERROR)
+	if (glGetError() != GL_NO_ERROR)
 	{
 		abort();
 	}
@@ -23,6 +23,7 @@
 	this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
 	this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
 	this->compiler->build(this->model, this->documents);
+	glLineWidth(2.0);
 }
 
 /*
@@ -57,16 +58,16 @@
 
 void PartRenderer::resizeGL(int width, int height)
 {
-	constexpr GLfloat near = 1.0f;
-	constexpr GLfloat far = 1e+05f;
+	constexpr GLdouble near = 1.0;
+	constexpr GLdouble far = 1e+05;
 	glViewport (0, 0, width, height);
 	glMatrixMode(GL_PROJECTION);
 	glLoadIdentity();
-	gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far);
+	gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far);
 	glMatrixMode(GL_MODELVIEW);
 }
 
-static int getGlTypeForVboClass(const gl::VboClass vboClass)
+static GLenum getGlTypeForVboClass(const gl::VboClass vboClass)
 {
 	switch (vboClass)
 	{
@@ -85,6 +86,20 @@
 #include <QMessageBox>
 void PartRenderer::paintGL()
 {
+	glEnable (GL_BLEND);
+	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+	glEnable (GL_POLYGON_OFFSET_FILL);
+	glPolygonOffset (1.0f, 1.0f);
+	glEnable (GL_DEPTH_TEST);
+	glShadeModel (GL_SMOOTH);
+	glEnable (GL_MULTISAMPLE);
+	glEnable (GL_LINE_SMOOTH);
+	glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
+	this->renderScene();
+}
+
+void PartRenderer::renderScene()
+{
 	switch (this->renderStyle)
 	{
 	case gl::RenderStyle::Normal:
@@ -95,6 +110,7 @@
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		break;
 	}
+	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 	glMatrixMode(GL_MODELVIEW);
 	// clear the drawing buffer.
 	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
@@ -105,33 +121,32 @@
 	glLoadIdentity();
 	// traslate the draw by z = -4.0
 	// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
-	glTranslatef(0.0,0.0,-4.5);
-	// Red color used to draw.
-	glColor3f(0.8, 0.2, 0.1);
+	glTranslatef(0.0, 0.0, -4.5 * this->compiler->modelDistance());
 	glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData());
-	//glutSolidTeapot(1.0);
+	xyz(glTranslatef, -this->compiler->modelCenter());
+	glEnableClientState(GL_NORMAL_ARRAY);
 	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;
+		const GLuint vboSurfaces = this->compiler->vbo({vboClass, gl::VboSubclass::Surfaces});
+		const GLuint vboColors = this->compiler->vbo({vboClass, gl::VboSubclass::RegularColors});
+		const GLuint vboNormals = this->compiler->vbo({vboClass, gl::VboSubclass::Normals});
+		const std::size_t count = this->compiler->vboSize({vboClass, gl::VboSubclass::Surfaces}) / 3_z;
 		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, vboNormals);
+		glNormalPointer(GL_FLOAT, 0, nullptr);
+		glDrawArrays(getGlTypeForVboClass(vboClass), 0, static_cast<GLsizei>(count));
 	}
 	glBindBuffer(GL_ARRAY_BUFFER, 0);
+	glDisableClientState(GL_NORMAL_ARRAY);
 	glDisableClientState(GL_VERTEX_ARRAY);
 	glDisableClientState(GL_COLOR_ARRAY);
-
 	//glFlush();
-	const int glError = this->glGetError();
+	const GLenum glError = this->glGetError();
 	if (glError != GL_NO_ERROR)
 	{
 		const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError)));
@@ -140,6 +155,7 @@
 			tr("Rendering error"),
 			QString{"Failed to render: %1"}.arg(glErrorString));
 	}
+	glDisable(GL_CULL_FACE);
 }
 
 static QPointF pointToPointF(const QPoint& point)
@@ -162,7 +178,7 @@
 	{
 		const QQuaternion versor = QQuaternion::fromAxisAndAngle(
 			QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f},
-			0.6 * std::hypot(move.x(), move.y())
+			0.6f * static_cast<float>(std::hypot(move.x(), move.y()))
 		);
 		this->rotation = versor * this->rotation;
 		this->update();

mercurial