2011-02-26 38 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,然后我可以做一堆四边形,并且对于每个四边形,我可以将其所有顶点设置为特定颜色。因此,我可以很容易地从同一个模板得到不同颜色的按钮...

回答