Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADDING SMOOTHNESS VIA TESSELLATION SHADERS #1688

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions avogadro/rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ set(shader_files
"mesh_fs.glsl"
"mesh_opaque_fs.glsl"
"mesh_vs.glsl"
"mesh_tcs.glsl"
"mesh_tev.glsl"
"solid_vs.glsl"
"solid_first_fs.glsl"
"spheres_fs.glsl"
Expand Down
32 changes: 19 additions & 13 deletions avogadro/rendering/mesh_fs.glsl
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
varying vec3 fnormal;
#version 400
precision highp float;

in vec3 teNormal;
in vec4 teColor;

out vec4 colorOut;

void main()
{
vec3 N = normalize(fnormal);
vec3 L = normalize(vec3(0, 1, 1));
vec3 E = vec3(0, 0, 1);
vec3 H = normalize(L + E);
float df = max(0.0, dot(N, L));
float sf = max(0.0, dot(N, H));
sf = pow(sf, 20.0);
vec4 ambient = gl_Color / 3.0;
vec4 diffuse = gl_Color;
vec4 specular = gl_Color * 3.0;
gl_FragColor = ambient + df * diffuse + sf * specular;
gl_FragColor.a = gl_Color.a;
vec3 N = normalize(teNormal);
vec3 L = normalize(vec3(0, 1, 1));
vec3 E = vec3(0, 0, 1);
vec3 H = normalize(L + E);
float df = max(0.0, dot(N, L));
float sf = max(0.0, dot(N, H));
sf = pow(sf, 20.0);
vec4 ambient = teColor / 3.0;
vec4 diffuse = teColor;
vec4 specular = teColor * 3.0;
colorOut = ambient + df * diffuse + sf * specular;
colorOut.a = teColor.a;
}
33 changes: 20 additions & 13 deletions avogadro/rendering/mesh_opaque_fs.glsl
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
varying vec3 fnormal;
#version 400
precision highp float;

in vec3 teNormal;
in vec4 teColor;

out vec4 colorOut;


void main()
{
vec3 N = normalize(fnormal);
vec3 L = normalize(vec3(0, 1, 1));
vec3 E = vec3(0, 0, 1);
vec3 H = normalize(L + E);
float df = max(0.0, dot(N, -L));
float sf = max(0.0, dot(N, -H));
sf = pow(sf, 32.0);
vec4 ambient = gl_Color / 2.2;
vec4 diffuse = gl_Color * 1.1;
vec4 specular = gl_Color * 5.0;
gl_FragColor = ambient + df * diffuse + sf * specular;
gl_FragColor.a = gl_Color.a;
vec3 N = normalize(teNormal);
vec3 L = normalize(vec3(0.0, 1.0, 1.0));
vec3 E = vec3(0.0, 0.0, 1.0);
vec3 H = normalize(L + E);
float df = max(0.0, dot(N, -L));
float sf = max(0.0, dot(N, -H));
sf = pow(sf, 20.0);
vec4 ambient = teColor / 2.2;
vec4 diffuse = teColor * 1.1;
vec4 specular = teColor * 5.0;
colorOut = ambient + df * diffuse + sf * specular;
colorOut.a = teColor.a;
}
83 changes: 83 additions & 0 deletions avogadro/rendering/mesh_tcs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#version 400
precision highp float;

layout(vertices = 1) out;

in vec3 vsNormal[];
in vec3 WorldPos_CS_in[];
in vec2 TexCoord_CS_in[];
in vec3 teVertex[];
in vec4 vsColor[];

patch out vec3 WorldPos_B030;
patch out vec3 WorldPos_B012;
patch out vec3 WorldPos_B021;
patch out vec3 WorldPos_B003;
patch out vec3 WorldPos_B102;
patch out vec3 WorldPos_B201;
patch out vec3 WorldPos_B300;
patch out vec3 WorldPos_B210;
patch out vec3 WorldPos_B120;
patch out vec3 WorldPos_B111;

patch out vec2 tcsVertex[3];
patch out vec3 tcsNormal[3];
patch out vec3 tevVertex[3];
patch out vec4 tcsColor[3];

vec3 ProjectToPlane(vec3 Point, vec3 PlanePoint, vec3 PlaneNormal)
{
vec3 v = Point - PlanePoint;
float Len = dot(v, PlaneNormal);
vec3 d = Len * PlaneNormal;
return (Point - d);
}

void CalcPositions()
{
WorldPos_B030 = WorldPos_CS_in[0];
WorldPos_B003 = WorldPos_CS_in[1];
WorldPos_B300 = WorldPos_CS_in[2];

vec3 EdgeB300 = WorldPos_B003 - WorldPos_B030;
vec3 EdgeB030 = WorldPos_B300 - WorldPos_B003;
vec3 EdgeB003 = WorldPos_B030 - WorldPos_B300;

WorldPos_B021 = WorldPos_B030 + EdgeB300 / 3.0;
WorldPos_B012 = WorldPos_B030 + EdgeB300 * 2.0 / 3.0;
WorldPos_B102 = WorldPos_B003 + EdgeB030 / 3.0;
WorldPos_B201 = WorldPos_B003 + EdgeB030 * 2.0 / 3.0;
WorldPos_B210 = WorldPos_B300 + EdgeB003 / 3.0;
WorldPos_B120 = WorldPos_B300 + EdgeB003 * 2.0 / 3.0;

WorldPos_B021 = ProjectToPlane(WorldPos_B021, WorldPos_B030, normalize(vsNormal[0]));
WorldPos_B012 = ProjectToPlane(WorldPos_B012, WorldPos_B003, normalize(vsNormal[1]));
WorldPos_B102 = ProjectToPlane(WorldPos_B102, WorldPos_B003, normalize(vsNormal[1]));
WorldPos_B201 = ProjectToPlane(WorldPos_B201, WorldPos_B300, normalize(vsNormal[2]));
WorldPos_B210 = ProjectToPlane(WorldPos_B210, WorldPos_B300, normalize(vsNormal[2]));
WorldPos_B120 = ProjectToPlane(WorldPos_B120, WorldPos_B030, normalize(vsNormal[0]));

vec3 Center = (WorldPos_B003 + WorldPos_B030 + WorldPos_B300) / 3.0;
WorldPos_B111 = (WorldPos_B021 + WorldPos_B012 + WorldPos_B102 +
WorldPos_B201 + WorldPos_B210 + WorldPos_B120) / 6.0;
WorldPos_B111 += (WorldPos_B111 - Center) / 2.0;
}

void main()
{
for (int i = 0 ; i < 3 ; i++) {
tcsVertex[i] = TexCoord_CS_in[i];
tevVertex[i] = teVertex[i];
tcsNormal[i] = vsNormal[i];
tcsColor[i] = vsColor[i];
}

CalcPositions();

gl_TessLevelOuter[0] = 50;
gl_TessLevelOuter[1] = 50;
gl_TessLevelOuter[2] = 50;
gl_TessLevelOuter[3] = 50;
gl_TessLevelInner[0] = 50;
gl_TessLevelInner[1] = 50;
}
76 changes: 76 additions & 0 deletions avogadro/rendering/mesh_tev.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#version 400
precision highp float;

layout(triangles, equal_spacing, ccw) in;

patch in vec3 WorldPos_B030;
patch in vec3 WorldPos_B021;
patch in vec3 WorldPos_B012;
patch in vec3 WorldPos_B003;
patch in vec3 WorldPos_B102;
patch in vec3 WorldPos_B201;
patch in vec3 WorldPos_B300;
patch in vec3 WorldPos_B210;
patch in vec3 WorldPos_B120;
patch in vec3 WorldPos_B111;

patch in vec2 tcsVertex[3];
patch in vec3 tcsNormal[3];
patch in vec3 tevVertex[3];
patch in vec4 tcsColor[3];

out vec2 teVertex;
out vec3 teNormal;
out vec3 WorldPos_FS_in;
out vec4 teColor;

uniform mat4 modelView;
uniform mat4 projection;

vec3 interpolate3D(vec3 v0, vec3 v1, vec3 v2)
{
return gl_TessCoord.x * v0 + gl_TessCoord.y * v1 + gl_TessCoord.z * v2;
}

vec2 interpolate2D(vec2 v0, vec2 v1, vec2 v2)
{
return gl_TessCoord.x * v0 + gl_TessCoord.y * v1 + gl_TessCoord.z * v2;
}

vec4 interpolate4D(vec4 v0, vec4 v1, vec4 v2)
{
return gl_TessCoord.x * v0 + gl_TessCoord.y * v1 + gl_TessCoord.z * v2;
}

void main()
{
vec2 p0 = tcsVertex[0];
vec2 p1 = tcsVertex[1];
vec2 p2 = tcsVertex[2];

vec3 n0 = tcsNormal[0];
vec3 n1 = tcsNormal[1];
vec3 n2 = tcsNormal[2];

teVertex = interpolate2D(p0, p1, p2);
teNormal = normalize(interpolate3D(tcsNormal[0], tcsNormal[1], tcsNormal[2]));
teColor = interpolate4D(tcsColor[0], tcsColor[1], tcsColor[2]);

float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
float w = gl_TessCoord.z;

float uPow3 = pow(u, 3);
float vPow3 = pow(v, 3);
float wPow3 = pow(w, 3);
float uPow2 = pow(u, 2);
float vPow2 = pow(v, 2);
float wPow2 = pow(w, 2);

WorldPos_FS_in = WorldPos_B300 * wPow3 + WorldPos_B030 * uPow3 + WorldPos_B003 * vPow3 +
WorldPos_B210 * 3.0 * wPow2 * u + WorldPos_B120 * 3.0 * w * uPow2 + WorldPos_B201 * 3.0 * wPow2 * v +
WorldPos_B021 * 3.0 * uPow2 * v + WorldPos_B102 * 3.0 * w * vPow2 + WorldPos_B012 * 3.0 * u * vPow2 +
WorldPos_B111 * 6.0 * w * u * v;

gl_Position = projection * vec4(WorldPos_FS_in, 1.0);
}
23 changes: 16 additions & 7 deletions avogadro/rendering/mesh_vs.glsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
attribute vec4 vertex;
attribute vec4 color;
attribute vec3 normal;
#version 400
precision highp float;

in vec3 vertex;
in vec3 normal;
in vec4 color;

uniform mat4 modelView;
uniform mat4 projection;
uniform mat3 normalMatrix;

varying vec3 fnormal;
out vec3 WorldPos_CS_in;
out vec2 TexCoord_CS_in;
out vec3 vsNormal;
out vec3 teVertex;
out vec4 vsColor;

void main()
{
gl_FrontColor = color;
gl_Position = projection * modelView * vertex;
fnormal = normalize(normalMatrix * normal);
vsColor = color;
teVertex = vertex;
WorldPos_CS_in = (modelView * vec4(vertex, 1.0)).xyz;
TexCoord_CS_in = vertex.xy;
vsNormal = normalize(normalMatrix * normal);
}
Loading
Loading