0

我有 6 个正方形,由 2 个三角形组成,每个正方形都应该有不同的纹理映射到它上面。相反,每个纹理上都有最后一个绑定的纹理,而不是它自己的。这是我的drawView和setView:

- (void)drawView:(GLView*)view 
{
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);


    static const Vertex3D vertices[] = {
        {0,0, 1}, //TL
        { 1024,0, 1}, //TR
        {0,-1024, 1}, //BL
        { 1024.0f, -1024.0f, 1}  //BR
    };


    static const GLfloat texCoords[] = {
        0.0, 1.0,
        1.0, 1.0,
        0.0, 0.0,
        1.0, 0.0
    };

    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

}

- (void)setupView:(GLView*)view {   
    // Bind the number of textures we need.
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP,GL_TRUE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glLoadIdentity();


    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"jpg"];
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:texData];

    if (image == nil)
        NSLog(@"Do real error checking here");

    GLuint width = CGImageGetWidth(image.CGImage);
    GLuint height = CGImageGetHeight(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc( height * width * 4 );
    CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );

    // Flip the Y-axis
    CGContextTranslateCTM (context, 0, height);
    CGContextScaleCTM (context, 1.0, -1.0);

    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
    CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    CGContextRelease(context);

    free(imageData);


}
4

2 回答 2

0

你总是使用纹理[0],所以你确实每次都会得到相同的纹理。您需要将您想要的纹理的 id 传递给 glBindTexture()。

于 2012-01-04T06:42:00.280 回答
0

我认为问题与纹理绑定有关,特别是与这一行有关:

glBindTexture(GL_TEXTURE_2D, 纹理[0]);

仔细检查您是否使用了纹理绑定所需的正确粘合值。

你在使用着色器吗?万一也仔细检查它,尽管很可能不是这样。

我建议使用纹理图集,以免每次在 GPU 中绑定不同的纹理时都会影响整体引擎的性能。

于 2012-01-04T11:11:06.217 回答