On monday i wrote a python script with modernGL that runs a fragment and a vertex shader everything worked fine and I had like 40 FPS. Two days later, I tried to rerun that same script and nothing appeared. All my old scripts that worked too were not functioning this time. So I tried to isolate the problem, and it seems that there is something to do with the context rendering of modernGL; I will explain.
I wrote this code with just the faulty lines. It creates the context and everything then render it but don't display it. Then in the main loop I just want to display a screen with a background color and a Hello World text.
`import moderngl as mgl
import numpy as np
import pygame
init = pygame.init()
print(init)
screen = pygame.display.set_mode([1920, 1080], pygame.RESIZABLE)
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 32)
font2 = pygame.font.SysFont("Arial", 64)
SIZE = WIDTH, HEIGHT = int(500), int(500)
COLOR_MODE = "RGBA"
ctx = mgl.create_context()
fbo = ctx.simple_framebuffer(SIZE, components=len(COLOR_MODE))
fbo = ctx.simple_framebuffer(SIZE, components=len(COLOR_MODE))
fbo.use()
vbo = ctx.buffer(np.array([
# x y z u v
1, 1, 0.0, 1., 1.,
1, -1, 0.0, 1., -1.,
-1, -1, 0.0, -1., -1.,
-1, 1, 0.0, -1., 1.,
], dtype=np.float32))
ibo = ctx.buffer(np.array([
0, 1, 3,
1, 2, 3,
], dtype=np.int32))
with open('shaders/Fragment.glsl', 'r') as file:
FS = file.read()
with open('shaders/Vertex.glsl', 'r') as file:
VS = file.read()
prog = ctx.program(vertex_shader=VS, fragment_shader=FS)
vao = ctx.vertex_array(prog, [(vbo, "3f 2f", "in_vert", "in_uv")], ibo)
vao.render(mode=mgl.TRIANGLES)
run = True
while run:
screen.fill(255)
text = font2.render("Hello World", 1, pygame.Color("orange"))
screen.blit(text, (10,20))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()`
The is 3 states with this code. If I just run this, I have a black pygame window: Black window when executing
If I comment the line 21: fbo.use(), then the shader that should be a square image of 500*500 is displayed as the background (but I never asked pygame to show it).: Distorded shader
Then, last case I also comment the line 42:
vao.render(mode=mgl.TRIANGLES)
and then the pygame window looks like what I want. Normal pygame window
For every situation, main loop is well running and the pygame events are working as expected.
I tried to uninstall and reinstall moderngl, pygame and glcontext (even with different older versions) but every time the script don't work.
Also I am sure I didn't modify my scripts, I copied some working backups and they don't work also.
If you have any hints I would be so nice. Thank you!