I got a little stuck on tiled textures. Basically, I want to focus the textures while transforming the object. So, 1x1x1 square, when I glScale to 20,20,20, I would like the texture to be tiled. The texture is set to 1x1,
- (void)loadTextures:(NSString *)textureName andWithIndex:(int)index {
CGImageRef textureImage = [UIImage imageNamed:textureName].CGImage;
if (!textureImage) {
NSLog(@"Error: failed to load texture");
return;
}
int texWidth = CGImageGetWidth(textureImage);
int texHeight = CGImageGetHeight(textureImage);
GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);
CGContextRef textureContext = CGBitmapContextCreate(
textureData, texWidth, texHeight, 8, texWidth * 4,
CGImageGetColorSpace(textureImage),
kCGImageAlphaPremultipliedLast
);
CGContextDrawImage(textureContext, CGRectMake(0,0,texWidth,texHeight), textureImage);
CGContextRelease(textureContext);
glGenTextures(1, &textures[index]);
glBindTexture(GL_TEXTURE_2D, textures[index]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
free(textureData);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
}
source
share