实现使用统一Sampler2D(lwjgl)的片段着色器

时间:2012-02-22 22:28:03

标签: opengl lwjgl fragment-shader

我无法成功运行着色器,我似乎错过了一些让它全部工作的步骤。我最终得到了错误:

Exception in thread "main" org.lwjgl.opengl.OpenGLException: Invalid operation (1282)
    at org.lwjgl.opengl.Util.checkGLError(Util.java:59)
    at org.lwjgl.opengl.GL20.glUniform1i(GL20.java:374)
    at sprites.Sprite.draw(Sprite.java:256)
    at gui.Game.drawFrame(Game.java:238)
    at gui.Game.gameLoop(Game.java:205)
    at gui.Game.startGame(Game.java:244)
    at tests.simple.SimpleShader.main(SimpleShader.java:36)

我的初始化始于:

int frag = FilterLoader.createShader("/tests/resources/shaders/grayscale.frag", GL20.GL_FRAGMENT_SHADER);

,createShader方法如下所示:

int shader = GL20.glCreateShader(type);

if(shader == 0)
    return 0;

StringBuilder code = new StringBuilder("");
String line;

try
{
    String path = FilterLoader.class.getResource(filename).getPath();
    BufferedReader reader = new BufferedReader(new FileReader(path));

    while((line = reader.readLine()) != null)
    {
        code.append(line + "\n");
    }
}
catch(Exception e)
{
    e.printStackTrace();
    System.err.println("Error reading in " + type + " shader");
    return 0;
}

GL20.glShaderSource(shader, code);
GL20.glCompileShader(shader);

return shader;

然后我将着色器附加到特定的Sprite:

two.addFragmentShader(frag); //two is a Sprite

只是简单地说:

fragmentShader = fragment_shader;
GL20.glAttachShader(shader, fragment_shader);
GL20.glLinkProgram(shader);

int 着色器之前已在Sprites构造函数中初始化:

shader = GL20.glCreateProgram();

这是以前的问题,但不再明显。现在我到达实际错误的位置,在Sprites(本例中为两个)draw方法中,看起来像这样:

if(true)
{
    GL20.glUseProgram(shader);
}

glPushMatrix();

glActiveTexture(GL13.GL_TEXTURE0);
imageData.getTexture().bind();

//The line below is where the error occurs.
GL20.glUniform1i(fragmentShader, GL13.GL_TEXTURE0);

int tx = (int)location.x;
int ty = (int)location.y;
glTranslatef(tx, ty, location.layer);


float texture_X = ((float)which_column/(float)columns);
float texture_Y = ((float)which_row/(float)rows);
float texture_XplusWidth = ((float)(which_column+wide)/(float)columns);
float texture_YplusHeight = ((float)(which_row+tall)/(float)rows);

glBegin(GL_QUADS);
{
    GL11.glTexCoord2f(texture_X, texture_Y);
    glVertex2f(0, 0);

    GL11.glTexCoord2f(texture_X, texture_YplusHeight);
    glVertex2f(0, getHeight());

    GL11.glTexCoord2f(texture_XplusWidth, texture_YplusHeight);
    glVertex2f(getWidth(), getHeight());

    GL11.glTexCoord2f(texture_XplusWidth, texture_Y);
    glVertex2f(getWidth(), 0);
}
glEnd();
GL20.glUseProgram(0);
glPopMatrix();

错误发生在这一行:

GL20.glUniform1i(fragmentShader, GL13.GL_TEXTURE0);

参考我的着色器:

// simple fragment shader

uniform sampler2D texture;

void main()
{
    vec4 color, texel;
    color = gl_Color;
    texel = texture2DRect(texture, gl_TexCoord[0].xy);
    color *= texel;

    float gray = dot(color.rgb, vec3(0.299, 0.587, 0.144));

    gl_FragColor = vec4(gray, gray, gray, color.a);
}

我已经阅读了教程,了解了错误,我无法弄清楚我错过了哪一步。

1 个答案:

答案 0 :(得分:7)

GL20.glUniform1i(fragmentShader, GL13.GL_TEXTURE0);

这是错误的。 glUniform1i的第一个参数是统一位置,您可以使用glGetUniformLocation获得该位置。

第二个参数是整数,但对于纹理采样器,您需要传递纹理单元编号(0,1,2等),并将纹理绑定到该纹理单元,例如:

glUseProgram(program);
int loc = glGetUniformLocation(program, "texture");
glUniform1i(loc, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texId);

然后它应该工作。