ios - OpenGL :ES on iPhone - Texture2D size and display questions

标签 ios opengl-es texture2d text-rendering

我在使用 Texture2D 时遇到问题,我想了解如何更好地使用它。

我从 here 中获取了 Crashlander Texture2D 类和 XCode 4 中的默认 OpenGL 项目,强制它加载 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];


}

最佳答案

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

关于ios - OpenGL :ES on iPhone - Texture2D size and display questions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666304/

相关文章:

IOS UIWebView 泄漏

opengl-es - 这个 WebGL 程序中的顶点着色器和片段着色器什么时候发生插值?

opengl - 是否有更有效的方法来处理 OpenGL ES 纹理的 2^n 像素大小要求?

c# - 如何在本地修改纹理?

javascript - 样式化 React Native Picker

ios - 以编程方式向 Swift 中的 View 添加约束时出错

ios - 解析嵌套的json数组

ios - 如何将 GLKMatrix4 转换为 GLfloat* 数组?

iphone - 检测 OpenGL 渲染场景中的触摸

CUDA - 使用(未对齐的)子图像(用于纹理绑定(bind)、NPP...)时有任何问题吗?