How to use the moderngl.TRIANGLES function in moderngl

To help you get started, we’ve selected a few moderngl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Contraz / demosys-py / demosys / geometry / plane.py View on Github external
for x in range(rx - 1):
                # quad poly left
                yield z * rz + x + 1
                yield z * rz + x
                yield z * rz + x + rx
                # quad poly right
                yield z * rz + x + 1
                yield z * rz + x + rx
                yield z * rz + x + rx + 1

    pos_data = numpy.fromiter(gen_pos(), dtype=numpy.float32)
    uv_data = numpy.fromiter(gen_uv(), dtype=numpy.float32)
    normal_data = numpy.fromiter(gen_normal(), dtype=numpy.float32)
    index_data = numpy.fromiter(gen_index(), dtype=numpy.uint32)

    vao = VAO("plane_xz", mode=moderngl.TRIANGLES)

    vao.buffer(pos_data, '3f', ['in_position'])
    vao.buffer(uv_data, '2f', ['in_uv'])
    vao.buffer(normal_data, '3f', ['in_normal'])

    vao.index_buffer(index_data, index_element_size=4)

    return vao
github Contraz / demosys-py / demosys / loaders / scene / gltf.py View on Github external
'POSITION': 'in_position',
            'NORMAL': 'in_normal',
            'TEXCOORD_0': 'in_uv',
            'TANGENT': 'in_tangent',
            'JOINTS_0': 'in_joints',
            'WEIGHTS_0': 'in_heights',
            'COLOR_0': 'in_color0',
        }

        meshes = []

        # Read all primitives as separate meshes for now
        # According to the spec they can have different materials and vertex format
        for primitive in self.primitives:

            vao = VAO(self.name, mode=primitive.mode or moderngl.TRIANGLES)

            # Index buffer
            component_type, index_vbo = self.load_indices(primitive)
            if index_vbo is not None:
                vao.index_buffer(context.ctx().buffer(index_vbo.tobytes()),
                                 index_element_size=component_type.size)

            attributes = {}
            vbos = self.prepare_attrib_mapping(primitive)

            for vbo_info in vbos:
                dtype, buffer = vbo_info.create()
                vao.buffer(
                    buffer,
                    " ".join(["{}{}".format(attr[1], DTYPE_BUFFER_TYPE[dtype]) for attr in vbo_info.attributes]),
                    [name_map[attr[0]] for attr in vbo_info.attributes],
github moderngl / moderngl-window / examples / uniform_block.py View on Github external
def render(self, time=0.0, frametime=0.0, target: moderngl.Framebuffer = None):
        self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST)

        rotation = Matrix44.from_eulers((time, time, time), dtype='f4')
        translation = Matrix44.from_translation((0.0, 0.0, -5.0), dtype='f4')
        modelview = translation * rotation

        self.view_buffer.write(modelview)

        with self.scope1:
            self.vao1.render(mode=moderngl.TRIANGLES)

        with self.scope2:
            self.vao2.render(mode=moderngl.TRIANGLES)
github moderngl / moderngl-window / examples / uniform_block.py View on Github external
def render(self, time=0.0, frametime=0.0, target: moderngl.Framebuffer = None):
        self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST)

        rotation = Matrix44.from_eulers((time, time, time), dtype='f4')
        translation = Matrix44.from_translation((0.0, 0.0, -5.0), dtype='f4')
        modelview = translation * rotation

        self.view_buffer.write(modelview)

        with self.scope1:
            self.vao1.render(mode=moderngl.TRIANGLES)

        with self.scope2:
            self.vao2.render(mode=moderngl.TRIANGLES)
github moderngl / moderngl-window / moderngl_window / geometry / sphere.py View on Github external
n += 3

    indices = [0] * rings * sectors * 6
    i = 0
    for r in range(rings - 1):
        for s in range(sectors - 1):
            indices[i] = r * sectors + s
            indices[i + 1] = (r + 1) * sectors + (s + 1)
            indices[i + 2] = r * sectors + (s + 1)

            indices[i + 3] = r * sectors + s
            indices[i + 4] = (r + 1) * sectors + s
            indices[i + 5] = (r + 1) * sectors + (s + 1)
            i += 6

    vao = VAO(name or "sphere", mode=mlg.TRIANGLES)

    vbo_vertices = numpy.array(vertices, dtype=numpy.float32)
    vao.buffer(vbo_vertices, '3f', [attr_names.POSITION])

    if normals:
        vbo_normals = numpy.array(normals, dtype=numpy.float32)
        vao.buffer(vbo_normals, '3f', [attr_names.NORMAL])

    if uvs:
        vbo_uvs = numpy.array(uvs, dtype=numpy.float32)
        vao.buffer(vbo_uvs, '2f', [attr_names.TEXCOORD_0])

    vbo_elements = numpy.array(indices, dtype=numpy.uint32)
    vao.index_buffer(vbo_elements, index_element_size=4)

    return vao
github moderngl / moderngl-window / moderngl_window / loaders / scene / gltf2.py View on Github external
'POSITION': self.meta.attr_names.POSITION,
            'NORMAL': self.meta.attr_names.NORMAL,
            'TEXCOORD_0': self.meta.attr_names.TEXCOORD_0,
            'TANGENT': self.meta.attr_names.TANGENT,
            'JOINTS_0': self.meta.attr_names.JOINTS_0,
            'WEIGHTS_0': self.meta.attr_names.WEIGHTS_0,
            'COLOR_0': self.meta.attr_names.COLOR_0,
        }

        meshes = []

        # Read all primitives as separate meshes for now
        # According to the spec they can have different materials and vertex format
        for primitive in self.primitives:

            vao = VAO(self.name, mode=primitive.mode or moderngl.TRIANGLES)

            # Index buffer
            component_type, index_vbo = self.load_indices(primitive)
            if index_vbo is not None:
                vao.index_buffer(
                    moderngl_window.ctx().buffer(index_vbo.tobytes()),
                    index_element_size=component_type.size,
                )

            attributes = {}
            vbos = self.prepare_attrib_mapping(primitive)

            for vbo_info in vbos:
                dtype, buffer = vbo_info.create()
                vao.buffer(
                    buffer,
github Contraz / demosys-py / demosys / geometry / quad.py View on Github external
0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
        0.0, 0.0, 1.0,
    ], dtype=numpy.float32)

    uvs = numpy.array([
        0.0, 1.0,
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 0.0,
        1.0, 1.0,
    ], dtype=numpy.float32)

    vao = VAO("geometry:quad", mode=moderngl.TRIANGLES)
    vao.buffer(pos, '3f', ["in_position"])
    vao.buffer(normals, '3f', ["in_normal"])
    vao.buffer(uvs, '2f', ["in_uv"])

    return vao
github Contraz / demosys-py / demosys / effects / text / effects / renderer_2d.py View on Github external
def _create_vao(self):
        data = numpy.array([
            0.0, -2.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 1.0,
            2.0, 0.0, 0.0, 1.0, 1.0,
            0.0, -2.0, 0.0, 0.0, 0.0,
            2.0, 0.0, 0.0, 1.0, 1.0,
            2.0, -2.0, 0.0, 1.0, 0.0,
        ], dtype=numpy.float32)

        vao = VAO("textrenderer", mode=moderngl.TRIANGLES)
        vao.buffer(data, '3f 2f', ['in_position', 'in_uv'])
        return vao
github moderngl / moderngl-window / moderngl_window / opengl / vao.py View on Github external
from typing import List

import numpy
import moderngl
import moderngl_window as mglw
from moderngl_window.opengl import types


# For sanity checking draw modes when creating the VAO
DRAW_MODES = {
    moderngl.TRIANGLES: 'TRIANGLES',
    moderngl.TRIANGLE_FAN: 'TRIANGLE_FAN',
    moderngl.TRIANGLE_STRIP: 'TRIANGLE_STRIP',
    moderngl.TRIANGLES_ADJACENCY: 'TRIANGLES_ADJACENCY',
    moderngl.TRIANGLE_STRIP_ADJACENCY: 'TRIANGLE_STRIP_ADJACENCY',
    moderngl.POINTS: 'POINTS',
    moderngl.LINES: 'LINES',
    moderngl.LINE_STRIP: 'LINE_STRIP',
    moderngl.LINE_LOOP: 'LINE_LOOP',
    moderngl.LINES_ADJACENCY: 'LINES_ADJACENCY',
}


class BufferInfo:
    """Container for a vbo with additional information"""
    def __init__(self, buffer: moderngl.Buffer, buffer_format: str, attributes=None, per_instance=False):
        """
github moderngl / moderngl-window / moderngl_window / loaders / scene / stl.py View on Github external
Scene: The Scene instance
        """
        path = self.find_scene(self.meta.path)
        if not path:
            raise ImproperlyConfigured("Scene '{}' not found".format(self.meta.path))

        file_obj = str(path)
        if file_obj.endswith('.gz'):
            file_obj = gzip.GzipFile(file_obj)

        stl_mesh = trimesh.load(file_obj, file_type='stl')
        scene = Scene(self.meta.resolved_path)
        scene_mesh = Mesh("mesh")
        scene_mesh.material = Material("default")

        vao = VAO("mesh", mode=moderngl.TRIANGLES)
        vao.buffer(numpy.array(stl_mesh.vertices, dtype='f4'), '3f', ['in_position'])
        vao.buffer(numpy.array(stl_mesh.vertex_normals, dtype='f4'), '3f', ['in_normal'])
        vao.index_buffer(numpy.array(stl_mesh.faces, dtype='u4'))
        scene_mesh.vao = vao
        scene_mesh.add_attribute('POSITION', 'in_position', 3)
        scene_mesh.add_attribute('NORMAL', 'in_normal', 3)

        scene.meshes.append(scene_mesh)
        scene.root_nodes.append(Node(mesh=scene_mesh))
        scene.prepare()

        return scene