CustomFilterMeshGenerator::CustomFilterMeshGenerator(unsigned columns, unsigned rows, const FloatRect& meshBox, CustomFilterMeshType meshType)
    : m_meshType(meshType)
    , m_points(columns + 1, rows + 1)
    , m_tiles(columns, rows)
    , m_tileSizeInPixels(meshBox.width() / m_tiles.width(), meshBox.height() / m_tiles.height())
    , m_tileSizeInDeviceSpace(1.0f / m_tiles.width(), 1.0f / m_tiles.height())
    , m_meshBox(meshBox)
{
    // Build the two buffers needed to draw triangles:
    // * m_vertices has a number of float attributes that will be passed to the vertex shader
    // for each computed vertex. This number is calculated in floatsPerVertex() based on the meshType.
    // * m_indices is a buffer that will have 3 indices per triangle. Each index will point inside
    // the m_vertices buffer.
    m_vertices.reserveCapacity(verticesCount() * floatsPerVertex());
    m_indices.reserveCapacity(indicesCount());

    // Based on the meshType there can be two types of meshes.
    // * attached: each triangle uses vertices from the neighbor triangles. This is useful to save some GPU memory
    // when there's no need to explode the tiles.
    // * detached: each triangle has its own vertices. This means each triangle can be moved independently and a vec3
    // attribute is passed, so that each vertex can be uniquely identified.
    if (m_meshType == MeshTypeAttached)
        generateAttachedMesh();
    else
        generateDetachedMesh();

#ifndef NDEBUG
    if (s_dumpCustomFilterMeshBuffers)
        dumpBuffers();
#endif
}
Example #2
0
//===================================================================
void Mesh::computeNormal()
{
    // calculates the vector components normal to the plane containing the vertices

    for (int index = 0; index < verticesCount(); index += 3) {
        QVector3D v1 = vertex(index + 1).pos() - vertex(index).pos();
        QVector3D v2 = vertex(index + 2).pos() - vertex(index).pos();

        // Caclulates the normal vector
        vertex(index).setNx((v1.y() * v2.z()) - (v2.y() * v1.z()));
        vertex(index).setNy((v1.x() * v2.z()) - (v2.x() * v1.z()));
        vertex(index).setNz((v1.x() * v2.y()) - (v2.x() * v1.y()));

        vertex(index).setNormal(vertex(index).normal().normalized());
        vertex(index + 1).setNormal(vertex(index).normal().normalized());
        vertex(index + 2).setNormal(vertex(index).normal().normalized());
    }
}