Hi,

Using the shader wrapper by Tristam ('GLSL wrapper for Pyglet') and
explanations from 
http://www.gamerendering.com/2008/10/05/bilinear-interpolation/,
I wrote a bilinear filter (code below).

Unfortunately, there seems to be some errors (at least on my machine)
when texture is zoomed in. Some pixels seem to get the wrong neighbors
and result in odd lines. A quick and dirty hack (that fail with
bicubic filter) is commented within the shader code, but I would like
to know what I'm doing wrong. If someone can test the code and tell me
if he gets the same odd lines.

Nicolas

[code]:

import numpy
import pyglet
from pyglet.gl import *
from shader import Shader

window = pyglet.window.Window(800, 800, resizable=True, visible=False)

# Bilinear interpolation
# http://www.gamerendering.com/2008/10/05/bilinear-interpolation/
shader = Shader(['''
    void main() {
        gl_TexCoord[0] = gl_MultiTexCoord0;
        gl_Position = ftransform();
}
'''], ['''
    uniform sampler2D image;
    uniform vec2 pixel;
    void main() {
        // retrieve the texture coordinate
        vec2 c = gl_TexCoord[0].st;
        vec4 tl = texture2D(image, c);
        // Dirty hack
        // vec4 tr = texture2D(image, c+pixel*vec2(1,0)*.99);
        // vec4 bl = texture2D(image, c+pixel*vec2(0,1)*.99);
        // vec4 br = texture2D(image, c+pixel*vec2(1,1)*.99);
        vec4 tr = texture2D(image, c+pixel*vec2(1,0));
        vec4 bl = texture2D(image, c+pixel*vec2(0,1));
        vec4 br = texture2D(image, c+pixel*vec2(1,1));
        vec2 f = fract(c/pixel);
        float a = mix(mix(tl, tr, f.x), mix(bl, br, f.x), f.y).a;
        gl_FragColor = vec4 (a,a,a,1);
    }
'''])

Z = numpy.random.random((8,8)).astype(numpy.float32)
tex_id = GLuint()
glGenTextures(1, byref(tex_id))
tex_id = tex_id.value
glBindTexture (GL_TEXTURE_2D, tex_id)
glPixelStorei (GL_UNPACK_ALIGNMENT, 1)
glPixelStorei (GL_PACK_ALIGNMENT, 1)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA16, Z.shape[0], Z.shape[1],
              0, GL_ALPHA, GL_FLOAT, Z.ctypes.data)

shader.bind()
shader.uniformi('image', 0)
shader.uniformf('pixel', 1./Z.shape[0], 1./Z.shape[1])
shader.unbind()

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glColor4f(1,1,1,1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex_id)

@window.event
def on_draw():
    window.clear()
    shader.bind()
    x,y,w,h = 0,0,window.width, window.height
    glBegin(GL_QUADS)
    glTexCoord2f(0,1), glVertex2f(x,   y)
    glTexCoord2f(0,0), glVertex2f(x,   y+h)
    glTexCoord2f(1,0), glVertex2f(x+w, y+h)
    glTexCoord2f(1,1), glVertex2f(x+w, y)
    glEnd()
    shader.unbind()

window.set_visible(True)
pyglet.app.run()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to