#include "mesh_view.h" #include MeshView::MeshView(const MyMesh &mesh, QOpenGLShaderProgram &program) : buffer(QOpenGLBuffer::VertexBuffer), 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"); } } MeshView::~MeshView() { vao.destroy(); buffer.destroy(); } 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"); program.setUniformValue("model", mesh.transform); {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); /* 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); } }