Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 绘制时OpenGL ES帧缓冲区镜像_Iphone_Opengl Es_Framebuffer_Fbo - Fatal编程技术网

Iphone 绘制时OpenGL ES帧缓冲区镜像

Iphone 绘制时OpenGL ES帧缓冲区镜像,iphone,opengl-es,framebuffer,fbo,Iphone,Opengl Es,Framebuffer,Fbo,我实在想不起来: 我无法让帧缓冲区工作,但我现在已经开始工作了。然而,在帧缓冲区生成的纹理上出现了这种不可思议的镜像,我不知道为什么。基本上,我将尝试使用GL_TRIANGLE_FAN在0,0处绘制纹理,纹理在右上角显示为法线(或多或少),但也在左下角显示为镜像。如果我用相同的纹理填充大部分或全部视口区域,结果会出现丑陋的z形重叠 截图将更公正地做到这一点 原始图像: 在(0,0)处绘制80x80 在(0,0)处绘制100x180 在(0,0)处绘制320x480 这是我的代码: 设置视

我实在想不起来:

我无法让帧缓冲区工作,但我现在已经开始工作了。然而,在帧缓冲区生成的纹理上出现了这种不可思议的镜像,我不知道为什么。基本上,我将尝试使用GL_TRIANGLE_FAN在0,0处绘制纹理,纹理在右上角显示为法线(或多或少),但也在左下角显示为镜像。如果我用相同的纹理填充大部分或全部视口区域,结果会出现丑陋的z形重叠

截图将更公正地做到这一点

原始图像:

在(0,0)处绘制80x80

在(0,0)处绘制100x180

在(0,0)处绘制320x480

这是我的代码:

设置视图:

//Apply the 2D orthographic perspective.
glViewport(0,0,320,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, 320, 480, 0, -10000.0f, 100.0f);

//Disable depth testing.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);

//Enable vertext and texture coordinate arrays.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glShadeModel(GL_SMOOTH);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);   

glGetError(); // Clear error codes

sprite = [Sprite createSpriteFromImage:@"TestSprite.png"];
[sprite retain];

[self createTextureBuffer];
创建纹理缓冲区

- (void) createTextureBuffer
{
    // generate texture
    glGenTextures(1, &bufferTexture);
    glBindTexture(GL_TEXTURE_2D, bufferTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0,  GL_RGBA, GL_UNSIGNED_BYTE, NULL);     // check if this is right

    // generate FBO
    glGenFramebuffersOES(1, &framebuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
    // associate texture with FBO
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, bufferTexture, 0);

    // clear texture bind
    glBindTexture(GL_TEXTURE_2D,0);

    // check if it worked (probably worth doing :) )
    GLuint status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
    if (status != GL_FRAMEBUFFER_COMPLETE_OES)
    {
        printf("FBO didn't work...");
    }   
}
运行渲染循环

- (void)drawView
{
    [self drawToTextureBuffer];

    // Make sure that you are drawing to the current context
    [EAGLContext setCurrentContext:context];

    //Bind the GLView's buffer.
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, 320, 480);

    //Clear the graphics context.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //Push the matrix so we can keep it as it was previously.
    glPushMatrix();

    //Rotate to match landscape mode.
    glRotatef(90.0, 0, 0, 1);
    glTranslatef(0.0f, -320.0f, 0.0f);

    //Store the coordinates/dimensions from the rectangle.
    float x = 0.0f;
    float y = 0.0f;
    float w = 480.0f;
    float h = 320.0f;

    // Set up an array of values to use as the sprite vertices.
    GLfloat vertices[] =
    {
        x,  y,
        x,  y+h,
        x+w,    y+h,
        x+w,    y
    };

    // Set up an array of values for the texture coordinates.
    GLfloat texcoords[] =
    {
        0,  0,
        0,  1,
        1,  1,
        0,  1
    };

    //Render the vertices by pointing to the arrays.
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

    // Set the texture parameters to use a linear filter when minifying.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    //Enable 2D textures.
    glEnable(GL_TEXTURE_2D);

    //Bind this texture.
    glBindTexture(GL_TEXTURE_2D, bufferTexture);

    //Finally draw the arrays.
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    //Restore the model view matrix to prevent contamination.
    glPopMatrix();

    GLenum err = glGetError();
    if (err != GL_NO_ERROR)
    {
        NSLog(@"Error on draw. glError: 0x%04X", err);
    }


    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
在第一个过程中,渲染循环将图像绘制到FBO中

- (void)drawToTextureBuffer
{
    if (!bufferWasCreated)
    {
        // render to FBO
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
        // set the viewport as the FBO isn't be the same dimension as the screen
        glViewport(0, 0, 512, 512);

        glPushMatrix();



        //Store the coordinates/dimensions from the rectangle.
        float x = 0.0f;
        float y = 0.0f;
        float w = 320.0f;
        float h = 480.0f;

        // Set up an array of values to use as the sprite vertices.
        GLfloat vertices[] =
        {
            x,  y,
            x,  y+h,
            x+w,    y+h,
            x+w,    y
        };

        // Set up an array of values for the texture coordinates.
        GLfloat texcoords[] =
        {
            0,  0,
            0,  1,
            1,  1,
            1,  0
        };

        //Render the vertices by pointing to the arrays.
        glVertexPointer(2, GL_FLOAT, 0, vertices);
        glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

        // Set the texture parameters to use a linear filter when minifying.
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        //Allow transparency and blending.
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        //Enable 2D textures.
        glEnable(GL_TEXTURE_2D);

        //Bind this texture.
        glBindTexture(GL_TEXTURE_2D, sprite.texture);

        //Finally draw the arrays.
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

        //Restore the model view matrix to prevent contamination.
        glPopMatrix();

        GLenum err = glGetError();
        if (err != GL_NO_ERROR)
        {
            NSLog(@"Error on draw. glError: 0x%04X", err);
        }

        //Unbind this buffer.
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);

        bufferWasCreated = YES;
    }
}

您在
-(void)drawView
中的
texcoords
似乎有误

GLfloat texcoords[] =
{
    0,  0,
    0,  1,
    1,  1,
    0,  1     << HERE should be 1, 0
};
GLfloat texcoords[]=
{
0,  0,
0,  1,
1,  1,

0,1您在
-(void)drawView
中的
texcoords
似乎是错误的

GLfloat texcoords[] =
{
    0,  0,
    0,  1,
    1,  1,
    0,  1     << HERE should be 1, 0
};
GLfloat texcoords[]=
{
0,  0,
0,  1,
1,  1,

0,1哈哈哈,他们是。非常感谢。哈哈哈。哇,多么愚蠢的错误。如果我不是FBO新手,我会意识到这个问题不可能与FBO有关。现在我知道了!哈哈哈,他们是。非常感谢。哈哈哈。哇,多么愚蠢的错误。如果我不是FBO新手,我会意识到这个问题不可能与FBO有关。现在我知道了知道!