ios - 在 OpenGL ES 中隐藏部分图像

标签 ios objective-c opengl-es glkit

我正在使用 GLKView 在 iOS 应用程序中渲染一些 Sprite 。

我的问题是,如何仅删除/绘制一张图像的一部分?例如,我有一个背景,上面有一个图像(两个 Sprite )。我想从顶部的图像中取出一些随机矩形,以便背景在这些矩形中可见。这可能吗?

我正在创建这样的纹理:

   - (id)initWithFile:(NSString *)fileName effect:(GLKBaseEffect *)effect position:(GLKVector2)position{
    if ((self = [super init])) {
        self.effect = effect;

        NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithBool:YES],
                                  GLKTextureLoaderOriginBottomLeft, 
                                  nil];

        NSError * error;    
        NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
        self.textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];


        self.contentSize = CGSizeMake(self.textureInfo.width, self.textureInfo.height);


        TexturedQuad newQuad;
        newQuad.bl.geometryVertex = CGPointMake(0, 0);
        newQuad.br.geometryVertex = CGPointMake(self.textureInfo.width, 0);
        newQuad.tl.geometryVertex = CGPointMake(0, self.textureInfo.height);
        newQuad.tr.geometryVertex = CGPointMake(self.textureInfo.width, self.textureInfo.height);

        newQuad.bl.textureVertex = CGPointMake(0, 0);
        newQuad.br.textureVertex = CGPointMake(1, 0);
        newQuad.tl.textureVertex = CGPointMake(0, 1);
        newQuad.tr.textureVertex = CGPointMake(1, 1);
        self.quad = newQuad;


        self.position = position;

        self.frameHeight = self.textureInfo.height;

    }
    return self;
}

然后像这样渲染它们

- (void)render { 

    self.effect.texture2d0.name = self.textureInfo.name;
    self.effect.texture2d0.enabled = YES;        

    self.effect.transform.modelviewMatrix = self.modelMatrix;

    [self.effect prepareToDraw];

    long offset = (long)&_quad;

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

}

最佳答案

通常这是通过第二个纹理来完成的,即 alpha map .在着色器中,Alpha 纹理将具有完全不透明的区域和完全透明的其他区域。 Alpha纹理的Alpha channel 乘以图像纹理Alpha即可得到最终的颜色。

关于ios - 在 OpenGL ES 中隐藏部分图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009670/

相关文章:

android - 低级流媒体 : A new API based on Khronos OpenMAX AL 1. 0.1

android - 使用 Firebase 进行指纹认证

ios - GKImagePicker 链接器命令失败,退出代码为 1

ios - iOS (iPad/iPhone) 上未保留 SVG 透明度

android - 仅定位市场中运行速度快的 Android 设备

ios - OpenGL ES - iPhone4 - 片段着色器变量计数

iphone - 是否有关于如何在 OpenGL ES 1.1 中使用顶点缓冲区对象 (VBO) 的最小化示例?

objective-c - NSMutableArray 被释放了吗?

ios - 从上一个选定部分的 UITableView 中删除行

ios - AppCode 无法在模拟器和设备上运行