moderngl | Modern OpenGL binding for python | Graphics library

 by   moderngl Python Version: 6.0.dev0 License: MIT

kandi X-RAY | moderngl Summary

kandi X-RAY | moderngl Summary

moderngl is a Python library typically used in User Interface, Graphics applications. moderngl has no bugs, it has no vulnerabilities, it has build file available, it has a Permissive License and it has medium support. You can install using 'pip install moderngl' or download it from GitHub, PyPI.

ModernGL is a python wrapper over OpenGL 3.3+ core that simplifies the creation of simple graphics applications like scientific simulations, games or user interfaces. Usually, acquiring in-depth knowledge of OpenGL requires a steep learning curve. In contrast, ModernGL is easy to learn and use, moreover it is capable of rendering with high performance and quality, with less code written. The majority of the moderngl code base is also written in C++ for high performance. NOTE: From moderngl 5.6 context creation is delegated to the glcontext package. This makes us able to expand and improve context creation without releasing new versions of moderngl. It also makes it possible for users to customize their own context creation and the bar for contributing should be lower. New backends can be created using ctypes or C++.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              moderngl has a medium active ecosystem.
              It has 1560 star(s) with 182 fork(s). There are 40 watchers for this library.
              There were 2 major release(s) in the last 6 months.
              There are 26 open issues and 378 have been closed. On average issues are closed in 306 days. There are 1 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of moderngl is 6.0.dev0

            kandi-Quality Quality

              moderngl has 0 bugs and 0 code smells.

            kandi-Security Security

              moderngl has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              moderngl code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              moderngl is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              moderngl releases are available to install and integrate.
              Deployable package is available in PyPI.
              Build file is available. You can build the component from source.
              Installation instructions, examples and code snippets are available.
              It has 11563 lines of code, 957 functions and 225 files.
              It has medium code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of moderngl
            Get all kandi verified functions for this library.

            moderngl Key Features

            No Key Features are available at this moment for moderngl.

            moderngl Examples and Code Snippets

            Numpy array to and from ModernGL buffer (open and save with cv2)
            Pythondot img1Lines of Code : 9dot img1License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            img = cv2.imread("test6.png")
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # optional
            img = np.flip(img, 0).copy(order='C')      # optional
            self.texture = self.ctx.texture(img.shape[1::-1], img.shape[2], img)
            
            raw = s
            Why does texture index 31 is overridden in moderngl?
            Pythondot img2Lines of Code : 20dot img2License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            from OpenGL.GL import *
            
            print(glGetInteger(GL_ACTIVE_TEXTURE) - GL_TEXTURE0)
            
            LEFT_TEXTURE_IDX = 0 
            RIGHT_TEXTURE_IDX = 1 
            
            self.texture_left = self.ctx.texture(left.
            Using a TextureArray in ModernGL
            Pythondot img3Lines of Code : 18dot img3License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            def createTextureArray(imageList, width, height)
            
                depth = len(imageList)
            
                dataList = []
                for filename in imageList:
                    
                    image = Image.open(filename)
                    if width != image.size[0] or height != image.size[1]:
                
            ModernGL set uniform
            Pythondot img4Lines of Code : 75dot img4License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            start_time = time.time()
            while not glfw.window_should_close(window):
                elapsed = time.time() - start_time
            
                # [...]
            
            while not glfw.window_should_close(window):
                # [...]
            
                uniform.value = elapsed
            <
            How do I render multiple objects with different textures in python-moderngl
            Pythondot img5Lines of Code : 84dot img5License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import moderngl
            from array import array
            from PIL import Image
            
            ctx = moderngl.create_context(standalone=True)
            framebuffer_size = (512, 512)
            
            texture1 = ctx.texture((2, 2), 3, array('B', [200, 0, 0] * 4))
            texture2 = ctx.texture((2, 2), 3, a
            How to set primitives to GL_LINES with moderngl in python
            Pythondot img6Lines of Code : 2dot img6License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            self.vao.render(moderngl.LINES)
            
            How do I read a moderngl fbo(frame buffer object) back into a numpy array?
            Pythondot img7Lines of Code : 3dot img7License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            raw = self.fbo1.read(components=4, dtype='f4') # RGBA, floats
            buf = np.frombuffer(raw, dtype='f4')
            
            How to enable Anti-aliasing in Moderngl EGL backend?
            Pythondot img8Lines of Code : 74dot img8License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            fbo = ctx.framebuffer(
                color_attachments=ctx.texture((512, 512), 4, samples=0),
            )
            
            fbo_msaa = ctx.framebuffer(
                color_attachments=ctx.texture((512, 512), 4, samples=8),
            )
            
            gl.glBindFramebuffer(gl.GL_READ_FRAM
            How to add a depth buffer in Moderngl EGL backend?
            Pythondot img9Lines of Code : 10dot img9License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            fbo_msaa = ctx.framebuffer(
                   color_attachments=ctx.texture((512, 512), 4, samples=8),
                   depth_attachment=ctx.depth_texture((512, 512), samples=8)
               )
            
            fbo_msaa = ctx.framebuffer(
                    color_attachment
            Z dimension disappears in moderngl?
            Pythondot img10Lines of Code : 76dot img10License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            cube_ibo_idxs = np.array([
                0, 1, 2,   0, 2, 3,
                3, 2, 6,   3, 6, 7,
                7, 6, 5,   7, 5, 4,
                7, 4, 0,   7, 0, 3,
                4, 5, 1,   4, 1, 0,
                1, 5, 6,   1, 6, 2
            ], dtype=np.int32)
            
            projection = matrix44.cre

            Community Discussions

            QUESTION

            Numpy array to and from ModernGL buffer (open and save with cv2)
            Asked 2020-Dec-02 at 15:03

            I am wanting to:

            1. Open the texture from an image via cv2 instead of via ModernGL's load_texture_2d method.
            2. Save the resulting image (in the write method) via cv2 rather than Pillow.

            I currently have the following code:

            ...

            ANSWER

            Answered 2020-Nov-29 at 03:54

            can you tell, in the write() method, what buf.shape is? I think it's a 1-d array at that point and it probably is height * width * 4 elements long.

            imwrite() needs it shaped right. try this before imwrite():

            Source https://stackoverflow.com/questions/65056007

            QUESTION

            Using a TextureArray in ModernGL
            Asked 2020-Sep-02 at 19:36

            I recently started using ModernGL, and today I would like to start working with texture arrays. I'm just stuck on how to pass the individual subtextures in Moderngl? In OpenGL I would call glTexSubImage3D. However, in the ModernGL documentation, Context.texture_array takes 3 arguments: size, components, and data. I think data is supposed to be all the images stacked? How would I go about this using PIL and possibly numpy?

            ...

            ANSWER

            Answered 2020-Sep-02 at 19:36

            You can read each image separately and append the images to a list. Finally convert the list to an numpy.array.
            In the following snippet imageList is a list of filenames and width and height is the size of an individual image (the images must all be the same size):

            Source https://stackoverflow.com/questions/63711867

            QUESTION

            ModernGL set uniform
            Asked 2020-Aug-30 at 15:08

            I'm considering switching to ModernGL over PyOpenGL, and I'm struggling to implement anything right now.

            First of all, I would like to try the classic "triangle that changes shape using a time uniform and sine function", but I'm stuck on how to write to the uniform.

            Here is what the documentation says about this:

            A uniform is a global GLSL variable declared with the “uniform” storage qualifier. These act as parameters that the user of a shader program can pass to that program.

            In ModernGL, Uniforms can be accessed using Program.__getitem__() or Program.__iter__().

            ...

            ANSWER

            Answered 2020-Aug-30 at 14:52

            The elapsed time is the difference between the start time and the current time. Get the start time before the application loop and compute the elapsed time in the loop in every frame:

            Source https://stackoverflow.com/questions/63658326

            QUESTION

            How do I render multiple objects with different textures in python-moderngl
            Asked 2020-Jul-29 at 12:35

            I am trying to create a headless renderer which takes 2 models with different textures and then renders them both into a single image

            ...

            ANSWER

            Answered 2020-Jul-28 at 13:12

            In the code you create two framebuffers. You mentioned you want a single image, and I assume you may want the models to be shown side by side.

            Here is a correction to your code:

            Full code:

            Source https://stackoverflow.com/questions/63123866

            QUESTION

            How to set primitives to GL_LINES with moderngl in python
            Asked 2020-Jul-25 at 12:34

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

            ...

            ANSWER

            Answered 2020-Jul-25 at 12:33

            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()

            Source https://stackoverflow.com/questions/63086665

            QUESTION

            ModernGL render returns GL_INVALID_ENUM error?
            Asked 2020-Apr-29 at 07:29

            I have written this GPU code in C using GLES and EGL, and it ran fine. I am now trying to port this GPU code to python using ModernGL, except right after I call the render function, ctx returns a GL_INVALID_ENUM error. I am using a NanoPi M1 Plus with Mali400 GPU which supports only OpenGL version 120.

            GPU CODE:

            ...

            ANSWER

            Answered 2020-Apr-29 at 07:29

            Mali400 GPU which supports only OpenGL version 120

            It doesn't support OpenGL at all; it supports OpenGL ES 1.1 and 2.0.

            Source https://stackoverflow.com/questions/61490240

            QUESTION

            How to enable Anti-aliasing in Moderngl EGL backend?
            Asked 2020-Apr-20 at 10:50

            This code renders a colored triangle when there is no anti-aliasing (samples=0). But when I turn on anti-aliasing (samples=1...32) it can't render anything. How to make it work with anti-aliasing? Perhaps I cannot read pixels from multisample fbos or textures directly but I don't know how to fix that.

            ...

            ANSWER

            Answered 2020-Apr-19 at 12:50

            It is not possible to read data from a multisample framebuffer directly. Note, in a multisample framebuffer the pixels are stored for each sample. The color for each sample has to be mixed to a single color. That can be achieved by glBlitFramebuffer.

            Create 2 framebuffers. Create a framebuffer with samples=0, this framebuffer is used to read the pixel data. Create a mutlisample framebuffer, which is the target of the rendering.

            Source https://stackoverflow.com/questions/61302400

            QUESTION

            How to add a depth buffer in Moderngl EGL backend?
            Asked 2020-Apr-19 at 13:16

            This code renders a colored triangle with anti-aliasing (samples=8) when a depth buffer line depth_attachment=ctx.depth_texture((512, 512), samples=8) is commented.

            But when I add a depth buffer it returns a GL error at the binding fbo_msaa framebuffer to GL_READ_FRAMEBUFFER. Do you know how to fix this error?

            ...

            ANSWER

            Answered 2020-Apr-19 at 13:16

            It works when using a Renderbuffer rather than a Texture for the multisample depth buffer:

            Instead of

            Source https://stackoverflow.com/questions/61304886

            QUESTION

            How to run moderngl in Colab?
            Asked 2020-Mar-30 at 19:16

            I'm trying to run moderngl in Colab. I installed it and ran a virtual display:

            ...

            ANSWER

            Answered 2020-Mar-30 at 19:16

            In Google Colab you can use the EGL backend with moderngl 5.6.

            Source https://stackoverflow.com/questions/60900067

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install moderngl

            You can install using 'pip install moderngl' or download it from GitHub, PyPI.
            You can use moderngl like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.

            Support

            OpenGL 3.3 came out in February 2010. With up to date drivers you will be able to use the most of the ModernGL functions, even on integrated graphics cards. (Compute Shaders will likely not work depending on how old your PC is.). You can still try using Mesa but performance would be limited.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            Install
          • PyPI

            pip install moderngl

          • CLONE
          • HTTPS

            https://github.com/moderngl/moderngl.git

          • CLI

            gh repo clone moderngl/moderngl

          • sshUrl

            git@github.com:moderngl/moderngl.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link