2021-10-02 20:47:02 +02:00
|
|
|
#include "mesh_view.h"
|
2021-10-02 22:51:20 +02:00
|
|
|
#include <QOpenGLContext>
|
2021-10-03 00:11:29 +02:00
|
|
|
#include <QOpenGLFunctions_2_1>
|
2021-10-02 20:47:02 +02:00
|
|
|
|
|
|
|
|
2021-10-02 22:51:20 +02:00
|
|
|
MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program)
|
|
|
|
: buffer(QOpenGLBuffer::VertexBuffer),
|
2021-10-03 00:11:29 +02:00
|
|
|
nverts(mesh.n_faces() * 3),
|
2021-10-02 20:47:02 +02:00
|
|
|
mesh(mesh) {
|
2021-10-03 00:11:29 +02:00
|
|
|
QVector<GLfloat> verts(nverts * 6);
|
2021-10-02 20:47:02 +02:00
|
|
|
size_t i = 0;
|
|
|
|
for (const FaceHandle face : mesh.faces()) {
|
|
|
|
for (const VertexHandle vec : mesh.fv_range(face)) {
|
|
|
|
verts[6*i + 0] = mesh.point(vec)[0];
|
|
|
|
verts[6*i + 1] = mesh.point(vec)[1];
|
|
|
|
verts[6*i + 2] = mesh.point(vec)[2];
|
|
|
|
verts[6*i + 3] = mesh.color.red();
|
|
|
|
verts[6*i + 4] = mesh.color.green();
|
|
|
|
verts[6*i + 5] = mesh.color.blue();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 22:51:20 +02:00
|
|
|
vao.create();
|
|
|
|
{QOpenGLVertexArrayObject::Binder binder(&vao);
|
|
|
|
buffer.create();
|
|
|
|
buffer.bind();
|
|
|
|
buffer.setUsagePattern(QOpenGLBuffer::StreamDraw);
|
2021-10-03 00:11:29 +02:00
|
|
|
buffer.allocate(verts.constData(), nverts * 6 * sizeof (GLfloat));
|
2021-10-02 22:51:20 +02:00
|
|
|
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));
|
|
|
|
program.enableAttributeArray("col");
|
|
|
|
}
|
2021-10-02 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MeshView::~MeshView() {
|
2021-10-02 22:51:20 +02:00
|
|
|
vao.destroy();
|
|
|
|
buffer.destroy();
|
2021-10-02 20:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-02 22:51:20 +02:00
|
|
|
void MeshView::paint(QOpenGLShaderProgram &program) {
|
|
|
|
QOpenGLContext *ctx = QOpenGLContext::currentContext();
|
|
|
|
QOpenGLExtraFunctions *glf = ctx->extraFunctions();
|
2021-10-03 00:11:29 +02:00
|
|
|
|
|
|
|
QOpenGLFunctions_2_1 *glf21 = nullptr;
|
|
|
|
glf21 = ctx->versionFunctions<QOpenGLFunctions_2_1>();
|
|
|
|
if (!glf21)
|
|
|
|
qFatal("Failed to get OpenGL 2.1 functions");
|
2021-10-02 20:47:02 +02:00
|
|
|
|
2021-10-02 22:51:20 +02:00
|
|
|
program.setUniformValue("model", mesh.transform);
|
2021-10-02 20:47:02 +02:00
|
|
|
|
2021-10-02 22:51:20 +02:00
|
|
|
{QOpenGLVertexArrayObject::Binder binder(&vao);
|
|
|
|
/* Mesh */
|
|
|
|
glf->glEnable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
glf->glPolygonOffset(1.0, 2);
|
|
|
|
glf->glDrawArrays(GL_TRIANGLES, 0, nverts);
|
2021-10-03 00:11:29 +02:00
|
|
|
glf21->glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
2021-10-02 22:51:20 +02:00
|
|
|
glf->glDisable(GL_POLYGON_OFFSET_FILL);
|
2021-10-02 20:47:02 +02:00
|
|
|
|
2021-10-02 22:51:20 +02:00
|
|
|
/* Wireframe */
|
|
|
|
program.setUniformValue("wireframe", 1);
|
|
|
|
glf->glDrawArrays(GL_TRIANGLES, 0, nverts);
|
2021-10-03 00:11:29 +02:00
|
|
|
glf21->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
glf->glLineWidth(2);
|
|
|
|
program.setUniformValue("wireframe", 0);
|
2021-10-02 22:51:20 +02:00
|
|
|
}
|
2021-10-02 20:47:02 +02:00
|
|
|
}
|