handmade.network » Forums » [OpenGL] rendering to 2d texture array layer
erpeo93
59 posts

None

#21236 [OpenGL] rendering to 2d texture array layer
1 week, 2 days ago Edited by erpeo93 on July 8, 2019, 8:46 a.m.

Hi everyone,

I'd like to start rendering to slices of a texture array but it doesn't seems to exist a lot of documentation on that, and so here's the snippet I came up with in the hope that someone is able to spot any possibile problem:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray);
    for(u32 textureIndex = 0; textureIndex < MAX_TEXTURE_COUNT; ++textureIndex)
    {
        glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureArray, 0, textureIndex);
        
        glViewport(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        glScissor(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        
        glClearColor(1, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        
        
        glViewport(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        glScissor(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        
        glBindBuffer(GL_ARRAY_BUFFER, opengl.screenFillVertexBuffer);
        OpenGLStartProgram(testTextureGen);
        
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        OpenGLEndProgram(testTextureGen);
    }
    glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
    glBindFrameBuffer(GL_DRAW_FRAMEBUFFER, 0);
    



The "testTextureGen" program just output a fixed color.
I'm currently placing this code at the very beginning of the opengl rendering, and even if at the moment it _seems_ to be working (textures are cleared and color is rendered to the texture) it seems that I'm screwing up the framebuffers when I let this code run, and in fact if I place this code "in the middle" of the rendering loop, I just get a black screen. (Of course the final call to "unbind" the framebuffer is different there)

I feel like I'm missing some "unbind" calls to tell opengl "ok I've finished drawing to this texture layer, now just please get back to normal business"... but I just don't know what it is.

Thanks in advance.
Leonardo




None
erpeo93
59 posts

None

#21237 [OpenGL] rendering to 2d texture array layer
1 week, 2 days ago

I am an idiot.

I was just forgotting to bind a framebuffer _before_ actually drawing.
Now it works fine, hope someone can reuse the code, as actually there is no examples on the internet on how to do this.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    glBindFrameBuffer(GL_FRAMEBUFFER, textureGenFrameBuffer);
    glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray);
    for(u32 textureIndex = 0; textureIndex < MAX_TEXTURE_COUNT; ++textureIndex)
    {
        glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureArray, 0, textureIndex);
        
        glViewport(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        glScissor(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        
        glClearColor(1, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        
        
        glViewport(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        glScissor(0, 0, MAX_IMAGE_DIM, MAX_IMAGE_DIM);
        
        glBindBuffer(GL_ARRAY_BUFFER, opengl.screenFillVertexBuffer);
        OpenGLStartProgram(testTextureGen);
        
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        OpenGLEndProgram(testTextureGen);
    }
    glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
    glBindFrameBuffer(GL_DRAW_FRAMEBUFFER, 0);




cheers!

None