将纹理映射到一个圆圈

时间:2014-03-11 00:22:31

标签: objective-c opengl-es texture-mapping

我正在尝试将纹理添加到我目前使用三角形风扇创建的圆圈中。圆圈的图像如下。缺少一个三角形,不知道为什么。除此之外,我还有一个纹理,我希望将其映射到圆形颜色的圆圈上。

这是我画“球”的地方:

-(void)drawBall:(ImageTextures *) textures;
{

float angle, radian, x, y;
float radius = self.diameter/2;

[textures bindTextureAt:0];

GLfloat vertexBuffer[362];
vertexBuffer[0] = self.x_Pos;
vertexBuffer[1] = self.y_Pos;
int count = 2;

for (angle = 0.0; angle <= 362.0; angle += 2.0) {
    radian = angle * (3.14/180.0f);

    float xcos = (float)cos(radian);
    float ysin = (float)sin(radian);
    x = xcos * radius  + self.x_Pos;
    y = ysin * radius  + self.y_Pos;

    vertexBuffer[count] = x;
    count = count + 1;
    vertexBuffer[count] = y;
    count = count + 1;
}

count = 0;


glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_TRIANGLE_FAN, 0, 180);

glColor4f(redValue, greenValue, blueValue, 0.1f);

glDisable(GL_TEXTURE_2D);

}

这是我实际加载纹理的地方:

- (void)loadTextureAt : (int) at from:(NSString *)file {
CGImageRef texImage = [UIImage imageNamed:file].CGImage;
if ( ! texImage ) {
    NSLog(@"Texture file not found!");
    return;
}

int texWidth = (int)CGImageGetWidth(texImage);
int texHeight = (int)CGImageGetHeight(texImage);

GLubyte *texData = (GLubyte*)malloc(texWidth * texHeight * 4);

CGContextRef texContext = CGBitmapContextCreate(texData, texWidth, texHeight, 8, texWidth*4,
                                                CGImageGetColorSpace(texImage), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(texContext, CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight), texImage);
CGContextRelease(texContext);

glBindTexture(GL_TEXTURE_2D, _textureIDs[at]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);

free(texData);
}

然后我调用使用:

绑定纹理
- (void)bindTextureAt : (int) at {
glEnable(GL_TEXTURE_2D);

glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glBindTexture(GL_TEXTURE_2D, _textureIDs[at]);
};

这就是我称之为绘制球以在屏幕上绘制所有球的地方:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);

for (int i = 0; i < [self.balls count]; i++) {
    [self.balls[i] drawBall: _textures];

}

}

任何帮助都将被赞赏这是现在球的形象和我想要映射到球的纹理。

Ball Missing one triangle The texture

0 个答案:

没有答案