Get the original texture color in the fragment shader in OpenGL

So, I need to make a shader to replace the gray colors in the texture with the given color. The fragment shader works correctly if I set the color to a specific concrete one, for example

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

However, I get an error when I try to get the original color of the texture. For some reason, it always returns black.

 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 not do anything - the texture should be unchanged. But instead, it turns completely black. I think the problem is that I'm not sure how to pass the texture as a parameter (to a uniform variable). I am currently using ID (integer), but it seems to always return to black. Therefore, in principle, I do not know how to set the value of a homogeneous texture (or get it in any other way, without using parameters). Code (in Java):
 program.setUniform("texture", t.getTextureID()); 

I use the class of the program that I got from here , as well as the SlickUtils texture class, but I believe that it does not matter.

+4
source share
2 answers
 program.setUniform("texture", t.getTextureID()); ^^^^^^^^^^^^^^^^ 

No no.

Texture object identifiers never enter a form.

Go to the index of the texture unit you want to select.

So, if you want to select from n th texture units ( GL_TEXTURE0 + n ), go to n :

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

In addition to what genpfault said when you say "replace the gray colors in the texture with the given color," it would be better to shorten the "write color from one texture to another, except for replacing gray with another color." Since you are not allowed to read and write to the same image in the same texture at the same time .

0
source

Source: https://habr.com/ru/post/1485822/


All Articles