2011-05-20 70 views
6

我敢肯定,在經過數小時的抨擊之後,這是無法完成的。iOS:如何在GLES2中渲染到1通道紋理

這是我迄今爲止的代碼(BELOW),用於渲染紋理;它完成這項工作,但是非常浪費。我只想要一個8位通道,可能是16位灰度。我不想要一個無用的R G和B頻道。

但是,如果我嘗試切換GL_RGBA/* * GL_ALPHA /在glTexImage2D,glCheckFramebufferStatus接球 '幀緩衝不完整的' 錯誤:36054 0x8CD6 GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT

在IRC傢伙建議GL_R但Xcode中不會爲提供autocompleteion ,所以它看起來可能是GLES

修剪掉的那些東西之一,但這看起來真的很奇怪!這是一個移動設備。當然,有些東西會減少四分之一的執行操作所需的位數......當然,這將是最後要做的事情之一。

?!?

任何人都可以明確地埋葬這一個嗎?是否可以在GLES2.0中渲染到單個通道紋理?

+ (void) blurTexture: (GLuint)  in_texId 
      POTWidth: (GLuint)  in_W 
      POTHeight: (GLuint)  in_H 
     returningTexId: (GLuint*) out_pTexId 
{ 
    // search HELP: 'Using a Framebuffer Object as a Texture' 
    // http://stackoverflow.com/questions/3613889/gl-framebuffer-incomplete-attachment-when-trying-to-attach-texture 

    // Create the destination texture, and attach it to the framebuffer’s color attachment point. 

    // create the texture 
    GLuint id_texDest; 
    { 
     // fragshader will use 0 
     glActiveTexture(GL_TEXTURE0); 

     // Ask GL to give us a texture-ID for us to use 
     glGenTextures(1, & id_texDest); 
     glBindTexture(GL_TEXTURE_2D, id_texDest); 


     // actually allocate memory for this texture 
     GLuint pixCount = in_W * in_H; 

     typedef struct { uint8_t r, g, b, a } rgba; 

     rgba * alphas = calloc(pixCount, sizeof(rgba)); 

     // XOR texture 
     int pix=0; 
     for (int x = 0; x < in_W; x++) 
     { 
      for (int y = 0; y < in_H; y++) 
      { 
       //alphas[ pix ].r = (y < 256) ? x^y : 0; 
       //alphas[ pix ].g = (y < 512) ? 127 : 0; 
       //alphas[ pix ].b = (y < 768) ? 127 : 0; 
       alphas[ pix ].a = (y < 512) ? x^y : 0; // half mottled, half black 

       pix++; 
      } 
     } 

     // set some params on the ACTIVE texture 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/ ); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); 

     // WRITE/COPY from P into active texture 
     glTexImage2D(GL_TEXTURE_2D, 0, 
        GL_RGBA /*GL_ALPHA*/, in_W, in_H, 0, 
        GL_RGBA /*GL_ALPHA*/, 
        GL_UNSIGNED_BYTE, 
        (void *) alphas); 

     //glGenerateMipmap(GL_TEXTURE_2D); 

     free(alphas); 

     glLogAndFlushErrors(); 

    } 


    GLuint textureFrameBuffer; 
    { 
     GLint oldFBO; 
     glGetIntegerv(GL_FRAMEBUFFER_BINDING, & oldFBO); 

     // create framebuffer 
     glGenFramebuffers(1, & textureFrameBuffer); 
     glBindFramebuffer(GL_FRAMEBUFFER, textureFrameBuffer); 

     // attach renderbuffer 
     glFramebufferTexture2D(GL_FRAMEBUFFER, 
           GL_COLOR_ATTACHMENT0, 
           GL_TEXTURE_2D, 
           id_texDest, 
           0); 

     //glDisable(GL_DEPTH_TEST); 

     // Test the framebuffer for completeness. This test only needs to be performed when the framebuffer’s configuration changes. 
     GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ; 
     NSAssert1(status == GL_FRAMEBUFFER_COMPLETE, @"failed to make complete framebuffer object %x", status); 

     // 36054 0x8CD6 GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 

     // unbind frame buffer 
     glBindFramebuffer(GL_FRAMEBUFFER, oldFBO); 
    } 




    glLogAndFlushErrors(); 

    // clear texture bitmap to backcolor 
    { 
     GLint oldFBO; 
     glGetIntegerv(GL_FRAMEBUFFER_BINDING, & oldFBO); 

     glBindFramebuffer(GL_FRAMEBUFFER, textureFrameBuffer); 
     glLogAndFlushErrors(); 

     { 
      float j = 0.1f; 
      glClearColor(j, j, j, j); 
      glLogAndFlushErrors(); 
      glClear(GL_COLOR_BUFFER_BIT); 
      glLogAndFlushErrors(); 
     } 

     glBindFramebuffer(GL_FRAMEBUFFER, oldFBO); 
    } 

    glDeleteFramebuffers(1, & textureFrameBuffer); 

    * out_pTexId = id_texDest; 

    glLogAndFlushErrors(); 
} 
+0

我認爲這將有可能渲染成RGBA紋理,然後使用glTexSubImage2D構造一個遍歷每個第4個字節的子紋理......我不知道是否有任何簡單的方法將其另存爲單獨的紋理(在這種情況下,原始可能會被刪除) – 2011-06-16 11:51:43

回答

6

從iOS 5.0開始,您可以使用GL_EXT_texture_rg渲染1和2通道紋理。

http://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_rg.txt

http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS5.html#//apple_ref/doc/uid/TP30915195-SW47

編輯:我測試過這一點,它的工作原理,但僅限於A5及以上(的iPad2/3/4 /迷你,iPhone4S的/ 5,iPod touch的第五代)。不幸的是,它不適用於A4和更舊的地方(iPhone4,iPod touch 4th gen,3GS,iPad1)。

+0

此擴展也可用於Mac OS X的OpenGL嗎? – 2014-08-12 13:16:24

4

不,OpenGL ES 2.0沒有任何可渲染的單聲道紋理格式。 GL_ALPHA和GL_LUMINANCE不可呈現,因此它們對RTT無用。

+1

謝謝!什麼是明智的解決方法? – 2011-05-24 04:19:25