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 <QOpenGLContext>
#include <QOpenGLFunctions_2_1>
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<GLfloat> 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<QOpenGLFunctions_2_1>();
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);
}
}

View File

@ -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);

View File

@ -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);
}