-
Notifications
You must be signed in to change notification settings - Fork 18
/
minimal_example_shared_context.py
109 lines (84 loc) · 2.78 KB
/
minimal_example_shared_context.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# pyopengl glGenVertexArrays() causes segfault in threads
# https://stackoverflow.com/questions/74339949/pyopengl-glgenvertexarrays-causes-segfault-in-threads/74340039#74340039
from OpenGL.GL import *
from OpenGL.GLU import *
import OpenGL.GL.shaders
import glfw
import time
import threading
import ctypes
sh_vert = """
#version 460 core
layout (location = 0) in vec4 a_position;
layout (location = 1) in vec2 a_uv;
out vec2 uv;
void main()
{
uv = a_uv;
gl_Position = a_position;
}
"""
sh_frag = """
#version 460 core
out vec4 frag_color;
in vec2 uv;
void main()
{
frag_color = vec4(1.0-uv.x, 1.0-uv.y, uv.x*uv.y, 1.0);
}
"""
if glfw.init() == glfw.FALSE:
exit()
loaded_lock = threading.Lock()
terminate_load_thread_event = threading.Event()
def load_thread_function():
global vbo, ebo, program
loaded_lock.acquire()
glfw.make_context_current(load_window)
attributes = (GLfloat * 20)(*[-1,-1,0,0,1, 1,-1,0,1,1, 1,1,0,1,0, -1,1,0,0,0])
indices = (GLuint * 6)(*[0,1,2, 0,2,3])
default_vao = glGenVertexArrays(1)
vbo = glGenBuffers(1)
ebo = glGenBuffers(1)
glBindVertexArray(default_vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, attributes, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, ebo)
glBufferData(GL_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
program = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(sh_vert, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(sh_frag, GL_FRAGMENT_SHADER)
)
loaded_lock.release()
while not glfw.window_should_close(load_window) and not terminate_load_thread_event.wait(0.01):
glfw.swap_buffers(load_window)
glfw.poll_events()
print("terminate load thread")
glfw.window_hint(glfw.VISIBLE, glfw.FALSE)
load_window = glfw.create_window(400, 400, "OpenGL load Window", None, None)
#glfw.make_context_current(load_window)
glfw.window_hint(glfw.VISIBLE, glfw.TRUE)
window = glfw.create_window(400, 400, "OpenGL Window", None, load_window)
glfw.make_context_current(window)
load_thread = threading.Thread(target=load_thread_function)
load_thread.start()
loaded_lock.acquire()
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glVertexAttribPointer(0, 3, GL_FLOAT, False, 5 * 4, None)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 2, GL_FLOAT, False, 5 * 4, ctypes.c_void_p(3 * 4))
glEnableVertexAttribArray(1)
glUseProgram(program)
while not glfw.window_should_close(window):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
glfw.swap_buffers(window)
glfw.poll_events()
terminate_load_thread_event.set()
load_thread.join()
print("terminate")
glfw.terminate()
exit()