Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is interpolation in OpenGL?

Tags:

opengl-es

I have just started learning OpenGL ES 2.0, during the process I met some terms that do not make sense to me even if there are definitions. Like this one: Interpolation

From the book OpenGL ES 2.0 Programming Guide, I see this definition for interpolation:

The mechanism used to generate a varying value for each fragment from the varying values assigned to each vertex of the primitive is called interpolation.

Though I understand what Linear Interpolation is from this answer, the definition above does not make sense to me.

Would you put it another way and make an example to elaborate on what interpolation is?

like image 906
neevek Avatar asked Sep 10 '12 05:09

neevek


1 Answers

As for a general definition "in other words" you could find a few on web.. As for example I could try to explain this to you:

You use interpolation when you have a limited amount of data trying to represent unlimited amount of data, for instance, you have 2 points representing a line:

For interpolation you always need a hint of representation, in line case you need 2 points (and the hint is "it's a line"), lets say they are vectors A and B. Now the solution is T(s) = A + s*(B-A) for any real number 's' to get endless line in both directions. To get a line between A and B you need to define 's' on the interval [0, 1] so that T(0)=A while T(1)=B and this is linear interpolation.

For a bit more complex interpolations you can look into openGL a bit:

Note that the algorithms used in GL and your GPU are much more complex and optimized then what I am about to write here but generally have the same result.

In case of shapes you usually use triangles defined with 3 vectors (vertex) and say that it is a triangle (you say 'triangles' in your draw method). After applying some matrices to those vectors you will get their 2d projection on the draw buffer (can happen in vertex shader). Now you need to fill all those points in the buffer with some color, texture or color interpolations. First of all, all those drawn points (their position on the buffer actually) are a result of a linear interpolating mechanism (you have no control of it in GL). Second as to what color will be filled is usually again the linear interpolation:

Try drawing a triangle with vertices of different color by setting a color pointer. You will get a nice smooth color specter on the whole surface that is a result of a linear interpolation of colors depending on interpolation of positions (you get the position interpolation result in fragment shader for each pixel that needs to be drawn). You could manually achieve this color interpolation result like this:

input:

Vector a, b, c; //original triangle positions
Vector interpolationABC; //current fragment interpolation between a, b and c
Color colorA, colorB, colorC; //colors for vectors a, b and c

output (color of this fragment):

Color output = (
(a - interpolationABC).length() * colorA + 
(b - interpolationABC).length() * colorB + 
(c - interpolationABC).length() * colorC ) 
/ (
(a - interpolationABC).length() + 
(b - interpolationABC).length() + 
(c - interpolationABC).length() );

This mechanism again represents a linear interpolation (a very slow one actually, but it should do the trick and is very readable)

As for an example of non linear interpolation you will need a bit of imagination: Lets say we are drawing a sphere. For purpose of keeping it simple let's say it's center is in (0,0,0) and its radius is 1. The sphere is created with N triangles and if N is not large enough (most cases) because of performance issue it is a bit "edgy" and we would like to smooth it out a bit. So we have to turn on some lights (it's just a circle with no lights anyway) and figure out how to set normals. On one hand you can compute a normal for surface defined by 3 points and each triangle will have a unique normal but will produce "a disco ball" effect, on the other hand you know that a normal of any point 'P' of sphere is normalized(P-center) or in this case the point itself. So each vertex normal is the vertex's position itself (so now each triangle has 3 different normals) and each fragment's normal is an interpolation of those normals:

If you use the same interpolation mechanism as for colors you will see that the length of normals is not "constant", resulting in flaw, forcing you to use a different mechanism (non linear). In this specific case you could simply normalize those linear fragment results and get the result you need, but also in this specific case you could just compute the normal from fragment position (you can do that for any shape that is mathematically presentable). But know in practice you will get a strange shape that has smooth parts and edges and might have normals of different size to represent some awesome effect and that is why there are great mechanisms for interpolating different parameters on shapes.

Again, interpolation mechanism is a mechanism that can produce unlimited number of results by knowing only a limited number of data (one of those mechanisms is called "linear interpolation"). Or in other words as you posted for a bit more specific example:

The mechanism used to generate a varying value for each fragment from the varying values assigned to each vertex of the primitive is called interpolation.

like image 110
Matic Oblak Avatar answered Oct 12 '22 12:10

Matic Oblak