passing textures to GLSL
Question: I'm trying to read a texture in a shader. And I only get a uniform screen
(only one value seems to be read). If I comment the shader lines declaration
(64 to 74), my texture is displayed correctly. So I guess it is correctly
declared.

here is my code:

    
    
    #!/usr/bin/python
    
    import sys
    import numpy as np
    
    try:
       from OpenGL.GL      import *
        from OpenGL.GL      import shaders
        from OpenGL.GLU     import *
        from OpenGL.GLUT    import *
    except:
        print '''ERROR: PyOpenGL not installed properly.'''
    
    ################################################################################
    # GLOBALS
    
    screen_w = 800
    screen_h = 600
    
    
    ################################################################################
    # SHADERS
    
    f_shader = """
    #version 120
    uniform sampler2D texture_w;
    void main() {
        vec2 c = vec2(int(gl_FragCoord[0]),int(gl_FragCoord[1]));
        vec4 t = texture2D(texture_w, c);
        gl_FragColor = t;
    }
    """
    
    v_shader = """
    #version 120
    void main() {
        gl_FrontColor = gl_Color;
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }
    """
    
    
    ################################################################################
    # FUNCS
    
    def setup():
        glEnable(GL_DEPTH_TEST)
    
        FRAGMENT_SHADER = shaders.compileShader(f_shader, GL_FRAGMENT_SHADER)
        VERTEX_SHADER = shaders.compileShader(v_shader, GL_VERTEX_SHADER)
    
    
        # Weight texture declaration
        img_data = np.random.rand(screen_w, screen_h, 3)
        img_data2 = img_data*255.0
    
        texture = glGenTextures(1)
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        glBindTexture(GL_TEXTURE_2D, texture)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_w, screen_h, 0, GL_RGB,     GL_UNSIGNED_BYTE, img_data2)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    
    
        shader = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
        shaders.glUseProgram(shader)
        u_loc = glGetUniformLocation(shader, "texture_w")
        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_2D, texture)
        glUniform1i(u_loc, 0)
    
    
    
    def display():
        "Global Display function"
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    
        glEnable(GL_TEXTURE_2D)
        glColor(1,1,1)
        glBegin(GL_TRIANGLES)
        glTexCoord2f(0, 0)
        glVertex(-1,-screen_w/float(screen_h),-2)
        glTexCoord2f(1, 1)
        glVertex(1,screen_w/float(screen_h),-2)
        glTexCoord2f(0, 1)
        glVertex(-1,screen_w/float(screen_h),-2)
    
        glTexCoord2f(0, 0)
        glVertex(-1,-screen_w/float(screen_h),-2)
        glTexCoord2f(1, 0)
        glVertex(1,-screen_w/float(screen_h),-2)
        glTexCoord2f(1, 1)
        glVertex(1,screen_w/float(screen_h),-2)
    
        glEnd()
        glutSwapBuffers()
    
    
    def reshape (w, h):
        global screen_w, screen_h
        glViewport (0, 0, w, h)
        screen_w, screen_h = w, h
    
        glMatrixMode (GL_PROJECTION)
        glLoadIdentity ()
        glOrtho(-1, 1, -float(w)/float(h), float(w)/float(h), 1, 10)
        glMatrixMode (GL_MODELVIEW)
    
    
    def keyboard(key, x, y):
        global mode
        if key == chr(27):
            sys.exit(0)
        elif key == 'f':
            glutFullScreen()
        else:
            print key
    
    
    ################################################################################
    # MAIN
    
    glutInit(sys.argv)
    glutInitDisplayString("double rgba depth samples=4")
    glutInitWindowSize (screen_w, screen_h)
    glutCreateWindow ('Weights')
    
    setup()
    
    glutDisplayFunc(display)
    glutReshapeFunc(reshape)
    glutKeyboardFunc(keyboard)
    glutMainLoop()
    

Does someone has any idea ?


Answer: Sorry guys, I just found the answer. Here are the shaders lines I'll use:

    
    
    f_shader = """
    #version 120
    uniform sampler2D texture_w;
    void main() {
        gl_FragColor = texture2D(texture_w, gl_TexCoord[0].xy);
    }
    """
    
    v_shader = """
    #version 120
    void main() {
        gl_FrontColor = gl_Color;
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        gl_TexCoord[0] = gl_MultiTexCoord0;
    }
    """
    

Thanks anyway :),

mike

