From 89342b1c5e5bb3a984f0c0e08390d5133762c95d Mon Sep 17 00:00:00 2001 From: ccolin Date: Sun, 3 Oct 2021 11:48:28 +0200 Subject: [PATCH] use per-mesh color instead of per-vertex --- src/mesh_view.cpp | 18 +++++++----------- src/my_mesh.h | 2 +- src/shader.frag | 6 ++---- src/shader.vert | 4 ---- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/mesh_view.cpp b/src/mesh_view.cpp index eef51d2..a656b35 100644 --- a/src/mesh_view.cpp +++ b/src/mesh_view.cpp @@ -11,15 +11,12 @@ MeshView::MeshView(const MeshProcessor &mesh_processor, QOpenGLShaderProgram &pr n_vertices(mesh_processor.mesh.n_vertices()), mesh_processor(mesh_processor) { const MyMesh &mesh = mesh_processor.mesh; - QVector vertices(n_vertices * 6); + QVector vertices(n_vertices * 3); size_t i = 0; for (const VertexHandle it : mesh.vertices()) { - vertices[6*i + 0] = mesh.point(it)[0]; - vertices[6*i + 1] = mesh.point(it)[1]; - vertices[6*i + 2] = mesh.point(it)[2]; - vertices[6*i + 3] = mesh.color.red(); - vertices[6*i + 4] = mesh.color.green(); - vertices[6*i + 5] = mesh.color.blue(); + vertices[3*i + 0] = mesh.point(it)[0]; + vertices[3*i + 1] = mesh.point(it)[1]; + vertices[3*i + 2] = mesh.point(it)[2]; i++; } QVector indices(n_faces * 3); @@ -39,15 +36,13 @@ MeshView::MeshView(const MeshProcessor &mesh_processor, QOpenGLShaderProgram &pr vertex_buffer.create(); vertex_buffer.bind(); vertex_buffer.setUsagePattern(QOpenGLBuffer::StreamDraw); - vertex_buffer.allocate(vertices.constData(), n_vertices * 6 * sizeof (GLfloat)); + vertex_buffer.allocate(vertices.constData(), n_vertices * 3 * sizeof (GLfloat)); index_buffer.create(); index_buffer.bind(); index_buffer.setUsagePattern(QOpenGLBuffer::StreamDraw); index_buffer.allocate(indices.constData(), n_faces * 3 * sizeof (GLuint)); - program.setAttributeBuffer("pos", GL_FLOAT, 0, 3, 6 * sizeof (GLfloat)); + program.setAttributeBuffer("pos", GL_FLOAT, 0, 3); program.enableAttributeArray("pos"); - program.setAttributeBuffer("col", GL_FLOAT, 3 * sizeof (GLfloat), 3, 6 * sizeof (GLfloat)); - program.enableAttributeArray("col"); } } @@ -70,6 +65,7 @@ void MeshView::paint(QOpenGLShaderProgram &program) { qFatal("Failed to get OpenGL 2.1 functions"); program.setUniformValue("model", mesh.transform); + program.setUniformValue("col", mesh.color.redF(), mesh.color.greenF(), mesh.color.blueF()); {QOpenGLVertexArrayObject::Binder binder(&vao); /* Mesh */ diff --git a/src/my_mesh.h b/src/my_mesh.h index c6931d2..a8839fb 100644 --- a/src/my_mesh.h +++ b/src/my_mesh.h @@ -19,7 +19,7 @@ struct MyTraits : public OpenMesh::DefaultTraits { class MyMesh : public OpenMesh::TriMesh_ArrayKernelT { public: QMatrix4x4 transform; - QColor color; + QColor color = {127, 127, 127}; }; typedef MyMesh::FaceHandle FaceHandle; diff --git a/src/shader.frag b/src/shader.frag index 35ef0e1..0ccd914 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -1,5 +1,4 @@ -varying vec3 frag_col; - +uniform vec3 col; uniform vec3 wf_col; uniform bool wireframe; uniform float alpha; @@ -8,6 +7,5 @@ void main() { if (wireframe) gl_FragColor = vec4(wf_col, alpha); else - // gl_FragColor = vec4(frag_col, alpha); - gl_FragColor = vec4(.5, .5, .5, alpha); + gl_FragColor = vec4(col, alpha); } diff --git a/src/shader.vert b/src/shader.vert index e1a20e8..f9c57af 100644 --- a/src/shader.vert +++ b/src/shader.vert @@ -1,7 +1,4 @@ attribute vec3 pos; -attribute vec3 col; - -varying vec3 frag_col; uniform mat4 proj; uniform mat4 view; @@ -9,5 +6,4 @@ uniform mat4 model; void main() { gl_Position = proj * view * model * vec4(pos, 1.0); - frag_col = col; }