Search code examples
c++openglvertex-bufferlinear-interpolation

Interpolate data buffers using OpenGL?


I am writing an application that loads the vertices of two meshes (which serve as keyframes) into two separate OpenGL float data buffers. The two meshes have the same number of vertices.

I would like to compute intermediary frames by using linear interpolation between these two buffers (where I can specify the interpolation weighting as a value between 0 and 1). I am currently performing the interpolation on the CPU, but I would like to offload the computation to the GPU in order to compute intermediary frames faster.

Is there a way to do this using only OpenGL (ie. not OpenCL)? If so, how?


Solution

  • Assuming that vertices are stored in the same order in both buffers, you can simply bind each of the buffers to an attribute in the vertex shader. In combination with a uniform between which controls the interpolation (let's call it t) which goes from 0 (only first buffer) to 1 (only second buffer), you can perform the linear interpolation before writing to gl_Position. A shader could look somehow like the following:

    in vec4 buffer1; //Bind buffer1 to this attribute
    in vec4 buffer2; //Bind buffer2 to this attribute
    
    uniform float t; //Interpolation parameter between 0 and 1
    
    void main()
    {
        gl_Position = (1 - t) * buffer1 + t * buffer2;
    }