I have recently been learning ModernGL with Python and Pygame. However I cannot work out how to create 2D images (a GUI) ontop of the ModernGL context (do I still use Pygame?)
Before when I tried to render pygame images to the screen, (with the ModernGL context) they did not render.
Code:
import ModenGL
import pygame as pg
ctx = mgl.create_context()
the_display = pg.display.set_mode((1200, 700), pg.OPENGL|pg.DOUBLEBUF)
while True:
the_image = pg.load.image("test.png")
the_display.blit(the_image, (0, 0))
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
self.mesh.destroy()
pg.quit()
sys.exit()
pg.display.flip()
the image is not rendered to the screen
You can't mix drawing or
blitwith Pygame with rendering with OpenGL. You have to use either one or the other. See How can I draw using pygame, while also drawing with pyopengl?.You have to convert a
pygame.Surfaceto an OpenGL texture. Get the pixel of a surface withpygame.Surface.get_bufferand use them to create the texture. With ModernGL this looks as follows:You can also write a class subclassed to
pygame.sprite.Groupand overwrites thedrawmethod. Use themoderngl.VertexArrayandmoderngl.Textureto render the sprites. Something similar could be done with native OpenGL (PyOpenGL), it would just be a little more code.See also PyGame and OpenGL
Minimal example: