2010-02-14 63 views
0

好吧,我想要實現的是加載一個圖像作爲一個單一的資源,然後將它的不同部分保存爲許多不同的紋理(基本上是將圖像切分成小方塊,然後保存他們分開)。iPhone OpenGL紋理加載器問題

對於我的遊戲,僅僅將原始圖像的不同部分映射到我的形狀將不起作用,並且能夠將每個「瓦片」作爲單獨的紋理進行渲染。

下面是我用於我的紋理加載器的代碼。我已經嘗試了加載的紋理的寬度和高度,但得到了一些奇怪的結果。

任何建議將不勝感激。

感謝

glBindTexture(GL_TEXTURE_2D, texture[0]); 

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 

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

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); 
CGColorSpaceRelease(colorSpace); 
CGContextClearRect(context, CGRectMake(0, 0, width, height)); 
CGContextTranslateCTM(context, 0, height - 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); 
[image release]; 
[texData release]; 
+0

@ simo311:我已將您的代碼重新格式化以顯示沒有水平滾動條的代碼。如果您不同意,請隨時回滾我的更改。 –

回答

0

好,以防萬一有人想做到這一點,我理解了它。您只需修改CGContextDrawImage行並更改寬度和高度參數即可。

這會導致整個紋理被加載,但只會繪製該行指定的區域。

基本上,

CGContextDrawImage(上下文,CGRectMake(0,0,寬度* 2,高度* 2),image.CGImage);

將繪製1/4的圖像(因爲它的高度和寬度都超過了紋理尺寸的兩倍)。

:)