Drawing Blur (Render to Texture)

Nibbie
Posts: 2
Joined: 2008.10
Post: #1
Hi Everyone,

after testing serveral stuff and get Render to Texture working i´ll hang on drawing some kind of blur.

i have a model displayed on screen. now i want to have some kind of blur around it.

in theorie this task will go as follows:

1. Render Scene / Model to Texture
2. Render Model
3. Render Blurr around it by drawing the texture several times inc tex coords and play with the alpha value.

thats theorie. but in practice it wont work.

here my code

Code:
-(void)RenderToTexture
{
    GLint status;
    const GLfloat squareVertices[] = {
        -0.5f, -0.5f,
        0.5f,  -0.5f,
        -0.5f,  0.5f,
        0.5f,   0.5f,
    };
    

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO);
    
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);

    status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
    
    if (status != GL_FRAMEBUFFER_COMPLETE_OES)
    {
        NSLog(@"Failed for new FrameBuffer");
        
    }
    glViewport(0, 0, 128, 128);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-1, 1, -1.5, 1.5, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glColor4f(0, 0,255, 0);
    glVertexPointer(2, GL_FLOAT,0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);

    
    
}

Code:
-(void)RenderBlur
{

    float spost = 0;
    float alphainc = 0.9f / 25;
    float alpha = 0.2f;
    
    glEnable(GL_TEXTURE_2D);
    glBindTexture(1, texture);
    glDisable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    glEnable(GL_BLEND);
    
    alphainc = alpha / 25;
    for (int i=0;i<25;i++) {
        GLfloat tex_coord[] = {
            0+spost,1-spost,
            0+spost,0+spost,
            1-spost,0+spost,
            1-spost,1-spost
        };
        GLfloat vert_coord[] = {
            0, 0,
            0,  backingHeight,
            backingWidth,  backingHeight,
            backingWidth,  0
        };
        
        glColor4f(1, 1, 1, alpha);
        spost +=0.02f;
        alpha = alpha - alphainc;
        glVertexPointer(2, GL_FLOAT, 0, vert_coord);
        glEnableClientState(GL_VERTEX_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, 0, tex_coord);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glDeleteTextures(1, &texture);
    
    
}

Code:
- (void)drawView {
    
    // Replace the implementation of this method to do your own custom drawing
    
    const GLfloat squareVertices[] = {
        -0.5f, -0.5f,
        0.5f,  -0.5f,
        -0.5f,  0.5f,
        0.5f,   0.5f,
    };
    
    
    const GLubyte squareColors[] = {
        255, 255,   0, 255,
        0,   255, 255, 255,
        0,     0,   0,   0,
        255,   0, 255, 255,
    };
    
    const GLfloat texcoords[] = {
        0,0,
        -1,0,
        1,-1,
        0,-1
    };
    
    const GLfloat texverts[] = {
        -0.5f,-0.5f,
        0.5f,-0.5f,
        -0.5f,0.5f,
        0.5f,0.5f
    };
    
    
    
    
    [EAGLContext setCurrentContext:context];

    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, texverts);
    glEnableClientState(GL_VERTEX_ARRAY);
    
    glClearColor(0.1,0.1,0.1,0.5);
    //glColor4f(255, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    [self RenderBlur];
    [self RenderToTexture];    
    glRotatef(3.0f, 0.0f, 0.0f, 1.0f);
    /*
    
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glVertexPointer(2, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);*/
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewFramebuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
result i get is seeing the texture(the quad) inside my rect.


Someone can point me in the right direction what i made worng?
Quote this message in a reply
Member
Posts: 45
Joined: 2008.04
Post: #2
In 'renderToTexture':

The usual opengles layer setup is already rendering to a framebuffer, so restoring it to zero afterwards may be incorrect... try:

GLuint oldFramebuffer = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *) &oldFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO);
// do stuff...
glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFramebuffer);


You probably want to reuse 'texture' rather than generate an new one (via the glGenTexture()) every frame too.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2008.10
Post: #3
aBabyRabbit Wrote:In 'renderToTexture':

The usual opengles layer setup is already rendering to a framebuffer, so restoring it to zero afterwards may be incorrect... try:

GLuint oldFramebuffer = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *) &oldFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO);
// do stuff...
glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFramebuffer);


You probably want to reuse 'texture' rather than generate an new one (via the glGenTexture()) every frame too.

Thank your for that hint. I´ll try it tomorrow.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.02
Post: #4
Macias,

Did you get your render to texture working in the end?
I'm trying to do something similar, but I'm getting stuck, either the code isn't rendering to the texture correctly, or my second pass where I use the texture isn't behaving (although I suspect this part is fine as I have a slow version using glCopyTexSubImage2D that works ok).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPad,render to texture, low framerate ldynasa 0 2,619 Nov 13, 2010 12:34 AM
Last Post: ldynasa
  Render to texture failing headkaze 7 7,842 Sep 1, 2010 09:47 PM
Last Post: headkaze
  Performance problems with render to texture method mbw234 6 5,340 Jun 25, 2010 01:47 AM
Last Post: Jamie W
  Drawing menu versus drawing game johncmurphy 4 4,346 Jan 6, 2010 09:14 AM
Last Post: AnotherJake
  Render to texture performance iPhoneGG 2 3,786 May 18, 2009 03:31 PM
Last Post: ShiftZ