2015-09-06 92 views
0

对于我的生活画面的图像,我无法呈现到iPhone模拟器的屏幕图像。我尽可能简化了我的代码。的Objective-C的iOS - 渲染使用GLKit

下面的代码是在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是不正确的,所以我固定他们。问题依然存在。

+0

如果有帮助,这段代码不应该是'那'遥远,因为我有一年前的工作。从那以后我不知道发生了什么事。 –

回答

0

解决方案:

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

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

请参考这里了解更多关于这个问题的细节。 OpenGL ES Shaders and 64-bit iPhone 5S