将纹理传递给GLSL

时间:2016-06-02 14:20:13

标签: python opengl glsl textures shader

我正在尝试在着色器中读取纹理。而且我只得到一个统一的屏幕(似乎只读取了一个值)。如果我评论着色器线声明(64到74),我的纹理会正确显示。所以我猜它是正确宣布的。

这是我的代码:

#!/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()

有人有任何想法吗?

1 个答案:

答案 0 :(得分:1)

对不起伙计们, 我刚刚找到了答案。 以下是我将使用的着色器线:

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;
}
"""

非常感谢:),

麦克