0
\$\begingroup\$

I'm trying out GLSL and one of the problems I'm facing is wrapping a random texture sampler in the shader. Searching for answers on the web first, this leads me to using these:

  • glTexParameter()

  • GL_TEXTURE_WRAP_S GL_REPEAT

  • GL_TEXTURE_WRAP_T GL_REPEAT

I'm not sure where to put this or how to use it. I'm using a custom engine for this shader and I would assume I could wrap the texture in the shader.

My main concern on this is my final output render having left and bottom artifacts. I got some previous advise it has to do with wrapping a random texture that is used for noise if that helps in my case.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ fract (tex_coord.st) will do the same thing, this is not particularly useful knowledge but I thought you might like to know ;) \$\endgroup\$ Aug 24, 2015 at 6:03
  • \$\begingroup\$ Thanks Andon, how do I assign this to the texture? I'm declaring uniform sampler2D randtex0 as the texture I would like to wrap and not clamp. \$\endgroup\$
    – scottyp
    Aug 25, 2015 at 0:02
  • \$\begingroup\$ My main concern on this is my final output render having left and bottom artifacts. I got some previous advise it has to do with wrapping a random texture that is used for noise if that helps in my case. \$\endgroup\$
    – scottyp
    Aug 25, 2015 at 0:08
  • 1
    \$\begingroup\$ Oh, really? Can you link to a screenshot illustrating your problem? Those artifacts almost certainly come from linear texture filtering, when you use linear filtering you have to be extremely careful with coordinates at the edges, especially if the texture is a spritesheet / atlas. \$\endgroup\$ Aug 25, 2015 at 0:11
  • \$\begingroup\$ thanks, I have to zoom in and take each area. here it is Left and Bottom screenshots. I did hear some issues about this and I was hoping for a solution. \$\endgroup\$
    – scottyp
    Aug 25, 2015 at 0:30

2 Answers 2

5
\$\begingroup\$

For people who come here from Google (like me), the shader version is:

fract (tex_coord.st)

this will loop coordinates in range 0.0 - 1.0

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wrapping your texture coordinates doesn't help with the problem that sampling at 0.0 or 1.0 with bilinear filtering will get you the average of the opposing pair of edge texels. Texel centers for a 4px image are at u=1/5, 2/5, 3/5, 4/5. \$\endgroup\$ Feb 3, 2016 at 12:57
4
\$\begingroup\$

You don't set these values in the shader. You set them in your program, generally when first creating the texture (though it can be set or changed whenever).

If you've already uploaded your texture data to OpenGL then all you have to do is this:

First bind the texture if it is not already.

glBindTexture(GL_TEXTURE_2D, texture_id);

Then set the parameters.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
\$\endgroup\$
3
  • \$\begingroup\$ Ok I see, I was thinking more of a direct shader approach like Andon commented above but thank you for this info. \$\endgroup\$
    – scottyp
    Aug 25, 2015 at 0:05
  • 1
    \$\begingroup\$ Just curious - why do you want to do it in the shader if OpenGL will do it for you? The less you have to write in your shader, the less that can go wrong with it and that you have to maintain. If you put it in the shader, it will likely be confusing to anyone (including you) maintaining the code in the future when they set the texture parameter and it doesn't change. \$\endgroup\$ Aug 25, 2015 at 3:50
  • \$\begingroup\$ Actually this is a quick post process filter that I converted and just needs minimal settings and technically all the work is done through the shader file except for material and other shaders. \$\endgroup\$
    – scottyp
    Aug 26, 2015 at 7:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .