Created
November 14, 2008 13:18
-
-
Save fajran/24896 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
from OpenGL.GL import * | |
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
import sys | |
import random | |
from math import cos, pi, sqrt | |
NPN = 32 | |
NMESH = 50 | |
DM = ((float) (1.0/(NMESH-1.0))) | |
NPIX = 256 | |
SCALE = 4.0 | |
iframe = 0; | |
Npat = 32; | |
alpha = (0.12*255); | |
sa = 0 | |
tmax = NPIX/(SCALE*NPN); | |
dmax = SCALE/NPIX; | |
window = 0 | |
def initGL(): | |
glViewport(0, 0, NPIX, NPIX); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glTranslatef(-1.0, -1.0, 0.0); | |
glScalef(2.0, 2.0, 1.0); | |
glTexParameteri(GL_TEXTURE_2D, | |
GL_TEXTURE_WRAP_S, GL_REPEAT); | |
glTexParameteri(GL_TEXTURE_2D, | |
GL_TEXTURE_WRAP_T, GL_REPEAT); | |
glTexParameteri(GL_TEXTURE_2D, | |
GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, | |
GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexEnvf(GL_TEXTURE_ENV, | |
GL_TEXTURE_ENV_MODE, GL_REPLACE); | |
glEnable(GL_TEXTURE_2D); | |
glShadeModel(GL_FLAT); | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
glClear(GL_COLOR_BUFFER_BIT); | |
def makePatterns(): | |
lut = range(0, 256) | |
phase = [] | |
pat = [] | |
for i in range(0, NPN): | |
phase.append(range(0, NPN)) | |
p = range(0, NPN) | |
for j in range(0, NPN): | |
p[j] = range(0, 4) | |
pat.append(p) | |
for i in range(0, 256): | |
if i < 127: | |
lut[i] = 0 | |
else: | |
lut[i] = 255 | |
for i in range(0, NPN): | |
for j in range(0, NPN): | |
phase[i][j] = random.randint(0, 256) | |
for k in range(0, Npat): | |
t = k*256/Npat; | |
for i in range(0, NPN): | |
for j in range(0, NPN): | |
pat[i][j][0] = lut[(t + phase[i][j]) % 255] | |
pat[i][j][1] = pat[i][j][0] | |
pat[i][j][2] = pat[i][k][0] | |
pat[i][j][3] = alpha | |
glNewList(k + 1, GL_COMPILE) | |
glTexImage2D(GL_TEXTURE_2D, 0, 4, NPN, NPN, 0, | |
GL_RGBA, GL_UNSIGNED_BYTE, pat); | |
glEndList(); | |
def getDP(x, y): | |
dx = x - 0.5 | |
dy = y - 0.5 | |
r = dx*dx + dy*dy | |
if (r < 0.0001): | |
r = 0.0001 | |
vx = sa*dx/r + 0.02 | |
vy = sa*dy/r | |
r = vx*vx + vy*vy | |
if (r > dmax*dmax): | |
r = sqrt(r) | |
vx *= dmax/r | |
vy *= dmax/r | |
px = x + vx; | |
py = y + vy; | |
return (px, py) | |
def display(): | |
global iframe | |
sa = 0.010*cos(iframe*2.0*pi/200.0) | |
for i in range(0, NMESH-1): | |
x1 = DM*i | |
x2 = x1 + DM | |
glBegin(GL_QUAD_STRIP); | |
for j in range(0, NMESH): | |
y = DM*j | |
glTexCoord2f(x1, y) | |
(px, py) = getDP(x1, y) | |
glVertex2f(px, py) | |
glTexCoord2f(x2, y) | |
(px, py) = getDP(x2, y) | |
glVertex2f(px, py) | |
glEnd(); | |
iframe = iframe + 1; | |
glEnable(GL_BLEND); | |
glCallList(iframe % Npat + 1); | |
glBegin(GL_QUAD_STRIP); | |
glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0); | |
glTexCoord2f(0.0, tmax); glVertex2f(0.0, 1.0); | |
glTexCoord2f(tmax, 0.0); glVertex2f(1.0, 0.0); | |
glTexCoord2f(tmax, tmax); glVertex2f(1.0, 1.0); | |
glEnd(); | |
glDisable(GL_BLEND); | |
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, | |
0, 0, NPIX, NPIX, 0); | |
glutSwapBuffers(); | |
def main(): | |
global window | |
glutInit(sys.argv) | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) | |
glutInitWindowSize(NPIX, NPIX) | |
window = glutCreateWindow(sys.argv[0]) | |
glutDisplayFunc(display) | |
glutIdleFunc(display) | |
initGL() | |
makePatterns() | |
glutMainLoop() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment