Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Somehow using GL_REPEAT on a sub-texture in a sprite-sheet?  (Read 5368 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles
« Posted 2014-07-09 16:55:34 »

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
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-07-09 18:14:13 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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)
1  
2  
3  
4  
5  
6  
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline quew8

JGO Knight


Medals: 53



« Reply #2 - Posted 2014-07-09 18:18:12 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-07-09 18:18:49 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline theagentd
« Reply #4 - Posted 2014-07-09 18:55:05 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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));
}

Myomyomyo.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2014-07-09 19:13:00 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2014-07-09 20:15:34 »

No, I already upscaled the parental textures to PoT.
I don't see how that is relevant. Just scale the sub-regions to POT and upload these (tiny) regions into texture-array layers.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline theagentd
« Reply #7 - Posted 2014-07-09 21:55:19 »

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

Myomyomyo.
Offline theagentd
« Reply #8 - Posted 2014-07-09 22:03:52 »

Yes.

Myomyomyo.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2014-07-10 05:33:18 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2014-07-10 12:48:15 »

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

passing vertex attributes

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


vertex shader

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
#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);
}


fragment shader

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
#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


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (439 views)
2019-09-04 15:33:17

hadezbladez (5294 views)
2018-11-16 13:46:03

hadezbladez (2213 views)
2018-11-16 13:41:33

hadezbladez (5562 views)
2018-11-16 13:35:35

hadezbladez (1156 views)
2018-11-16 13:32:03

EgonOlsen (4592 views)
2018-06-10 19:43:48

EgonOlsen (5467 views)
2018-06-10 19:43:44

EgonOlsen (3127 views)
2018-06-10 19:43:20

DesertCoockie (4023 views)
2018-05-13 18:23:11

nelsongames (4739 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!