Coder Social home page Coder Social logo

awygle / moderngl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from moderngl/moderngl

1.0 3.0 0.0 5.92 MB

Modern OpenGL binding for python

Home Page: https://moderngl.readthedocs.io

License: MIT License

Makefile 0.04% Python 23.09% C++ 76.88%

moderngl's Introduction

ModernGL

docs build build health pypi license platforms

Pythonic OpenGL and GLSL support

Installation

pip install ModernGL

Description

Why ModernGL?

If you prefer GPU accelerated high quality graphics then you should develop your applications using ModernGL. It is much simpler than PyOpenGL and capable of rendering with the same quality and performace.

  • Full linting support - (using vscode and pylint)
  • Create GLSL shaders with only a few lines of code
  • Create framebuffers and validate them with a single call
  • Access cool OpenGL features by writing clean and self-explaining code
  • vscode snippets for fast prototyping
  • Render to pillow image - (no window required)

Why is it simpler?

With the original OpenGL API you have to write a couple of lines to achieve a simple task like compiling a shader or running a computation on the GPU. With ModernGL you will need just a few lines to achieve the same result.

Using PyOpenGL

vbo1 = glGenBuffers(1)
GL.glBindBuffer(GL_ARRAY_BUFFER, vbo1)
GL.glBufferData(GL_ARRAY_BUFFER, b'Hello World!', GL_STATIC_DRAW)

vbo2 = glGenBuffers(1)
GL.glBindBuffer(GL_ARRAY_BUFFER, vbo2)
GL.glBufferData(GL_ARRAY_BUFFER, b'\x00' * 1024, GL_DYNAMIC_DRAW)

Using ModernGL

vbo1 = ctx.buffer(b'Hello World!')
vbo2 = ctx.buffer(reserve=1024, dynamic=True)

Some cool features

# Read the content
>>> vbo1.read()
b'Hello World!'

# Copy between buffers
>>> ctx.copy_buffer(vbo2, vbo1)

>>> vbo2.read(5)
b'Hello'

# Buffer re-specification
>>> vbo2.orphan()
>>> vbo2.write(b'Some other data')

Is ModernGL faster than PyOpenGL?

In some cases yes, the core functions of ModernGL are written in C++, OpenGL functions are called in quick succession so these calls together count as a single python function call.

What version of OpenGL is used?

Most of the calls only require OpenGL 3.3 but Subroutines and Compute Shaders require OpenGL 4.0 and OpenGL 4.3

Is my old PC supported?

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. (No, Compute Shaders won't work)

Render to pillow image

size = (256, 256)
fbo = ctx.framebuffer(ctx.renderbuffer(size))
fbo.use()

# Render scene

Image.frombytes('RGB', size, fbo.read(components=3))

Render to pillow image (multisample)

size = (256, 256)
fbo1 = ctx.framebuffer(ctx.renderbuffer(size, samples=8))
fbo2 = ctx.framebuffer(ctx.renderbuffer(size))
fbo1.use()

# Render scene

ctx.copy_framebuffer(fbo2, fbo1)
Image.frombytes('RGB', size, fbo1.read(components=3))

Render to pillow image (larger than screen)

Just change the size to (4096, 4096)

This will require much more video ram.

Working with uniforms and attributes

# The declaration of the program is omitted.

>>> prog.uniforms
{
    'Matrix': <Uniform: 0>,
    'Lights': <Uniform: 1>,
    'Color': <Uniform: 2>,
}

>>> prog.uniforms['Lights'].value
[(0.0, 0.0, 100.0), [(10.0, 10.0, 90.0)]

>>> prog.attributes
{
    'vertex': <Attribute: 0>,
    'normal': <Attribute: 1>,
    'texcoord': <Attribute: 2>,
}

>>> ctx.detect_format(prog, ['vertex', 'normal'])
'4f3f'

>>> color_uniform = prog.uniforms['Color']
>>> color_uniform.value = (0.0, 1.0, 0.0)

Don't read uniform values too often they force some GPUs to sync.

How can I start using ModernGL?

Take a look at the examples and docs.

Where can I use ModernGL?

Anywhere where OpenGL is supported. ModernGL is capable of rendering using a standalone_context as well. Rendering to a window only requires a valid OpenGL context.

Can ModernGL create a Window?

NO, Window creation is up to you. You can choose from a large variety of modules that can create a window: PyQt5, pyglet, pygame, GLUT and many others.

Limitations using ModernGL over PyOpenGL?

All the neccessary calls are (or can be) implemented in ModernGL. However you can interract with the ModernGL objects from PyOpenGL. If something is missing write an issue or raise a PR.

Supported platforms

  • Windows
  • Linux
  • Mac

Installing from source

Installing on Ubuntu from source

apt-get install python3-dev libgl1-mesa-dev libx11-dev
python3 setup.py install

Building the sphinx documentation

pip install -r docs/requirements.txt
python setup.py build_sphinx

Running tests

pytest

Some of the tests may be skipped when the supported OpenGL version is below the requirements of the given test.

Code quality

Code is tested with pep8, flake8, prospector and pylint

Community

Contributors

and many others

Thank You!

Contributions are welcome. (Please add yourself to the list)

moderngl's People

Contributors

aljenci avatar botonddombi avatar brianbruggeman avatar fekga avatar gitter-badger avatar minchinweb avatar nudomarinero avatar shakram02 avatar simleek avatar stuaxo avatar szabolcsdombi avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.