The problem
I'm just trying to make a game like minecraft, but I just can't add to a vbo from another process. The strange thing is that the logs appear two times and the window just closes instantly.
The code
import pygame, multiprocessing
from OpenGL.GL import *
from ctypes import *
pygame.init ()
screen = pygame.display.set_mode ((800,600), pygame.OPENGL|pygame.DOUBLEBUF, 24)
glViewport (0, 0, 800, 600)
glClearColor (0.0, 0.5, 0.5, 1.0)
glEnableClientState (GL_VERTEX_ARRAY)
vbo = glGenBuffers (1)
glBindBuffer (GL_ARRAY_BUFFER, vbo)
def triangle():
vertices = [ 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0 ]
glBufferData (GL_ARRAY_BUFFER, len(vertices)*4, (c_float*len(vertices))(*vertices), GL_STATIC_DRAW)
if __name__ == '__main__':
multiprocessing.Process(target=triangle).start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
glClear (GL_COLOR_BUFFER_BIT)
glBindBuffer (GL_ARRAY_BUFFER, vbo)
glVertexPointer (3, GL_FLOAT, 0, None)
glDrawArrays (GL_TRIANGLES, 0, 3)
pygame.display.flip ()
Logs
pygame 2.1.0 (SDL 2.0.16, Python 3.8.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Unable to load numpy_formathandler accelerator from OpenGL_accelerate
pygame 2.1.0 (SDL 2.0.16, Python 3.8.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Unable to load numpy_formathandler accelerator from OpenGL_accelerate
Expected output
UPDATE
I changed the code to:
import pygame, threading
from pyglet.gl import *
from OpenGL.GL import *
from OpenGL.WGL import *
from ctypes import *
pygame.init ()
screen = pygame.display.set_mode ((800,600), pygame.OPENGL|pygame.DOUBLEBUF, 24)
glViewport (0, 0, 800, 600)
glClearColor (0.0, 0.5, 0.5, 1.0)
glEnableClientState (GL_VERTEX_ARRAY)
vbo = glGenBuffers (1)
glBindBuffer (GL_ARRAY_BUFFER, vbo)
hwnd = pygame.display.get_wm_info()['window']
context = wglGetCurrentContext()
def triangle(window_handle, context):
context2 = wglCreateContext(window_handle)
wglMakeCurrent(window_handle, context2)
vbo2 = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo2)
glBufferData(GL_ARRAY_BUFFER, 12, (GLfloat * 12)(0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0), GL_STATIC_DRAW)
glVertexPointer(3, GL_FLOAT, 0, None)
glFlush()
wglMakeCurrent(None, None)
wglDeleteContext(context2)
if __name__ == '__main__':
threading.Thread(target=triangle, args=[hwnd, context]).start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
glClear (GL_COLOR_BUFFER_BIT)
glBindBuffer (GL_ARRAY_BUFFER, vbo)
glVertexPointer (3, GL_FLOAT, 0, None)
glDrawArrays (GL_TRIANGLES, 0, 3)
pygame.display.flip ()
But now it just opens the window, and closes it again! The window just closes abruptly. It gives the errors mentioned below. But if I comment line 28 and simply write triangle(hwnd, context)
, everything works just fine.
Errors:
Traceback (most recent call last):
File "C:\Users\...\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\...\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "f:/StackOverflow - Code/vbo_plus_thread.py", line 27, in triangle
vbo2 = glGenBuffers(1)
File "src/latebind.pyx", line 39, in OpenGL_accelerate.latebind.LateBind.__call__
File "src/wrapper.pyx", line 318, in OpenGL_accelerate.wrapper.Wrapper.__call__
File "src/wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__
File "src/errorchecker.pyx", line 58, in OpenGL_accelerate.errorchecker._ErrorChecker.glCheckError
OpenGL.error.GLError: GLError(
err = 1282,
description = b'invalid operation',
baseOperation = glGenBuffers,
pyArgs = (
1,
<object object at 0x000001E23B9B8100>,
),
cArgs = (1, array([0], dtype=uint32)),
cArguments = (1, array([0], dtype=uint32))
)