简体   繁体   English

Objective-C iOS-使用GLKit将图像渲染到屏幕上

[英]Objective-C iOS - render an image to the screen using GLKit

For the life of me, I can't render an image to the iPhone simulator screen. 为了我的一生,我无法在iPhone模拟器屏幕上渲染图像。 I've simplified my code as much as possible. 我已尽可能简化了我的代码。

The following code is in ViewController.m, a class that extends GLKViewController and is also a GLKViewDelegate. 以下代码位于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);
}

and some of the structs used... 和一些使用的结构...

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

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

Right now the only thing that is working is 现在唯一有效的是

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

Which does adjust the background colour. 可以调整背景颜色。 There is no 'soccerball' image. 没有“足球”图像。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

EDIT - The TextureVertex CGPoints were incorrect so I fixed them. 编辑-TextureVertex CGPoints不正确,所以我修复了它们。 The problem still persists. 问题仍然存在。

Solution: 解:

The TexturedVertex struct must not use CGPoint, but rather GLKVector2. TexturedVertex结构不得使用CGPoint,而应使用GLKVector2。

This is because there is a conversion issue from the float values stored in these points. 这是因为存储在这些点中的浮点值存在转换问题。 GLKit expects float values that have single point precision, but CGPoint float values have double point precision and things get weird. GLKit期望浮点值具有单点精度,但是CGPoint浮点值具有双点精度,事情变得很奇怪。 Furthermore, this problem only occurs after iOS 7.0 此外,此问题仅在iOS 7.0之后出现

Refer to here for more detail on the issue. 有关此问题的更多详细信息,请参阅此处。 OpenGL ES Shaders and 64-bit iPhone 5S OpenGL ES着色器和64位iPhone 5S

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

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