glsl flat qualifier?

So, I read the “Official OpenGL Handbook,” and in the section where they taught material lighting, they suddenly used a “flat” qualifier for the input variable in the fragment shader. I got down to business, and all I came up with was “flat shading” and “smooth shading” and the differences between them, which I cannot understand how this relates to the simple variable “MatIndex”. here is a sample code from a book:

struct MaterialProperties { vec3 emission; vec3 ambient; vec3 diffuse; vec3 specular; float shininess; }; // a set of materials to select between, per shader invocation const int NumMaterials = 14; uniform MaterialProperties Material[NumMaterials]; flat in int MatIndex; // input material index from vertex shader 

What does it mean?

+5
source share
2 answers

In general, there is no 1: 1 mapping between a vertex and a fragment. By default, the corresponding data to the vertex is interpolated by primitive to generate the corresponding related data for each fragment, which makes smooth shading.

Using the flat keyword, interpolation is not performed, so each fragment generated during the rasterization of this particular primitive will receive the same data. Since primitives are usually defined by more than one vertex, this means that in this case only data from one vertex is used. This is called the calling vertex in OpenGL.

Also note that integer types are not interpolated. You must declare them as flat anyway.

In your specific example, the code means that each primitive can have only one material, which is determined by the material identifier of the calling vertex of each primitive.

+16
source

This is part of how the attribute is interpolated for the fragment shader, by default it is the correct perspective interpolation.

to GLSL 4.5 specification section

A variable that qualifies as flat will not be interpolated. Instead, it will have the same value for each fragment in the triangle. This value will come from one calling vertex, as described in the OpenGL Graphics System Specification. A variable can be qualified as Flat; it can also be qualified as a center of gravity or a sample , which will mean the same as qualifying it as just flat

Integral attributes must be flat.

You can find a table whose vertex is a provocative vertex in the openGL spec table in section 13.4.

+4
source

Source: https://habr.com/ru/post/1209609/


All Articles