Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  [GLSL] Making a Texture Atlas repeat.  (Read 3374 times)
0 Members and 1 Guest are viewing this topic.
Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Posted 2012-12-16 18:24:33 »

I want to produce the same effect GL_REPEAT does, but with a texture atlas. This means the texture is not one big texture in opengl, but only a part of a big texture sheet.

I want to accomplish this with GLSL, since I already use it and think it's the only way.
I've tried this but this only scales the texture, and I'm not sure what "offset" is...

I would have following inputs in my fragment shader:
the uniform texture,
the varying texture coord as a vec2,
and an attribute vec2 "repeat", which is for example 4, if the user wants to tile the texture 4 times over the surface.


Why that way, why not
  • Simply use one texture
   Answer: I'd need to put lots of floor textures into seperate textures, and I'd need to call glBindTexture a lot, which would kill the performance...
  • Simply use more quads:
   This kills performance too, since a repeating texture would have exactly the same effect and would need much less vertex power.

Anyways: If there is another way than GLSL and usual texture sheets, please educate me... I've thought about texture arrays, is that possible?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #1 - Posted 2012-12-16 18:54:17 »

Tip: the fract() function. It takes in a floating point number and returns (x - floor(x)), which is a the decimal value between 0 and 1. The rest depends on how your texture atlas looks.

Myomyomyo.
Offline Roquen
« Reply #2 - Posted 2012-12-16 20:37:54 »

by decimal we mean floating point
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2012-12-16 20:39:09 »

by decimal we mean floating point

Yes... but I still couldn't figure it out :/

I still have to fiddle around with the input I give to my shaders, since only giving a [ïcode]vec2 repeat[/icode] is not enough...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #4 - Posted 2012-12-16 21:27:14 »

Something like this (kind of psuedo code)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
//You can make these attributes too to avoid having to change them between draw calls.
uniform vec2 atlasCoord; //Top left corner of the atlas image
uniform vec2 atlasSize; //Size of the atlas image

attribute vec2 localTexCoords; //From 0 to 1, repeats outside that range

void main(){
    vec4 textureSample = texture2D(sampler, atlasCoord + fract(localTexCoords)*atlasSize);
    ...
}

Myomyomyo.
Offline davedes
« Reply #5 - Posted 2012-12-16 21:50:02 »

IMO its easier just to render multiple sprites. No need for extra floats per vertex or uniforms (which in most instances will just be unused anyways).

Keep in mind most games use variation, so instead of repeating the same grass tile, you end up scattering the ground with multiple grass variants.

Pushing more verts is really no big deal for OpenGL -- its designed for complex 3D scenes after all, and we're pushing a relatively small number of verts even with the whole screen filled with tiles.

Offline Roquen
« Reply #6 - Posted 2012-12-16 22:03:40 »

One thing to remember when writing you're own samplers is you gotta account for everything..in this case if you're no point-sampling the texture, then you need to remember to have a 'border' around the base texture so the proper samples are read.
(BTW: Isn't the call just 'texture' these days?)
Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2012-12-16 22:05:48 »

Ah yeah... I guess I'll just drop it, since GL_LINEAR does not work properly Smiley
I think I'll educate myself some texture arrays...



<edit>But you can see its repeating 4 times, and I fully understand what you did there now, theagentd Smiley thank you...</edit>

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #8 - Posted 2012-12-16 22:08:26 »

IMO its easier just to render multiple sprites. No need for extra floats per vertex or uniforms (which in most instances will just be unused anyways).

Keep in mind most games use variation, so instead of repeating the same grass tile, you end up scattering the ground with multiple grass variants.

Pushing more verts is really no big deal for OpenGL -- its designed for complex 3D scenes after all, and we're pushing a relatively small number of verts even with the whole screen filled with tiles.
Your point being? This shader allows you to draw all your sprites in one draw call instead of doing thousands of texture binds and draw calls.

(BTW: Isn't the call just 'texture' these days?)
Yes, but that's for OGL3+ if I remember correctly...

@matheus23
You can fix that by either disabling GL_LINEAR, using a 2D texture array with GL_CLAMP_TO_BORDER (requires OGL 3) or adding a 1 pixel border around each image.

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-12-16 22:09:41 »

@matheus23
You can fix that by either disabling GL_LINEAR, using a 2D texture array with GL_CLAMP_TO_BORDER (requires OGL 3) or adding a 1 pixel border around each image.

If I have a 2D texture array I can use GL_REPEAT anyways, can't i?...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2012-12-16 22:11:05 »

@matheus23
You can fix that by either disabling GL_LINEAR, using a 2D texture array with GL_CLAMP_TO_BORDER (requires OGL 3) or adding a 1 pixel border around each image.

If I have a 2D texture array I can use GL_REPEAT anyways, can't i?...
Ah, yes, of course. I got too fixated on eliminating the bleeding...

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #11 - Posted 2012-12-16 22:32:27 »

Are te ture arrays really just supported on ogl 3? How well is the extension supported on 2.1 drivers?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #12 - Posted 2012-12-16 22:50:27 »

Are te ture arrays really just supported on ogl 3? How well is the extension supported on 2.1 drivers?
It isn't at all. It apparently requires OpenGL 3 hardware.
http://feedback.wildfiregames.com/report/opengl/feature/GL_EXT_texture_array

Myomyomyo.
Offline davedes
« Reply #13 - Posted 2012-12-16 23:00:15 »

theagentd - as long as your sprite batcher is big enough, my solution also only leads to a single draw call and texture bind (at the expense of pushing a few more tris).

Fyi my Mac 10.6.8 (GL 2.1) supports the texture arrays extension. I wouldn't count on using it though, for maximum compatibility.

Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #14 - Posted 2012-12-16 23:10:27 »

 Undecided not supported by roughly half of all users... doesn't look good Sad

I plan on supporting ogl 2.1hardware... how would i solve the problem with the filtering then?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #15 - Posted 2012-12-17 01:32:12 »

theagentd - as long as your sprite batcher is big enough, my solution also only leads to a single draw call and texture bind (at the expense of pushing a few more tris).

Fyi my Mac 10.6.8 (GL 2.1) supports the texture arrays extension. I wouldn't count on using it though, for maximum compatibility.
That's most likely because your hardware supports OGL3 but the Mac driver does not. Exactly what graphics card do you have?

Undecided not supported by roughly half of all users... doesn't look good Sad

I plan on supporting ogl 2.1hardware... how would i solve the problem with the filtering then?
Dilate the texture. Basically expand each texture one pixel in all 4 directions by copying the color of the edge pixels one pixel out. That way the linear filtering will bleed into the same color and you won't get any artifacts. If you're using mipmaps, you'll need a much bigger border though.

Myomyomyo.
Offline Roquen
« Reply #16 - Posted 2012-12-17 07:25:20 »

copying the color of the edge pixels one pixel out
To attempt to be more clear..if the part in question is to be wrapped, then the one-pixel border needs to be a copy of the opposite (logical) edge in the top,bottom, right & left cases and the corners the opposite corner.  Generally since texture coordinates are sub-texel resolution the border needs extra data to simulate the sampling that would happen if it wasn't in an atlas.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (63 views)
2014-04-15 18:08:23

BurntPizza (61 views)
2014-04-15 03:46:01

UprightPath (74 views)
2014-04-14 17:39:50

UprightPath (57 views)
2014-04-14 17:35:47

Porlus (75 views)
2014-04-14 15:48:38

tom_mai78101 (100 views)
2014-04-10 04:04:31

BurntPizza (160 views)
2014-04-08 23:06:04

tom_mai78101 (255 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!