-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to get an offscreen (pbuffer) render wrapper working
This does *not* work correctly unless you have an actual display, so it's somewhat useless/broken. What we want is a way to get a pbuffer context without needing a display, and EGL_DEFAULT_DISPLAY doesn't seem like the correct value.
- Loading branch information
Showing
2 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from __future__ import print_function | ||
import os | ||
if not os.environ.get( 'PYOPENGL_PLATFORM' ): | ||
os.environ['PYOPENGL_PLATFORM'] = 'egl' | ||
import logging, contextlib | ||
from functools import wraps | ||
from OpenGL.GL import * | ||
from OpenGL.EGL import * | ||
from OpenGL import arrays | ||
log = logging.getLogger( __name__ ) | ||
|
||
API_MAP = { | ||
EGL_OPENGL_BIT:EGL_OPENGL_API, | ||
EGL_OPENGL_ES2_BIT:EGL_OPENGL_ES_API, | ||
EGL_OPENGL_ES_BIT:EGL_OPENGL_ES_API, | ||
} | ||
|
||
def write_ppm(buf, filename): | ||
f = open(filename, "w") | ||
if f: | ||
h, w, c = buf.shape | ||
print( "P3", file=f) | ||
print( "# ascii ppm file created by os_egl",file=f) | ||
print( "%i %i" % (w, h),file=f) | ||
print("255",file=f) | ||
for y in range(h - 1, -1, -1): | ||
for x in range(w): | ||
pixel = buf[y, x] | ||
l = " %3d %3d %3d" % (pixel[0], pixel[1], pixel[2]) | ||
f.write(l) | ||
f.write("\n") | ||
|
||
|
||
@contextlib.contextmanager | ||
def egl_context( | ||
width=400, | ||
height=400, | ||
api=EGL_OPENGL_BIT, | ||
attributes = ( | ||
EGL_BLUE_SIZE, 8, | ||
EGL_RED_SIZE,8, | ||
EGL_GREEN_SIZE,8, | ||
EGL_DEPTH_SIZE,24, | ||
EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, | ||
EGL_CONFIG_CAVEAT, EGL_NONE, # Don't allow slow/non-conformant | ||
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, | ||
), | ||
output='output.ppm', | ||
): | ||
"""Setup a context for rendering""" | ||
major,minor = GLint(), GLint() | ||
display = eglGetDisplay(EGL_DEFAULT_DISPLAY) | ||
if display == EGL_NO_DISPLAY: | ||
raise RuntimeError(EGL_NO_DISPLAY, ) | ||
# print("Display: %s"%(display.address,)) | ||
eglInitialize( display, major, minor) | ||
num_configs = GLint() | ||
configs = (EGLConfig*2)() | ||
local_attributes = list(attributes[:]) | ||
local_attributes.extend( [ | ||
# EGL_CONFORMANT, api, | ||
EGL_NONE, # end of list | ||
]) | ||
log.info("Attributes: %s", local_attributes) | ||
local_attributes= arrays.GLintArray.asArray( local_attributes ) | ||
try: | ||
eglChooseConfig(display, local_attributes, configs, 2, num_configs) | ||
surface_attributes = [ | ||
EGL_WIDTH,width, | ||
EGL_HEIGHT,height, | ||
EGL_NONE, | ||
] | ||
surface = eglCreatePbufferSurface( | ||
display, | ||
configs[0], | ||
surface_attributes, | ||
) | ||
except GLError as err: | ||
log.error("Error code: %s on %s", err.err, err.baseOperation) | ||
raise | ||
eglBindAPI(API_MAP[api]) | ||
ctx = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, None) | ||
if ctx == EGL_NO_CONTEXT: | ||
raise RuntimeError( 'Unable to create context' ) | ||
eglMakeCurrent( display, surface, surface, ctx ) | ||
yield ctx,surface | ||
content = glReadPixelsub(0,0, width, height, GL_RGB, outputType=None) | ||
if output: | ||
write_ppm(content, output) | ||
# eglSwapBuffers( display, surface ) | ||
|
||
def main(): | ||
with egl_context() as setup: | ||
ctx,surface = setup | ||
glClearColor(1.0,1.0,1.0,1.0) | ||
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) | ||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(level=logging.INFO) | ||
main() |