fix the rendering

This commit is contained in:
ccolin 2021-10-03 00:11:29 +02:00
parent 7601caaada
commit 31a97e7c2e
3 changed files with 34 additions and 32 deletions

View File

@ -1,11 +1,13 @@
#include "mesh_view.h" #include "mesh_view.h"
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QOpenGLFunctions_2_1>
MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program) MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program)
: buffer(QOpenGLBuffer::VertexBuffer), : buffer(QOpenGLBuffer::VertexBuffer),
nverts(mesh.n_faces() * 3),
mesh(mesh) { mesh(mesh) {
GLfloat *verts = new GLfloat[mesh.n_faces() * 3 * 6]; QVector<GLfloat> verts(nverts * 6);
size_t i = 0; size_t i = 0;
for (const FaceHandle face : mesh.faces()) { for (const FaceHandle face : mesh.faces()) {
for (const VertexHandle vec : mesh.fv_range(face)) { for (const VertexHandle vec : mesh.fv_range(face)) {
@ -24,8 +26,7 @@ MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program)
buffer.create(); buffer.create();
buffer.bind(); buffer.bind();
buffer.setUsagePattern(QOpenGLBuffer::StreamDraw); buffer.setUsagePattern(QOpenGLBuffer::StreamDraw);
buffer.allocate(verts, nverts * 6 * sizeof (GLfloat)); buffer.allocate(verts.constData(), nverts * 6 * sizeof (GLfloat));
delete[] verts;
program.setAttributeBuffer("pos", GL_FLOAT, 0, 3, 6 * sizeof (GLfloat)); program.setAttributeBuffer("pos", GL_FLOAT, 0, 3, 6 * sizeof (GLfloat));
program.enableAttributeArray("pos"); program.enableAttributeArray("pos");
program.setAttributeBuffer("col", GL_FLOAT, 3 * sizeof (GLfloat), 3, 6 * sizeof (GLfloat)); program.setAttributeBuffer("col", GL_FLOAT, 3 * sizeof (GLfloat), 3, 6 * sizeof (GLfloat));
@ -43,10 +44,11 @@ MeshView::~MeshView() {
void MeshView::paint(QOpenGLShaderProgram &program) { void MeshView::paint(QOpenGLShaderProgram &program) {
QOpenGLContext *ctx = QOpenGLContext::currentContext(); QOpenGLContext *ctx = QOpenGLContext::currentContext();
QOpenGLExtraFunctions *glf = ctx->extraFunctions(); QOpenGLExtraFunctions *glf = ctx->extraFunctions();
void (*glPolygonMode)(GLenum, GLenum) = nullptr;
glPolygonMode = (typeof glPolygonMode) ctx->getProcAddress("glPolygonMode"); QOpenGLFunctions_2_1 *glf21 = nullptr;
if (!glPolygonMode) glf21 = ctx->versionFunctions<QOpenGLFunctions_2_1>();
qWarning("glPolygonMode not available"); if (!glf21)
qFatal("Failed to get OpenGL 2.1 functions");
program.setUniformValue("model", mesh.transform); program.setUniformValue("model", mesh.transform);
@ -55,14 +57,14 @@ void MeshView::paint(QOpenGLShaderProgram &program) {
glf->glEnable(GL_POLYGON_OFFSET_FILL); glf->glEnable(GL_POLYGON_OFFSET_FILL);
glf->glPolygonOffset(1.0, 2); glf->glPolygonOffset(1.0, 2);
glf->glDrawArrays(GL_TRIANGLES, 0, nverts); 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); glf->glDisable(GL_POLYGON_OFFSET_FILL);
/* Wireframe */ /* Wireframe */
program.setUniformValue("wireframe", 1); program.setUniformValue("wireframe", 1);
glf->glDrawArrays(GL_TRIANGLES, 0, nverts); glf->glDrawArrays(GL_TRIANGLES, 0, nverts);
if (glPolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glf21->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glf->glLineWidth(3); glf->glLineWidth(2);
program.setUniformValue("wireframe", 1); program.setUniformValue("wireframe", 0);
} }
} }

View File

@ -20,34 +20,33 @@ QSize MeshViewer::sizeHint() const {
void MeshViewer::initializeGL() { void MeshViewer::initializeGL() {
QOpenGLFunctions *glf = context()->functions(); QOpenGLFunctions *glf = context()->functions();
#ifdef QT_DEBUG #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); QOpenGLDebugLogger *logger = new QOpenGLDebugLogger(this);
if (!logger->initialize()) { if (!logger->initialize()) {
qDebug("OpenGL debug output unavailable"); qDebug("OpenGL debug output unavailable");
} }
connect(logger, &QOpenGLDebugLogger::messageLogged, connect(logger, &QOpenGLDebugLogger::messageLogged,
[](const QOpenGLDebugMessage &message) { [](const QOpenGLDebugMessage &message) {
qDebug() << "OpenGL: " << message.message(); qDebug() << "OpenGL:" << message.message();
}); });
logger->startLogging(); logger->startLogging();
#endif #endif
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex,
QOpenGLShader vertex_shader(QOpenGLShader::Vertex); ":/shader.vert")) {
if (!vertex_shader.compileSourceFile(":/shader.vert")) { qFatal("%s", program.log().toUtf8().constData());
qCritical() << vertex_shader.log();
} }
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment,
QOpenGLShader fragment_shader(QOpenGLShader::Fragment); ":/shader.frag")) {
if (fragment_shader.compileSourceFile(":/shader.frag")) { qFatal("%s", program.log().toUtf8().constData());
qCritical() << fragment_shader.log();
}
if (!program.addShader(&vertex_shader)) {
qCritical() << program.log();
}
if (!program.addShader(&fragment_shader)) {
qCritical() << program.log();
} }
if (!program.link()) { if (!program.link()) {
qCritical() << program.log(); qFatal("%s", program.log().toUtf8().constData());
} }
program.bind(); program.bind();
@ -59,7 +58,7 @@ void MeshViewer::initializeGL() {
glf->glEnable(GL_DEPTH_TEST); glf->glEnable(GL_DEPTH_TEST);
glf->glEnable(GL_MULTISAMPLE); glf->glEnable(GL_MULTISAMPLE);
qDebug("Mesh viewer: initialization complete"); qDebug("MeshViewer: initialization complete");
emit initialized(); emit initialized();
} }
@ -76,6 +75,7 @@ void MeshViewer::paintGL() {
QMatrix4x4 trans; QMatrix4x4 trans;
trans.translate(0, 0, -cam_dist); trans.translate(0, 0, -cam_dist);
QMatrix4x4 view = trans * rot; QMatrix4x4 view = trans * rot;
program.bind();
program.setUniformValue("view", view); program.setUniformValue("view", view);
for (MeshView &m : meshes) { for (MeshView &m : meshes) {
m.paint(program); m.paint(program);

View File

@ -5,9 +5,9 @@ uniform bool wireframe;
uniform float alpha; uniform float alpha;
void main() { void main() {
if (!wireframe) if (wireframe)
// gl_FragColor = vec4(wf_col, alpha); gl_FragColor = vec4(wf_col, alpha);
gl_FragColor = vec4(.5, .5, .5, 1);
else else
gl_FragColor = vec4(frag_col, alpha); // gl_FragColor = vec4(frag_col, alpha);
gl_FragColor = vec4(.5, .5, .5, alpha);
} }