2

Hy folks,

I'm working on an animation in opengl es. The animation should draw squares. This works already, but how can I copy the content from the Framebuffer into the renderbuffer, the problem is that with every frame only one square is on the display.

When I tested it in the simulator and it worked. With every new frame a knew square is drawn and the others that have been drawn already are still on the screen. But when I tested it on the device it didn't worked. The screen was just blinking.

I think this happens because my mac like every other computer is using doublebuffering and the iPhone dosen't.

Please help me.

This is how I'm setting up my Framebuffer:

- (BOOL)createFramebuffer {

    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    if (USE_DEPTH_BUFFER) {
            glGenRenderbuffersOES(1, &depthRenderbuffer);
            glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
            glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
            glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
        }

        if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
            NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
            return NO;
        }

        return YES;
    }

I also wrote a setupView Methode:

- (void)setupView {
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    //  glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    //  glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations


    glViewport(0,0,backingWidth,backingHeight);                     // Reset The Current Viewport

    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                   // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)backingWidth/(GLfloat)backingHeight,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix

    isSetUp = YES;

}

This is my drawView Methode:

const GLfloat squareVertices[] = { -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f };

[EAGLContext setCurrentContext:context];

if (!isSetUp) {
    [self setupView];
}


glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

//glClear(GL_COLOR_BUFFER_BIT);

//////////////////////Variablen
float xg = -26;
float yg = 35.5;
float z = -100.0;

float abstandXg = 3.0;
float abstandYg = 3.0;

for (int i = 0; i < 26; i++) {
    //arrayKoordinatenY[j] = y - abstandY * (j);
    for (int j = 0; j<=17; j++) {

        // glLoadIdentity(); 
        //arrayKoordinatenX[i] = x + abstandX * (i);
        //glTranslatef(arrayKoordinatenX[j],arrayKoordinatenY[i],z);

        glTranslatef(xg,yg,z);


        glEnableClientState(GL_VERTEX_ARRAY);


        glColor4f(0.0f,0.0f,0.0f,0.0f);
        glVertexPointer(3, GL_FLOAT, 0, squareVertices);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

        arrayKoordinatenX[j]=xg;
        xg = xg + abstandXg;        

    }
    xg = -26.0;

    arrayKoordinatenY[i]=yg;
    yg = yg - abstandYg;
}
//// Here comes the Part where the squares are draw
.
.
.
/////
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Phil_M
  • 45
  • 5

1 Answers1

0
glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
glLoadIdentity(); 

OK - but where is the same thing for GL_MODELVIEW ?

Max
  • 16,679
  • 4
  • 44
  • 57
  • In the setupView Method or do you mean something else ? – Phil_M May 04 '11 at 18:25
  • you must load identity every time you render a frame (for model and projection matrices). – Max May 04 '11 at 18:28
  • So you mean I should write it like this ?? `glMatrixMode(GL_PROJECTION); glMatrixMode(GL_MODELVIEW); glLoadIdentity();` – Phil_M May 04 '11 at 18:30
  • no. you should write glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); – Max May 04 '11 at 18:31
  • ok thanks for your really fast answer =D. It works in the simulator. I have to wait until tomorrow to test it on the device. Thank you very much. – Phil_M May 04 '11 at 18:36
  • I have only one more question. With this changes the old squares will be drawn in also in the next frame ?? – Phil_M May 04 '11 at 18:38
  • So the main idea is: before drawing the next frame you clear the working space (buffers) and you reset all transformations (by loading identity matrices to GL_MODELVIEW and GL_PROJECTION). After this you have clear space to present the next frame (in the end it is just an image). So answering you question: only things that you tell OpenGL to draw are drawn + that is left from the previous frame (if you clear it, then nothing is left). – Max May 04 '11 at 18:49
  • Ok and now I don't need to use `glClear(GL_COLOR_BUFFER_BIT);` to rest the whole buffer ?? because glClear fixed the blinking problem, the renderbuffer was flashing every time a new frame was displayed on the screen – Phil_M May 04 '11 at 18:56
  • It was probably flashing cause you didn't specify the color. You have to call glClearColor(0.6f, 0.6f, 0.6f, 1.0f); before glClear(GL_COLOR_BUFFER_BIT); – Max May 04 '11 at 19:02