2012-03-31 39 views
2

我是OpenGL ES的新手。作爲一個測試,我試圖在GLKView上繪製一個簡單的三角形。沒有編譯或運行時錯誤,也沒有OpenGL錯誤。它只是不畫三角形。它設置清晰的彩色OK,這意味着OpenGL的工作,但它只是不畫三角形,我究竟做錯了......在GLKView中繪圖

繪圖回調代碼

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{ 
    const GLfloat triangleVertices[] = { 
     0.0, 2.0, 3.0,     // Triangle top centre 
     -2.0, -2.0, 3.0,     // bottom left 
     2.0, -2.0, 3.0     // bottom right 
    }; 



    glViewport(0, 0, 12, 12); 
    [self checkError]; 

    glMatrixMode(GL_MODELVIEW); 

    // Our new drawing code goes here 
    glLoadIdentity(); 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    [self checkError]; 


    // Red 
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f); 

    glVertexPointer(3, GL_FLOAT, 0, &triangleVertices); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glDrawArrays(GL_TRIANGLES, 0, 3);  

    [self checkError]; 
} 

整個文件的代碼

#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 

@property (nonatomic, retain) EAGLContext *context; 

@end 

@implementation ViewController 

@synthesize context = _context; 
@synthesize Zoom = _Zoom; 

- (void)dealloc 
{ 
    [_context release]; 

    [super dealloc]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1] autorelease]; 

    if (!_context) { 
     NSLog(@"Failed to create ES context"); 
    } 

    _Zoom = 1; 

    GLKView *view = (GLKView *)self.view; 
    view.context = self.context; 
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24; 

    [EAGLContext setCurrentContext:self.context]; 

    [self setupGL]; 

    [self checkError]; 
} 

- (void)viewDidUnload 
{  
    [super viewDidUnload]; 

    if ([EAGLContext currentContext] == self.context) { 
     [EAGLContext setCurrentContext:nil]; 
    } 
    self.context = nil; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc. that aren't in use. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } else { 
     return YES; 
    } 
} 

- (void)setupGL 
{ 
    [EAGLContext setCurrentContext:self.context]; 

    CGFloat w = self.view.bounds.size.width; 
    CGFloat h = self.view.bounds.size.height; 

    glEnable(GL_DEPTH_TEST); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    glClearColor(0.65f, 0.65f, 1.00f, 1.0f); 


    double mLat = (((double)h/2) * (_Zoom/60)); 
    double mLon = (((double)w/2) * (_Zoom/60)); 

    glOrthof((float)-mLon, (float)mLon, (float)-mLat, (float)mLat, -5.0f, 5.0f); 
    glViewport(0, 0, w, h); 
} 

-(void) checkError 
{ 
    GLenum error = glGetError(); 

    if (error == GL_NO_ERROR) 
     return; 

    switch (error) 
    { 
     case GL_INVALID_ENUM: 
      NSLog(@"Invalid Enum"); 
      break; 
    } 
} 


#pragma mark - GLKView and GLKViewController delegate methods 

- (void)update 
{ 

} 

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{ 
    const GLfloat triangleVertices[] = { 
     0.0, 2.0, 3.0,     // Triangle top centre 
     -2.0, -2.0, 3.0,     // bottom left 
     2.0, -2.0, 3.0     // bottom right 
    }; 



    glViewport(0, 0, 12, 12); 
    [self checkError]; 

    glMatrixMode(GL_MODELVIEW); 

    // Our new drawing code goes here 
    glLoadIdentity(); 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    [self checkError]; 


    // Red 
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f); 

    glVertexPointer(3, GL_FLOAT, 0, &triangleVertices); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glDrawArrays(GL_TRIANGLES, 0, 3);  

    [self checkError]; 
} 

@end 

回答

1

您對glViewport(12,12)(在-glkView:drawInRect:)調用告訴你試圖通過12像素面積的12像素繪製系統。那是你要的嗎?這似乎很小。

+0

'setupGL'裏面還有一個。 iOS上沒有'GLU'? – 2012-04-01 03:59:59

+0

我明白了,但每次繪製視圖時都會被'drawInRect:'中的一個覆蓋,對吧?不是''setupGL'調用過一次,還是很少,而'drawInRect:'被調用的頻率很高? – user1118321 2012-04-01 04:38:52

+0

我希望這個,但是,我沒有資格回答這個問題...... – 2012-04-01 05:23:52