Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Hello,

I am attempting to draw a square using GLKit and OpenGL ES. I get no errors, but my square doesn't appear on the screen. I think I might be missing something in my code. Here is my code in the ViewController.m:

Code:
GLfloat gSquareVertexData[12] = {
    -0.5f, 0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.5f, 0.5f, 0.0f

};


@interface ViewController (){

    GLuint _program;
    GLuint _vertexArray;
    GLuint _vertexBuffer;
    
  
    
    
}
@end


@implementation ViewController
@synthesize context = _context;
@synthesize effect = _effect;

- (void)viewDidLoad
{
    [super viewDidLoad];
    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    
    GLKView *view = (GLKView*)self.view;
    view.context = _context;
    
    
    [self setupGL];
    
}

-(void)setupGL{

    [EAGLContext setCurrentContext:_context];
    
    [self loadShaders];
    
    
    _effect = [[GLKBaseEffect alloc]init];
    
    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);
    
    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gSquareVertexData), gSquareVertexData, GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    
    GLsizei stride = sizeof(GLfloat)*3;
    
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, stride, NULL);
    
    glBindVertexArrayOES(0);
    

}

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
    glClearColor(0.65f, 0.65f, 0.65f, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glBindVertexArrayOES(_vertexArray);
    
    [self.effect prepareToDraw];
    
    glUseProgram(_program);
    
    glDrawArrays(GL_TRIANGLES, 0, 2);
        
    
}

-(BOOL)loadShaders{
    GLuint vertexShader, fragmentShader;
    
    NSString *vertexshaderPath, *fragmentShaderPath;
    
    _program = glCreateProgram();
    
    vertexshaderPath = [[NSBundle mainBundle]pathForResource:@"fragment" ofType:@"glsl"];
    fragmentShaderPath = [[NSBundle mainBundle]pathForResource:@"vertex" ofType:@"glsl"];
    
   if(![self compileShader:&vertexShader type:GL_VERTEX_SHADER file:vertexshaderPath]){
        NSLog(@"Vertex Shader fail");
    
    }else{
        NSLog(@"Vertex shader compiled with success");
    }
    
    if(![self compileShader:&fragmentShader type:GL_FRAGMENT_SHADER file:fragmentShaderPath]){
        NSLog(@"Error compiling fragment shader!");
    }else{
        NSLog(@"Fragment shader compiled succesfully");
    }
    
    glAttachShader(_program, vertexShader);
    glAttachShader(_program, fragmentShader);
    
    glBindAttribLocation(_program, GLKVertexAttribPosition, "Position");

    
    
    
    if(![self linkProgram:_program]){
        NSLog(@"Failed to link program: %d", _program);
    }else{
        NSLog(@"Successful link");
    }
    
    
    
    return YES;
    
}

-(BOOL)compileShader:(GLuint*)shader type:(GLenum)type file:(NSString *)file{

    const GLchar *source;
    source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil]UTF8String];
    
    *shader = glCreateShader(type);
    glShaderSource(*shader, 1, &source, NULL);
    glCompileShader(*shader);
    
    return YES;


}

-(BOOL)linkProgram:(GLuint)pg{

    glLinkProgram(pg);
    
    return YES;

}


@end

My fragment shader:

Code:
void main( void )
{
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

My Vertex Shader:
Code:
attribute mediump vec3 Position;
 
void main( void ) 
{
    gl_Position =  vec4(Position, 1.0);
}


Does anyone notice something immediately that is missing? Is everything there? OpenGL ES is configured correctly because when I set the clear color, the view's color changes. I just can't make my square draw.

Thanks!
 
Last edited:
The first thing that stands out to me is that you have no update loop to calculate the projection matrix and modelView matrix then multiplying the two together into the modelViewProjection matrix to pass into the shader.

Secondly you are just drawing the data for the square you are not doing any transformations from model space into view space etc. So you probably won't see anything.

You need to create a projection matrix with something like

GLKMatrix4 proj = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, .1f, 1000.0f);

Pass this matrix into your shader and multiply it by the square position.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.