Objective-C iOS - 使用GLKit

时间:2015-09-06 07:11:27

标签: ios objective-c iphone glkit

对于我的生活,我无法将图像渲染到iPhone模拟器屏幕。我尽可能简化了我的代码。

以下代码位于ViewController.m中,这是一个扩展GLKViewController的类,也是一个GLKViewDelegate。

- (void)viewDidLoad {
    [super viewDidLoad];

    /*Setup EAGLContext*/
    self.context = [self createBestEAGLContext];
    [EAGLContext setCurrentContext:self.context];

    /*Setup View*/
    GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    view.context = self.context;
    view.delegate = self;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    self.view = view;
}


- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

    /*Setup GLK effect*/
    self.effect = [[GLKBaseEffect alloc] init];
    self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0, 320, 480, 0, -1, 1);

    glClearColor(0.5, 1, 1, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES],
                          GLKTextureLoaderOriginBottomLeft,
                          nil];
    NSError * error;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"soccerball" ofType:@"jpg"];

    GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
    if (textureInfo == nil) {
        NSLog(@"Error loading file: %@", [error localizedDescription]);
    }

    TexturedQuad newQuad;
    newQuad.bl.geometryVertex = CGPointMake(0, 0);
    newQuad.br.geometryVertex = CGPointMake(textureInfo.width, 0);
    newQuad.tl.geometryVertex = CGPointMake(0, textureInfo.height);
    newQuad.tr.geometryVertex = CGPointMake(textureInfo.width, textureInfo.height);

    newQuad.bl.textureVertex = CGPointMake(0, 0);
    newQuad.br.textureVertex = CGPointMake(1, 0);
    newQuad.tl.textureVertex = CGPointMake(0, 1);
    newQuad.tr.textureVertex = CGPointMake(1, 1);

    self.effect.texture2d0.name = textureInfo.name;
    self.effect.texture2d0.enabled = YES;

    GLKMatrix4 modelMatrix = GLKMatrix4Identity;
    modelMatrix = GLKMatrix4Translate(modelMatrix, 100, 200, 0);

    self.effect.transform.modelviewMatrix = modelMatrix;

    [self.effect prepareToDraw];

    long offset = (long)&(newQuad);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

和一些结构使用......

typedef struct {
    CGPoint geometryVertex;
    CGPoint textureVertex;
} TexturedVertex;

typedef struct {
    TexturedVertex bl;
    TexturedVertex br;
    TexturedVertex tl;
    TexturedVertex tr;
} TexturedQuad;

现在唯一有效的是

glClearColor(0.5, 1, 1, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

这会调整背景颜色。没有“足球”形象。

非常感谢任何帮助。

编辑 - TextureVertex CGPoints不正确所以我修复了它们。问题仍然存在。

1 个答案:

答案 0 :(得分:0)

解决方案:

TexturedVertex结构不能使用CGPoint,而是使用GLKVector2。

这是因为存储在这些点中的浮点值存在转换问题。 GLKit期望浮点值具有单点精度,但CGPoint浮点值具有双点精度,事情变得奇怪。此外,此问题仅发生在iOS 7.0之后

有关此问题的详细信息,请参阅此处。 OpenGL ES Shaders and 64-bit iPhone 5S