iOS非方形纹理上的OpenGL ES 2.0在渲染时会变形

时间:2012-10-16 06:41:49

标签: iphone ios opengl-es opengl-es-2.0

我正在开发iOS上的OpenGL ES 2.0,我有这个......

  1. 带有一些图像(大小为1024x768)的纹理(T1)。
  2. 关闭屏幕FBO(FBO1)关联纹理T1,以便我可以将某些内容呈现为T1。
  3. 相同尺寸的另一种纹理(T2)。
  4. 另一个与T2相关的FBO(FBO2)。 (我将用它渲染纹理T2)。
  5. 现在循环中,我将T1的内容渲染到FBO2中(因此T1转移到T2),然后将T2渲染到FBO1(T2被转移回T1)。

    经过几次迭代(仅7-8次)后,加载到T1的原始图像严重失真/模糊(好像它会出现水平模糊)。

    但如果我用方形图像做同样的事情(不需要2的幂),图像仍然清晰。

    这是我用来创建纹理的代码..

    GLuint texture = 0;
    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);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    

    这是我用来创建FBO的代码......

     glGenFramebuffers(1, &targetFBO);
     glBindFramebuffer(GL_FRAMEBUFFER, targetFBO);
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
    

    我的着色器是最基本的着色器,只需将纹理渲染为gl_FragColor。

    这是我的渲染Objective-C代码......

    - (void) draw {
        GLfloat textureCoordinates[] = {
            0.0f, 0.0f,
            1.0f, 0.0f,
            0.0f,  1.0f,
            1.0f,  1.0f,
        };
    
        GLfloat imageVertices[] = {
            -1.0f, -1.0f,
            1.0f, -1.0f,
            -1.0f,  1.0f,
            1.0f,  1.0f,
        };
    
        glViewport(0, 0, targetWidth, targetHeight);
        glBindFramebuffer(GL_FRAMEBUFFER, targetFBO);
        glUseProgram(program);
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, textureID);
        glUniform1i(inputTextureLocation, 2);
        glDisable(GL_BLEND);
    
        glVertexAttribPointer(positionLocation, 2, GL_FLOAT, 0, 0, imageVertices);
        glVertexAttribPointer(inputTextureCoordinateLocation, 2, GL_FLOAT, 0, 0, textureCoordinates);
        // Clear the screen
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
    
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);   
    }
    

    我的顶点着色器......

     attribute vec4 position;
     attribute vec4 inputTextureCoordinate;
    
     varying vec2 textureCoordinate;
    
     void main()
     {
         gl_Position = position;
         textureCoordinate = inputTextureCoordinate.xy;
     }
    

    片段着色器......

     varying highp vec2 textureCoordinate;
    
     uniform sampler2D inputTexture;
    
     void main()
     {
         gl_FragColor = texture2D(inputTexture, textureCoordinate);
     }
    

    原始图片... Original Image

    图片扭曲... Distorted image

    如何避免非方形纹理模糊?

    编辑:问题实际上是No-Power-of-Two纹理。不只是非正方形。我无法找到解决方案,因此我将使用POT纹理。

0 个答案:

没有答案