.cargo/
ombre/
ombre-derive/
reve/
src/
renderer/
shaders/
blinn/
fragment.glsl
6.2 KiB
geometry.glsl
4.7 KiB
vertex.glsl
1.7 KiB
constant/
display/
mvp/
mesh.rs
9.3 KiB
scene.rs
3.2 KiB
shaders.rs
6.0 KiB
camera.rs
3.8 KiB
lib.rs
11.7 KiB
main.rs
162 B
renderer.rs
18.6 KiB
Cargo.toml
362 B
README
398 B
syntaxe/
.gitignore
34 B
Cargo.lock
8.5 KiB
Cargo.toml
523 B
README
1.0 KiB
rust-toolchain.toml
77 B
rustfmt.toml
55 B
reve/src/renderer/shaders/blinn/fragment.glsl
raw
| 1 | // TODO: When tonemapping is enabled, the diffuse channel needs to be brightened. |
| 2 | #version 330 core |
| 3 | |
| 4 | const uint InteractionModeObjects = 0u; |
| 5 | const uint InteractionModeEdges = 1u; |
| 6 | const uint InteractionModePoints = 2u; |
| 7 | const uint InteractionModePolygons = 3u; |
| 8 | |
| 9 | // Render diffuse color. |
| 10 | const uint RenderDiffuse = 1u; |
| 11 | // Flat shading. |
| 12 | const uint RenderFlatShaded = 1u << 1; |
| 13 | // Show specular highlights. |
| 14 | const uint RenderSpecular = 1u << 2; |
| 15 | // Show textures. |
| 16 | const uint RenderTexture = 1u << 3; |
| 17 | // Render wireframes. |
| 18 | const uint RenderWireframe = 1u << 4; |
| 19 | |
| 20 | const uint VertexStyleDefault = 1u << 0; |
| 21 | const uint VertexStyleConstant = 1u << 1; |
| 22 | |
| 23 | const float PI = 3.1415926535897932384626433832795; |
| 24 | |
| 25 | // Parameters for selection pattern. |
| 26 | const float SelectionGridSpacing = 3.0; // Controls the size of the grid cells |
| 27 | const float SelectionDotRadius = 1.0; // Controls the size of the dots, relative to the grid size |
| 28 | |
| 29 | smooth in vec3 fragPosWorld; |
| 30 | smooth in vec2 textureCoord; |
| 31 | smooth in vec3 viewDirWorld; |
| 32 | smooth in vec4 vertexColor; |
| 33 | smooth in float vertexOpacity; |
| 34 | smooth in vec3 vertexNormalSmooth; |
| 35 | flat in int vertexObjectId; |
| 36 | flat in float vertexSelected; |
| 37 | flat in float vertexHovered; |
| 38 | flat in vec3 vertexNormalFlat; |
| 39 | flat in uint vertexStyle; |
| 40 | noperspective in vec3 vertexDist; |
| 41 | |
| 42 | layout(location = 0) out vec4 fragColor; |
| 43 | layout(location = 1) out uint objectIdOutput; |
| 44 | |
| 45 | uniform bool enableTonemap; |
| 46 | uniform uint renderMode; |
| 47 | uniform vec3 light; |
| 48 | uniform sampler2D diffuseSampler; |
| 49 | uniform sampler2D specularSampler; |
| 50 | uniform uint interactionMode; |
| 51 | |
| 52 | struct lightState { |
| 53 | vec4 intensity; |
| 54 | vec4 ambience; |
| 55 | float attenuation; |
| 56 | float incidence; |
| 57 | }; |
| 58 | |
| 59 | vec4 tonemap(in vec4 color) { |
| 60 | const float A = 0.22; // Shoulder strength |
| 61 | const float B = 0.30; // Linear Strength |
| 62 | const float C = 0.10; // Linear Angle |
| 63 | const float D = 0.20; // Toe Strength |
| 64 | const float E = 0.01; // Toe Numerator |
| 65 | const float F = 0.30; // Toe Denominator |
| 66 | |
| 67 | return ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E/F; |
| 68 | } |
| 69 | |
| 70 | vec4 computeFragColor(in vec4 diffuse, in vec4 specular, in float blinnTerm, in lightState l) { |
| 71 | float W = 11.2; // Whitepoint |
| 72 | |
| 73 | vec4 color = (diffuse * l.intensity * l.incidence * l.attenuation) + |
| 74 | (specular * l.attenuation * blinnTerm) + |
| 75 | (diffuse * l.ambience); |
| 76 | |
| 77 | float exposure = 1.0f; |
| 78 | |
| 79 | if (enableTonemap) { |
| 80 | return tonemap(exposure * color) / tonemap(vec4(W)); |
| 81 | } else { |
| 82 | return color; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | float cookTorrance(in float roughness, in vec3 halfAngle, in vec3 normal, in vec3 viewDir, in vec3 lightDir) { |
| 87 | float F0 = 0.8; // Reflectance at normal incidence |
| 88 | float m2 = roughness * roughness; |
| 89 | |
| 90 | float NdotH = dot(normal, halfAngle); |
| 91 | float NdotV = dot(normal, viewDir); |
| 92 | float NdotL = dot(normal, lightDir); |
| 93 | float VdotH = dot(viewDir, halfAngle); |
| 94 | |
| 95 | // Beckman's distribution function D |
| 96 | float x = (NdotH * NdotH - 1.0) / (NdotH * NdotH * m2); |
| 97 | float D = exp(x) / (4.0 * m2 * pow(NdotH, 4)); |
| 98 | |
| 99 | // Fresnel term F |
| 100 | float F = pow(1.0 - NdotV, 5.0); |
| 101 | F *= (1.0 - F0); |
| 102 | F += F0; |
| 103 | |
| 104 | // Self-shadowing term G |
| 105 | float X = 2.0 * NdotH / VdotH; |
| 106 | float G = min(1.0, min(X * NdotL, X * NdotV)); |
| 107 | |
| 108 | return (D * F * G) / (NdotV * PI); |
| 109 | } |
| 110 | |
| 111 | float gaussian(in float roughness, in vec3 halfAngle, in vec3 normal) { |
| 112 | float angleNormalHalf = acos(dot(halfAngle, normal)); |
| 113 | float exponent = angleNormalHalf / roughness; |
| 114 | exponent = -(exponent * exponent); |
| 115 | |
| 116 | return exp(exponent); |
| 117 | } |
| 118 | |
| 119 | float blinn(in float roughness, in vec3 halfAngle, in vec3 normal) { |
| 120 | float blinnTerm = clamp(dot(normal, halfAngle), 0, 1); |
| 121 | blinnTerm = pow(blinnTerm, roughness); |
| 122 | |
| 123 | return blinnTerm; |
| 124 | } |
| 125 | |
| 126 | void main() { |
| 127 | lightState l; |
| 128 | |
| 129 | if (vertexStyle == VertexStyleConstant) { |
| 130 | objectIdOutput = uint(vertexObjectId) + 1u; // Vertex IDs start from `1`, as the zero value is reserved for no selection. |
| 131 | fragColor = vertexSelected >= 1.0 ? vec4(1.0, 0.0, 0.0, 1.0) : vertexColor; |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // TODO: Intensity should be a uniform. |
| 136 | l.intensity = vec4(1.0, 1.0, 0.9, 0.0) * 30; |
| 137 | l.ambience = vec4(0.05, 0.05, 0.08, 0.0); |
| 138 | |
| 139 | const float roughness = 0.3; |
| 140 | const float specularIntensity = 15.0; |
| 141 | |
| 142 | vec4 diffuse = bool(renderMode & RenderDiffuse) |
| 143 | ? vertexColor |
| 144 | : vec4(0.0); |
| 145 | |
| 146 | vec3 vertexNormal = bool(renderMode & RenderFlatShaded) |
| 147 | ? vertexNormalFlat |
| 148 | : vertexNormalSmooth; |
| 149 | |
| 150 | vec4 specular = bool(renderMode & RenderSpecular) |
| 151 | ? vec4(texture(specularSampler, textureCoord).rgb * specularIntensity, 1.0) |
| 152 | : vec4(0.0); |
| 153 | |
| 154 | if (bool(renderMode & RenderTexture)) { |
| 155 | vec2 uv = vec2(textureCoord.s, 1.0 - textureCoord.t); |
| 156 | vec4 texel = texture(diffuseSampler, uv); |
| 157 | vec4 textured = vec4(mix(texel.rgb, vertexColor.rgb, vertexColor.a), texel.a); |
| 158 | |
| 159 | diffuse = textured; |
| 160 | } |
| 161 | |
| 162 | float lightDistance = length(light - fragPosWorld); |
| 163 | vec3 lightDirWorld = normalize(light - fragPosWorld); |
| 164 | |
| 165 | l.attenuation = 1.0 / (1.0 + lightDistance * lightDistance); |
| 166 | l.incidence = clamp(dot(lightDirWorld, vertexNormal), 0, 1); |
| 167 | |
| 168 | vec3 halfAngle = normalize(lightDirWorld + viewDirWorld); |
| 169 | float specTerm = cookTorrance(roughness, halfAngle, vertexNormal, viewDirWorld, lightDirWorld); |
| 170 | |
| 171 | if (l.incidence == 0.0) { |
| 172 | specTerm = 0.0; |
| 173 | } |
| 174 | vec4 outputColor = computeFragColor(diffuse, specular, specTerm, l); |
| 175 | |
| 176 | // Polygon selections. |
| 177 | if (interactionMode == InteractionModePolygons) { |
| 178 | if (bool(vertexSelected)) { |
| 179 | vec2 gridPosition = mod(gl_FragCoord.xy, SelectionGridSpacing); |
| 180 | vec2 fromCenter = abs(gridPosition - SelectionGridSpacing * 0.5); // Distance of each fragment from the center of the grid cell |
| 181 | float isDot = step(length(fromCenter), SelectionDotRadius); // Check if the fragment is within the dot radius |
| 182 | |
| 183 | if (isDot >= 1.0) { |
| 184 | outputColor = vec4(1.0, 0.0, 0.0, 1.0); |
| 185 | } |
| 186 | } |
| 187 | if (bool(vertexHovered)) { |
| 188 | outputColor = mix(outputColor, vec4(1.0, 0.0, 0.0, 1.0), 0.25); |
| 189 | } |
| 190 | } |
| 191 | // Wireframe overlay. |
| 192 | if (bool(renderMode & RenderWireframe)) { |
| 193 | float minDistance = min(vertexDist.x, min(vertexDist.y, vertexDist.z)); |
| 194 | float wireWidth = 1.0; |
| 195 | vec4 wireColor = mix(vec4(1.0), outputColor, 0.33); |
| 196 | float wireAlpha = smoothstep(wireWidth, 2.0 * wireWidth, minDistance); |
| 197 | |
| 198 | outputColor = mix(wireColor, outputColor, wireAlpha); |
| 199 | } |
| 200 | objectIdOutput = uint(vertexObjectId); |
| 201 | fragColor = vec4(outputColor.rgb, vertexOpacity); |
| 202 | } |