【问题标题】:IOS replace CATiledLayer with OpenGLIOS用OpenGL替换CATiledLayer
【发布时间】:2013-07-18 12:53:44
【问题描述】:

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

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

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

根据我的阅读和思考,我认为,当收到要重绘的图像时,我需要将其转换为纹理,得到rect 应该重新绘制并从中生成 verteces,然后是路径 vartecestexture顶点着色器

欢迎任何想法或建议。

(请不要写类似:“为什么要使用 OpenGL 而不是 CATiledLayer?”,即使这不是最佳选择,我也想弄清楚如何让它以这种方式工作) .

【问题讨论】:

    标签: iphone ios opengl-es catiledlayer


    【解决方案1】:

    好的,由于没有人回答我的问题,我将分享我所拥有的。 首先,我使用的是 OpenGL ES 2.0

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

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

    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) 完成后,我等待整个远程屏幕加载图像,然后使用以下代码将整个屏幕的图像设置为 texture

    -(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);
    }
    

    此代码使用 GLKitGLKTextureLoader,但是可以简单地将其替换为 glteximage2d()

    之后的视图看起来像这样:

    3) 当收到更新时,我们需要更新图像和矩形来知道要更新哪个部分:

    -(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

    附言

    现在我暂停了我的 CATiledLayer -> OpenGL ES 调查,因为我还有其他工作要做,但我会在返回 OpenGL 时更新我的​​答案。

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多