<p>The full explanation of what I was doing wrong is: </p>

<p>The depth parameter in the <code>glTexSubimage3D</code> 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).</p>

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

<p>In other words, following the array analogy, this is the value inside of the brackets. </p>

<p>The correct approach would thus be:</p>

<pre><code>glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 2, 894, 894, 1, color_format, GL_UNSIGNED_BYTE, data);
</code></pre>

<p>Which in C++ pseudo code would be:</p>

<pre><code>Texture_Array[2] = texture;
</code></pre>

CC BY-SA 4.0
2018-07-10T04:08:51.377Z
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.
Makogan
CC BY-SA 4.0
2018-07-08T17:38:15.103Z
2018-07-09T05:52:47.547Z
2018-07-09T05:52:47.547Z
Makogan
<blockquote>
<pre><code>glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 894, 894, 0, color_format, GL_UNSIGNED_BYTE, data);
</code></pre>
</blockquote>

<p>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.</p>

<p>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.</p>

CC BY-SA 4.0
2018-07-07T08:49:23.763Z
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?
Makogan
CC BY-SA 4.0
2018-07-07T13:12:32.760Z
An array texture *is* "an entire texture". What you're doing is uploading a 2D slice to an array texture.
Nicol Bolas
CC BY-SA 4.0
2018-07-07T17:01:59.933Z
Ok, how do I select which slice of the array texture to load the current byte array into?
Makogan
CC BY-SA 4.0
2018-07-07T17:16:06.077Z
@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.
Nicol Bolas
CC BY-SA 4.0
2018-07-07T19:48:34.100Z
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.
Makogan
CC BY-SA 4.0
2018-07-07T02:56:26.683Z
2018-07-07T13:12:22.163Z
2018-07-07T13:12:22.163Z
Nicol Bolas
<p>The C++ psuedo code to transmit the idea of what I want to do would be:</p>

<pre><code>Texture_array tarr = Texture_array(6);
tarr[0] = texture;
</code></pre>

<p>Which I am attempting to do as follows:</p>

<pre><code>    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);
</code></pre>

<p>On the fragment shader I attempt to read the value as:</p>

<pre><code>vec3 c = vec3(texture(text, vec3(f_uv.xy,0)));
</code></pre>

<p>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)</p>

<opengl><texture><data-structure><3dtexture>
How to assign a texture to a single entry of a texture array?
Makogan
# How to assign a texture to a single entry of a texture array?

(Asked by: username_0 on 2018-07-06)

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)

**Tags:** opengl, texture, data-structure, 3dtexture


---

## 2 answers

### Answer by username_0 on 2018-07-08. Score: 2 | Accepted answer

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 username_1 username_1).

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;

```

> **Comments:**
> **username_0**: 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. username_1 username_1' 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 username_1 username_1 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.  

---

### Answer by username_1 on 2018-07-07. Score: 2

> ```
> 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.

> **Comments:**
> **username_0**: 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?  
> **username_1**: An array texture *is* "an entire texture". What you're doing is uploading a 2D slice to an array texture.  
> **username_0**: Ok, how do I select which slice of the array texture to load the current byte array into?  
> **username_1**: @username_0: 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.  
> **username_0**: 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.  

---

