How to set primitives to GL_LINES with moderngl in python

642 Views Asked by At

I am using the following code to render a rectangle on screen, using moderngl and moderngl_window. This is mostly derived from their examples:

import moderngl
import moderngl_window as mglw
import glfw

import numpy as np

import OpenGL.GL
import OpenGL.GLUT
import OpenGL.GLU


class MyWin(mglw.WindowConfig):
    gl_version = (3, 3)
    title = "Hello World"
    window_size = (1280, 720)
    aspect_ratio = 16/9;
    resizable = True
    samples = 4


    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.prog = self.ctx.program(
                vertex_shader='''
                   #version 330
                    in vec2 vert;
                    void main() {
                            gl_Position = vec4(vert.x, vert.y*1.7777, 0.0, 1.0);
                    }
                ''',
                fragment_shader='''
                    #version 330
                    out vec4 color;
                    void main() {
                            color = vec4(0.3, 0.5, 1.0, 1.0);
                    }
                ''',
            )

        self.vertices = np.array([
            -0.8, 0.8,
            -0.8, -0.8,
            0.8, -0.8,
            
            0.8, 0.8,
            -0.8, 0.8,
            0.8,-0.8
        ])



        self.vbo = self.ctx.buffer(self.vertices.astype('f4').tobytes())
        # self.texCoordBuffer = self.ctx.buffer(texCoord.astype('f4').tobytes());
        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'vert');
        # self.vao.bind(1,'f',self.texCoordBuffer,'2f');
        # self.time = self.prog['time'];

        
      
    @classmethod
    def setVertices(self, vertices):
        print("vertices have been set")
        self.vertices = vertices
        

    @classmethod
    def run(cls):       
        mglw.run_window_config(cls);

    def render(self, time, frame_time):         
        # self.time.value = time
        self.ctx.clear(0.0, 0.0, 0.0)
        self.vao.render()

but I can't figure out what function I need to call with moderngl to set the rendering mode to GL_LINES instead of GL_TRIANGLES

I think I need to call glDrawArraysbut I can't find how to access it with moderngl.

Thanks for your help!

1

There are 1 best solutions below

0
On BEST ANSWER

You can choose the Primitive type by setting the mode argument when you invoke moderngl.VertexArray.render(). The default argument is TRIANGLES. Set the mode LINES for the primitive type GL_LINES:

self.vao.render()

self.vao.render(moderngl.LINES)