iPhone opengl ES 将帧缓冲区复制到渲染缓冲区

发布于 2024-11-05 06:09:34 字数 3984 浏览 0 评论 0原文

大家好,

我正在用 opengl es 制作动画。动画应该绘制正方形。这已经可以工作了,但是我如何将帧缓冲区中的内容复制到渲染缓冲区中,问题是每一帧只有一个方块显示在屏幕上。

当我在模拟器中测试它并且它有效时。对于每个新帧,都会绘制一个已知的方块,而其他已经绘制的方块仍然在屏幕上。但当我在设备上测试它时,它不起作用。屏幕只是闪烁。

我认为发生这种情况是因为我的 Mac 和其他所有计算机一样都使用双缓冲,而 iPhone 没有。

请帮我。

这就是我设置 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;
    }

我还编写了一个 setupView 方法:

- (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;

}

这是我的 drawView 方法:

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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-11-12 06:09:35
glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
glLoadIdentity(); 

好的 - 但是 GL_MODELVIEW 的相同之处在哪里?

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

OK - but where is the same thing for GL_MODELVIEW ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文