-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorsound.py
163 lines (129 loc) · 6.52 KB
/
colorsound.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pygame
from pygame.locals import DOUBLEBUF, OPENGL, QUIT, KEYUP, K_ESCAPE
from OpenGL.GL import *
from OpenGL.GL import shaders as gl_shaders # wrapper
from sys import exit as exitsystem
import numpy as np
VERTEX_SHADER_IM = """
#version 330 core
layout(location = 0) in vec3 vPos;
void main()
{
gl_Position = vec4(vPos, 1.0);
}
"""
with open('render.frag') as frag_im:
FRAGMENT_SHADER_IM = frag_im.read()
with open('a.frag') as frag_a:
FRAGMENT_SHADER_A = frag_a.read()
with open('b.frag') as frag_b:
FRAGMENT_SHADER_B = frag_b.read()
class ColorSound:
def __init__(self):
pygame.init()
self.frame_counter = 0
self.resolution = 1920, 1080
# self.resolution = 3840, 2160
pygame.display.set_mode(self.resolution, DOUBLEBUF | OPENGL)
#################################################
# Create shaders and programs ( vertex + frag ) #
#################################################
# shaders compilation
gl_shaders.compileShader(VERTEX_SHADER_IM, GL_VERTEX_SHADER)
display_sh = gl_shaders.compileShader(FRAGMENT_SHADER_IM, GL_FRAGMENT_SHADER)
a_sh = gl_shaders.compileShader(FRAGMENT_SHADER_A, GL_FRAGMENT_SHADER)
b_sh = gl_shaders.compileShader(FRAGMENT_SHADER_B, GL_FRAGMENT_SHADER)
# programs creation
self.display_prog = gl_shaders.compileProgram(display_sh)
self.a_prog = gl_shaders.compileProgram(a_sh)
self.b_prog = gl_shaders.compileProgram(b_sh)
# get and send uniforms
glUseProgram(self.display_prog)
glUniform2f(glGetUniformLocation(self.display_prog, 'iResolution'), *self.resolution)
self.display_channel_a = glGetUniformLocation(self.display_prog, "iChannel0")
self.display_channel_b = glGetUniformLocation(self.display_prog, "iChannel1")
glUseProgram(self.a_prog)
glUniform2f(glGetUniformLocation(self.a_prog, 'iResolution'), *self.resolution)
self.a_prog_channel_a = glGetUniformLocation(self.a_prog, "kernelTexture")
self.a_prog_channel_b = glGetUniformLocation(self.a_prog, "iChannel1")
glUseProgram(self.b_prog)
glUniform2f(glGetUniformLocation(self.b_prog, 'iResolution'), *self.resolution)
self.uni_mouse_pos = glGetUniformLocation(self.b_prog, 'iMouse')
self.uni_mouse_left_down = glGetUniformLocation(self.b_prog, 'iMouseLeftDown')
self.uni_mouse_right_down = glGetUniformLocation(self.b_prog, 'iMouseRightDown')
self.b_prog_channel_a = glGetUniformLocation(self.b_prog, "kernelTexture")
self.b_prog_channel_b = glGetUniformLocation(self.b_prog, "iChannel1")
##################
# Vertex buffers #
##################
self.vertex_buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer)
vertices = np.array([-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0], dtype='float32') # 4 triangles, fullscreen
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
glEnableVertexAttribArray(0) # pointer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
################
# Framebuffers #
################
self.texture_a = glGenTextures(1)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, self.texture_a)
# with GL_RGBA16F, light artifacts
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, *self.resolution, 0, GL_RGBA, GL_BYTE, None)
# important: store negative values
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) # nearest ?
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
self.a_fb = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, self.a_fb)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture_a, 0)
self.texture_b = glGenTextures(1)
glActiveTexture(GL_TEXTURE2)
glBindTexture(GL_TEXTURE_2D, self.texture_b)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, *self.resolution, 0, GL_RG, GL_BYTE, None)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
self.b_fb = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, self.b_fb)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture_b, 0)
self.clock = pygame.time.Clock()
def debug_texture_sum(self, color_chanel):
"""Convert pixel area of current binded framebuffer texture to numpy array"""
pixels = glReadPixels(0, 0, *self.resolution, GL_RGBA, GL_FLOAT)
return pixels[:, :, color_chanel][:, :, 1 > 0.99].sum()
def mainloop(self):
while 1:
self.frame_counter += 1
self.clock.tick(120) # cap fps
for event in pygame.event.get():
mouse_left_down = pygame.mouse.get_pressed()[0]
mouse_right_down = pygame.mouse.get_pressed()[2]
if (event.type == QUIT) or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
exitsystem()
glBindFramebuffer(GL_FRAMEBUFFER, self.a_fb)
glUseProgram(self.a_prog)
glUniform1i(self.a_prog_channel_a, self.texture_a)
glUniform1i(self.a_prog_channel_b, self.texture_b)
glDrawArrays(GL_QUADS, 0, 4)
glBindFramebuffer(GL_FRAMEBUFFER, self.b_fb)
glUseProgram(self.b_prog)
glUniform1i(self.b_prog_channel_a, self.texture_a)
glUniform1i(self.b_prog_channel_b, self.texture_b)
glUniform1i(self.uni_mouse_left_down, mouse_left_down)
glUniform1i(self.uni_mouse_right_down, mouse_right_down)
glUniform2f(self.uni_mouse_pos, *pygame.mouse.get_pos())
glDrawArrays(GL_QUADS, 0, 4)
# if self.frame_counter % 60 == 0:
# print(self.debug_texture_sum(0))
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glUseProgram(self.display_prog)
glUniform1i(self.display_channel_a, self.texture_a)
glUniform1i(self.display_channel_b, self.texture_b)
glDrawArrays(GL_QUADS, 0, 4)
pygame.display.set_caption(f"FPS: {self.clock.get_fps():.0f}")
pygame.display.flip() # Update the full display Surface to the screen
if __name__ == '__main__':
ColorSound().mainloop()