2010-11-19 51 views
3

我有點卡在平鋪紋理。基本上我想在轉換對象時平鋪紋理。所以一個1x1x1的正方形當我glScale到20,20,20時,我想紋理平鋪。質地設置爲1x1的,iPhone OpenGL:平鋪紋理

- (void)loadTextures:(NSString *)textureName andWithIndex:(int)index { 

// load image as a CG ref 
CGImageRef textureImage = [UIImage imageNamed:textureName].CGImage; 
// if failed, bail 
if (!textureImage) { 
    NSLog(@"Error: failed to load texture"); 
    return; 
} 


// figure out the width and height 
int texWidth = CGImageGetWidth(textureImage); 
int texHeight = CGImageGetHeight(textureImage); 

// alloc space for the texture 
GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4); 

// create a CA context ref 
CGContextRef textureContext = CGBitmapContextCreate( 
       textureData, texWidth, texHeight, 8, texWidth * 4, 
       CGImageGetColorSpace(textureImage), 
       kCGImageAlphaPremultipliedLast 
      ); 

// draw the image to in-memory buffer 
CGContextDrawImage(textureContext, CGRectMake(0,0,texWidth,texHeight), textureImage); 

// done with context - release it 
CGContextRelease(textureContext); 

// have GL create handle for our texture 
glGenTextures(1, &textures[index]); 

// tell GL that the image is 2D 
glBindTexture(GL_TEXTURE_2D, textures[index]); 

// send our data down to GL, copy into graphics hardware 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); 

// free our in-memory copy of the data 
free(textureData); 

// specify min/max filters 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

// tell GL to turn on textures 
glEnable(GL_TEXTURE_2D); 
} 

回答

3
glMatrixMode(GL_TEXTURE); 
glLoadIdentity(); 
glScalef(20,20,1); 
glMatrixMode(GL_MODELVIEW); 
+0

這似乎舒展質感 – Burf2000 2010-11-19 22:14:49

+1

是我的錯,那工作一種享受 – Burf2000 2010-11-19 22:46:34