5

Before adding a geometry shader I declared a variable in the vertex shader:

out vec3 normal; 

To be received by the fragment shader as:

in vec3 normal; 

However if I add a geometry shader to the program, the linker tells me that normal has not been declared as an output from the prvious stage. But I am not sure how to recieve nor send the output in the geometry shader.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Makogan
  • 8,208
  • 7
  • 44
  • 112

1 Answers1

5

The outputs of the verrtex shader ar inputs to the geometry shader and the outputs of the geoemtry shader are inputs to the fragment shader.
Thei inputs to the geometry shader will be an array of the length of the primitive's vertex count. (See Geometry Shader - Inputs).

This means you have to declare an input array and an output in the geometry shader:

in  vec3 normal[];
out vec3 geo_normal;

Pass the input to the output:

geo_normal = normal[i];
EmitVertex();

Declare an input in the fragment shader:

in vec3 geo_normal;



An option would be to use Layout Qualifiers

Geometry shader:

in vec3 normal[]; // <---- array

out layout(location=1) vec3 geo_normal;  

geo_normal = normal[i];
EmitVertex();

Fragment shader:

in layout(location=1) vec3 normal; // link by layout location 1 and not by name 



Another option would be to use Interface Blocks:

Vertex shader:

out TData
{
    vec3 normal;
} outData;

outData.normal = .....;

Geometry shader:

in TData
{
    vec3 normal;
} inData[]; // <---- array

out TData
{
    vec3 normal;
} outData;

outData.normal = inData.normal[i];
EmitVertex();

Fragment shader:

in TData
{
    vec3 normal;
} inData;

..... = inData.normal;
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • So if I add the geometry shader I need to manally merge the normals based on the barycentric coodinates of teh triangle myself? – Makogan Mar 31 '18 at 18:24
  • 1
    @Makogan No, in the geometry shader you have access to all the vertex coordiantes for one primitive (e.g to the 3 corners of a triangle). You can pass the attributes from the vertex shader to the geoemtry shader and you can do the transformations which you would otherwise have done in the vertex shader, then in the geometry shader (and of course many more geometric operations based on a primitive). – Rabbid76 Apr 01 '18 at 15:31