Search code examples
opengllinedraw

opengl how to draw a dynamic line?


I want to draw a line in OpenGL which will very often receive new points.

I want to achieve an object that draws a line behind itself:

enter image description here

So do I have to make an array and append all new points to it (BufferData set to GL_DYNAMIC_DRAW). And then redraw the line with glDrawArrays. Tesselation + width of stroke would be inside the vertexshader?

Or is there a better way?


Solution

  • The idea is to plot a point of the current position after a given delay. The time delay defines how smooth the actual line will be. Then you will have to calculate 2 new points based on the plotted positioned point which both gets multiplied by a given width.

    For those plots, you also have to get the movement direction of your object to calculate the correct normals.

    On the OpenGl side, you will have to initialize a fixed-sized vertex buffer

    gl.glBufferData(gl.GL_ARRAY_BUFFER, 8 * self.maxPoints, None, gl.GL_DYNAMIC_DRAW)
    # 8 bytes 2 points = 1 plot
    # maxPoints amount of max plots
    

    object where you will upload every frame those 2 points (gl.glBufferSubData).

    My result:

    Sketch

    Result (Controlling with wasd):

    With Wireframe on

    bigger time delay

    small delay