mod_geo-tp/src/mesh_view.cpp

69 lines
2.0 KiB
C++
Raw Normal View History

2021-10-02 20:47:02 +02:00
#include "mesh_view.h"
#include <QOpenGLContext>
2021-10-02 20:47:02 +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++;
}
}
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() {
vao.destroy();
buffer.destroy();
2021-10-02 20:47:02 +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");
program.setUniformValue("model", mesh.transform);
2021-10-02 20:47:02 +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
/* 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
}