13

I'm trying to render to a texture using OpenGL ES 2.0, but I can't seem to make it work.

This is how I proceed:

    struct RenderTexture
    {
        GLuint framebuffer;
        GLuint tex;
        GLint old_fbo;


        RenderTexture(GLuint width, GLuint height)
        {
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);

            glGenFramebuffers(1, &framebuffer);
            glGenTextures(1, &tex);

            glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
            glBindTexture(GL_TEXTURE_2D, tex);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
                         width, height, 0, GL_RGBA, 
                         GL_UNSIGNED_BYTE, NULL);
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 
                                   tex, 0);

            glClearColor(1, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT);

            GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
            if (status != GL_FRAMEBUFFER_COMPLETE) {
                cout << status << endl; // this is not called
            }

            glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
        }

        void begin()
        {
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
            glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
        }

        void end()
        {
            glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
        }
    };

But when I try drawing on it and using the resulting texture, the texture is drawn as totally black.

If I just don't wrap the drawing code in render_tex->begin(); and render_tex->end();, everything draws correctly, leading me to believe that the problem is isolated to the code above.

sharvey
  • 7,635
  • 7
  • 48
  • 66

4 Answers4

7

Make sure the texture is not bound before trying to render into it. Even if not using texturing at all, trying to render into a currently bound texture may invoke undefined behaviour and just not work.

You should actually call glBindTexture(GL_TEXTURE_2D, 0) after the glTexImage2D in your RenderTexture constructor, or maybe restore the previously bound texture, like you do with the FBO. Just make sure the tex is not bound when you render into the FBO.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • I added `glBindTexture(GL_TEXTURE_2D, 0);` in the constructor, right after checking the FBO status. It's probably good practice, but it did not change the result. – sharvey Dec 14 '11 at 02:31
  • @sharvey And you also don't accidentally bind the texture when you render into the FBO? – Christian Rau Dec 14 '11 at 09:09
  • The texture is in fact no bound when drawing to the FBO. Not only is TEXTURE_2D bound to zero at the end of the constructor, another texture is bound later on during the actual drawing. – sharvey Dec 15 '11 at 01:30
6

This has been a while, but as I wrote in the comments, be sure you're initializing a power of 2 texture.

sharvey
  • 7,635
  • 7
  • 48
  • 66
0

I had exact the same problem as u do. Try to add

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

right after glBindTexture(GL_TEXTURE_2D, tex); and black square have to disappear.

0

You don't seem to make use of glActiveTexture. I recommend you to call glActiveTexture(GL_TEXTURE0+tex); before each glBindTexture(tex);, which will save you a lot of headaches when you use more than one texture. I guess the error is in the code you use for drawing the texture on the screen.

Hristo
  • 6,382
  • 4
  • 24
  • 38