0

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

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

我正在创建这样的纹理:

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

}
4

1 回答 1

1

通常,这是通过作为alpha 贴图的第二个纹理完成的。在着色器中,alpha 纹理将具有完全不透明的区域和完全透明的其他区域。阿尔法纹理的阿尔法通道乘以图像纹理阿尔法得到最终的颜色。

于 2013-02-21T19:01:00.687 回答