OpenGL:ES on iPhone - Texture2D size and display questions
I'm having issues with Texture2D and I'd like to understand how to use it better.
I've taken the Crashlander Texture2D class from here and a default OpenGL project in XCode 4, forcing it to load OpenGL ES1.1
First, a conceptual question. The size on the Texture2D init method is clearly an OpenGL size, but what relation to the OpenGL world does the fontSize parameter have?
Second, debugging. The result I get from the code below is a black (Or whatever colour I set in glColor) square where the text should be.
Here's the changes I've made in my code:
- (void)awakeFromNib
{
EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!aContext) {
aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
}
self.labelAtTheTop = [[[Texture2D alloc] initWithString:@"Some Text" dimensions:CGSizeMake(1, 1) alignment:UITextAlignmentLeft fontName:@"Helvetica" fontSize:14.0f] autorelease];
if (!aContext)
NSLog(@"Failed to create ES context");
else if (![EAGLContext setCurrentContext:aContext])
NSLog(@"Failed to set ES context current");
self.context = aContext;
[aContext release];
[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];
animating = FALSE;
animationFrameInterval = 1;
self.displayLink = nil;
}
- (void)drawFrame
{
[(EAGLView *)self.view setFramebuffer];
// Replace the implementation of this method to do your own custom drawing.
glClearColor(0.5f开发者_如何学C, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glPushMatrix();
glLoadIdentity();
[self.labelAtTheTop drawAtPoint:CGPointMake(0, 0)];
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
// Disable modes so they don't interfere with other parts of the program
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
[(EAGLView *)self.view presentFramebuffer];
}
Crashlander is really an old code base, so I would suggest avoiding it. There is a perfectly good 2D engine for the iPhone called Cocos2D http://www.cocos2d-iphone.org/. About the code, try commenting glDisable(GL_COLOR_MATERIAL);
plus glColor4f(0,0,0,1);
actually represents black color, try commenting this too. I think fontSize is the size of font in screen points.
[EDIT]
If you want to learn something about OpenGLES here is a good intro tutorial http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html
精彩评论