iPhone 上的 OpenGL:ES - Texture2D 大小和显示问题
我在使用 Texture2D 时遇到问题,我想了解如何更好地使用它。
我从 此处 获取了 Crashlander Texture2D 类以及 XCode 中的默认 OpenGL 项目4、强制其加载OpenGL ES1.1
首先,一个概念问题。 Texture2D init 方法中的大小显然是 OpenGL 大小,但是 fontSize 参数与 OpenGL 世界有什么关系呢?
二、调试。我从下面的代码中得到的结果是文本应该在的位置的黑色(或我在 glColor 中设置的任何颜色)正方形。
这是我在代码中所做的更改:
- (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, 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];
}
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, 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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Crashlander 确实是一个旧的代码库,所以我建议避免使用它。 iPhone 有一个非常好的 2D 引擎,名为 Cocos2D http://www.cocos2d-iphone.org/。关于代码,尝试注释
glDisable(GL_COLOR_MATERIAL);
加上glColor4f(0,0,0,1);
实际上代表黑色,也尝试注释它。我认为 fontSize 是屏幕点中字体的大小。[编辑]
如果您想了解有关 OpenGLES 的知识,这里有一个很好的入门教程
http://iphonedevelopment.blogspot.com /2009/05/opengl-es-from-ground-up-table-of.html
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);
plusglColor4f(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