moderngl | Modern OpenGL binding for python | Graphics library
kandi X-RAY | moderngl Summary
kandi X-RAY | moderngl Summary
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
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of moderngl
moderngl Key Features
moderngl Examples and Code Snippets
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
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.
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]:
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
<
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
raw = self.fbo1.read(components=4, dtype='f4') # RGBA, floats
buf = np.frombuffer(raw, dtype='f4')
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
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
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
Trending Discussions on moderngl
QUESTION
I am wanting to:
- Open the texture from an image via cv2 instead of via ModernGL's
load_texture_2d
method. - 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:54can 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():
QUESTION
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:36You 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):
QUESTION
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__()
orProgram.__iter__()
.
ANSWER
Answered 2020-Aug-30 at 14:52The 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:
QUESTION
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:12In 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:
QUESTION
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:33You 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()
QUESTION
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:29Mali400 GPU which supports only OpenGL version 120
It doesn't support OpenGL at all; it supports OpenGL ES 1.1 and 2.0.
QUESTION
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:50It 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.
QUESTION
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:16It works when using a Renderbuffer rather than a Texture for the multisample depth buffer:
Instead of
QUESTION
I'm trying to run moderngl in Colab. I installed it and ran a virtual display:
...ANSWER
Answered 2020-Mar-30 at 19:16In Google Colab you can use the EGL backend with moderngl 5.6.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install moderngl
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
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page