The C++ psuedo code to transmit the idea of what I want to do would be:

Texture_array tarr = Texture_array(6);
tarr[0] = texture;

Which I am attempting to do as follows:

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 894, 894, 6);
    glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 894, 894, 0, color_format, GL_UNSIGNED_BYTE, data);
    //Set the texture parameters of the image
    glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

On the fragment shader I attempt to read the value as:

vec3 c = vec3(texture(text, vec3(f_uv.xy,0)));

However the end result is completely blak, despite data being an array of a valid texture (I have tested it and know data has been properly initialized, it works with normal textures, just not texture arrays)

glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 894, 894, 0, color_format, GL_UNSIGNED_BYTE, data);

You're transmitting 3D data. But you told OpenGL that the 3D block of data has a depth of 0. So the number of pixels you're transmitting is... 0.

I have no idea why OpenGL doesn't provide an error when passing 0 for any of the sizing components (since that means you're not passing data). But that's your problem. You need to pass 1 for the depth if you're uploading a single 2D slice to the array.

  • $\begingroup$ I see, how should I specify the parameters to get the equivalent of Texture_array[0] = texture then? In other words, how can I assign an entire texture to a single layer of a texture array? $\endgroup$ – Makogan Jul 7 '18 at 8:49
  • $\begingroup$ An array texture is "an entire texture". What you're doing is uploading a 2D slice to an array texture. $\endgroup$ – Nicol Bolas Jul 7 '18 at 13:12
  • $\begingroup$ Ok, how do I select which slice of the array texture to load the current byte array into? $\endgroup$ – Makogan Jul 7 '18 at 17:01
  • $\begingroup$ @Makogan: If you only wanted to upload a single row of pixel data in a 2D texture, how would you tell OpenGL which row to upload to? That's your answer. $\endgroup$ – Nicol Bolas Jul 7 '18 at 17:16
  • $\begingroup$ I have never done it so I have no experience with that operation either. And it's quite clear that I have some miss understanding about how glTexSubImage3D works, since I was completely convinced that passing 0 as a parameter would instruct OpenGL to load the pixel data into the 0th layer of the specified texture. So I would like to request a more straightforward answer if possible. $\endgroup$ – Makogan Jul 7 '18 at 19:48

The full explanation of what I was doing wrong is:

The depth parameter in the glTexSubimage3D refers to the depth of the data being sent. Since I am trying to send a single layer this depth should be 1 not 0 (the depth of an image/layer) (As explained by Nicol Bolas).

The zoffset parameter in the same function refers to the offset in layers where the data will be put inside the texture array.

In other words, following the array analogy, this is the value inside of the brackets.

The correct approach would thus be:

glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 2, 894, 894, 1, color_format, GL_UNSIGNED_BYTE, data);

Which in C++ pseudo code would be:

Texture_Array[2] = texture;
  • $\begingroup$ I don't see the point of adding this answer when it is pretty much exactly what NicolBolas already said. His answer is the solution to the problem you asked for and ought to be the accepted answer, not one that simply repeats what he said and adds information that is irrelevant to the question asked. $\endgroup$ – Christian Rau Jul 9 '18 at 21:54
  • $\begingroup$ The added information is not irrelevant, the question as the title is posted asks, how to assign a texture to a single entry of a texture array. Nicol Bolas' answer correctly identifies the mistake I made. However he does not say how to solve it or how to achieve the final, desired result of texture_arrray[0]=texture As a matter of fact I asked Nicol Bolas in the comments how to actually do what I was attemtping. If you read the comments you noticed there's no direct answer. So after figuring out how to solve the issue I made this answer which ACTUALLY answers the question in the title. $\endgroup$ – Makogan Jul 10 '18 at 4:08
  • $\begingroup$ Take into account that I actually get mroe points by awarding Nicol bolas' answer than I do by accepting my own. If I haven't accepted that answer as the actual answer is because I think his answer is incomplete as it does not fully and directly answer the question. It just identifies the problem without explaining how to achieve the desired result. $\endgroup$ – Makogan Jul 10 '18 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.