PDA

View Full Version : Am I using my geometry shader wrong?



hashbrown
09-23-2016, 11:02 PM
I'm learning how to use the geometry shader and managed to display vertex normals. But when I use this shader type, the only colors I see are (default) black lines representing the normals. My texture and lighting disappears.

Is there a way I can render primitives with the geometry shader without the texture and lighting disappearing? My guess is creating another VBO just for geometry shader work, but I'm sure I'm wrong.

I'll share what I got for the vertex and geometry shaders so far, thanks in advanced!




// Vertex
#version 330 core

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 normals;

uniform mat4 mvp;
uniform mat3 nrmMatrix;


out vec3 position0;
out vec2 texCoord0;
out vec3 normals0;
out mat3 nrmMatrix0;
out mat4 model0;

out vec3 dirLightPos0;

out VS_OUT {
vec3 normal;
} vs_out;


void main() {
position0 = position;
texCoord0 = texCoord;
normals0 = normals;
nrmMatrix0 = nrmMatrix;
model0 = model;


vs_out.normal = vec3(projection * vec4(nrmMatrix * normals, 1.0));

gl_Position = mvp * vec4(position, 1.0);
}

// Geometry

#version 330 core

layout (triangles) in;
layout (line_strip, max_vertices = 6) out;

in VS_OUT {
vec3 normal;

} gs_in[];

const float MAG = 0.2f;

out vec3 color0;

void GenerateLine(int index) {
gl_Position = gl_in[index].gl_Position;
EmitVertex();

gl_Position = gl_in[index].gl_Position + vec4(gs_in[index].normal, 0.0f) * MAG;
EmitVertex();
EndPrimitive();
}

void main() {
GenerateLine(0);
GenerateLine(1);
GenerateLine(2);

color0 = vec3(1, 0, 0);
}

GClements
09-23-2016, 11:28 PM
The only variables which your geometry shader outputs are color0 and gl_Position, and color0 isn't being set prior to the EmitVertex() calls, so its value in the fragment shader is undefined. If you want to output other variables, you have to define them as outputs and set their values prior to the EmitVertex() call.

If you want to pass variables through the geometry shader, you have to define them as both inputs and outputs (either with different names or using interface blocks) and copy the input to the output prior to each EmitVertex() call. This won't happen automatically; when a geometry shader is used, the only variables available to the fragment shader are those which are declared as outputs in the geometry shader. Vertex shader outputs are only available to the geometry shader.

hashbrown
09-24-2016, 08:23 AM
The only variables which your geometry shader outputs are color0 and gl_Position, and color0 isn't being set prior to the EmitVertex() calls, so its value in the fragment shader is undefined. If you want to output other variables, you have to define them as outputs and set their values prior to the EmitVertex() call.

If you want to pass variables through the geometry shader, you have to define them as both inputs and outputs (either with different names or using interface blocks) and copy the input to the output prior to each EmitVertex() call. This won't happen automatically; when a geometry shader is used, the only variables available to the fragment shader are those which are declared as outputs in the geometry shader. Vertex shader outputs are only available to the geometry shader.

As always thanks again GC, you're right! I was expecting this process to happen automatically, wrong thinking.