use per-mesh color instead of per-vertex

This commit is contained in:
ccolin 2021-10-03 11:48:28 +02:00
parent b3d4899546
commit 89342b1c5e
4 changed files with 10 additions and 20 deletions

View File

@ -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<GLfloat> vertices(n_vertices * 6);
QVector<GLfloat> 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<GLuint> 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 */

View File

@ -19,7 +19,7 @@ struct MyTraits : public OpenMesh::DefaultTraits {
class MyMesh : public OpenMesh::TriMesh_ArrayKernelT<MyTraits> {
public:
QMatrix4x4 transform;
QColor color;
QColor color = {127, 127, 127};
};
typedef MyMesh::FaceHandle FaceHandle;

View File

@ -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);
}

View File

@ -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;
}