<p>After trying everything possible I found my mistake:</p>
<p>The shaders are fine, the only thing which is wrong is the frameBufferObject binding. To be honest, the texture to FBO binding.</p>
<p><strong>the only thing I replaced is this:</strong></p>
<pre><code>glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, m_cubeMapTexture, 0);
</code></pre>
<p><strong>by the correct method call:</strong></p>
<pre><code>glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_cubeMapTexture, 0);
    
</code></pre>
<p><strong>The result of the changed code is this:</strong></p>
<p><a href="https://i.sstatic.net/UB0s3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UB0s3.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2020-09-23T12:37:13.023Z
2020-09-23T12:37:13.023Z
Thomas
<p>I want to do some complicated stuff but have problems with my geometry shader / FBO bindings.
To hold it as easy as possible I reduced the code to a minimum. If this works, I could continue the hard part. So don't wonder, why I am trying to do it in this way...</p>
<p><strong>The &quot;easy&quot; thing:</strong></p>
<p>I have two triangles (a quad) which are going over the whole screen from (-1,-1,0) to (1,1,0).
Inside my geometry shader I want to copy these two triangles six times to cover all six faces of my cubemap. BTW: I don't need a depth or stencil buffer.</p>
<p><strong>Lets look at the cubemap texture generation:</strong></p>
<pre><code>glGenTextures(1, &amp;m_cubeMapTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_cubeMapTexture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
checkGLError();
// set textures
for (int i = 0; i &lt; 6; ++i)
    glTexImage2D(static_cast&lt;GLenum&gt;(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
</code></pre>
<p><strong>This texture will be plugged into a frameBufferObject in the following way:</strong></p>
<pre><code>glGenFramebuffers(1, &amp;m_cubeMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_cubeMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, m_cubeMapTexture, 0);

static const GLuint draw_buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5 };
glDrawBuffers(6, draw_buffers);
</code></pre>
<p><strong>The vertex shader:</strong></p>
<pre><code>#version 420
in vec3 POSITION;
                                                                                
out VS_OUT                                                            
{                                                                    
   vec3 position;                                                     
} vs_out;                                                            
                                                                                
void main()                                                           
{                                                                   
    vs_out.position = POSITION;  
}
</code></pre>
<p>Just take the input and pass it to the geometry shader</p>
<p><strong>Lets continue with the geometry shader:</strong></p>
<pre><code>#version 420                                                        
layout (triangles) in;                                               
layout (triangle_strip, max_vertices = 36) out;                     
uniform mat3 orientation[6];                                        
                                                                              
in VS_OUT                                                           
{                                                                   
    vec3 position;                                                   
} gs_in[];                                                        
                                                                              
out GS_OUT                                                         
{                                                                   
    vec3 position;                                                    
} gs_out;                                                          
                                                                              
void main()                                                          
{                                                                  
    for (int i = 0; i &lt; 6; ++i)                                     
    {                                                                
        gl_Layer = i;                                              
        gl_Position = vec4(gs_in[0].position, 1.0f);                
        gs_out.position = orientation[i] * vec3(gs_in[0].position.xy, 1.0f);      
        EmitVertex();                                               
                                                                              
        gl_Layer = i;                                                
        gl_Position = vec4(gs_in[1].position, 1.0f);                 
        gs_out.position = orientation[i] * vec3(gs_in[1].position.xy, 1.0f);        
        EmitVertex();                                              
                                                                              
        gl_Layer = i;                                                
        gl_Position = vec4(gs_in[2].position, 1.0f);                
        gs_out.position = orientation[i] * vec3(gs_in[2].position.xy, 1.0f);        
        EmitVertex();                                                
                                                                              
        EndPrimitive();                                              
    }                                                               
}     
</code></pre>
<p>The for loop creates each triangle six times (one for each cubemap face &quot;gl_Layer = i&quot;).
the gl_Position is the input quad without any transformations.
the gs_out.position is rotated by the orientation matrix (a rotation matrix for each face). This gs_out.position should be interpolated within the fragment shader.</p>
<p><strong>Last, but not least: The fragment shader</strong></p>
<pre><code>#version 420                                             
in GS_OUT                                              
{                                                    
    vec3 position;                                       
} fs_in;                                                
                                                                  
out vec4 color;                                     
                                                                  
void main()                                            
{                                                      
    color = vec4(fs_in.position, 1.0f);                
}          
</code></pre>
<p>The interpolated fs_in.position will be stored into the cubemap.</p>
<p>If you just look at the code and try to imagine what the output should be, you'll end up to say:
&quot;That is the RGB cube + the minus part which is stored in the cubemap.&quot;</p>
<p><strong>And now I'll show you the output:</strong></p>
<p><a href="https://i.sstatic.net/mfI4o.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mfI4o.png" alt="enter image description here" /></a></p>
<p>My first intention was changing the &quot;orientation&quot; matrices but I noticed, that this is not the problem:
By changing the for loop counter within my geometry shader to &quot;for (int i = 0; i &lt; <strong>1</strong>; ++i)&quot; I got the following output:</p>
<p><a href="https://i.sstatic.net/so8M2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/so8M2.png" alt="enter image description here" /></a></p>
<p>So there are only the two triangles, which are not copied anymore and each cubemap face is colored in the same way. gl_Layer was only set to zero but the other layers are also colored. How can that be?
How can I change the code, so that only the face &quot;gl_Layer&quot; will be colored?</p>

<opengl><glsl><c++><frame-buffer><geometry-shader>
draw on cubemap with help of geometry shader each triangle covers each cubemap face (why??)
Thomas
# draw on cubemap with help of geometry shader each triangle covers each cubemap face (why??)

(Asked by: username_0 on 2020-09-23)

I want to do some complicated stuff but have problems with my geometry shader / FBO bindings.
To hold it as easy as possible I reduced the code to a minimum. If this works, I could continue the hard part. So don't wonder, why I am trying to do it in this way...

**The "easy" thing:**

I have two triangles (a quad) which are going over the whole screen from (-1,-1,0) to (1,1,0).
Inside my geometry shader I want to copy these two triangles six times to cover all six faces of my cubemap. BTW: I don't need a depth or stencil buffer.

**Lets look at the cubemap texture generation:**

```
glGenTextures(1, &m_cubeMapTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_cubeMapTexture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
checkGLError();
// set textures
for (int i = 0; i < 6; ++i)
    glTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);

```

**This texture will be plugged into a frameBufferObject in the following way:**

```
glGenFramebuffers(1, &m_cubeMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_cubeMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, m_cubeMapTexture, 0);

static const GLuint draw_buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5 };
glDrawBuffers(6, draw_buffers);

```

**The vertex shader:**

```
#version 420
in vec3 POSITION;
                                                                                
out VS_OUT                                                            
{                                                                    
   vec3 position;                                                     
} vs_out;                                                            
                                                                                
void main()                                                           
{                                                                   
    vs_out.position = POSITION;  
}

```

Just take the input and pass it to the geometry shader

**Lets continue with the geometry shader:**

```
#version 420                                                        
layout (triangles) in;                                               
layout (triangle_strip, max_vertices = 36) out;                     
uniform mat3 orientation[6];                                        
                                                                              
in VS_OUT                                                           
{                                                                   
    vec3 position;                                                   
} gs_in[];                                                        
                                                                              
out GS_OUT                                                         
{                                                                   
    vec3 position;                                                    
} gs_out;                                                          
                                                                              
void main()                                                          
{                                                                  
    for (int i = 0; i < 6; ++i)                                     
    {                                                                
        gl_Layer = i;                                              
        gl_Position = vec4(gs_in[0].position, 1.0f);                
        gs_out.position = orientation[i] * vec3(gs_in[0].position.xy, 1.0f);      
        EmitVertex();                                               
                                                                              
        gl_Layer = i;                                                
        gl_Position = vec4(gs_in[1].position, 1.0f);                 
        gs_out.position = orientation[i] * vec3(gs_in[1].position.xy, 1.0f);        
        EmitVertex();                                              
                                                                              
        gl_Layer = i;                                                
        gl_Position = vec4(gs_in[2].position, 1.0f);                
        gs_out.position = orientation[i] * vec3(gs_in[2].position.xy, 1.0f);        
        EmitVertex();                                                
                                                                              
        EndPrimitive();                                              
    }                                                               
}     

```

The for loop creates each triangle six times (one for each cubemap face "gl\_Layer = i").
the gl\_Position is the input quad without any transformations.
the gs\_out.position is rotated by the orientation matrix (a rotation matrix for each face). This gs\_out.position should be interpolated within the fragment shader.

**Last, but not least: The fragment shader**

```
#version 420                                             
in GS_OUT                                              
{                                                    
    vec3 position;                                       
} fs_in;                                                
                                                                  
out vec4 color;                                     
                                                                  
void main()                                            
{                                                      
    color = vec4(fs_in.position, 1.0f);                
}          

```

The interpolated fs\_in.position will be stored into the cubemap.

If you just look at the code and try to imagine what the output should be, you'll end up to say:
"That is the RGB cube + the minus part which is stored in the cubemap."

**And now I'll show you the output:**

[![enter image description here](https://i.sstatic.net/mfI4o.png)](https://i.sstatic.net/mfI4o.png)

My first intention was changing the "orientation" matrices but I noticed, that this is not the problem:
By changing the for loop counter within my geometry shader to "for (int i = 0; i < **1**; ++i)" I got the following output:

[![enter image description here](https://i.sstatic.net/so8M2.png)](https://i.sstatic.net/so8M2.png)

So there are only the two triangles, which are not copied anymore and each cubemap face is colored in the same way. gl\_Layer was only set to zero but the other layers are also colored. How can that be?
How can I change the code, so that only the face "gl\_Layer" will be colored?

**Tags:** opengl, glsl, c++, frame-buffer, geometry-shader


---

## 1 answers

### Answer by username_0 on 2020-09-23. Score: 3 | Accepted answer

After trying everything possible I found my mistake:

The shaders are fine, the only thing which is wrong is the frameBufferObject binding. To be honest, the texture to FBO binding.

**the only thing I replaced is this:**

```
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, m_cubeMapTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, m_cubeMapTexture, 0);

```

**by the correct method call:**

```
glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_cubeMapTexture, 0);
    

```

**The result of the changed code is this:**

[![enter image description here](https://i.sstatic.net/UB0s3.png)](https://i.sstatic.net/UB0s3.png)


---

