Jump to content
  • Advertisement
Sign in to follow this  

2D Texture Arrays

This topic is 2724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm trying to implement a 2D Texture Array to use to display the tiles in my 2D game.
I've setup a VBO, loaded in a shader program and I have been able to get to the point where I can display coloured quads on the screen with no problems at all. I'm now trying to get the texturing working.

I have an ImageLoader class that handles the conversion from Bitmap to GL Texture.

I have a 512x512 tileset, I split it into 32x32 chunks, and feed the resulting bytes into one long byte array. I've been able to use this same byte array in a 3D Texture but the 3D textures interpolated between layers so I was getting blending between transparent textures so I'm now trying 2D Texture Arrays.

Now, my problem is that I cant seem to get the texturing working no matter what I try. I've attached a picture of the output, It flickers and what you see in the picture is only there for a couple of milliseconds then disappears. Picture Here

Here's a snippet from the texture generation, am I doing this right?(F.Y.I, it's in C# so it may look different to what you are used to)

newTexture = GL.GenTexture();
TextureTarget target = TextureTarget.Texture2DArray;
GL.BindTexture(target, newTexture);





GL.TexImage3D(target,0,PixelInternalFormat.Rgba,32, 32, counter,0,PixelFormat.Bgra,PixelType.UnsignedByte,byteArrayPointer);

When I'm rendering the VBO I bind the texture that GL gave me here,

GL.BindTexture(TextureTarget.Texture2DArray, this.TileSet.TextureID);




GL.BindBuffer(BufferTarget.ArrayBuffer, layerGraphicsCollection.VertexBufferObject);
GL.VertexPointer(2, VertexPointerType.Float, 0, IntPtr.Zero);

GL.BindBuffer(BufferTarget.ArrayBuffer, layerGraphicsCollection.ColourBufferObject);
GL.ColorPointer(4, ColorPointerType.UnsignedByte, 0, IntPtr.Zero);

GL.BindBuffer(BufferTarget.ArrayBuffer, layerGraphicsCollection.TextureBufferObject);
GL.TexCoordPointer(3, TexCoordPointerType.Float, 0, IntPtr.Zero);

GL.BindBuffer(BufferTarget.ElementArrayBuffer, layerGraphicsCollection.ElementBufferObject);

GL.DrawElements(BeginMode.Quads, layerGraphicsCollection.LayerVerticies.Indices.Length,

DrawElementsType.UnsignedInt, IntPtr.Zero);




Also my Fragment Shader looks like this

uniform sampler2DArray tex;

void main()

vec3 coord;
coord.x = gl_TexCoord[0].s;
coord.y = gl_TexCoord[0].t;
coord.z = 0.1; //testing
vec4 color = texture2DArray(tex, coord.xyz);

gl_FragColor = color;


It's a 2D Game so my Vertex Storage Class looks something like this

Vector2[] vertices;
Vector3[] texcoords;
private int[] indicies;
private int[] colors;

There really isn't that much on 2D Texture Arrays so I'm a bit stumped.

Share this post

Link to post
Share on other sites
I've not used texture arrays myself yet, but you seem to just pass one texture atlas to the shader. So you always get the same layer, no matter what you set coord.z to. (From the extension description: layer = max (0, min(d - 1, floor (coord.p + 0.5)) where d = num_layers and coord.z = coord.z in your case, so max(0, min(1-1, floor(0.1 + 0.5)) would always return 0 ).

You said you use a texture atlas, so there's no need to use texture2DArray at all.
Keep in mind that each tile needs its own texture coordinates, so vertices can't be shared between tiles.

Share this post

Link to post
Share on other sites
I tried to use a texture atlas but I could never get it to have linear filtering without having seams.

The 0.1 in coord.Z is just testing. I still get the same output if I set it to anything greater than one.

My textures are in a atlas at startup but my program splits the tiles up and reads them into a byte array which gets sent to the gpu.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!