glDrawArrays(GL_POINTS, 0 4) does not draw points in iOS 5.0 and openGL ES 2.0

sia picture sia · Mar 12, 2012 · Viewed 7k times · Source

I have a simple OpenGL ES 2.0 code to draw points. Here is the VAO setup:

    static const GLfloat squareVertices[] = {
        -0.5f, -0.5f, -1.0f,
        0.5f, -0.5f, -1.0f,
        0.5f,  0.5f, -1.0f,
        -0.5f, 0.5f,  -1.0f
    };

    static const GLfloat squareColors[] = {
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f,
        1.0f, 0.0f, 1.0f, 1.0f
    };
    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1, &_colorP_VBO);
    glBindBuffer(GL_ARRAY_BUFFER, _colorP_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(squareColors), squareColors, GL_STATIC_DRAW);

    glEnableVertexAttribArray(ATTRIB_COLOR);
    glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glBindVertexArrayOES(0);

and here is the render code:

    [glView setDisplayFramebuffer:glView.viewFrameBuffer];

    glUseProgram(simpleShaderProgram);
    glBindVertexArrayOES(_vertexArray);

//  glLineWidth(5.0f);
//      glDrawArrays(GL_LINE_LOOP, 0, 4);
//      glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
//  glEnable(GL_POINT_SMOOTH);

    glPointSize(25.0f);
    glDrawArrays(GL_POINTS, 0, 4);

    glBindVertexArrayOES(0);

    glUseProgram(0);

    glBindFramebuffer(GL_FRAMEBUFFER, 0); //unbind the FBO  
    [glView presentFramebuffer:glView.viewFrameBuffer];

When I use GL_LINE_LOOP or GL_TRIANGLE_FAN it draw colorful square on screen. However when i use GL_POINTS the renderer does not draw vertices and screen is blank. I appreciate if somebody let me know what i am missing. Thanks

The followings are my vertex and fragment shaders:

attribute vec4 position;
attribute vec4 v_color;

varying vec4 colorVarying;


void main()
{
    colorVarying = v_color;
    gl_Position =  position;

}

------------------------------------------------
precision mediump float;
varying vec4 colorVarying;


void main()
{
    gl_FragColor = colorVarying;
}

Answer

Ben Zotto picture Ben Zotto · Mar 14, 2012

You're using ES 2.0, so the glPointSize() call probably doesn't do anything. Point size should be set inside your shader, using the gl_PointSize built in output variable.