Search code examples
iosswiftopengl-esopengl-es-2.0glkit

Cant draw a varying amount of points


So I have this code. My goal is basically to draw a varying amount of points. As you can see this code is supposed to draw points based on the array, and whenever the user taps more points are added. However something goes wacky!

Basically everything is fine, until you tap and add a point. When you do add one point only the first and new point are drawn. Heres what happens at each array size

  1. First point Drawn
  2. Two points drawn
  3. All three points are drawn
  4. Only first and last drawn
  5. First and last two
  6. Nothing happens.
  7. Nothing happens 8-(probably infinity). Only the first point

    var vVertices:[GLfloat] = [0, 0, 0,
        -1, 0, 0,
        0.5, 0.5, 0]
    // var vVertices:[GLfloat] = []
    var VAO:GLuint = 0
    var VBO:GLuint = 0
    
    var cc:GLfloat = 0.0
    override func glkView(view: GLKView, drawInRect rect: CGRect) {
    
    
    
    
        glClearColor(cc, 0.65, 0.65, 1.0)
        glClear(GLbitfield(GL_COLOR_BUFFER_BIT) | GLbitfield(GL_DEPTH_BUFFER_BIT))
        cc += 0.001
    
        //Activate the shader
        glUseProgram(program);
        glEnable(GLenum(GL_POINT_SMOOTH))
        let loc = glGetUniformLocation(program, "color")
        if (loc != -1)
        {
            //Pass the color
            glUniform4f(loc, 0, 0, 1, 1)
        }
        let loc2 = glGetUniformLocation(program, "pointSize")
        if (loc != -1)
        {
            //Pass the point size
            glUniform1f(loc2, 20)
        }
    
    
        // Load the vertex data
        let pointsc = vVertices.count / 3
        print(pointsc)
    
        glVertexAttribPointer(0, GLint(pointsc), GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, vVertices);
        glEnableVertexAttribArray(0);
        glDrawArrays(GLenum(GL_POINTS), 0, GLint(pointsc));
    
    
    
    }
    
    func placePoint(x: GLfloat, y: GLfloat)
    {
    
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        vVertices.append(GLfloat(arc4random_uniform(10)) / 10.0)
        vVertices.append(GLfloat(arc4random_uniform(10)) / 10.0)
        vVertices.append(0)
    }
    

Solution

  • You are using glVertexAttribPointer incorrectly. Its size parameter does not describe the size of the array, but the size of each singl array element. It allows you to specify a scalar (size 1) or vector with 2 to 4 dimensions. Other values besides 1,2,3 and 4 will result in an GL_INVALID_VALUE error and the call having no further effect.