1 |
from math import sin,cos,sqrt,pi |
from math import sin,cos,sqrt,pi,atan2 |
2 |
|
|
3 |
ripple_scale = .25 |
ripple_scale = .25 |
4 |
size = 256 |
size = 128 |
5 |
|
|
6 |
args = ["radius", "2", "ripple_scale", str(ripple_scale), |
args = ["radius", "2", "ripple_scale", str(ripple_scale), |
7 |
"scale", ".25", "bias", ".75", "eps", ".125"] |
"scale", ".25", "bias", ".75", "eps", ".125"] |
20 |
|
|
21 |
|
|
22 |
def lerp(a, b, f): |
def lerp(a, b, f): |
23 |
return [ (1-f) * a[i] + f * b[i] for i in range(0, len(a)) ] |
return [ (1-f) * a[i] + f * b[i] for i in range(0, len(a)) ] |
|
|
|
|
def getirr(texid, angle, pos): |
|
24 |
|
|
25 |
texmat = js([ |
def vecmul(v, m): return [ a * m for a in v ] |
26 |
cos(angle), sin(angle), 0, 0, |
def vecadd(u, v): return [ u[i] + v[i] for i in range(0, len(u)) ] |
27 |
-sin(angle), cos(angle), 0, 0, |
def vecsub(u, v): return [ u[i] - v[i] for i in range(0, len(u)) ] |
|
0, 0, 1, 0, |
|
|
-sin(angle)*pos, cos(angle)*pos, 0, 1, |
|
|
]) |
|
28 |
|
|
29 |
col0l = [ (1.0,0.5,0.5), (0.5,0.5,1.0), (0.5,1.0,0.5), (1.0,0.5,0.5) ] |
def getIrregu(texid, x0, y0, x1, y1, outer = 0, texscale = 1, texangle = 0): |
30 |
col1l = [ (.75,0.5,0.5), (0.5,0.5,.75), (0.5,.75,0.5), (.75,0.5,0.5) ] |
len = sqrt((x1 - x0)**2 + (y1 - y0)**2) |
31 |
|
|
32 |
a = angle * 6 / (2 * pi) |
m = 0.5 * ripple_scale * texscale / len |
33 |
while a < 0: a+=3 |
vec = (m * (y1 - y0), -m * (x1 - x0), 0, 0) # cw90 |
|
i = int(a) % 3 |
|
|
f = a - int(a) |
|
34 |
|
|
35 |
col0 = js(lerp(col0l[i], col0l[i+1], f)) |
vert0 = js(vecmul(vecsub((x0,y0,0,1),vec), 1)) |
36 |
col1 = js(lerp(col1l[i], col1l[i+1], f)) |
vert1 = js(vecmul(vecadd((x0,y0,0,1),vec), .5)) |
37 |
|
vert2 = js(vecmul(vecsub((x1,y1,0,1),vec), 1)) |
38 |
|
vert3 = js(vecmul(vecadd((x1,y1,0,1),vec), .5)) |
39 |
|
|
40 |
|
tex0 = js(vecmul((x0,y0,0,texscale), 1)) |
41 |
|
tex1 = js(vecmul((x0,y0,0,texscale), .5)) |
42 |
|
tex2 = js(vecmul((x1,y1,0,texscale), 1)) |
43 |
|
tex3 = js(vecmul((x1,y1,0,texscale), .5)) |
44 |
|
|
45 |
|
#print "Vertices:" |
46 |
|
#print vert0, tex0 |
47 |
|
#print vert1, tex1 |
48 |
|
#print vert2, tex2 |
49 |
|
#print vert3, tex3 |
50 |
|
|
51 |
|
if outer: |
52 |
|
angle = atan2(y1 - y0, x1 - x0) - texangle |
53 |
|
|
54 |
|
col0l = [ (1.0,0.5,0.5), (0.5,0.5,1.0), (0.5,1.0,0.5), (1.0,0.5,0.5) ] |
55 |
|
col1l = [ (.75,0.5,0.5), (0.5,0.5,.75), (0.5,.75,0.5), (.75,0.5,0.5) ] |
56 |
|
|
57 |
|
a = angle * 6 / (2 * pi) |
58 |
|
while a < 0: a+=3 |
59 |
|
i = int(a) % 3 |
60 |
|
f = a - int(a) |
61 |
|
|
62 |
|
col0 = js(lerp(col0l[i], col0l[i+1], f)) |
63 |
|
col1 = js(lerp(col1l[i], col1l[i+1], f)) |
64 |
|
|
65 |
#print col0, col1 |
src = "SRC_COLOR" |
66 |
|
else: |
67 |
|
col0 = "1 .5 .5 1" |
68 |
|
col1 = ".75 .5 .5 1" |
69 |
|
|
70 |
|
src = "SRC_ALPHA" |
71 |
|
|
|
e = 0.5 |
|
72 |
code = """ |
code = """ |
73 |
PushAttrib ENABLE_BIT |
PushAttrib ENABLE_BIT |
74 |
|
|
75 |
BindTexture TEXTURE_2D %(texid)s |
BindTexture TEXTURE_2D %(texid)s |
76 |
""" |
TexParameter TEXTURE_2D TEXTURE_MIN_FILTER LINEAR_MIPMAP_LINEAR |
77 |
|
TexParameter TEXTURE_2D TEXTURE_MAG_FILTER LINEAR |
78 |
|
Hint PERSPECTIVE_CORRECTION_HINT NICEST |
79 |
|
Enable TEXTURE_2D |
80 |
|
""" % locals() |
81 |
|
|
82 |
if GL.hasExtension("GL_EXT_texture_lod_bias"): |
if GL.hasExtension("GL_EXT_texture_lod_bias"): |
83 |
code += """ |
code += """ |
84 |
TexEnv TEXTURE_FILTER_CONTROL_EXT TEXTURE_LOD_BIAS_EXT %s |
TexEnv TEXTURE_FILTER_CONTROL_EXT TEXTURE_LOD_BIAS_EXT %s |
85 |
""" % lodbias |
""" % lodbias |
|
|
|
|
code += """ |
|
|
PushMatrix |
|
|
Translate -.5 -.75 0 |
|
|
|
|
|
# Try to convince the GL implementation to do |
|
|
# perspective correct color interpolation |
|
|
Hint PERSPECTIVE_CORRECTION_HINT NICEST |
|
86 |
|
|
87 |
#Debug mountains |
code += """ |
|
#TexImage2D TEXTURE_2D 0 LUMINANCE_ALPHA 8 1 0 LUMINANCE_ALPHA \ |
|
|
.5 .5 1 1 .5 .5 .85 .85 .5 .5 .7 .7 .5 .5 .55 .55 |
|
|
#TexParameter TEXTURE_2D TEXTURE_BASE_LEVEL 0 |
|
|
#TexParameter TEXTURE_2D TEXTURE_MAX_LEVEL 0 |
|
|
TexParameter TEXTURE_2D TEXTURE_MIN_FILTER LINEAR_MIPMAP_LINEAR |
|
|
TexParameter TEXTURE_2D TEXTURE_MAG_FILTER LINEAR |
|
|
|
|
|
Enable TEXTURE_2D |
|
|
Enable ALPHA_TEST |
|
|
AlphaFunc GEQUAL %(e)s |
|
|
|
|
|
#Disable ALPHA_TEST |
|
|
#BlendFunc SRC_ALPHA ONE_MINUS_SRC_ALPHA |
|
|
#Enable BLEND |
|
|
#Disable TEXTURE_2D |
|
|
|
|
|
Color 1 1 1 1 |
|
|
|
|
|
TexEnv TEXTURE_ENV TEXTURE_ENV_MODE MODULATE |
|
|
|
|
|
# Debug strip |
|
|
#Begin QUAD_STRIP |
|
|
#TexCoord 0 0 0 1 |
|
|
#Vertex 0 %(e)s 0 1 |
|
|
#TexCoord 0 0 0 %(e)s |
|
|
#Vertex 0 %(e)s 0 %(e)s |
|
|
#TexCoord 1 0 0 1 |
|
|
#Vertex 1 %(e)s 0 1 |
|
|
#TexCoord %(e)s 0 0 %(e)s |
|
|
#Vertex %(e)s %(e)s 0 %(e)s |
|
|
#End |
|
|
|
|
|
BlendFunc ZERO ZERO |
|
|
Enable BLEND |
|
|
|
|
|
# Draw outer edge |
|
|
|
|
88 |
TexEnv TEXTURE_ENV TEXTURE_ENV_MODE COMBINE_ARB |
TexEnv TEXTURE_ENV TEXTURE_ENV_MODE COMBINE_ARB |
89 |
TexEnv TEXTURE_ENV COMBINE_RGB_ARB DOT3_RGBA_ARB |
TexEnv TEXTURE_ENV COMBINE_RGB_ARB DOT3_RGBA_ARB |
90 |
TexEnv TEXTURE_ENV SOURCE0_RGB_ARB TEXTURE |
TexEnv TEXTURE_ENV SOURCE0_RGB_ARB TEXTURE |
91 |
TexEnv TEXTURE_ENV OPERAND0_RGB_ARB SRC_COLOR |
TexEnv TEXTURE_ENV OPERAND0_RGB_ARB %(src)s |
92 |
TexEnv TEXTURE_ENV SOURCE1_RGB_ARB PREVIOUS_ARB |
TexEnv TEXTURE_ENV SOURCE1_RGB_ARB PREVIOUS_ARB |
93 |
TexEnv TEXTURE_ENV OPERAND1_RGB_ARB SRC_COLOR |
TexEnv TEXTURE_ENV OPERAND1_RGB_ARB SRC_COLOR |
94 |
|
|
95 |
# Ignored with dot3: |
# Create edge stencil |
|
#TexEnv TEXTURE_ENV COMBINE_ALPHA_ARB REPLACE |
|
|
#TexEnv TEXTURE_ENV SOURCE0_ALPHA_ARB TEXTURE |
|
|
#TexEnv TEXTURE_ENV OPERAND0_ALPHA_ARB SRC_ALPHA |
|
|
#TexEnv TEXTURE_ENV SOURCE1_ALPHA_ARB TEXTURE |
|
|
#TexEnv TEXTURE_ENV OPERAND1_ALPHA_ARB SRC_ALPHA |
|
|
|
|
|
MatrixMode TEXTURE |
|
|
LoadMatrix %(texmat)s |
|
|
MatrixMode MODELVIEW |
|
|
|
|
|
Begin QUAD_STRIP |
|
|
|
|
|
TexCoord 0 0 0 1 |
|
|
Color %(col0)s |
|
|
Vertex 0 %(e)s 0 1 |
|
|
|
|
|
TexCoord 0 0 0 %(e)s |
|
|
Color %(col1)s |
|
|
Vertex 0 %(e)s 0 %(e)s |
|
|
|
|
|
TexCoord 1 0 0 1 |
|
|
Color %(col0)s |
|
|
Vertex 1 %(e)s 0 1 |
|
|
|
|
|
TexCoord %(e)s 0 0 %(e)s |
|
|
Color %(col1)s |
|
|
Vertex %(e)s %(e)s 0 %(e)s |
|
|
|
|
|
End |
|
|
|
|
|
Disable BLEND |
|
96 |
|
|
97 |
# Create inner edge stencil |
Enable ALPHA_TEST |
98 |
|
AlphaFunc LESS 0.5 |
99 |
|
ColorMask 0 0 0 0 |
100 |
|
|
101 |
StencilMask 255 |
StencilMask 255 |
|
|
|
102 |
Enable STENCIL_TEST |
Enable STENCIL_TEST |
|
ColorMask 0 0 0 0 |
|
103 |
StencilFunc ALWAYS 0 255 |
StencilFunc ALWAYS 0 255 |
104 |
StencilOp INCR INCR INCR |
StencilOp INCR INCR INCR |
105 |
|
|
|
TexEnv TEXTURE_ENV OPERAND0_RGB_ARB SRC_ALPHA |
|
|
|
|
106 |
Begin QUAD_STRIP |
Begin QUAD_STRIP |
107 |
|
|
108 |
TexCoord 0 0 0 1 |
Color %(col0)s |
109 |
Color 1 .5 .5 |
TexCoord %(tex0)s |
110 |
Vertex 0 %(e)s 0 1 |
Vertex %(vert0)s |
|
|
|
|
TexCoord 0 0 0 %(e)s |
|
|
Color .75 .5 .5 |
|
|
Vertex 0 %(e)s 0 %(e)s |
|
|
|
|
|
TexCoord 1 0 0 1 |
|
|
Color 1 .5 .5 |
|
|
Vertex 1 %(e)s 0 1 |
|
|
|
|
|
TexCoord %(e)s 0 0 %(e)s |
|
|
Color .75 .5 .5 |
|
|
Vertex %(e)s %(e)s 0 %(e)s |
|
|
|
|
|
End |
|
111 |
|
|
112 |
# Draw white using the stencil |
Color %(col1)s |
113 |
|
TexCoord %(tex1)s |
114 |
|
Vertex %(vert1)s |
115 |
|
|
116 |
StencilFunc LESS 0 255 |
Color %(col0)s |
117 |
StencilOp ZERO ZERO ZERO |
TexCoord %(tex2)s |
118 |
ColorMask 1 1 1 1 |
Vertex %(vert2)s |
119 |
|
|
120 |
Disable TEXTURE_2D |
Color %(col1)s |
121 |
Color 1 1 1 1 |
TexCoord %(tex3)s |
122 |
|
Vertex %(vert3)s |
123 |
|
|
124 |
Begin QUAD_STRIP |
End |
|
Vertex 0 .5 |
|
|
Vertex 0 1 |
|
|
Vertex 1 .5 |
|
|
Vertex 1 1 |
|
|
End |
|
125 |
|
|
126 |
|
ColorMask 1 1 1 1 |
127 |
Disable STENCIL_TEST |
Disable STENCIL_TEST |
128 |
|
PopAttrib |
129 |
|
|
130 |
PopMatrix |
""" % locals() |
|
|
|
|
PopAttrib |
|
|
""" |
|
|
|
|
|
return getDList(code % vars()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getIrregu(x0, y0, x1, y1, s0, t0, s1, t1): |
|
|
tlen = sqrt((s1 - s0)**2 + (t1 - t0)**2) |
|
|
len = sqrt((x1 - x0)**2 + (y1 - y0)**2) |
|
131 |
|
|
132 |
|
return code |
133 |
|
|
134 |
class IrreguScene: |
class IrreguScene: |
135 |
def __init__(self): |
def __init__(self): |
137 |
self.dist = 1 |
self.dist = 1 |
138 |
self.angle = 0 |
self.angle = 0 |
139 |
self.pos = 0 |
self.pos = 0 |
140 |
|
|
141 |
|
self.tx, self.ty = 0, 0 |
142 |
|
|
143 |
def scene(self, vs): |
def scene(self, vs): |
144 |
putnoc(vs, background((0.7,0.7,1.0))) |
putnoc(vs, background((0.7,0.7,1.0))) |
148 |
vs.matcher.add(cs1, "1") |
vs.matcher.add(cs1, "1") |
149 |
vs.matcher.add(cs2, "2") |
vs.matcher.add(cs2, "2") |
150 |
|
|
151 |
irr = getirr(tex.getTexId(), self.angle, self.pos) |
cs3 = vs.coords.affineCoordsys(0, 0, 512, 384, self.scale, 0, 0, self.scale) |
152 |
irrinv = getirr(texinv.getTexId(), self.angle, self.pos) |
vs.matcher.add(cs3, "3") |
153 |
|
|
154 |
|
texscale = 1 |
155 |
|
|
156 |
|
x0 = -1 - 0.5 * ripple_scale * texscale |
157 |
|
x1 = 1 + 0.5 * ripple_scale * texscale |
158 |
|
y0 = x0 |
159 |
|
y1 = x1 |
160 |
|
|
161 |
vs.map.put(irr, cs1) |
|
162 |
vs.map.put(irrinv, cs2) |
cs4 = vs.coords.affineCoordsys(0, 0, |
163 |
|
self.tx * cos(self.angle) + |
164 |
|
self.ty * sin(self.angle), |
165 |
|
self.tx * -sin(self.angle) + |
166 |
|
self.ty * cos(self.angle), |
167 |
|
cos(self.angle), sin(self.angle), |
168 |
|
-sin(self.angle), cos(self.angle)) |
169 |
|
vs.matcher.add(cs4, "4") |
170 |
|
vs.map.put(GLRen.createTextureMatrix(), cs4) |
171 |
|
|
172 |
|
paint = """ |
173 |
|
PushAttrib ENABLE_BIT |
174 |
|
Enable STENCIL_TEST |
175 |
|
Disable ALPHA_TEST |
176 |
|
StencilFunc EQUAL 0 255 |
177 |
|
StencilOp ZERO ZERO ZERO |
178 |
|
Disable TEXTURE_2D |
179 |
|
Begin QUAD_STRIP |
180 |
|
Vertex %(x0)s %(y0)s |
181 |
|
Vertex %(x1)s %(y0)s |
182 |
|
Vertex %(x0)s %(y1)s |
183 |
|
Vertex %(x1)s %(y1)s |
184 |
|
End |
185 |
|
PopAttrib |
186 |
|
""" % locals() |
187 |
|
|
188 |
|
i0 = tex.getTexId() |
189 |
|
i1 = texinv.getTexId() |
190 |
|
|
191 |
|
vs.map.put(getDList(getIrregu(i0,x0,-1,x1,-1,1, texscale, self.angle) + |
192 |
|
getIrregu(i0,+1,y0,+1,y1,1, texscale, self.angle) + |
193 |
|
getIrregu(i1,x1,+1,x0,+1,1, texscale, self.angle) + |
194 |
|
getIrregu(i1,-1,y1,-1,y0,1, texscale, self.angle) + |
195 |
|
"Color 0 0 0" + paint), cs3) |
196 |
|
|
197 |
|
vs.map.put(getDList(getIrregu(i0,x0,-1,x1,-1,0, texscale, self.angle) + |
198 |
|
getIrregu(i0,+1,y0,+1,y1,0, texscale, self.angle) + |
199 |
|
getIrregu(i1,x1,+1,x0,+1,0, texscale, self.angle) + |
200 |
|
getIrregu(i1,-1,y1,-1,y0,0, texscale, self.angle) + |
201 |
|
"Color 1 1 1" + paint), cs3) |
202 |
|
|
203 |
def key(self, k): |
def key(self, k): |
204 |
if k == "+": self.scale *= 1.4142135623731; print round(self.scale) |
if k == "+": self.scale *= 1.4142135623731; print round(self.scale) |
205 |
if k == "-": self.scale /= 1.4142135623731; print round(self.scale) |
if k == "-": self.scale /= 1.4142135623731; print round(self.scale) |
206 |
if k == "Prior": self.dist *= 1.1 |
|
207 |
if k == "Next": self.dist /= 1.1 |
if k == "Prior": |
208 |
if k == "Up": self.pos -= 1./128 |
self.angle -= pi/180; print "angle",round(self.angle / pi * 180) |
209 |
if k == "Down": self.pos += 1./128 |
if k == "Next": |
210 |
if k == "Left": self.angle -= pi/180 |
self.angle += pi/180; print "angle",round(self.angle / pi * 180) |
211 |
if k == "Right": self.angle += pi/180 |
|
212 |
|
if k == "Up": self.ty -= .1 |
213 |
|
if k == "Down": self.ty += .1 |
214 |
|
if k == "Left": self.tx -= .1 |
215 |
|
if k == "Right": self.tx += .1 |
216 |
|
|
217 |
global lodbias |
global lodbias |
218 |
if k == "b": lodbias += .25; print "lodbias", lodbias |
if k == "b": lodbias += .25; print "lodbias", lodbias |
219 |
if k == "B": lodbias -= .25; print "lodbias", lodbias |
if k == "B": lodbias -= .25; print "lodbias", lodbias |
220 |
print "angle",round(self.angle / pi * 180) |
|
221 |
|
|
222 |
currentScene = IrreguScene() |
currentScene = IrreguScene() |