2011-02-26 37 views
1

我在GL中重寫了一些運行速度不夠快的核心動畫代碼。核心動畫/ GLES2.0:從CALayer獲取位圖到GL紋理

在我以前的版本中,每個按鈕都由CALayer表示,其中包含整體形狀和文本內容的子圖層。

我想要做的是設置.setRasterize什麼= YES這一層上,迫使它呈現到自己的內部位圖,然後發送到我的GL代碼,目前:

// Create A 512x512 greyscale texture 
{ 
    // MUST be power of 2 for W & H or FAILS! 
    GLuint W = 512, H = 512; 

    printf("Generating texture: [%d, %d]\n", W, H); 

    // Create a pretty greyscale pixel pattern 
    GLubyte *P = calloc(1, (W * H * 4 * sizeof(GLubyte))); 

    for (GLuint i = 0; (i < H); ++i) 
    { 
     for (GLuint j = 0; (j < W); ++j) 
     { 
      P[((i * W + j) * 4 + 0)] = 
      P[((i * W + j) * 4 + 1)] = 
      P[((i * W + j) * 4 + 2)] = 
      P[((i * W + j) * 4 + 3)] = (i^j); 
     } 
    }  

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

    // make it the ACTIVE texture, ie functions like glTexImage2D will 
    // automatically know to use THIS texture 
    glBindTexture(GL_TEXTURE_2D, greyscaleTexture); 

    // set some params on the ACTIVE texture 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 

    // WRITE/COPY from P into active texture 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, H, 0, GL_RGBA, GL_UNSIGNED_BYTE, P); 

    free(P); 

    glLogAndFlushErrors(); 
} 

會有人幫助我一起補丁?

編輯:我實際上想要創建一個黑色和白色的面具,所以每個像素要麼是0x00或0xFF,然後我可以做一堆四邊形,並且對於每個四邊形,我可以將其所有頂點設置爲特定顏色。因此,我可以很容易地從同一個模板得到不同顏色的按鈕...

回答