Use the following code to load the textures:
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);
Then draw:
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);
Flip-chip is the result, I ask, why is it?
Reply:
Because opengl + y coordinate system is up, the picture + y coordinate system is down, you take the picture down look, or to look down v
Reply:
No comments:
Post a Comment