Iphone 尝试使用帧缓冲区渲染到纹理始终会导致白色纹理

Iphone 尝试使用帧缓冲区渲染到纹理始终会导致白色纹理,iphone,opengl-es,framebuffer,Iphone,Opengl Es,Framebuffer,使用StackOverflow中的两篇文章,我使用帧缓冲区创建了一个简单的纹理渲染 glGetError(); //Generate the texture that we will draw to (saves us a lot of processor). glEnable(GL_TEXTURE_2D); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Use

使用StackOverflow中的两篇文章,我使用帧缓冲区创建了一个简单的纹理渲染

    glGetError();

    //Generate the texture that we will draw to (saves us a lot of processor).
    glEnable(GL_TEXTURE_2D);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Use OpenGL ES to generate a name for the texture.
    // Pass by reference so that our texture variable gets set.
    glGenTextures(1, &texture);

    // Bind the texture name. 
    glBindTexture(GL_TEXTURE_2D, texture);

    // Specify a 2D texture image, providing a pointer to the image data in memory.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    //Create a frame buffer to draw to. This will allow us to directly edit the texture.
    GLint oldFBO;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO);
    glGenFramebuffersOES(1, &textureFrameBuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO);

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

GLint oldFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO);

//Bind our frame buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);

//Clear out the texture.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Draw the letters to the frame buffer. (calls glDrawArrays a bunch of times, binds various textures, etc.) Does everything in 2D.
[self renderDialog:displayString withSprite:displaySprite withName:displaySpriteName];

//Unbind the frame buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO);

GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
    NSLog(@"Error on string creation. glError: 0x%04X", err);
}
这里的问题是它不起作用。在混合中有些东西被破坏了,因为我的最终纹理只是一个白色的正方形。我没有任何gl错误。这是我的密码

声明实例变量

GLuint texture;
GLuint textureFrameBuffer;
生成纹理和帧缓冲区

    glGetError();

    //Generate the texture that we will draw to (saves us a lot of processor).
    glEnable(GL_TEXTURE_2D);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Use OpenGL ES to generate a name for the texture.
    // Pass by reference so that our texture variable gets set.
    glGenTextures(1, &texture);

    // Bind the texture name. 
    glBindTexture(GL_TEXTURE_2D, texture);

    // Specify a 2D texture image, providing a pointer to the image data in memory.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    //Create a frame buffer to draw to. This will allow us to directly edit the texture.
    GLint oldFBO;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO);
    glGenFramebuffersOES(1, &textureFrameBuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO);

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

GLint oldFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO);

//Bind our frame buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);

//Clear out the texture.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Draw the letters to the frame buffer. (calls glDrawArrays a bunch of times, binds various textures, etc.) Does everything in 2D.
[self renderDialog:displayString withSprite:displaySprite withName:displaySpriteName];

//Unbind the frame buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO);

GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
    NSLog(@"Error on string creation. glError: 0x%04X", err);
}
在帧缓冲区中绘制一个大字符串

    glGetError();

    //Generate the texture that we will draw to (saves us a lot of processor).
    glEnable(GL_TEXTURE_2D);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    // Use OpenGL ES to generate a name for the texture.
    // Pass by reference so that our texture variable gets set.
    glGenTextures(1, &texture);

    // Bind the texture name. 
    glBindTexture(GL_TEXTURE_2D, texture);

    // Specify a 2D texture image, providing a pointer to the image data in memory.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    //Create a frame buffer to draw to. This will allow us to directly edit the texture.
    GLint oldFBO;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO);
    glGenFramebuffersOES(1, &textureFrameBuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO);

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

GLint oldFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldFBO);

//Bind our frame buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);

//Clear out the texture.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Draw the letters to the frame buffer. (calls glDrawArrays a bunch of times, binds various textures, etc.) Does everything in 2D.
[self renderDialog:displayString withSprite:displaySprite withName:displaySpriteName];

//Unbind the frame buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO);

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

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    glGetError();

    //Draw the text.
    [EAGLView enable2D];

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

    //Store the coordinates/dimensions from the rectangle.
    float x = 0;
    float y = [Globals getPlayableHeight] - dialogRect.size.height;
    float w = [Globals getPlayableWidth];
    float h = dialogRect.size.height;

    // 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,          h / 128,
        w / 512,    h / 128,
        w / 512,    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);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    //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.
    [EAGLView bindTexture: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);
    }

我称之为工作的任何外部事物在其他环境中都很好。有什么想法吗?我对帧缓冲区几乎一无所知,所以任何关于故障排除的帮助都会很好。

纹理参数是基于每个纹理设置的。在创建或绑定要渲染的纹理之前,您发布的代码似乎正在设置
GL\u TEXTURE\u MIN\u FILTER
。如果没有在其他地方设置过滤器,并且没有为其余级别指定纹理图像,则纹理可能不完整,这就是为什么会变白


作为将来的参考,设置帧缓冲区后不存在GL错误并不意味着帧缓冲区可用于渲染。您还应通过调用
glcheckframeBufferStatus(GL\u framebuffer\OES)
并验证返回
GL\u framebuffer\u complete
来检查framebuffer是否已完成。

我将尝试。事实上,我以前做过完成检查,这是真的——我只是没有把它包含在上面的代码中。如果这个帧缓冲区是完整的,我想这并不一定意味着一切都能正常工作,对吗?另外,我不确定我是否了解GL_TEXTURE_MIN_FILTER-我在调用glGenTextures之前设置了它(请参见第一个代码块)。每个纹理都有自己的缩小/放大功能和包裹模式。调用glTexParameterf仅影响调用函数时当前绑定的纹理。如果要更改要渲染到的纹理的缩小功能,则需要在创建和绑定纹理后调用glTexParameterf。听起来,帧缓冲区完整性与您的特定情况无关,但最好记住更奇特的帧缓冲区对象配置。