Created
August 21, 2012 22:02
-
-
Save TimothyFitz/3419835 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/cygdrive/c/Users/timothy/Packages/pypy-1.9/pypy.exe | |
#!/cygdrive/c/Python27/python.exe | |
import platform | |
print "Running on", platform.python_implementation() | |
import pyglet | |
pyglet.options['debug_gl'] = False | |
from pyglet.gl import * | |
from pyglet.graphics.vertexbuffer import VertexBufferObject | |
import random, ctypes | |
import time, array | |
from gletools import ShaderProgram, FragmentShader, VertexShader, GeometryShader | |
window = pyglet.window.Window(width=1000, height=1000) | |
label = pyglet.text.Label('Hello, world', | |
font_name='Times New Roman', | |
font_size=36, | |
x=window.width//2, y=window.height//2, | |
anchor_x='center', anchor_y='center') | |
fps_display = pyglet.clock.ClockDisplay() | |
def rand_brown(): | |
P = lambda: random.randrange(-10,10) | |
return [c/255.0 for c in [150+P(), 75+P(), 10+P(), 255.0]] | |
FLOAT_SIZE = 4 | |
COLOR_SIZE = 4 * FLOAT_SIZE | |
POINT_2D_SIZE = 2 * FLOAT_SIZE | |
mesh_vbo = VertexBufferObject(64**2 * (POINT_2D_SIZE + COLOR_SIZE), GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW) | |
#c_vbo = VertexBufferObject(64**2, GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW) | |
def gen_mesh(): | |
#t0 = time.time() | |
v = [] | |
c = [] | |
height = width = 8 | |
for x in range(0, 64*width, width): | |
for y in range(0, 64*height, height): | |
v += [x,y] | |
c += rand_brown() | |
t1 = time.time() | |
array_data = array.array('f', v+c) | |
address, length = array_data.buffer_info() | |
data = ctypes.c_void_p(address) | |
#data = (GLfloat * (len(v) + len(c)))(*(v+c)) | |
#print data | |
t2 = time.time() | |
#print (t2 - t1) * 1000 | |
mesh_vbo.set_data(data) | |
#t2 = time.time() | |
#print (t1 - t0) * 1000, (t2 - t1) * 1000 | |
vl = gen_mesh() | |
def draw_mesh(): | |
mesh_vbo.bind() | |
glEnableClientState(GL_VERTEX_ARRAY) | |
glEnableVertexAttribArray(0) | |
glEnableVertexAttribArray(1) | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0) | |
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 64**2 * POINT_2D_SIZE) | |
glDrawArrays(GL_POINTS, 0, 64 ** 2) | |
mesh_vbo.unbind() | |
def update(dt): | |
pass | |
pyglet.clock.schedule_interval(update, 0.01) | |
program = ShaderProgram( | |
VertexShader(''' | |
#version 330 | |
layout(location = 0) in vec2 position; | |
layout(location = 1) in vec4 color; | |
uniform vec2 translate; | |
smooth out vec4 gsColor; | |
void main() | |
{ | |
gl_Position.xy = position + translate; | |
gl_Position.zw = vec2(0.0, 1.0); | |
gsColor = color; | |
}'''), | |
GeometryShader(''' | |
#version 330 | |
layout (points) in; | |
layout (triangle_strip, max_vertices=4) out; | |
uniform vec2 screen_size; | |
smooth in vec4 gsColor[]; | |
smooth out vec4 fsColor; | |
void main() | |
{ | |
mat4 projectionMatrix = mat4( | |
2.0/screen_size.x, 0.0, 0.0, 0.0, | |
0.0, 2.0/screen_size.y, 0.0, 0.0, | |
0.0, 0.0, -1.0, 0.0, | |
-1.0, -1.0, -1.0, 1.0 | |
); | |
gl_Position = gl_in[0].gl_Position; | |
gl_Position = projectionMatrix * gl_Position; | |
fsColor = gsColor[0]; | |
EmitVertex(); | |
gl_Position = gl_in[0].gl_Position; | |
gl_Position.x += 8.0; | |
gl_Position = projectionMatrix * gl_Position; | |
fsColor = gsColor[0]; | |
EmitVertex(); | |
gl_Position = gl_in[0].gl_Position; | |
gl_Position.y += 8.0; | |
gl_Position = projectionMatrix * gl_Position; | |
fsColor = gsColor[0]; | |
EmitVertex(); | |
gl_Position = gl_in[0].gl_Position; | |
gl_Position.xy += vec2(8.0, 8.0); | |
gl_Position = projectionMatrix * gl_Position; | |
fsColor = gsColor[0]; | |
EmitVertex(); | |
EndPrimitive(); | |
} | |
'''), | |
FragmentShader(''' | |
#version 330 | |
smooth in vec4 fsColor; | |
out vec4 outputColor; | |
void main() | |
{ | |
outputColor = fsColor; | |
}''') | |
) | |
@window.event | |
def on_draw(): | |
program.vars.screen_size = [float(s) for s in window.get_size()] | |
program.vars.translate = 100.0, 100.0 | |
with program: | |
gen_mesh() | |
window.clear() | |
draw_mesh() | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment