0

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?

MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59

1 Answers1

5

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;
}
BDL
  • 21,052
  • 22
  • 49
  • 55
  • 6
    Or [`mix()`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mix.xhtml). – genpfault Aug 28 '17 at 12:01
  • This is probably a silly question, but how do I bind a VBO to a shader attribute? I can't seem to find how. – MathuSum Mut Aug 28 '17 at 13:16
  • 1
    How do you do that now with your one VBO? Have a look at `glVertexAttribPointer`. – BDL Aug 28 '17 at 13:18
  • Oh, so the first parameter "index" corresponds to the position of the attribute in the order declared in the shader. Thanks :) – MathuSum Mut Aug 28 '17 at 13:22
  • 3
    The index paramter corresponds to the location of the attribute. You can either fix this in the shader by a `layout (location=` or query it with [`glGetAttributeLocation`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetAttribLocation.xhtml). – BDL Aug 28 '17 at 13:24