绘制开放gl纹理的一部分

时间:2013-10-14 18:43:00

标签: ios opengl-es-2.0 texture2d sprite-sheet

我试图只绘制部分精灵表。但它只是缩放图像而没有绘制图像宽度的1/3。如何裁剪图像,只有1/3显示。文字在下面(ios 7,opengl es 2) - (GLKMatrix4)modelMatrix {

GLKMatrix4 modelMatrix = GLKMatrix4Identity;
modelMatrix = GLKMatrix4Translate(modelMatrix, x, y, 0);
return modelMatrix;

}
- (void)setSprite:(NSString *)fileName effect:(GLKBaseEffect *)newEffect {

// 1
self.effect = newEffect;

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

// 3
NSError * error;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];

// 4
self.textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (self.textureInfo == nil) {
    NSLog(@"Error loading file: %@", [error localizedDescription]);
        return ;
}

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, 1);
newQuad.br.textureVertex = CGPointMake(1, 1);
newQuad.tl.textureVertex = CGPointMake(0, 0);
newQuad.tr.textureVertex = CGPointMake(1, 0);

self.quad = newQuad;

}

- (void)render {

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

// 2
y++;
self.effect.transform.modelviewMatrix = self.modelMatrix;
[self.effect prepareToDraw];

// 3
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);


// 4
TexturedQuad q;
q.bl.textureVertex = CGPointMake(0, 1);
q.br.textureVertex = CGPointMake(1, 1);
q.tl.textureVertex = CGPointMake(0, 0);
q.tr.textureVertex = CGPointMake(1, 0);
q.bl.geometryVertex = CGPointMake(0, 0);
q.br.geometryVertex = CGPointMake(self.textureInfo.width/3, 0);
q.tl.geometryVertex = CGPointMake(0, self.textureInfo.height);
q.tr.geometryVertex = CGPointMake(self.textureInfo.width/3, self.textureInfo.height);
long offset2 =(long)&q;
//long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset2 + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset2 + offsetof(TexturedVertex, textureVertex)));

// 5
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

}

更新:         TexturedQuad newQuad;     newQuad.bl.geometryVertex = CGPointMake(0,0);     newQuad.br.geometryVertex = CGPointMake(self.textureInfo.width / 3,0);     newQuad.tl.geometryVertex = CGPointMake(0,self.textureInfo.height);     newQuad.tr.geometryVertex = CGPointMake(self.textureInfo.width / 3,self.textureInfo.height);

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

1 个答案:

答案 0 :(得分:0)

这就是纹理坐标的用途。你的'textureVertex'指定了纹理的全尺寸(0.0到1.0)映射到四边形。您需要将其中一些值更改为1/3或2/3。例如:

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