您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> IOS >> iphone openGLES加载纹理问题

iphone openGLES加载纹理问题

来源:网络整理     时间:2016/4/10 22:12:38     关键词:opengles,iphone

关于网友提出的“iphone openGLES加载纹理问题”问题疑问,本网通过在网上对“iphone openGLES加载纹理问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: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);
结果片是上翻转,请问,这是为什么呀?


解决方案1:

因为opengl的坐标系是+y向上,图片坐标系是+y向下,你要把图片倒一下,或者把v倒一下


以上介绍了“iphone openGLES加载纹理问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/639917.html

相关图片

相关文章