Sign in to follow this  
Misantes

OpenGL OpenGL texture array difficulties

Recommended Posts

Hello,

Referencing from here: http://www.opengl.org/wiki/Array_Texture, I've been trying to implement a texture array in my program. Unfortunately, I'm going wrong somewhere, but after a couple days of trying to figure out where, I'm a little stumped.

 

There is no error reported, and the objects themselves render fine (just without textures, so the world is a bunch of black/grey objects lit by the lighting, but minus any sort of textures).

 

A simplified version of the functions are roughly:

  

GLuint TextureArray;

glGenTextures(1, &TextureArray);
glBindTexture(GL_TEXTURE_2D_ARRAY, TextureArray);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 500, 500, 2);//last three numbers are size of images and array

LoadTexture("prettypicture.png", 0);
LoadTexture("anotherprettypicture.png",1); 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

void Main::LoadTexture(const char* imageName, int layerNumber)
{
    image.loadFromFile(imageName);//just a function to load image. Works fine when not in texture atlas
    glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, layerNumber, image.getSize().x, image.getSize().y,1, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
} 

Then, in the fragment shader (very paraphrased, don't take much too literally here):

in vec2 UV;
out vec4 color;
uniform2DArraySampler myTextureSampler;

int main(){
    color = texture(myTextureSampler, vec3(UV, index));
}

and when drawing:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, TextureArray);

There's of course much more, though this is roughly what was changed from my working code. If I didn't include it here, it remains unchanged from my working examples (and so, likely the place I'm going wrong and causing the program to not rendering textures). 

 

Either I'm missing a vital piece of information, or I'm doing something undoubtedly silly. The fact that no error is reported, leads me to think that the shader is probably fine, as are the draw commands, and I'm simply generating the texture array wrong.

 

Anyhow, any pointers or advice are more than welcome, and I appreciate any help smile.png

 

Cheers!

Edited by Misantes

Share this post


Link to post
Share on other sites

In glTexStorage3D, the parameter levels should probably be 1. I think that 0, as used in the shown code, will produce a GL_INVALID_VALUE. Are you sure that OpenGL doesn't report an error there?

 

BTW: It is recommended in general that values of internalFormat is "sized", i.e. GL_RGBA8 instead of GL_RGBA in your case.

Edited by haegarr

Share this post


Link to post
Share on other sites

glTexParameteri(GL_TEXTURE_2D........ ARRAY?

Man, that's an embarrassing oversight on my behalf. I won't get a chance to implement that until later tonight, but I'll update and let you know if that fixes it.

 

In glTexStorage3D, the parameter levels should probably be 1. I think that 0, as used in the shown code, will produce a GL_INVALID_VALUE. Are you sure that OpenGL doesn't report an error there?

 

BTW: It is recommended in general that values of internalFormat is "sized", i.e. GL_RGBA8 instead of GL_RGBA in your case.

Ah, you're correct. I do have a 1 in my actual code, just a typo when editing it down for the example.

 

And thank you for the heads up with the internalFormat, I wasn't aware that needed to be sized. This is one of those gaps in my opengl knowledge (one of oh-so-many). But, out of curiosity, what is the difference between the two? I'm totally capable of looking this up :P But if you have time, I'd always welcome some context for it.

 

I'll update a bit later on the changes, and thanks a ton for the quick responses! :D

Share this post


Link to post
Share on other sites


And thank you for the heads up with the internalFormat, I wasn't aware that needed to be sized. This is one of those gaps in my opengl knowledge (one of oh-so-many). But, out of curiosity, what is the difference between the two? I'm totally capable of looking this up But if you have time, I'd always welcome some context for it.

AFAIK: The generic values allow the implementation to choose something that seems appropriate, what in theory may also mean a lower resolution. On the other hand, the sized formats request for a specific resolution.

Share this post


Link to post
Share on other sites

Ah, thank you again.

With both your and NumberXaero's advice, it seems to be roughly working (textures are rendering). There's some issues with the textures that are of differing sizes not working (which makes sense. Though, I was a little surprised it didn't render a poorly sized version. I was kind of anticipating that) so I'll see if I can suss that out. Is there an easy workaround for that that you know of offhand, or is it easier to just have a couple texture arrays? I can make many of the textures in the game the same size, but certain things, like the skybox for example, aren't ever going to be the same size as other textures without really screwing up the resolution.

 

But, thanks again for the help, it's genuinely appreciated :)

Edited by Misantes

Share this post


Link to post
Share on other sites

I'm not aware of what your use case is, so recommending something is difficult...

 

If the clips stored within the levels of the texture array differ in size (but still fit into the texture image), you have in general 2 options:

1.) You scale the images so that they fit into the texture space, or

2.) you use adapted u,v tuples to address the clip.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this