I have a game in pygame. I try to optimize fps and using shader render for uing gpu for the optimization.
I wrote a little test file for seeing how rendering with shader and it work well.
import pygame
import moderngl
from array import array
# Initialiser Pygame et créer une fenêtre
pygame.init()
window = pygame.display.set_mode((800, 600), pygame.OPENGL | pygame.DOUBLEBUF)
display = pygame.Surface(window.get_size())
clock = pygame.time.Clock()
# Charger et redimensionner l'image
img = pygame.transform.scale(pygame.image.load("data/images/background.png").convert(), (400, 300))
img_rect = img.get_rect()
ctx = moderngl.create_context(standalone=False)
quad_buffer = ctx.buffer(data = array('f', [
-1.0, 1.0, 0.0, 0.0, # topleft
1.0, 1.0, 1.0, 0.0, # topright
-1.0, -1.0, 0.0, 1.0, # bottomleft
1.0, -1.0, 1.0, 1.0, # bottomright
]))
vert_shader = '''
#version 330 core
in vec2 vert;
in vec2 texcoord;
out vec2 uvs;
void main() {
uvs = texcoord;
gl_Position = vec4(vert, 0.0, 1.0);
}
'''
frag_shader = '''
#version 330 core
uniform sampler2D texture;
in vec2 uvs;
out vec4 fragColor;
void main() {
fragColor = vec4(texture(texture, uvs).rgb, 1.0);
}
'''
program = ctx.program(vertex_shader=vert_shader, fragment_shader=frag_shader)
render_object = ctx.vertex_array(program, [(quad_buffer, '2f 2f', 'vert', 'texcoord')])
def surface_to_texture(ctx, surface):
texture = ctx.texture(surface.get_size(), 4)
texture.filter = (moderngl.NEAREST, moderngl.NEAREST)
texture.swizzle = 'BGRA'
texture.write(surface.get_view('1'))
return texture
# Boucle principale
running = True
t = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
t += 1
# Obtenir la position de la souris
mouse_x, mouse_y = pygame.mouse.get_pos()
# Mettre à jour la position de l'image
img_rect.center = (mouse_x, mouse_y)
# Dessiner l'image
display.fill((0, 0, 0)) # Effacer l'écran
display.blit(img, img_rect)
frame_texture = surface_to_texture(ctx, display)
frame_texture.use(0)
program['texture'] = 0
render_object.render(mode = moderngl.TRIANGLE_STRIP)
# Mettre à jour l'affichageS
pygame.display.flip()
frame_texture.release()
clock.tick(60)
# Fermer Pygame
pygame.quit()
When i do the same things in my game, whatever i do, when i use ‘mgl.create_context()’ i have “cannot detect OpenGL context” error.
here is the game code
def shader_init() -> tuple[mgl.Context, mgl.VertexArray, mgl.Program]:
ctx = mgl.create_context()
quad_buffer = ctx.buffer(data = array('f', [
-1.0, 1.0, 0.0, 0.0, # topleft
1.0, 1.0, 1.0, 0.0, # topright
-1.0, -1.0, 0.0, 1.0, # bottomleft
1.0, -1.0, 1.0, 1.0, # bottomright
]))
vert_shader = '''
#version 330 core
in vec2 vert;
in vec2 texcoord;
out vec2 uvs;
void main() {
uvs = texcoord;
gl_Position = vec4(vert, 0.0, 1.0);
}
'''
frag_shader = '''
#version 330 core
uniform sampler2D texture;
in vec2 uvs;
out vec4 fragColor;
void main() {
fragColor = vec4(texture(texture, uvs).rgb, 1.0);
}
'''
program = ctx.program(vertex_shader=vert_shader, fragment_shader=frag_shader)
render_object = ctx.vertex_array(program, [(quad_buffer, '2f 2f', 'vert', 'texcoord')])
return ctx, render_object, program
self.ctx, self.render_object, self.program = shader_init()
i try other synthax that look exactly like the original file and whatever id do, its just the
ctx = mgl.context()
line.
i try to modify mgl.context()
line with glversion, mode, and standalone kwargs.
the only thing remove the error is mgl.context(standalone = False)
but nothing display on the screen.
Else, i find another post where a guy had the same issue and he finally find the solution. He said
I found a solution to my problem. it was some problem with environment variables, it was enough to remove one variable that shouldn’t be there and reinstall moderngl. thanks for trying to help.
Someone ask for the environment variables name and he reply
I dont remember full name but it’s like OPENGL_CONTEXT_(propably)CREATE
It was only not windows user variable(all variables I adding for all user)
Obviously i don’t have any environment variables that have a name that could be related, and i’m on windows 11.
I try to uninstall and reinstall, i do the same thing. The very weird thing is that it work well on the other file that is in the same folder.
Triskel26 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.