在GLKView中绘图

时间:2012-03-31 22:08:34

标签: objective-c ios opengl-es

我是OpenGL ES的新手。作为测试我试图在GLKView上绘制一个简单的三角形。没有编译或运行时错误,也没有OpenGL错误。它只是不绘制三角形。它设置了清晰的颜色OK,这意味着OpenGL正在工作,但它只是没有绘制三角形,我做错了什么......

绘图回调代码

- (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 个答案:

答案 0 :(得分:1)

您对glViewport(12,12)的通话(在-glkView:drawInRect:中)告诉系统您正在尝试以12像素×12像素区域进行绘制。那是你要的吗?这似乎很小。