4
$\begingroup$

OK so I have a simple setup going on. Basically I am just drawing some textures and shapes. However sizing is going weird... let me show you how any maybe you can diagnose.

Basically everything is broken into components that work fine by themselves, however when together it is disastrous.

Case #1 Section of actual texture appears with an enlarged resolution in the bottom left corner.

glViewport(0, 0, GLsizei(width), GLsizei(height))
layer_shapes.begin()
glViewport(0, 0, GLsizei(width), GLsizei(height))
checkFades()
drawShapes()
layer_shapes.end()
drawTexture(layer_shapes, alpha: 0.0)

enter image description here Case #2 Image appears enlarged and full screen

layer_shapes.begin()
checkFades()
drawShapes()
layer_shapes.end()
drawTexture(layer_shapes, alpha: 0.0)

enter image description here

Case #3 proving that texture drawing works by drawing to front buffer (I put a texture into a fbo and never drew to the fbo)

drawTexture(layer_shapes, alpha: 0.0)

enter image description here

Case #4 Proving that shape drawing works (This is what it should look like, except with an accumulation effect)

drawShapes()

enter image description here (Please note discoloration is due to a debugging thing in the fragment shader, not an error)

In summary Cases 1 and 2 do not work as intended (they enlarge only part of the shape drawing and put it in the wrong proportion on the screen). Cases 3 and 4 show the individual complnents (drawing shapes, and drawing textures) working when everything is drawn to the front buffer without any 'glViewport' calls.

The desired behavior is that an image like case 4 would be drawn, however the image would be an accumulation of previous frames (Notice how i never clear the layer_shapes fbo) so the shapes (that are moving) would leave a trail. The proportions are just being weird!

And Incase you want to see it here is how I set up my texture fbo's. They get initialized and then have .load() called on them

class TextureBuffer {
    var framebuffer:GLuint = 0
    var tex:GLuint = 0
    var old_fbo:GLint = 0
    var w:GLsizei = 0
    var h:GLsizei = 0


    init(widthi: GLsizei = 0, heighti: GLsizei = 0)
    {
        w = widthi
        h = heighti
    }

    func begin()
    {
        glGetIntegerv(GLenum(GL_FRAMEBUFFER_BINDING), &old_fbo)
        glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebuffer)
    }

    func checkStatus()
    {
        let status = glCheckFramebufferStatus(GLenum(GL_FRAMEBUFFER))
        if (status != GLenum(GL_FRAMEBUFFER_COMPLETE))
        {
            print("DIDNT GO WELL WITH")
            print(status)
        }
    }

    func end()
    {
        glBindFramebuffer(GLenum(GL_FRAMEBUFFER), GLenum(old_fbo))
    }

    func loadTexture(name: String, type: String) -> GLuint
    {
        var it:GLKTextureInfo = GLKTextureInfo()
        let pic = UIImage(named: name + "." + type)!.CGImage
        let path = NSBundle.mainBundle().pathForResource(name, ofType: type)

        do
        {
            try it = GLKTextureLoader.textureWithContentsOfFile(path!, options: nil)
        }
        catch
        {
            do
            {
                //print("ERR Loading: " + path!)
                try it = GLKTextureLoader.textureWithCGImage(pic!, options: nil)
            }
            catch
            {
                print("Error loading UIImage: " +  pic.debugDescription)
                it = GLKTextureInfo()
            }
        }
        return it.name
    }

    func load(width: CGFloat?, height: CGFloat?)
    {
        if let lwidth = width
        {
            w = GLsizei(lwidth)
        }
        if let lheight = height
        {
            h = GLsizei(lheight)
        }
        glBindTexture(GLenum(GL_TEXTURE_2D), 0)
        glGetIntegerv(GLenum(GL_FRAMEBUFFER_BINDING), &old_fbo)

        glGenFramebuffers(1, &framebuffer)
        tex = loadTexture("Black", type: "png")

        glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebuffer)


        glBindTexture(GLenum(GL_TEXTURE_2D), tex)
        //------>>>>glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(w), GLsizei(h), 0, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), nil)
        glFramebufferTexture2D(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_TEXTURE_2D), tex, 0)


        let status = glCheckFramebufferStatus(GLenum(GL_FRAMEBUFFER))
        if (status != GLenum(GL_FRAMEBUFFER_COMPLETE))
        {
            print("DIDNT GO WELL WITH", width, " " , height)
            print(status)
        }

        glBindFramebuffer(GLenum(GL_FRAMEBUFFER), GLenum(old_fbo))
    }


}

A thought is there was a line near the end (I put an arrow pointing to it) that I had to remove to get the fbo to work properly. Previously the code worked fine, however it is not working fine now.

I do have a suspicion that since I have removed that line that caused nothing to work that the texture that is generated by it is the wrong size since the texture it is loading is of a non-device size. However I do not know why that call causes everything not to work.

$\endgroup$
  • $\begingroup$ 1. Does glGetError return anything else than no error? 2. "Blown up" does not really mean anything to me. Maybe it's just me but I'm having hard time figuring out what the problem is and/or expected behavior. $\endgroup$ – Andreas May 15 '16 at 11:30
  • $\begingroup$ As far as I can understand, the images each show some aspect working as it is intended. It isn't clear to me which one (if any) shows the problem you are having. Could you edit to either add an image showing the problem, or indicate which existing image already shows the problem? $\endgroup$ – trichoplax May 15 '16 at 11:42
  • $\begingroup$ @Andreas so sorry this wasn't clear! I have made some edits that hopefully make things a bit easier to understand. The simulation I am doing is just a bunch of moving particles that are going to leave a trail behind them. Thankyou so much for looking into this. $\endgroup$ – J.Doe May 15 '16 at 16:18
  • $\begingroup$ @trichoplax Same thing for you, I have edited it to make that bit more clear. $\endgroup$ – J.Doe May 15 '16 at 16:18
  • 1
    $\begingroup$ As far as opengl errors go all indicate something did not have desired effect. It matters not who wrote the code. Fix it. Btw what does the FBO look like in the debugger? Anything unusual? $\endgroup$ – Andreas May 15 '16 at 19:55
3
$\begingroup$

Are you calling glViewport when switching drawing to FBO ? This should be called when switching into a different resolution render target. I say this because case #1 is a symptom of incorrect glViewport.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.