forked from spiraltechnica/Spout-for-Python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspout_FBO_sender_example.py
More file actions
149 lines (119 loc) · 4.78 KB
/
Copy pathspout_FBO_sender_example.py
File metadata and controls
149 lines (119 loc) · 4.78 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Add relative directory ../Library to import path, so we can import the SpoutSDK.pyd library. Feel free to remove these if you put the SpoutSDK.pyd file in the same directory as the python scripts.
import sys
sys.path.append('../Library')
import SpoutSDK
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def main():
# window details
width = 800
height = 600
display = (width,height)
# window setup
pygame.init()
pygame.display.set_caption('Spout Python Sender')
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)
# OpenGL init
# setup OpenGL perspective
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
# setup default colours, blending modes, rotation and translation parameters
glMatrixMode(GL_MODELVIEW)
# reset the drawing perspective
glLoadIdentity()
# can disable depth buffer because we aren't dealing with multiple geometry in our scene
glDisable(GL_DEPTH_TEST)
glEnable(GL_ALPHA_TEST)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0,0.0,0.0,0.0)
glColor4f(1.0, 1.0, 1.0, 1.0);
glTranslatef(0,0, -5)
glRotatef(25, 2, 1, 0)
# init spout sender
spoutSender = SpoutSDK.SpoutSender()
spoutSenderWidth = width
spoutSenderHeight = height
# Its signature in C++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
spoutSender.CreateSender('Spout Python Sender', spoutSenderWidth, spoutSenderHeight, 0)
# init spout sender texture ID
senderTextureID = glGenTextures(1)
# initialise texture
glBindTexture(GL_TEXTURE_2D, senderTextureID)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
# fill texture with blank data
glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,spoutSenderWidth,spoutSenderHeight,0);
glBindTexture(GL_TEXTURE_2D, 0)
# create fbo and connect it to our senderTexture
fbo = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, senderTextureID, 0)
glBindFramebuffer(GL_FRAMEBUFFER, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# setup frame
glActiveTexture(GL_TEXTURE0)
glClearColor(0.0,0.0,0.0,0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# bind our fbo and render some content to it - by using an FBO we will get alpha transparency sent with Spout
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
#Start fbo frame afresh by setting clear color and performing a clear operation
glClearColor(0.0,0.0,0.0,0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Perform a rotation and since we aren't resetting our perspective with glLoadIdentity, then each frame will perform a successive rotation on top of what we already see
glRotatef(1, 3, 1, 1)
# draw cube
Cube()
# back to the default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0)
# read the FBO and then use Blit to copy into the backbuffer.
# A faster approach may be to render the fbo to a textured quad, but this will do for now.
glBindFramebuffer (GL_READ_FRAMEBUFFER, fbo);
glBlitFramebuffer (0,0, spoutSenderWidth,spoutSenderHeight, 0,0, spoutSenderWidth,spoutSenderHeight, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST)
glBindFramebuffer(GL_FRAMEBUFFER, 0)
# send texture to Spout
# Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, True, 0)
# update display
pygame.display.flip()
pygame.time.wait(10)
main()