Somehow using GL_REPEAT on a sub-texture in a sprite-sheet?

Hello.

I have been researching for a bit on how to make a section of a texture render in GL_REPEAT. Is there any way to do this other than just manually repeating the sub-texture by drawing the sub-texture multiple times? The farthest I have gotten is Array Textures, but I can’t make out if they are good for sprite-sheets, or compatible enough. Then people keep referring to using shaders to map correctly, but I can’t really work out what I am supposed to do in said shader.

Are there any options I am missing here, or is manual repeating the only logical/compatible way?

Thanks,
-wes

Use shaders.

I would imagine that if you were to sample from the range u=[0.1 … 0.4] and v=[0.7 … 0.8], you’d do something like this in the fragment shader:


vec2 uv = ...;

float uMin = 0.1;
float uMax = 0.4;
// map [-? .. +?] to [0.1 .. 0.4]
float u = uv.x; // -? .. +?
u = fract(u); // 0.0 .. 1.0
u *= uMax - uMin; // 0.0 .. 0.3
u += uMin; // 0.1 .. 0.4

float vMin = 0.7;
float vMax = 0.8;
// map [-? .. +?] to [0.7 .. 0.8]
float v = uv.y; // -? .. +?
v = fract(v); // 0.0 .. 1.0
v *= vMax - vMin; // 0.0 .. 0.1
v += vMin; // 0.7 .. 0.8

uv = vec2(u, v);

Or for short: (again, totally untested)


vec2 uv = ...;

vec2 min = vec2(0.1, 0.7); // notice the
vec2 max = vec2(0.4, 0.8); //    transposed values

uv = min + fract(uv) * (max - min);

After that you’d use texcoords in the range [0-1] to sample your sub-region, and use a wider range to cause texture-repeat.

I agree with @Riven, the only problem (that I can see) is that the uMin, uMax etc variables would have to be passed as uniforms. This means that you could only batch together draws of the same sub-image rather than of the same texture, which I think is quite a big part of the bonus from using texture atlases. Still better than individual textures though.

The only way around this (that I can see) is to use texture atlases but as @wessles says, they are only so useful.

Just pass them in as vertex attributes… using four 16 bit normalized unsigned ints - if you’re prematurely optimizing and think four 32 bit floats are going to totally kill the framerate.

Having said that, you’re probably going to suffer from poor sampling results due to mipmapping and color bleeding. Just go with GL_TEXTURE_2D_ARRAY… as it solves almost all your problems, and introduces only a few.

If you do this and want mipmaps to function, you’ll get discontinuities in your texture coordinates, so you need to manually calculate your texture gradients from the original texture coordinates:


uniform vec2 uvMin;
uniform vec2 uvMax;

in vec2 texCoords;

out vec4 fragColor;

void main(){
    vec2 modifiedTexCoords = uvMin + fract(texCoords) * (uvMax - uvMin);
    fragColor = textureGrad(mySampler, modifiedTexCoords, dFdx(texCoords), dFdy(texCoords));
}

Can’t you upscale your sprites to the next POT?

Make a GL_TEXTURE_2D_ARRAY for each POT, bind all textures, and pick a sampler in your fragment shader, using a vertex attribute.

I don’t see how that is relevant. Just scale the sub-regions to POT and upload these (tiny) regions into texture-array layers.

If the hardware supports texture arrays, it also supports non-power-of-two textures with filtering.

Yes.

I’d think nPOT texture arrays have very limited usability in the context of replacing a sprite atlas, as all layers in the texture array have to have the same dimensions. It’s not very likely that all your sprites are exactly the same size - whether that is POT or nPOT - so you’re basically going to have to scale your sprites to some dimension, and in that case POT makes most sense.

With a set of Texture Arrays, you’d do this:

[icode]passing vertex attributes[/icode]


	glTexCoord4f(u, v, texArraySlice, texArrayTexUnit);
	glVertex3f(x, y, z);

[icode]vertex shader[/icode]


#version 330

varying      vec2 texCoord;
varying flat int  texSlice;
varying flat int  texUnit;

void main(){
    gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xy, 0, 1);
    texCoord =     gl_MultiTexCoord0.xy;
    texSlice = int(gl_MultiTexCoord0.z);
    texUnit  = int(gl_MultiTexCoord0.w);
}

[icode]fragment shader[/icode]


#version 330

varying      vec2 texCoord;
varying flat int  texSlice;
varying flat int  texUnit;

uniform sampler2DArray samplerSet[4];

void main() {
	vec3 texcoord = vec3(texCoord, float(texSlice));
	
	vec4 texel; // AFAIK, cannot do this more efficiently :o(
	     if(texUnit == 0) texel = texture(samplerSet[0], texcoord);
	else if(texUnit == 1) texel = texture(samplerSet[1], texcoord);
	else if(texUnit == 2) texel = texture(samplerSet[2], texcoord);
	else if(texUnit == 3) texel = texture(samplerSet[3], texcoord);
	else                  texel = vec4(1.0, 0.0, 1.0, 1.0); // pretty purple means broken!
	
	gl_FragColor = texel;
}

http://pastebin.java-gaming.org/4cd0b7b12081a