0

Here's my texture binding code:

-(void) loadTexture:(NSString *)textureFilePath{
    NSImage *image=[NSImage imageNamed:textureFilePath];
    NSData *imageData = [image TIFFRepresentation];

    CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, nil);
    CGImageRef cgImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil);

    int width = image.size.width;
    int height = image.size.height;

    GLubyte *bitmapData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
    CGContextRef cgContext = CGBitmapContextCreate(bitmapData, width, height, 8, width * 4, CGImageGetColorSpace(cgImage), kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(cgContext, CGRectMake(0.0, 0.0,width,height), cgImage);
    CGContextRelease(cgContext);

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,bitmapData);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}

And there's part of my draw call involving texture mapping, the image I use is 512x512:

glBegin(GL_QUADS);
{
    glColor3f(1.0f, 1.0f, 1.0f);
    [self loadTexture:@"texture.png"];
    glTexCoord2i(0,1);glVertex3i(-1,1,0);
    glTexCoord2i(0,0);glVertex3i(-1,-1,0);
    glTexCoord2i(1,0);glVertex3i(1,-1,0);
    glTexCoord2i(1,1);glVertex3i(1,1,0);
}
glEnd();

And I've enabled texture mapping:

glEnable(GL_TEXTURE_2D);

The result is completely white…(And other part of the draw call that doesn't involving texture are correct.)

I've been searching possible solution for hours and I still can't tell what part is wrong.

Nathaniel_Wu
  • 191
  • 1
  • 7

0 Answers0