2021-10-02 20:47:02 +02:00
|
|
|
#include "mesh_view.h"
|
2021-10-02 22:51:20 +02:00
|
|
|
#include <QOpenGLContext>
|
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-02 20:47:02 +02:00
|
|
|
mesh(mesh) {
|
|
|
|
GLfloat *verts = new GLfloat[mesh.n_faces() * 3 * 6];
|
|
|
|
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);
|
|
|
|
buffer.allocate(verts, nverts * 6 * sizeof (GLfloat));
|
|
|
|
delete[] verts;
|
|
|
|
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-02 20:47:02 +02:00
|
|
|
void (*glPolygonMode)(GLenum, GLenum) = nullptr;
|
|
|
|
glPolygonMode = (typeof glPolygonMode) ctx->getProcAddress("glPolygonMode");
|
|
|
|
if (!glPolygonMode)
|
|
|
|
qWarning("glPolygonMode not available");
|
|
|
|
|
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);
|
|
|
|
if (glPolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
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);
|
|
|
|
if (glPolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
glf->glLineWidth(3);
|
|
|
|
program.setUniformValue("wireframe", 1);
|
|
|
|
}
|
2021-10-02 20:47:02 +02:00
|
|
|
}
|