2012-03-31 2 views
2

OpenGL ES를 처음 사용합니다. 테스트로서 나는 GLKView에 간단한 삼각형을 그리려고합니다. 컴파일 또는 런타임 오류가없고 OpenGL 오류도 없습니다. 단지 삼각형을 그리지 않습니다. ... 그것은 OpenGL을 작동하는 것을 의미 맑은 색 OK를 설정하지만 그것은 단지 나던 무슨 일이 내가 잘못하고있는 중이 야, 삼각형을 그릴 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'가 없습니까? –

+0

그걸 보았습니다 만, 뷰가 그려 질 때마다'drawInRect :'에 의해 오버 라이딩됩니다. 맞습니까? 'drawInRect :'가 꽤 자주 호출되는 반면'-setupGL'은 한 번 호출되거나 꽤 드물게 호출되지 않습니까? – user1118321

+0

그러나 나는이 답변을 기대할만한 자격이 없다고 기대합니다. –