testman
testman

Reputation: 258

Is manipulation of the interpolation process from vertex to fragment shader possible?

I am new to glsl programming. As far as I understand it variables in the fragment shader are linearly interpolated values given from the vertex shader. That is why for example you have a colour gradient when you set different colours to different vertices.

Lets say I want to render a surface with low polygon count, e.g. a cube, and I define vertex normals. I want each surface have the same normal because the lighting probably looks terrible when the normals are interpolated.

So does it really work like this? And is there a possibilty to interfere in that interpolation process?

Upvotes: 1

Views: 446

Answers (2)

Eskalior
Eskalior

Reputation: 252

It would be interpolated if you assign normals 'per vertex'. But this is not how you would do this for a cube. The cube contains 8 vertices but you need 24 normals, depending on the face. So for the left - lower - front vertex you would assign a own normal for the bottom face, the left face and the front face .

This is the same like coloring the cube. if you want to have interpolated colors for every corner of the cube, only assign a color per vertex. if you want to have every side colored in its own way, you need to assign the color per face.

To the part with manipulation: you could manipulate every input you get. If this makes sense is questionable. for example to prevent interpolation you could make an if condition for every value between 0..1 and set it to 1. same for negative values and same if it is exactly 0. Then it should work for per vertex assignment. May require some trial and error.

To make it more clear: The shader draws the triangles out of 3 vertex values. each contains position, normal, color, what you want. So for the shader it doesnt matter if a vertex with same position has different values for color etc in a different triangle. Interpolation only happens inside the triangle, so if the normals of the vertices in each triangle are the same, the whole triangle has this normal. But beware: using other drawing styles like triangle strip makes this more difficult, as he uses one vertex definition for multiple triangles.

Upvotes: 0

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26569

You have two options:

The first one is to duplicate each vertex and assign it a different normal; each face gets their own vertices and normals, so interpolating the normals across them yields the same result.

The second one is to use the flat interpolation quantifier on the normal output of the vertex shader; the normal will the be picked from the provoking vertex of the primitive. This is more memory efficient than duplicating each vertex, but you need to be careful of the order that you are rendering in, so that the face is assigned the correct normal.

Upvotes: 2

Related Questions