当前位置: 代码迷 >> Iphone >> iphone openGLES加载纹理有关问题
  详细解决方案

iphone openGLES加载纹理有关问题

热度:102   发布时间:2016-04-25 06:50:57.0
iphone openGLES加载纹理问题
用下面代码加载了纹理:
CGImageRef textureImage = [UIImage imageNamed:@"apple128.png"].CGImage;
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);
glGenTextures(1, &tex[0]);
glBindTexture(GL_TEXTURE_2D, tex[0]);
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);

然后绘制:
static const GLfloat vertices[] = 
{
-30, 30, -0.0, 
-30, -30, -0.0, 
30, -30, -0.0, 
30, 30, -0.0,
};


static const GLfloat texCoords[] = 
{
  0.0, 1.0,
  0.0, 0.0,
  1.0, 0.0,
  1.0, 1.0,
  };
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex[0]);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer( 2, GL_FLOAT, 0, texCoords );

  glVertexPointer(3, GL_FLOAT, 0, vertices);
  glEnableClientState(GL_VERTEX_ARRAY);  
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

结果片是上翻转,请问,这是为什么呀?

------解决方案--------------------
因为opengl的坐标系是+y向上,图片坐标系是+y向下,你要把图片倒一下,或者把v倒一下
  相关解决方案