From 31a97e7c2e445ad245e123462b496d6a58712b9a Mon Sep 17 00:00:00 2001 From: ccolin Date: Sun, 3 Oct 2021 00:11:29 +0200 Subject: [PATCH] fix the rendering --- src/mesh_view.cpp | 24 +++++++++++++----------- src/mesh_viewer.cpp | 34 +++++++++++++++++----------------- src/shader.frag | 8 ++++---- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/mesh_view.cpp b/src/mesh_view.cpp index 57f7ad1..e6d96a5 100644 --- a/src/mesh_view.cpp +++ b/src/mesh_view.cpp @@ -1,11 +1,13 @@ #include "mesh_view.h" #include +#include MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program) : buffer(QOpenGLBuffer::VertexBuffer), + nverts(mesh.n_faces() * 3), mesh(mesh) { - GLfloat *verts = new GLfloat[mesh.n_faces() * 3 * 6]; + QVector verts(nverts * 6); size_t i = 0; for (const FaceHandle face : mesh.faces()) { for (const VertexHandle vec : mesh.fv_range(face)) { @@ -24,8 +26,7 @@ MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program) buffer.create(); buffer.bind(); buffer.setUsagePattern(QOpenGLBuffer::StreamDraw); - buffer.allocate(verts, nverts * 6 * sizeof (GLfloat)); - delete[] verts; + buffer.allocate(verts.constData(), nverts * 6 * sizeof (GLfloat)); program.setAttributeBuffer("pos", GL_FLOAT, 0, 3, 6 * sizeof (GLfloat)); program.enableAttributeArray("pos"); program.setAttributeBuffer("col", GL_FLOAT, 3 * sizeof (GLfloat), 3, 6 * sizeof (GLfloat)); @@ -43,10 +44,11 @@ MeshView::~MeshView() { void MeshView::paint(QOpenGLShaderProgram &program) { QOpenGLContext *ctx = QOpenGLContext::currentContext(); QOpenGLExtraFunctions *glf = ctx->extraFunctions(); - void (*glPolygonMode)(GLenum, GLenum) = nullptr; - glPolygonMode = (typeof glPolygonMode) ctx->getProcAddress("glPolygonMode"); - if (!glPolygonMode) - qWarning("glPolygonMode not available"); + + QOpenGLFunctions_2_1 *glf21 = nullptr; + glf21 = ctx->versionFunctions(); + if (!glf21) + qFatal("Failed to get OpenGL 2.1 functions"); program.setUniformValue("model", mesh.transform); @@ -55,14 +57,14 @@ void MeshView::paint(QOpenGLShaderProgram &program) { glf->glEnable(GL_POLYGON_OFFSET_FILL); glf->glPolygonOffset(1.0, 2); glf->glDrawArrays(GL_TRIANGLES, 0, nverts); - if (glPolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glf21->glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glf->glDisable(GL_POLYGON_OFFSET_FILL); /* Wireframe */ program.setUniformValue("wireframe", 1); glf->glDrawArrays(GL_TRIANGLES, 0, nverts); - if (glPolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glf->glLineWidth(3); - program.setUniformValue("wireframe", 1); + glf21->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glf->glLineWidth(2); + program.setUniformValue("wireframe", 0); } } diff --git a/src/mesh_viewer.cpp b/src/mesh_viewer.cpp index c5f1f37..d8ee361 100644 --- a/src/mesh_viewer.cpp +++ b/src/mesh_viewer.cpp @@ -20,34 +20,33 @@ QSize MeshViewer::sizeHint() const { void MeshViewer::initializeGL() { QOpenGLFunctions *glf = context()->functions(); #ifdef QT_DEBUG + const QSurfaceFormat &format = context()->format(); + qDebug("MeshViewer: OpenGL %s%d.%d %s", + format.renderableType() == QSurfaceFormat::OpenGLES ? "ES " : "", + format.majorVersion(), format.minorVersion(), + format.profile() == QSurfaceFormat::CoreProfile ? "core profile" + : (format.profile() == QSurfaceFormat::CompatibilityProfile ? "compatibility profile" + : "")); QOpenGLDebugLogger *logger = new QOpenGLDebugLogger(this); if (!logger->initialize()) { qDebug("OpenGL debug output unavailable"); } connect(logger, &QOpenGLDebugLogger::messageLogged, [](const QOpenGLDebugMessage &message) { - qDebug() << "OpenGL: " << message.message(); + qDebug() << "OpenGL:" << message.message(); }); logger->startLogging(); #endif - - QOpenGLShader vertex_shader(QOpenGLShader::Vertex); - if (!vertex_shader.compileSourceFile(":/shader.vert")) { - qCritical() << vertex_shader.log(); + if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, + ":/shader.vert")) { + qFatal("%s", program.log().toUtf8().constData()); } - - QOpenGLShader fragment_shader(QOpenGLShader::Fragment); - if (fragment_shader.compileSourceFile(":/shader.frag")) { - qCritical() << fragment_shader.log(); - } - if (!program.addShader(&vertex_shader)) { - qCritical() << program.log(); - } - if (!program.addShader(&fragment_shader)) { - qCritical() << program.log(); + if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, + ":/shader.frag")) { + qFatal("%s", program.log().toUtf8().constData()); } if (!program.link()) { - qCritical() << program.log(); + qFatal("%s", program.log().toUtf8().constData()); } program.bind(); @@ -59,7 +58,7 @@ void MeshViewer::initializeGL() { glf->glEnable(GL_DEPTH_TEST); glf->glEnable(GL_MULTISAMPLE); - qDebug("Mesh viewer: initialization complete"); + qDebug("MeshViewer: initialization complete"); emit initialized(); } @@ -76,6 +75,7 @@ void MeshViewer::paintGL() { QMatrix4x4 trans; trans.translate(0, 0, -cam_dist); QMatrix4x4 view = trans * rot; + program.bind(); program.setUniformValue("view", view); for (MeshView &m : meshes) { m.paint(program); diff --git a/src/shader.frag b/src/shader.frag index c14c16c..35ef0e1 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -5,9 +5,9 @@ uniform bool wireframe; uniform float alpha; void main() { - if (!wireframe) - // gl_FragColor = vec4(wf_col, alpha); - gl_FragColor = vec4(.5, .5, .5, 1); + if (wireframe) + gl_FragColor = vec4(wf_col, alpha); else - gl_FragColor = vec4(frag_col, alpha); + // gl_FragColor = vec4(frag_col, alpha); + gl_FragColor = vec4(.5, .5, .5, alpha); }