-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGLServerExample.py
More file actions
80 lines (57 loc) · 1.89 KB
/
OpenGLServerExample.py
File metadata and controls
80 lines (57 loc) · 1.89 KB
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
from itertools import islice, cycle
import glfw
from OpenGL.GL import *
from OpenGL.GLUT import *
import syphon
def render(texture: GLuint, data: bytes, width: int, height: int):
glClear(GL_COLOR_BUFFER_BIT)
# Draw the numpy array image as a texture
glBindTexture(GL_TEXTURE_2D, texture)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data)
glBegin(GL_QUADS)
glTexCoord2f(0, 0)
glVertex2f(-1, -1)
glTexCoord2f(1, 0)
glVertex2f(1, -1)
glTexCoord2f(1, 1)
glVertex2f(1, 1)
glTexCoord2f(0, 1)
glVertex2f(-1, 1)
glEnd()
glBindTexture(GL_TEXTURE_2D, 0)
def main():
texture_width, texture_height = 640, 480
if not glfw.init():
return
window = glfw.create_window(texture_width, texture_height, "OpenGL Demo", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
# start server
server = syphon.SyphonOpenGLServer("OpenGL Test")
# Enable vsync
glfw.swap_interval(1)
# create texture
glEnable(GL_TEXTURE_2D)
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glBindTexture(GL_TEXTURE_2D, 0)
glClearColor(0, 0, 0, 1)
value = 0
print("publishing...")
while not glfw.window_should_close(window):
# generate random pixels
value = (value + 1) % 255
pixels = bytes(islice(cycle([value, 255 - value, 255, 255]), texture_width * texture_height * 4))
glfw.poll_events()
render(texture, pixels, texture_width, texture_height)
if server.has_clients:
server.publish_frame_texture(texture)
glfw.swap_buffers(window)
server.stop()
glfw.terminate()
if __name__ == "__main__":
main()