0
\$\begingroup\$

I'm currently working on a sfml based library where I'm implementing widgets with shaders. I'm defining an quad with position and texcoord in sfml to make the shader apply to the object like this:

quad[0].position = sf::Vector2f(100, 100);
quad[0].texCoords = sf::Vector2f(0, 0);
quad[1].position = sf::Vector2f(410, 100);
quad[1].texCoords = sf::Vector2f(1, 0);
quad[2].position = sf::Vector2f(410, 410);
quad[2].texCoords = sf::Vector2f(1, 1);
quad[3].position = sf::Vector2f(100, 410);
quad[3].texCoords = sf::Vector2f(0, 1);

This way I'll make gl_TexCoord[0].st ready to use without dividing with resolution to make it in rage [0, 1]. But when I'm adding #version 330 I get a deprection error. Since I'm using OpenGL 3.3, I'm looking to stick with the moder shader methods and to avoid deprecated stuffs.

Here's how my shader looks like and works without version definition:

void main() {
     vec2 pos = gl_TexCoord[0].st; // deprecated since #120
     gl_FragColor = vec4(pos.y, pos.x, 1.0, 1.0); // also deprecated too
 }
\$\endgroup\$
1
\$\begingroup\$

Texture coordinates use a different system. First you need a sampler2D:

uniform sampler2D sampler;

then declare an out for the color:

out vec4 out_Color;

doesn't have to have that name IIRC. But something like that. Then in the main method:

out_Color = texture(sampler, texCoords);

where texCoords is a vec2 of the texture coordinates.

Texture coordinates are passed like an attribute in the vertex file:

in vec2 a_texCoord0;

and in the vertex shader it's just passed to the fragment shader:

out texCoords;

void main(){
    ...
    texCoords = a_texCoord0;
}

and you have to bind the texture:

GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);

And in 330, attribute is in and varying is out in the vertex shader. varying in the fragment shader is in

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.