OpenGLES: Square won't draw?

Discussion in 'iOS Programming' started by Blakeasd, Jun 27, 2013.

  1. Blakeasd, Jun 27, 2013
    Last edited: Jun 27, 2013

    Blakeasd macrumors 6502a

    Dec 29, 2009

    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:

    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;
    @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];
        [EAGLContext setCurrentContext:_context];
        [self loadShaders];
        _effect = [[GLKBaseEffect alloc]init];
        glGenVertexArraysOES(1, &_vertexArray);
        glGenBuffers(1, &_vertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(gSquareVertexData), gSquareVertexData, GL_STATIC_DRAW);
        GLsizei stride = sizeof(GLfloat)*3;
        glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, stride, NULL);
    -(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
        glClearColor(0.65f, 0.65f, 0.65f, 1.0);
        [self.effect prepareToDraw];
        glDrawArrays(GL_TRIANGLES, 0, 2);
        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");
            NSLog(@"Vertex shader compiled with success");
        if(![self compileShader:&fragmentShader type:GL_FRAGMENT_SHADER file:fragmentShaderPath]){
            NSLog(@"Error compiling fragment shader!");
            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);
            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);
        return YES;
        return YES;
    My fragment shader:

    void main( void )
        gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    My Vertex Shader:
    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.

  2. teagls macrumors regular

    May 16, 2013
    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.

Share This Page