Skip to content

Instantly share code, notes, and snippets.

@tim37021
Last active January 22, 2021 09:54
Show Gist options
  • Save tim37021/64539815648de2bbd16e to your computer and use it in GitHub Desktop.
Save tim37021/64539815648de2bbd16e to your computer and use it in GitHub Desktop.
Geometry shader for switching between flat and smooth shading
#version 330
layout(triangles) in;
layout(triangle_strip, max_vertices=3) out
uniform int enableSmooth=1;
in vec3 vNorm[3];
in vec3 vWorldPos[3];
in vec2 vTexCoord[3];
// output for each vertex emiting
out vec3 gNorm;
out vec3 gWorldPos;
out vec2 gTexCoord;
void main(){
// calculate flat normal
vec3 oa=vWorldPos[1]-vWorldPos[0];
vec3 ob=vWorldPos[2]-vWorldPos[0];
vec3 norm=normalize(cross(oa, ob));
for(int i=0; i<3; i++){
// preparing vertex data for emiting
gl_Position=gl_in[i].gl_Position;
// switch between flat and smooth shading
gNorm=enableSmooth==1? vNorm[i]: norm;
gWorldPos=vWorldPos[i];
gTexCoord=vTexCoord[i];
EmitVertex();
}
// after assembling...
EndPrimitive();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment