Question

I am trying to use render to texture concept with cube map textures but somehow texture is not getting applied to geometry in draw call. Here is my code

#define WIDTH 64
#define HEIGHT 64
glGenTextures(1, &id1);
glBindTexture(GL_TEXTURE_CUBE_MAP, id1);

glGenFramebuffers(1, &Fboid);
for(int i=0;i<6;i++)
{
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);


glTexImage2D(types[i], 0, GL_RGBA,WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE,0);


}
glBindFramebuffer(GL_FRAMEBUFFER, Fboid);


    for(int i=0;i<6;i++)                      
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,types[i],id1,0);


draw_cube();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

draw();

I have verified that framebuffer binding is complete or not. Also draw_cube() correctly draws geometry as per expected. When i remove FBO and supply data to glteximage2D() it correctly applies cubemap texture to geometry in draw() call. Width height is same as the viewport set in draw_cube call.

What's wrong with above code?

Was it helpful?

Solution

It is hard to say what excactly belongs into the body of your for loop (hint: correct formatting of your question would have helped). But nevertheless, the way it is currently written doesn't make much sense at all.

If it is only the single line after the for statement (as it currently seems), then you're binding all the cube faces to the same color attachment and thus only the last call (types[5]) will prevail, which seems strange. If you intend to bind all six faces at once (and thus make draw_cube render to all six faces in one go using multiple render targets), then you should use GL_COLOR_ATTACHMENT0 + i instead of just GL_COLOR_ATTACHMENT0. But in this case, depending on your hardware/driver, 6 might be quite high (so to say: too high) a number of active multiple render targets.

But maybe the draw_cube call also belongs into the for-loop (thus rendering the six faces individually). In this case you should put it into the loop:

for(int i=0;i<6;i++)
{
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,types[i],id1,0);
    draw_cube();
}

EDIT: As Nicol Bolas points out in his comment, you might also use layered rendering and bind the whole cube map as render target, using the geometry shader output variable gl_Layer to determine the cube map face into which a primitive should go.

In this case you would just have a single call to

//note: just ...Texture, without ...2D!
glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, id1, 0); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top