psychopy.tools.gltools.createUVSphere

psychopy.tools.gltools.createUVSphere(radius=0.5, sectors=16, stacks=16, flipFaces=False)[source]

Create a UV sphere.

Procedurally generate a UV sphere by specifying its radius, and number of stacks and sectors. The poles of the resulting sphere will be aligned with the Z-axis.

Surface normals and texture coordinates are automatically generated. The returned normals are computed to produce smooth shading.

Parameters:
  • radius (float, optional) – Radius of the sphere in scene units (usually meters). Default is 0.5.

  • sectors (int, optional) – Number of longitudinal and latitudinal sub-divisions. Default is 16 for both.

  • stacks (int, optional) – Number of longitudinal and latitudinal sub-divisions. Default is 16 for both.

  • flipFaces (bool, optional) – If True, normals and face windings will be set to point inward towards the center of the sphere. Texture coordinates will remain the same. Default is False.

Returns:

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

Return type:

tuple

Examples

Create a UV sphere and VAO to render it:

vertices, textureCoords, normals, faces =             gltools.createUVSphere(sectors=32, stacks=32)

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)

The color of the sphere can be changed by calling glColor*:

glColor4f(1.0, 0.0, 0.0, 1.0)  # red
gltools.drawVAO(vao, GL.GL_TRIANGLES)

Raw coordinates can be transformed prior to uploading to VBOs. Here we can rotate vertex positions and normals so the equator rests on Z-axis:

r = mt.rotationMatrix(90.0, (1.0, 0, 0.0))  # 90 degrees about +X axis
vertices = mt.applyMatrix(r, vertices)
normals = mt.applyMatrix(r, normals)

Back to top