<p>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.</p>

CC BY-SA 3.0
2016-05-30T08:37:34.313Z
2016-05-30T08:37:34.313Z
PaulHK
<p>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.</p>

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

<p><strong>Case #1 Section of actual texture appears with an enlarged resolution in the bottom left corner.</strong></p>

<pre><code>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)
</code></pre>

<p><a href="https://i.sstatic.net/BQqGr.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BQqGr.jpg" alt="enter image description here"></a>
<strong>Case #2 Image appears enlarged and full screen</strong></p>

<pre><code>layer_shapes.begin()
checkFades()
drawShapes()
layer_shapes.end()
drawTexture(layer_shapes, alpha: 0.0)
</code></pre>

<p><a href="https://i.sstatic.net/jfR7w.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jfR7w.jpg" alt="enter image description here"></a></p>

<p><strong>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)</strong></p>

<pre><code>drawTexture(layer_shapes, alpha: 0.0)
</code></pre>

<p><a href="https://i.sstatic.net/YvdOz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YvdOz.png" alt="enter image description here"></a></p>

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

<pre><code>drawShapes()
</code></pre>

<p><a href="https://i.sstatic.net/9hlqX.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9hlqX.jpg" alt="enter image description here"></a>
(Please note discoloration is due to a debugging thing in the fragment shader, not an error)</p>

<p><strong>In summary</strong> 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.</p>

<p><strong>The desired behavior is</strong> 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! </p>

<p>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</p>

<pre><code>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), &amp;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) -&gt; 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), &amp;old_fbo)

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

        glBindFramebuffer(GLenum(GL_FRAMEBUFFER), framebuffer)


        glBindTexture(GLenum(GL_TEXTURE_2D), tex)
        //------&gt;&gt;&gt;&gt;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))
    }


}
</code></pre>

<p>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.</p>

<p>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.</p>

CC BY-SA 3.0
2016-05-15T11:30:47.090Z
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.
Andreas
CC BY-SA 3.0
2016-05-15T11:42:12.197Z
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?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-05-15T16:18:39.363Z
@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.
J.Doe
CC BY-SA 3.0
2016-05-15T16:18:58.380Z
@trichoplax Same thing for you, I have edited it to make that bit more clear.
J.Doe
CC BY-SA 3.0
2016-05-15T16:24:14.013Z
@Andreas also glGetError does not return anything, except for one error every time I create a shader.  That has not proven to be significant.  Also this error happens on the code apple wrote for their template so It is probably a non-issue.
J.Doe
CC BY-SA 3.0
2016-05-15T17:35:22.787Z
This is a little clearer. If I understand correctly, you want the texture shown in image 1 to cover the image, but without being scaled up as in image 2. So you want to tile the image with the texture shown in image 1?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-05-15T19:19:35.480Z
@trichoplax No, I am thinking that the texture in the FBO is not big enough.
J.Doe
CC BY-SA 3.0
2016-05-15T19:55:22.667Z
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?
Andreas
CC BY-SA 3.0
2016-05-15T21:21:42.247Z
@Andreas Yeah for some reason the "Depth Attachment" appears to also have a renderbuffer.
J.Doe
<opengl><texture><opengl-es>
What is causing this odd scaling behavior?
J.Doe
# What is causing this odd scaling behavior?

(Asked by: username_0 on 2016-05-15)

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](https://i.sstatic.net/BQqGr.jpg)](https://i.sstatic.net/BQqGr.jpg)
**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](https://i.sstatic.net/jfR7w.jpg)](https://i.sstatic.net/jfR7w.jpg)

**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](https://i.sstatic.net/YvdOz.png)](https://i.sstatic.net/YvdOz.png)

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

```
drawShapes()

```

[![enter image description here](https://i.sstatic.net/9hlqX.jpg)](https://i.sstatic.net/9hlqX.jpg)
(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.

**Tags:** opengl, texture, opengl-es

> **Comments:**
> **username_1**: 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.  
> **username_2**: 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?  
> **username_0**: @username_1 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.  
> **username_0**: @username_2 Same thing for you, I have edited it to make that bit more clear.  
> **username_0**: @username_1 also glGetError does not return anything, except for one error every time I create a shader.  That has not proven to be significant.  Also this error happens on the code apple wrote for their template so It is probably a non-issue.  
> **username_2**: This is a little clearer. If I understand correctly, you want the texture shown in image 1 to cover the image, but without being scaled up as in image 2. So you want to tile the image with the texture shown in image 1?  
> **username_0**: @username_2 No, I am thinking that the texture in the FBO is not big enough.  
> **username_1**: 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?  
> **username_0**: @username_1 Yeah for some reason the "Depth Attachment" appears to also have a renderbuffer.  

---

## 1 answers

### Answer by username_3 on 2016-05-30. Score: 4 | Accepted answer

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.


---

