1 |
from math import sin,cos,sqrt,pi,atan2 |
from math import sin,cos,sqrt,pi,atan2 |
2 |
|
|
3 |
|
from gfx.libpaper.texcache import getCachedTexture |
4 |
|
|
5 |
ripple_scale = .25 |
ripple_scale = .25 |
6 |
size = 128 |
size = 256 |
7 |
|
lodbias = 0 |
8 |
|
|
9 |
|
profiles = [] |
10 |
|
|
11 |
if (GL.hasExtension("GL_NV_register_combiners") and |
if (GL.hasExtension("GL_NV_register_combiners") and |
12 |
GL.hasExtension("GL_EXT_secondary_color")): |
GL.hasExtension("GL_EXT_secondary_color")): |
13 |
|
profiles.append("NV") |
14 |
|
|
15 |
|
if GL.hasExtension("GL_EXT_texture_env_dot3"): |
16 |
|
profiles.append("DOT3") |
17 |
|
|
18 |
|
profiles.append("STD") |
19 |
|
|
20 |
print "Using full displacement resolution textures with GL_NV_register_combiners" |
def init(profile): |
21 |
|
global tex, texinv |
22 |
|
global texenv_inner, texenv_outer, alphalevel, w0, w1 |
23 |
|
global colmap0, colmap1 |
24 |
|
|
25 |
from gfx.libutil.nvcode import combinercode |
if profile == "NV": |
|
texenv_outer = combinercode(""" |
|
|
SecondaryColorEXT %(dotvec)s |
|
26 |
|
|
27 |
Enable REGISTER_COMBINERS_NV |
print "Using full displacement resolution textures with GL_NV_register_combiners" |
28 |
CombinerParameterNV NUM_GENERAL_COMBINERS_NV 2 |
|
29 |
|
from gfx.libutil.nvcode import combinercode |
30 |
|
texenv_outer = combinercode(""" |
31 |
|
SecondaryColorEXT %(dotvec)s |
32 |
|
|
33 |
|
Enable REGISTER_COMBINERS_NV |
34 |
|
CombinerParameterNV NUM_GENERAL_COMBINERS_NV 2 |
35 |
|
|
36 |
|
# Compute TEXTURE . SECONDARY_COLOR dot product |
37 |
|
CI0 RGB A TEXTURE0 UNSIGNED_IDENTITY_NV RGB |
38 |
|
CI0 RGB B SECONDARY_COLOR_NV UNSIGNED_IDENTITY_NV RGB |
39 |
|
CO0 RGB SPARE0_NV DISCARD_NV DISCARD_NV NONE NONE TRUE FALSE FALSE |
40 |
|
|
41 |
|
# Add PRIMARY_COLOR |
42 |
|
CI1 RGB A SPARE0_NV UNSIGNED_IDENTITY_NV RGB |
43 |
|
CI1 RGB B ZERO UNSIGNED_INVERT_NV RGB |
44 |
|
CI1 RGB C PRIMARY_COLOR_NV UNSIGNED_IDENTITY_NV RGB |
45 |
|
CI1 RGB D ZERO UNSIGNED_INVERT_NV RGB |
46 |
|
CO1 RGB DISCARD_NV DISCARD_NV SPARE0_NV NONE NONE FALSE FALSE FALSE |
47 |
|
|
48 |
|
# Use as alpha |
49 |
|
FCI G SPARE0_NV UNSIGNED_IDENTITY_NV BLUE |
50 |
|
""") |
51 |
|
|
52 |
|
texenv_inner = combinercode(""" |
53 |
|
SecondaryColorEXT 0 0 0 |
54 |
|
|
55 |
|
Enable REGISTER_COMBINERS_NV |
56 |
|
CombinerParameterNV NUM_GENERAL_COMBINERS_NV 1 |
57 |
|
|
58 |
|
CI0 ALPHA A TEXTURE0 UNSIGNED_IDENTITY_NV ALPHA |
59 |
|
CI0 ALPHA B ZERO UNSIGNED_INVERT_NV ALPHA |
60 |
|
CI0 ALPHA C PRIMARY_COLOR_NV UNSIGNED_IDENTITY_NV BLUE |
61 |
|
CI0 ALPHA D ZERO UNSIGNED_INVERT_NV ALPHA |
62 |
|
CO0 ALPHA DISCARD_NV DISCARD_NV SPARE0_NV NONE NONE FALSE FALSE FALSE |
63 |
|
|
64 |
|
FCI G SPARE0_NV UNSIGNED_IDENTITY_NV ALPHA |
65 |
|
""") |
66 |
|
|
67 |
|
alphalevel = 1 |
68 |
|
w0,w1 = 1,1 |
69 |
|
def colmap0(dotvec): return "1 1 1" |
70 |
|
def colmap1(dotvec): return "0 0 0" |
71 |
|
|
72 |
|
tex_scale = 1 |
73 |
|
tex_bias = 0 |
74 |
|
tex_format = "RGBA" |
75 |
|
|
76 |
|
elif profile == "DOT3": |
77 |
|
print "Using 1/4 displacement resolution textures with GL_EXT_texture_env_dot3" |
78 |
|
|
79 |
# Compute TEXTURE . SECONDARY_COLOR dot product |
texenv = """ |
80 |
CI0 RGB A TEXTURE0 UNSIGNED_IDENTITY_NV RGB |
Hint PERSPECTIVE_CORRECTION_HINT NICEST |
81 |
CI0 RGB B SECONDARY_COLOR_NV UNSIGNED_IDENTITY_NV RGB |
TexEnv TEXTURE_ENV TEXTURE_ENV_MODE COMBINE_EXT |
82 |
CO0 RGB SPARE0_NV DISCARD_NV DISCARD_NV NONE NONE TRUE FALSE FALSE |
TexEnv TEXTURE_ENV COMBINE_RGB_EXT DOT3_RGBA_EXT |
83 |
|
TexEnv TEXTURE_ENV SOURCE0_RGB_EXT TEXTURE |
84 |
# Add PRIMARY_COLOR |
TexEnv TEXTURE_ENV OPERAND0_RGB_EXT %s |
85 |
CI1 RGB A SPARE0_NV UNSIGNED_IDENTITY_NV RGB |
TexEnv TEXTURE_ENV SOURCE1_RGB_EXT PREVIOUS_EXT |
86 |
CI1 RGB B ZERO UNSIGNED_INVERT_NV RGB |
TexEnv TEXTURE_ENV OPERAND1_RGB_EXT SRC_COLOR |
87 |
CI1 RGB C PRIMARY_COLOR_NV UNSIGNED_IDENTITY_NV RGB |
""" |
88 |
CI1 RGB D ZERO UNSIGNED_INVERT_NV RGB |
texenv_inner = texenv % "SRC_ALPHA" |
89 |
CO1 RGB DISCARD_NV DISCARD_NV SPARE0_NV NONE NONE FALSE FALSE FALSE |
texenv_outer = texenv % "SRC_COLOR" |
90 |
|
|
91 |
# Use as alpha |
alphalevel = .5 |
92 |
FCI G SPARE0_NV UNSIGNED_IDENTITY_NV BLUE |
w0,w1 = 1,.5 |
93 |
""") |
def colmap0(dotvec): return js([.5 + .5 * c for c in dotvec]) |
94 |
|
def colmap1(dotvec): return js([.5 + .25 * c for c in dotvec]) |
95 |
texenv_inner = combinercode(""" |
|
96 |
SecondaryColorEXT 0 0 0 |
tex_scale = .25 |
97 |
|
tex_bias = .75 |
98 |
Enable REGISTER_COMBINERS_NV |
tex_format = "RGBA" |
99 |
CombinerParameterNV NUM_GENERAL_COMBINERS_NV 1 |
|
100 |
|
elif profile == "STD": |
101 |
CI0 ALPHA A TEXTURE0 UNSIGNED_IDENTITY_NV ALPHA |
print "Using full displacement resolution texture with standard GL" |
102 |
CI0 ALPHA B ZERO UNSIGNED_INVERT_NV ALPHA |
|
103 |
CI0 ALPHA C PRIMARY_COLOR_NV UNSIGNED_IDENTITY_NV BLUE |
texenv = """ |
104 |
CI0 ALPHA D ZERO UNSIGNED_INVERT_NV ALPHA |
TexEnv TEXTURE_ENV TEXTURE_ENV_MODE ADD |
105 |
CO0 ALPHA DISCARD_NV DISCARD_NV SPARE0_NV NONE NONE FALSE FALSE FALSE |
""" |
106 |
|
texenv_inner = texenv |
107 |
FCI G SPARE0_NV UNSIGNED_IDENTITY_NV ALPHA |
texenv_outer = texenv |
108 |
""") |
|
109 |
|
alphalevel = 1 |
110 |
tex_scale = 1 |
w0,w1 = 1,1 |
111 |
tex_bias = 0 |
def colmap0(dotvec): return "1 1 1 1" |
112 |
alphalevel = 1 |
def colmap1(dotvec): return "0 0 0 0" |
113 |
w0,w1 = 1,1 |
|
114 |
|
tex_scale = 1 |
115 |
|
tex_bias = 0 |
116 |
|
tex_format = "INTENSITY" |
117 |
|
else: |
118 |
|
assert(0) |
119 |
|
|
|
def colmap0(dotvec): return "1 1 1" |
|
|
def colmap1(dotvec): return "0 0 0" |
|
120 |
|
|
121 |
elif GL.hasExtension("GL_EXT_texture_env_dot3"): |
args = ["radius", "2", "ripple_scale", str(ripple_scale), |
122 |
|
"scale", str(tex_scale), "bias", str(tex_bias), "eps", ".125"] |
123 |
|
|
|
print "Using 1/4 displacement resolution textures with GL_EXT_texture_env_dot3" |
|
|
|
|
|
texenv = """ |
|
|
Hint PERSPECTIVE_CORRECTION_HINT NICEST |
|
|
TexEnv TEXTURE_ENV TEXTURE_ENV_MODE COMBINE_EXT |
|
|
TexEnv TEXTURE_ENV COMBINE_RGB_EXT DOT3_RGBA_EXT |
|
|
TexEnv TEXTURE_ENV SOURCE0_RGB_EXT TEXTURE |
|
|
TexEnv TEXTURE_ENV OPERAND0_RGB_EXT %s |
|
|
TexEnv TEXTURE_ENV SOURCE1_RGB_EXT PREVIOUS_EXT |
|
|
TexEnv TEXTURE_ENV OPERAND1_RGB_EXT SRC_COLOR |
|
|
""" |
|
|
texenv_inner = texenv % "SRC_ALPHA" |
|
|
texenv_outer = texenv % "SRC_COLOR" |
|
|
|
|
|
tex_scale = .25 |
|
|
tex_bias = .75 |
|
|
alphalevel = .5 |
|
|
w0,w1 = 1,.5 |
|
|
|
|
|
def colmap0(dotvec): return js([.5 + .5 * c for c in dotvec]) |
|
|
def colmap1(dotvec): return js([.5 + .25 * c for c in dotvec]) |
|
|
else: |
|
|
assert(0) |
|
|
|
|
|
|
|
|
args = ["radius", "2", "ripple_scale", str(ripple_scale), |
|
|
"scale", str(tex_scale), "bias", str(tex_bias), "eps", ".125"] |
|
|
|
|
|
tex = GL.createTexture() |
|
|
texinv = GL.createTexture() |
|
|
tex.shade_all_levels(size, size, 0, 4, "RGBA", "RGBA", "irregu", args) |
|
|
texinv.shade_all_levels(size, size, 0, 4, "RGBA", "RGBA", "irregu", args + |
|
|
["invert", "1"]) |
|
124 |
|
|
125 |
|
tex = getCachedTexture([size, size, 0, 4, tex_format, "RGBA", "irregu", |
126 |
|
args], shade_all_levels = 1) |
127 |
|
texinv = getCachedTexture([size, size, 0, 4, tex_format, "RGBA", "irregu", |
128 |
|
args + ["invert", "1"]], |
129 |
|
shade_all_levels = 1) |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
init(profiles[0]) |
134 |
|
|
135 |
|
def globalkey(k): |
136 |
|
if "F1" <= k <= "F4": |
137 |
|
global profiles |
138 |
|
init(profiles[int(k[1]) - 1]) |
139 |
|
|
140 |
|
|
|
lodbias = 0 |
|
141 |
|
|
142 |
# alpha(y) = e / y * f >= e |
# alpha(y) = e / y * f >= e |
143 |
# y <= f |
# y <= f |
150 |
def vecadd(u, v): return [ u[i] + v[i] for i in range(0, len(u)) ] |
def vecadd(u, v): return [ u[i] + v[i] for i in range(0, len(u)) ] |
151 |
def vecsub(u, v): return [ u[i] - v[i] for i in range(0, len(u)) ] |
def vecsub(u, v): return [ u[i] - v[i] for i in range(0, len(u)) ] |
152 |
|
|
153 |
def getIrregu(texid, x0, y0, x1, y1, outer = 0, texscale = 1, texangle = 0): |
def getIrregu(invert, x0, y0, x1, y1, outer = 0, texscale = 1, texangle = 0): |
154 |
|
|
155 |
|
if invert: |
156 |
|
texid = texinv.getTexId() |
157 |
|
else: |
158 |
|
texid = tex.getTexId() |
159 |
|
|
160 |
len = sqrt((x1 - x0)**2 + (y1 - y0)**2) |
len = sqrt((x1 - x0)**2 + (y1 - y0)**2) |
161 |
|
|
162 |
m = 0.5 * ripple_scale * texscale / len |
m = 0.5 * ripple_scale * texscale / len |
163 |
vec = (m * (y1 - y0), -m * (x1 - x0), 0, 0) # cw90 |
nvec = (m * (y1 - y0), -m * (x1 - x0), 0, 0) # cw90 |
164 |
|
|
165 |
vert0 = js(vecmul(vecsub((x0,y0,0,1),vec), w0)) |
vert0 = js(vecmul(vecsub((x0,y0,0,1),nvec), w0)) |
166 |
vert1 = js(vecmul(vecadd((x0,y0,0,1),vec), w1)) |
vert1 = js(vecmul(vecadd((x0,y0,0,1),nvec), w1)) |
167 |
vert2 = js(vecmul(vecsub((x1,y1,0,1),vec), w0)) |
vert2 = js(vecmul(vecsub((x1,y1,0,1),nvec), w0)) |
168 |
vert3 = js(vecmul(vecadd((x1,y1,0,1),vec), w1)) |
vert3 = js(vecmul(vecadd((x1,y1,0,1),nvec), w1)) |
169 |
|
|
170 |
tex0 = js(vecmul((x0,y0,0,texscale), w0)) |
tex0 = js(vecmul((x0,y0,0,texscale), w0)) |
171 |
tex1 = js(vecmul((x0,y0,0,texscale), w1)) |
tex1 = js(vecmul((x0,y0,0,texscale), w1)) |
214 |
else: |
else: |
215 |
code += texenv_inner % locals() |
code += texenv_inner % locals() |
216 |
|
|
217 |
|
if outer and texenv_inner == texenv_outer: |
218 |
|
code += """ |
219 |
|
PushMatrix |
220 |
|
Translate %s |
221 |
|
""" % js(vecmul(nvec[:3], 24. / scale)) |
222 |
|
|
223 |
alpha = alphalevel |
alpha = alphalevel |
224 |
code += """ |
code += """ |
225 |
# Create edge stencil |
# Create edge stencil |
259 |
|
|
260 |
""" % locals() |
""" % locals() |
261 |
|
|
262 |
|
if outer and texenv_inner == texenv_outer: |
263 |
|
code += """ |
264 |
|
PopMatrix |
265 |
|
""" |
266 |
|
|
267 |
return code |
return code |
268 |
|
|
269 |
class IrreguScene: |
class IrreguScene: |
275 |
def scene(self, vs): |
def scene(self, vs): |
276 |
putnoc(vs, background((0.7,0.7,1.0))) |
putnoc(vs, background((0.7,0.7,1.0))) |
277 |
|
|
278 |
|
global scale |
279 |
|
scale = self.scale |
280 |
|
|
281 |
cs3 = vs.coords.affineCoordsys(0, 0, 512, 384, self.scale, 0, 0, self.scale) |
cs3 = vs.coords.affineCoordsys(0, 0, 512, 384, self.scale, 0, 0, self.scale) |
282 |
vs.matcher.add(cs3, "3") |
vs.matcher.add(cs3, "3") |
283 |
|
|
315 |
PopAttrib |
PopAttrib |
316 |
""" % locals() |
""" % locals() |
317 |
|
|
|
i0 = tex.getTexId() |
|
|
i1 = texinv.getTexId() |
|
|
|
|
318 |
vs.map.put(getDListNocoords(""" |
vs.map.put(getDListNocoords(""" |
319 |
PushMatrix |
PushMatrix |
320 |
|
|
321 |
MultMatrix 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 |
MultMatrix 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 |
322 |
""")) |
""")) |
323 |
|
|
324 |
vs.map.put(getDList(getIrregu(i0,x0,-1,x1,-1,1, texscale, self.angle) + |
vs.map.put(getDList(getIrregu(0,x0,-1,x1,-1,1, texscale, self.angle) + |
325 |
getIrregu(i0,+1,y0,+1,y1,1, texscale, self.angle) + |
getIrregu(0,+1,y0,+1,y1,1, texscale, self.angle) + |
326 |
getIrregu(i1,x1,+1,x0,+1,1, texscale, self.angle) + |
getIrregu(1,x1,+1,x0,+1,1, texscale, self.angle) + |
327 |
getIrregu(i1,-1,y1,-1,y0,1, texscale, self.angle) + |
getIrregu(1,-1,y1,-1,y0,1, texscale, self.angle) + |
328 |
"Color 0 0 0" + paint), cs3) |
"Color 0 0 0" + paint), cs3) |
329 |
|
|
330 |
vs.map.put(getDList(getIrregu(i0,x0,-1,x1,-1,0, texscale, self.angle) + |
vs.map.put(getDList(getIrregu(0,x0,-1,x1,-1,0, texscale, self.angle) + |
331 |
getIrregu(i0,+1,y0,+1,y1,0, texscale, self.angle) + |
getIrregu(0,+1,y0,+1,y1,0, texscale, self.angle) + |
332 |
getIrregu(i1,x1,+1,x0,+1,0, texscale, self.angle) + |
getIrregu(1,x1,+1,x0,+1,0, texscale, self.angle) + |
333 |
getIrregu(i1,-1,y1,-1,y0,0, texscale, self.angle) + |
getIrregu(1,-1,y1,-1,y0,0, texscale, self.angle) + |
334 |
"Color 1 1 1" + paint), cs3) |
"Color 1 1 1" + paint), cs3) |
335 |
|
|
336 |
vs.map.put(getDListNocoords(""" |
vs.map.put(getDListNocoords(""" |