简体   繁体   English

iPhone上的OpenGL:ES-Texture2D大小和显示问题

[英]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. 我在Texture2D上遇到问题,我想了解如何更好地使用它。

I've taken the Crashlander Texture2D class from here and a default OpenGL project in XCode 4, forcing it to load OpenGL ES1.1 我从这里获取了Crashlander Texture2D类,并在XCode 4中获取了一个默认的OpenGL项目,迫使它加载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? Texture2D init方法的大小显然是OpenGL的大小,但是fontSize参数与OpenGL世界有什么关系?

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. 我从下面的代码中得到的结果是文本应位于的黑色(或我在glColor中设置的任何颜色)正方形。

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];


}

Crashlander is really an old code base, so I would suggest avoiding it. Crashlander确实是一个旧的代码库,所以我建议避免使用它。 There is a perfectly good 2D engine for the iPhone called Cocos2D http://www.cocos2d-iphone.org/ . iPhone有一个非常好的2D引擎,称为Cocos2D http://www.cocos2d-iphone.org/ About the code, try commenting glDisable(GL_COLOR_MATERIAL); 关于代码,请尝试注释glDisable(GL_COLOR_MATERIAL); plus glColor4f(0,0,0,1); glColor4f(0,0,0,1); actually represents black color, try commenting this too. 实际上代表黑色,也可以尝试对此进行评论。 I think fontSize is the size of font in screen points. 我认为fontSize是屏幕点的字体大小。

[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 如果您想了解有关OpenGLES的信息,请参阅http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html,这是一个很好的入门教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM