专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Iphone

iphone openGLES加载纹理有关问题

发布时间:2010-05-30 01:25:22 文章来源:www.iduyao.cn 采编人员:星星草
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倒一下
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: