Skip to content

Instantly share code, notes, and snippets.

@N3RDIUM
Created February 18, 2022 15:41
Show Gist options
  • Save N3RDIUM/81ebd1d6950009f3ac12b694a6748361 to your computer and use it in GitHub Desktop.
Save N3RDIUM/81ebd1d6950009f3ac12b694a6748361 to your computer and use it in GitHub Desktop.
PyOpenGL and GLFW example: adding stuff to VBOs from threads!
# imports
from OpenGL.GL import *
from OpenGL.GLU import *
import glfw
import threading
# initialise glfw
if glfw.init() == glfw.FALSE:
exit()
event = threading.Event()
def shard_context(window, vbo):
# make a new context
glfw.window_hint(glfw.VISIBLE, glfw.FALSE)
window2 = glfw.create_window(300, 300, "Window 2", None, window)
glfw.make_context_current(window2)
event.set()
# add the triangle to the VBO.
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferSubData(GL_ARRAY_BUFFER, 0, 12*4, (GLfloat * 12)(0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0))
glFlush()
# make the main window
window = glfw.create_window(300, 300, "Window 1", None, None)
glfw.make_context_current(window)
# make the vbo
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, 12 * 4, None, GL_STATIC_DRAW)
# start the triangle thread
glfw.make_context_current(None)
thread = threading.Thread(target=shard_context, args=[window, vbo])
thread.start()
event.wait()
glfw.make_context_current(window)
# vbo stuff
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, None)
# mainloop
while not glfw.window_should_close(window):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glColor3f(1, 0, 0)
# draw the vbo
glDrawArrays (GL_TRIANGLES, 0, 3)
glfw.swap_buffers(window)
glfw.poll_events()
# exit
glfw.terminate()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment