psychopy.tools.gltools.createMeshGrid

psychopy.tools.gltools.createMeshGrid(size=(1.0, 1.0), subdiv=0, tessMode='diag')[source]

Create a grid mesh.

Procedurally generate a grid mesh by specifying its size and number of sub-divisions. Texture coordinates are computed automatically. The origin is at the center of the mesh. The generated grid is perpendicular to the +Z axis, origin of the grid is at its center.

Parameters:
  • size (tuple or float) – Dimensions of the mesh. If a single value is specified, the plane will be square. Provide a tuple of floats to specify the width and length of the plane (eg. size=(0.2, 1.3)).

  • subdiv (int, optional) – Number of subdivisions. Zero subdivisions are applied by default, and the resulting mesh will only have vertices at the corners.

  • tessMode (str, optional) – Tessellation mode. Specifies how faces are subdivided. Options are ‘center’, ‘radial’, and ‘diag’. Default is ‘diag’. Modes ‘radial’ and ‘center’ work best with an odd number of subdivisions.

Returns:

Vertex attribute arrays (position, texture coordinates, and normals) and triangle indices.

Return type:

tuple

Examples

Create a grid mesh and draw it:

vertices, textureCoords, normals, faces = gltools.createPlane()

vertexVBO = gltools.createVBO(vertices)
texCoordVBO = gltools.createVBO(textureCoords)
normalsVBO = gltools.createVBO(normals)
indexBuffer = gltools.createVBO(
    faces.flatten(),
    target=GL.GL_ELEMENT_ARRAY_BUFFER,
    dataType=GL.GL_UNSIGNED_INT)

vao = gltools.createVAO({0: vertexVBO, 8: texCoordVBO, 2: normalsVBO},
    indexBuffer=indexBuffer)

# in the rendering loop
gltools.drawVAO(vao, GL.GL_TRIANGLES)

Randomly displace vertices off the plane of the grid by setting the Z value per vertex:

vertices, textureCoords, normals, faces =             gltools.createMeshGrid(subdiv=11)

numVerts = vertices.shape[0]
vertices[:, 2] = np.random.uniform(-0.02, 0.02, (numVerts,)))  # Z

# you must recompute surface normals to get correct shading!
normals = gltools.calculateVertexNormals(vertices, faces)

# create a VAO as shown in the previous example here to draw it ...

Back to top