3
votes

I am trying to implement a custom interpolation technique in GLSL shader.

I have switched off the default OpenGL bilinear filters using flat interpolation specifier for my texture coordinates. I followed the technique that is specified in the below link:

How to switch off default interpolation in OpenGL

While rasterizing, the image now gets an image which is based on the provoking vertex.

Is it possible for me to introduce an interpolation mechanism to decide on the colors filled between vertices in a triangle ? Or is this hardcoded in OpenGL ?

I am a newbie in GLSL world and hence would request you to provide me with a non complicated answer.

2

2 Answers

8
votes

Interpolation is hard-coded into OpenGL. If you want to do your own interpolation, you will have to provide to the fragment shader:

  1. The barycentric coordinates for that particular fragment. That can be done by passing, for the three vertices of the triangle, vec3(1, 0, 0), vec3(0, 1, 0), and vec3(0, 0, 1).
  2. All three uninterpolated values for your triangle's data that you wish to interpolate. This will require 3 flat input variables in your FS (or an array of 3 inputs, which is the same thing).

Of course, you'll need to match the 1, 0, 0 triangle with the particular uninterpreted value for that vertex of the triangle. And the same goes with the other two indices.

This basically requires a geometry shader, since it's very difficult for a VS to pass barycentric coordinates or to provide the right uninterpolated data. But with the barycentric coordinates of the position, and the values to be interpolated, you should be able to implement whatever interpolations scheme you like. #2 could include more than 3 values, for example.

1
votes

Usual approach to needing a custom interpolation function is to use the standard interpolation for a texture coordinate, and look up whatever data you want in a texture.