Skip to content

Instantly share code, notes, and snippets.

@mousetail
Last active October 1, 2018 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mousetail/7ac16bad2be2cc27b5ec69f0bd66b4f8 to your computer and use it in GitHub Desktop.
Save mousetail/7ac16bad2be2cc27b5ec69f0bd66b4f8 to your computer and use it in GitHub Desktop.
import OpenGL.GL as GL
import OpenGL.GL.shaders
import pygame
import math
import random
import PIL
import colorsys
vertex_shader = """
#version 330
uniform float angle;
uniform float aspect;
in vec4 position;
in vec4 normal;
in vec2 tex_coord;
out vec2 texCoordV;
out vec4 pos;
void main()
{
mat4 matrix = mat4(vec4(cos(angle),sin(angle),0,0),vec4(-sin(angle),cos(angle),0,0),
vec4(0,0,1,0),vec4(0,0,0,1));
matrix *= mat4(vec4(1,0,0,0),vec4(0,aspect,0,0),vec4(0,0,1,0),vec4(0,0,1,1));
pos = position * matrix;
gl_Position = pos;
texCoordV=tex_coord;
}
"""
FADE_START = 00
FADE_END = 30
fragment_shader = """
#version 330
uniform sampler2D tex;
in vec2 texCoordV;
in vec4 pos;
uniform vec4 col;
void main()
{
float fac = 1 - clamp((pos.z - """+str(FADE_START)+""") / """+str(FADE_END-FADE_START)+""", 0, 1);
gl_FragColor = texture2D(tex, texCoordV) * 0.6 * col * fac;
}
"""
class OrangeObject(object):
def __init__(self, angle, length, z, locations):
self.angle = angle
self.length = length
self.width = 1
self.z = z
self.locations = locations
self.speed=0.1
self.rotspeed=0.1
def draw(self):
GL.glUniform1f(self.locations[b'angle'], self.angle)
GL.glVertexAttrib2fv(self.locations[b'tex_coord'], (0.95,(self.angle/2)/math.pi))
GL.glBegin(GL.GL_TRIANGLES)
rect((((-self.width/2, 1, self.z),(0.0,0.0)),
((self.width/2, 1, self.z),(0.0,1)),
((-self.width/2, 1, self.z+self.width),(0.128,0.0)),
((self.width/2, 1, self.z+self.width),(0.128,1))
), locations=self.locations)
rect((((-self.width/2, 1, self.z+self.width),(0.128,0.0)),
((self.width/2, 1, self.z+self.width),(0.128,1)),
((-self.width/2, 1, self.z+self.width+self.length),(1-0.128,0.0)),
((self.width/2, 1, self.z+self.width+self.length),(1-0.128,1))
), locations=self.locations)
rect((((-self.width/2, 1, self.z+self.width+self.length),(1-0.128,0.0)),
((self.width/2, 1, self.z+self.width+self.length),(1-0.128,1)),
((-self.width/2, 1, self.z+self.width*2+self.length),(1,0.0)),
((self.width/2, 1, self.z+self.width*2+self.length),(1,1))
), locations=self.locations)
GL.glEnd()
def update(self):
self.z-=self.speed
if self.z + self.width * 2 + self.length < 0:
self.z = 30
self.angle=random.random() * 2 * math.pi
self.width=random.random() * 0.25 + 0.128
self.length=random.random() * 4 + 1
self.speed=random.random() * 0.05 + 0.05
self.rotspeed=random.random() * 0.025 + 0.0128
self.angle+=self.rotspeed
def vertex(v, locations):
#print(locations)
GL.glVertexAttrib2fv(locations[b'tex_coord'], v[1])
GL.glVertex(v[0][0],v[0][1],v[0][2],1)
def rect(r, locations):
vertex(r[0], locations)
vertex(r[1], locations)
vertex(r[2], locations)
vertex(r[2], locations)
vertex(r[1], locations)
vertex(r[3], locations)
def load_image(name):
img = pygame.image.load(name)
textureData = pygame.image.tostring(img, "RGBA", 1)
width = img.get_width()
height = img.get_height()
print(len(textureData)/width/height)
im = GL.glGenTextures(1)
GL.glBindTexture(GL.GL_TEXTURE_2D, im)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE, textureData)
GL.glEnable(GL.GL_TEXTURE_2D)
return im
def main():
pygame.init()
DISPLAY_DIMENSIONS = (640 * 2, 480 * 2)
display = pygame.display.set_mode(DISPLAY_DIMENSIONS, pygame.DOUBLEBUF | pygame.OPENGL)
clock = pygame.time.Clock()
FPS = 60
GL.glClearColor(0.5, 0.5, 0.5, 1.0)
GL.glEnable(GL.GL_DEPTH_TEST)
shader = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL.GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL.GL_FRAGMENT_SHADER)
)
img = load_image('rounded_line.png')
print(img)
attribs = [b"aspect",b"angle",b"tex",b"col"]
vertex_attribs=[b"tex_coord",b"position"]
locations = {k:GL.glGetUniformLocation(shader, k) for k in attribs}
locations = {**locations, **{k:GL.glGetAttribLocation(shader, k) for k in vertex_attribs}}
print(locations)
objects=[]
for i in range(96):
objects.append(OrangeObject(math.pi * i / 3,0, -40, locations=locations))
GL.glEnable(GL.GL_BLEND)
GL.glDisable(GL.GL_DEPTH_TEST)
GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE) #_MINUS_SRC_ALPHA
GL.glClearColor(0,0,0,1)
hue = 0
while True:
clock.tick(FPS)
for e in pygame.event.get():
if e.type == pygame.QUIT:
return
col = colorsys.hsv_to_rgb(hue, 0.8, 0.6)
GL.glClearColor(col[0], col[1], col[2], 1)
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
GL.glUseProgram(shader)
GL.glBindTexture(GL.GL_TEXTURE_2D, img)
GL.glUniform1f(locations[b"aspect"], DISPLAY_DIMENSIONS[0]/DISPLAY_DIMENSIONS[1])
GL.glUniform4f(locations[b"col"], *col, 1)
GL.glUniform1i(locations[b"tex"], 0)
for i in objects:
i.draw()
for i in objects:
i.update()
pygame.display.flip()
hue+=0.0005
#if (1 < hue < 3):
# pygame.image.save(display, "glOutput/{0:.4f}.png".format(hue))
if __name__ == '__main__':
try:
main()
finally:
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment