Skip to content

Quad Mesh Class

QuadMesh

Bases: BaseMesh

A mesh representing a quad in 3D space.

Parameters:

Name Type Description Default
opengl_context Context

The OpenGL context.

required
program Program

The shader program used to render the mesh.

required
Source code in pynecraft/mesh/quad.py
class QuadMesh(BaseMesh):
    """A mesh representing a quad in 3D space.

    Args:
        opengl_context (moderngl.Context): The OpenGL context.
        program (moderngl.Program): The shader program used to render the mesh.
    """

    def __init__(
        self, opengl_context: moderngl.Context, program: moderngl.Program
    ) -> None:
        super().__init__(opengl_context, program)

        # This means that there are 2 sets of attributes consisting of 3 sets of
        # floats per vertex.
        self.vbo_format = "3f 3f"

        # The attributes to be passed to the vertex shader
        self.attributes = ["in_position", "in_color"]

        self.vertex_array_object = self.get_vertex_array_object()

    def get_vertex_data(self) -> np.array:
        """Returns the vertex data for the mesh.

        Returns:
            np.array: The vertex data.
        """
        vertices = [
            (0.5, 0.5, 0.0),
            (-0.5, 0.5, 0.0),
            (-0.5, -0.5, 0.0),
            (0.5, 0.5, 0.0),
            (-0.5, -0.5, 0.0),
            (0.5, -0.5, 0.0),
        ]
        colors = [(0, 1, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (1, 1, 0), (0, 0, 1)]
        return np.hstack([vertices, colors], dtype="float32")

get_vertex_data()

Returns the vertex data for the mesh.

Returns:

Type Description
array

np.array: The vertex data.

Source code in pynecraft/mesh/quad.py
def get_vertex_data(self) -> np.array:
    """Returns the vertex data for the mesh.

    Returns:
        np.array: The vertex data.
    """
    vertices = [
        (0.5, 0.5, 0.0),
        (-0.5, 0.5, 0.0),
        (-0.5, -0.5, 0.0),
        (0.5, 0.5, 0.0),
        (-0.5, -0.5, 0.0),
        (0.5, -0.5, 0.0),
    ]
    colors = [(0, 1, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (1, 1, 0), (0, 0, 1)]
    return np.hstack([vertices, colors], dtype="float32")