2013-09-28 74 views
-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; 

}

我現在希望是更多清楚我需要。

+2

請解釋你正在嘗試做什麼。 「合併兩個紋理」沒有進一步的上下文可能意味着什麼也沒有。同時解釋一下到目前爲止你所嘗試的是什麼。 – derhass

+0

我懷疑當你說「合併」你真正的意思是「混合」。在這種情況下,您實際上不會獲得紋理,但會將使用一種紋理進行繪製的結果與之前的結果(例如其他紋理)相結合以生成新像素。在很多情況下(例如阿爾法混合),這是一個依賴訂單的操作。無論如何,沒有看到任何類型的代碼或更好的描述,無法回答這個問題。 –

回答

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_TEXTURE0GL_TEXTURE0 + 79。 OpenGL ES 2.0保證了2個TMU和iOS(它總是在PowerVR SGX硬件上運行,爲您提供8個)。

我的問題是,使用可變location設置活動紋理單元后,您嘗試生成3個紋理名稱(句柄),並將它們存儲在GLuint數組命名location

location不能既是一個紋理名稱數組,也是你想要綁定的TMU。更重要的是,在glGenTextures (...)之後,您立即綁定了一個名爲2的紋理,這很沒有意義。我期望看到glGenTextures (...)之後的glBindTexture (...)的下一個呼叫,以引用來自陣列內的某些東西:location []

使事情變得更糟,呼籲[self loadTexture:@"image1.png" intoLocation:textures[1]]之前,你有一行代碼:glGenTextures(0, &textures[3]);它告訴OpenGL生成紋理0名,並將它們存儲在GLuint一個數組中的地址開始:&textures [3]

你的代碼有太多的錯誤來理解你實際嘗試做什麼。我認爲你可能需要在一般情況下進行多紋理和紋理化。