-2

我试图通过绑定两个纹理来获得纹理。但屏幕上只显示一个纹理。下面的代码是我如何加载纹理..

- (void)loadTexture:(NSString *)name intoLocation:(GLuint)location {

CGImageRef textureImage = [UIImage imageNamed:name].CGImage;
if (textureImage == nil) {
    NSLog(@"Failed to load texture image");
    return;
}

NSInteger texWidth = CGImageGetWidth(textureImage);
NSInteger 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, 0.0, (float)texWidth, (float)texHeight), textureImage);
CGContextRelease(textureContext);
glActiveTexture(location);
glGenTextures(3, &location);
glBindTexture(GL_TEXTURE_2D,2);
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);}

这里是我调用纹理加载的方法:

- (BOOL)createFramebuffer {
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
                                kEAGLDrawablePropertyColorFormat, nil];

context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

if (!context || ![EAGLContext setCurrentContext:context]) {
    [self release];

}
    [self setupView];
 glGenTextures(0, &textures[3]);

[self loadTexture:@"image1.png" intoLocation:textures[1]];
[self loadTexture:@"image2.png" intoLocation:textures[0]];




glGenFramebuffersOES(2, &viewFramebuffer);
glGenRenderbuffersOES(2, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

if (USE_DEPTH_BUFFER) {
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
    NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
    return NO;
}

return YES;

}

我希望现在更清楚我需要什么。

4

1 回答 1

3

感谢您使用实际代码更新您的问题。现在,在您的代码中,有一件事让我觉得不寻常,它与您的变量有关location

glActiveTexture (location);
glGenTextures   (3, &location);
glBindTexture   (GL_TEXTURE_2D, 2);
glTexImage2D    (GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

glActiveTexture (...)设置所有修改 TMU(纹理映射单元)状态的调用所引用的纹理单元。在桌面 OpenGL 中,它可以具有以下范围内的任何值:GL_TEXTURE0to GL_TEXTURE0 + 79。OpenGL ES 2.0 保证 2 个 TMU 和 iOS(一直在 PowerVR SGX 硬件上运行,为您提供 8 个)。

我遇到的问题是,在使用变量设置活动纹理单元后location,您立即尝试生成 3 个纹理名称(句柄)并将它们存储在GLuint名为的数组中location

location不能既是纹理名称数组又是要绑定的 TMU。更重要的是,在glGenTextures (...)你绑定一个名为 的纹理之后2,这几乎没有意义。我希望看到下一次调用glBindTexture (...)followingglGenTextures (...)以引用数组中的某些内容:location [].

更糟糕的是,在调用之前[self loadTexture:@"image1.png" intoLocation:textures[1]],你有一行代码:glGenTextures(0, &textures[3]);它告诉 OpenGL 生成 0 个纹理名称并将它们存储在一个GLuint以地址开头的数组中:&textures [3]

您的代码有太多错误,无法理解您实际尝试做的事情。我认为您可能需要全面了解多纹理和纹理。

于 2013-09-28T18:40:18.163 回答