#include "mesh_view.h" #include MeshView::MeshView(QOpenGLExtraFunctions *glf, const MyMesh &mesh, int pos_attr, int col_attr) : glf(glf), 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++; } } delete[] verts; GLuint vao, vbo; glf->glGenVertexArrays(1, &vao); glf->glGenBuffers(1, &vbo); glf->glBindVertexArray(vao); glf->glBindBuffer(GL_ARRAY_BUFFER, vbo); glf->glBufferData(GL_ARRAY_BUFFER, nverts * 6 * sizeof (GLfloat), verts, GL_DYNAMIC_DRAW); glf->glVertexAttribPointer(pos_attr, 3, GL_FLOAT, GL_FALSE, 6 * sizeof (GLfloat), 0); glf->glEnableVertexAttribArray(pos_attr); glf->glVertexAttribPointer(col_attr, 3, GL_FLOAT, GL_FALSE, 6 * sizeof (GLfloat), (const void *) (3 * sizeof (GLfloat))); glf->glEnableVertexAttribArray(col_attr); glf->glBindVertexArray(0); } MeshView::~MeshView() { glf->glDeleteVertexArrays(1, &vao); glf->glDeleteBuffers(1, &vbo); } void MeshView::paint(QOpenGLContext *ctx, int model_attr, int wireframe_attr) const { void (*glPolygonMode)(GLenum, GLenum) = nullptr; glPolygonMode = (typeof glPolygonMode) ctx->getProcAddress("glPolygonMode"); if (!glPolygonMode) qWarning("glPolygonMode not available"); glf->glUniformMatrix4fv(model_attr, 1, GL_FALSE, mesh.transform.constData()); /* Mesh */ glf->glBindVertexArray(vao); 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 */ glf->glUniform1f(wireframe_attr, 1); glf->glDrawArrays(GL_TRIANGLES, 0, nverts); if (glPolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glf->glLineWidth(3); glf->glUniform1f(wireframe_attr, 0); }