IOS用OpenGL替换CATiledLayer

时间:2013-07-18 12:53:45

标签: iphone ios opengl-es catiledlayer

我有一个远程控制器应用,它使用 CATiledLayer 来显示远程桌面。所有绘图均由 drawRect: 方法完成。调整远程屏幕分辨率时,我有一些非常罕见的崩溃。

我试图调试此问题,但没有任何帮助,所以(在收到我的智慧大学的一些建议后)我决定使用 CAEAGLLayer 而不是<重写我的代码强> CATiledLayer

我正在学习 OpenGL ES 2.0 ,但所有这些教程都是关于构建3D模型的。我想要的就是能够将纹理应用到View上的适当区域。

根据我的阅读和思考,我认为,当收到要重绘的图像时,我需要将其转换为纹理,获取 rect < / strong>应该重新生成并从中生成椎骨,然后路径 varteces 纹理 < / strong>到 顶点着色器

欢迎任何想法或建议。

(请不要写下这样的内容:“为什么你要使用OpenGL而不是CATiledLayer?”即使它不是最好的选择我也想知道如何让它以这种方式工作)

1 个答案:

答案 0 :(得分:0)

好的,既然对我的问题没有回答,我会分享我所拥有的。 首先,我使用 OpenGL ES 2.0

1)我画了两个三角形来覆盖整个屏幕

enter image description here

存储顶点和三角形我使用以下结构(在我的情况下不使用 颜色 值):

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} Vertex;

const Vertex Vertices[] = {
    {{1, -1, 0}, {1, 0, 0, 1}, {1, 0}},
    {{1, 1, 0}, {0, 1, 0, 1}, {1, 1}},
    {{-1, 1, 0}, {0, 0, 1, 1}, {0, 1}},
    {{-1, -1, 0}, {0, 0, 0, 1}, {0, 0}}
};

const GLubyte Indices[] = {
    0, 1, 2,
    2, 3, 0
};

2)完成后我等待整个远程屏幕加载图像,然后使用以下代码将整个屏幕的图像设置为纹理

-(void)setupTextureFromCGImage:(CGImageRef)imageRef{

    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithBool:_snapshot.isImageMirrored],
                              GLKTextureLoaderOriginBottomLeft,
                              nil];

    NSError * error;
    GLKTextureInfo * info = [GLKTextureLoader textureWithCGImage:imageRef options:options error:&error];
    if (info == nil) {
        NSLog(@"Error loading file: %@", [error localizedDescription]);
        return;
    }
    self.effect.texture2d0.name = info.name;
    self.effect.texture2d0.enabled = true;
    _bigTexture = info.name;
    glBindTexture(GL_TEXTURE_2D, self.effect.texture2d0.name);
}

此代码使用 GLKit GLKTextureLoader ,但这可以简单地替换为 glteximage2d()

在该视图看起来与此类似:

enter image description here

3)当收到更新时,我们需要更新图像和rect以知道要更新的部分:

-(void)loadSubImageTextureImageBuffer:(const char *)buffer inRect:(CGRect)rect
{
CGRect rectGL = rect;
rectGL.origin.y = _snapshot.imageRect.size.height - (rectGL.origin.y +     rectGL.size.height);// convert coordinates from UIView's upper left to OpenGL's lower left

glTexSubImage2D(GL_TEXTURE_2D,
                    0,
                    rectGL.origin.x,
                    rectGL.origin.y,
                    rectGL.size.width,
                    rectGL.size.height,
                    GL_BGRA,
                    GL_UNSIGNED_BYTE,
                    buffer);
}

对于绘图我使用的自定义着色器不是 GLKView ,它们只会将纹理映射到顶点。

OpenGL绘图的工作速度比 CATiledLayer 更快,但并非极端但仍然如此。我将在未来调查一些小问题。

希望这对某些人有用。如果您需要更多详细信息,请随时与我联系 haawaplus@gmail.com

<强> P.S

现在我暂停了我的 CATiledLayer - &gt; OpenGL ES 调查,因为我还有其他工作要做,但是当我回到OpenGL时,我会更新我的答案。