# 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

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

Cheers!

Edited by Misantes

##### Share on other sites

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

##### 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 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 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 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 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

##### Share on other sites

Ah, that makes total sense. I'm sure there will be some trial and error but I think I can figure that out from here. Thanks again for the help!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978472
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33