Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize OpenGL context with PyGame instead of GLUT

I'm trying to start with OpenGL, using Python and PyGame.

I'm going to use PyGame instead of GLUT to do all the initializing, windows opening, input handling, etc.

However, my shaders are failing to compile, unless I specify exactly the version of OpenGL and profile.

They do compile with GLUT initialization from the book:

glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(400, 400)

# this is what I need
glutInitContextVersion(3, 3)
glutInitContextProfile(GLUT_CORE_PROFILE)

glutCreateWindow("main")

But, with simple PyGame initialization like this:

pygame.init()
display = (400, 400)
pygame.display.set_mode(display, pygame.DOUBLEBUF|pygame.OPENGL)

which doesn't specify exact OpenGL version 3.3 and CORE_PROFILE, the same program would fail when trying to compile shaders:

RuntimeError: ('Shader compile failure (0): 0:2(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES\n', ['\n #version 330 core\n layout(location = 0) in vec4 position;\n void main()\n {\n gl_Position = position;\n }\n '], GL_VERTEX_SHADER)

My question is: how do I do this initialization with PyGame?

like image 710
lithuak Avatar asked Jul 29 '16 17:07

lithuak


People also ask

Does PyGame use OpenGL?

Pygame is a library built on top of SDL that can do a lot of stuff, pyopengl is a thin wrapper arount opengl that only can do opengl graphics. You can even use pyopengl for graphics in a game that uses pygame for the other parts like sound and input.

Can you use OpenGL with Python?

First off, PyOpenGL is just some Python bindings (some Python code that acts like a sort of wrapper around native code), so you can manipulate OpenGL within the context of Python. OpenGL is a cross-language API, so you can take your knowledge of OpenGL to other languages.

What is PyGame OpenGL?

PyOpenGL is the standardized library used as a bridge between Python and the OpenGL APIs, and PyGame is a standardized library used for making games in Python. It offers built-in handy graphical and audio libraries and we'll be using it to render the result more easily at the end of the article.


1 Answers

The problem is that pygame is initializing an OpenGL context that either isn't modern enough or you are getting a "compatibility" OpenGL profile instead of a "core" OpenGL profile.

This is actually an issue in pygame itself, and I filed a bug about it. The pygame folks are fixing it by adding the appropriate functions to get at the OpenGL attributes. If you need to work around it, you can use ctypes to dig down to the SDL layer and set the OpenGL attributes.

Discussion, interim solutions, and feature add pull here:

https://github.com/pygame/pygame/issues/1124#issuecomment-507021897

https://github.com/pygame/pygame/pull/1127

like image 200
Andrew Lentvorski Avatar answered Sep 23 '22 16:09

Andrew Lentvorski