简体   繁体   English

在OpenGL中的片段着色器中获取原始纹理颜色

[英]Get original texture color in a Fragment Shader in OpenGL

So, I need to make a shader to replace the gray colors in the texture with a given color. 所以,我需要制作一个着色器,用给定的颜色替换纹理中的灰色。 The fragment shader works properly if I set the color to a given specific one, like 如果我将颜色设置为给定的特定颜色,片段着色器可以正常工作

gl_FragColor = vec4(1, 1, 0, 1);

However, I'm getting an error when I try to retrieve the original color of the texture. 但是,当我尝试检索纹理的原始颜色时,我收到错误。 It always return black, for some reason. 由于某种原因,它总是返回黑色。

uniform sampler2D texture; //texture to change

void main() {
  vec2 coords = gl_TexCoord[0].xy;
  vec3 normalColor = texture2D(texture, coords).rgb; //original color

  gl_FragColor = vec4(normalColor.r, normalColor.g, normalColor.b, 1);
}

Theoretically, it should do nothing - the texture should be unchanged. 从理论上讲,它应该什么也不做 - 纹理应该保持不变。 But it gets entirely black instead. 但它完全变黑了。 I think the problem is that I'm not sure how to pass the texture as a parameter (to the uniform variable). 我认为问题在于我不确定如何将纹理作为参数传递给统一变量。 I'm currently using the ID (integer), but it seems to return always black. 我目前正在使用ID(整数),但似乎总是返回黑色。 So I basically don't know how to set the value of the uniform texture (or to get it in any other way, without using the parameters). 所以我基本上不知道如何设置均匀纹理的值(或者以任何其他方式获取它,而不使用参数)。 The code (in Java): 代码(用Java):

program.setUniform("texture", t.getTextureID());

I'm using the Program class, that I got from here , and also SlickUtils Texture class, but I believe that is irrelevant. 我正在使用我从这里获得的Program类,以及SlickUtils Texture类,但我相信这是无关紧要的。

program.setUniform("texture", t.getTextureID());
                              ^^^^^^^^^^^^^^^^

Nope nope nope. 不,不,不。

Texture object IDs never go in uniforms. 纹理对象ID 永远不会穿制服。

Pass in the index of the texture unit you want to sample from. 传入要从中采样的纹理单元的索引。

So if you want to sample from the n th texture unit ( GL_TEXTURE0 + n ) pass in n : 所以如果你想从第n个纹理单元( GL_TEXTURE0 + n )中取样,则传入n

program.setUniform("texture", 0);
                              ^ or whatever texture unit you've bound `t` to

In addition to what genpfault said, when you say "replace the gray colors in the texture with a given color", that had better be a shorthand for "write the color from one texture to another, except replacing gray with a different color". 除了genpfault所说的,当你说“用给定的颜色替换纹理中的灰色”时, 最好是“将颜色从一种纹理写入另一种纹理,而不是用不同的颜色替换灰色”的简写。 Because you are not allowed to simultaneously read from and write to the same image in the same texture . 因为不允许同时在同一纹理中读取和写入同一图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM