Window Guide¶
Using Built In Window Types¶
The library provides shortcuts for window creation
in the moderngl_window
module that will
also handle context activation.
The moderngl_window.conf.Settings
instance
has sane default parameters for a window.
See the WINDOW
attribute.
import moderngl_window
from moderngl_window.conf import settings
settings.WINDOW['class'] = 'moderngl_window.context.glfw.Window'
settings.WINDOW['gl_version'] = (4, 1)
# ... etc ...
# Creates the window instance and activates its context
window = moderngl_window.create_window_from_settings()
There are more sane ways to apply different configuration values
through convenient methods in the Settings
class.
Window classes can of course also be instantiated manually if preferred, but this can generated a bit of extra work.
import moderngl_window
window_str = 'moderngl_window.context.pyglet.Window'
window_cls = moderngl_window.get_window_cls(window_str)
window = window_cls(
title="My Window",
gl_version=(4, 1),
size=(1920, 1080),
...,
)
moderngl_window.activate_context(ctx=window.ctx)
You could also simply import the class directory and instantiate it, but that defeats the purpose of trying to be independent of a specific window library.
The rendering loop for build in windows are simple:
while not window.is_closing:
window.clear()
# Render stuff here
window.swap_buffers()
The swap_buffers
method is important as it also pulls new input
events for the next frame.
Old Guide¶
When not using a WindowConfig
instance there are a few simple steps to get started.
Register the moderngl.Context¶
When not using the built in window types you need to at least tell
moderngl_window what your moderngl.Context
is.
import moderngl
import moderngl_window
# Somewhere in your application a standalone or normal context is created
ctx = moderngl.create_standalone_context(require=330)
ctx = moderngl.create_context(require=330)
# Make sure you activate this context
moderngl_window.activate_context(ctx=ctx)
If there are no context activated the library will raise an exception when doing opertations that requires one such as texture and scene loading.
When using the built in window types the context activation is normally done for you on creation.
Register Resource Directories¶
The resource loading system are using relative paths. These paths are relative one or multiple directories we registed in the resource system.
The moderngl_window.resources
module has methods for this.
from pathlib import Path
from moderngl_window import resources
# We recommend using pathlib
resources.register_dir(Path('absolute/path/to/resource/dir').resolve())
# .. but strings also works
resources.register_dir('absolute/path/to/resource/dir')
These needs to be absolute paths or an exception is raised. You can register as many paths as you want. The resource system will simply look for the file in every registered directory in the order they were added until it finds a match.
This library also supports separate search directories for shader programs, textures, scenes and various data files.