Search code examples
iosswiftopengl-esvertex-buffer

Varying Line Width with Open GL using GL_POINTS (iOS)


I'm making a drawing application using swift (based on GLPaint) and open gl. Now I would like to improve the curve so that it varies with stroke speed (in eg thicker if drawing fast)

However, since my knowledge in open gl is quite limited I need some guidance. What I want to do is to vary the size of my texture/point for each CGPoint I calculate and add to the screen. Is it possible?

func addQuadBezier(var from:CGPoint, var ctrl:CGPoint, var to:CGPoint, startTime:CGFloat, endTime:CGFloat) {

    scalePoints(from: from, ctrl: ctrl, to: to)
    let pointCount = calculatePointsNeeded(from: from, to: to, min: 16.0, max: 256.0)

    var vertexBuffer: [GLfloat] = [GLfloat](count: Int(pointCount), repeatedValue:0.0)

    var t : CGFloat = startTime + 0.0002
    for i in 0..<Int(pointCount) {

        let p = calculatePoint(from:from, ctrl: ctrl, to: to)        
        vertexBuffer.insert(p.x.f, atIndex: i*2) 
        vertexBuffer.insert(p.y.f, atIndex: i*2+1)    
        t += (CGFloat(1)/CGFloat(pointCount))
    }

    glBufferData(GL_ARRAY_BUFFER.ui, Int(pointCount)*2*sizeof(GLfloat), vertexBuffer, GL_STATIC_DRAW.ui)
    glDrawArrays(GL_POINTS.ui, 0, Int(pointCount).i)        

}  

func render()
{
    context.presentRenderbuffer(GL_RENDERBUFFER.l)
}

where render() is called every 1/60 s.

shader

attribute vec4 inVertex;

uniform mat4 MVP;
uniform float pointSize;
uniform lowp vec4 vertexColor;

varying lowp vec4 color;

void main()
{
    gl_Position = MVP * inVertex;
    gl_PointSize = pointSize;
    color = vertexColor;
}

Thanks in advance!


Solution

  • In your vertex shader, set gl_pointSize to the width you want. That measurement is in framebuffer pixels, so if the size of your framebuffer changes with the device's scale factor, you'll need to adjust your point size appropriately.