2015-09-06 39 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