Skip to content

Instantly share code, notes, and snippets.

@recp
Last active November 16, 2017 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recp/b0368c74c35d9d6912f524624bfbf5a3 to your computer and use it in GitHub Desktop.
Save recp/b0368c74c35d9d6912f524624bfbf5a3 to your computer and use it in GitHub Desktop.
/*
* Copyright (c), Recep Aslantas.
* MIT License (MIT), http://opensource.org/licenses/MIT
*/
#version 410
#define AmbientlLight 1u
#define DirectionalLight 2u
#define PointLight 3u
#define SpotLight 4u
#define Discard 0u
#define UseTex 1u
#define UseColor 2u
#define MixTexColor 3u
in vec3 vPosition;
in vec3 vNormal;
in vec3 vEye;
in vec2 vTexCoord[3];
uniform sampler2D tex2D[3];
// ColorOrTexture -> cr (ColoR) tx (TeXture): crtx
struct crtx {
vec4 color;
uint method;
uint tex;
};
struct Material {
crtx emission;
crtx ambient;
crtx specular;
crtx reflective;
crtx transparent;
crtx diffuse;
float shininess;
float reflectivEyety;
float transparency;
float indexOfRefraction;
};
struct Light {
int enabled;
uint type;
vec4 ambient;
vec4 color;
vec3 position;
vec3 direction;
float cutoffCos;
float cutoffExp;
float constAttn;
float linAttn;
float quadAttn;
};
subroutine
vec4
technique(vec4 light, vec3 L);
vec4
color(in crtx ct) {
switch (ct.method) {
case UseColor:
return ct.color;
case UseTex:
return texture(tex2D[ct.tex], vTexCoord[ct.tex]);
case MixTexColor:
return texture(tex2D[ct.tex], vTexCoord[ct.tex]) * ct.color;
default:
return vec4(0.0); /* DISCARD */
}
}
subroutine uniform technique effect;
uniform Material material;
uniform Light lights[16];
uniform int lightCount;
// techniques
subroutine(technique)
vec4
phong(vec4 light, vec3 L) {
float Ls, Ld;
Ld = max(0.0, dot(vNormal, L));
if (Ld == 0.0)
Ls = 0.0;
else
Ls = pow(max(0.0, dot(reflect(-L, vNormal), vEye)),
material.shininess);
return color(material.emission)
+ light * color(material.diffuse) * Ld
+ light * color(material.specular) * Ls;
}
subroutine(technique)
vec4
blinn(vec4 light, vec3 L) {
vec3 H;
float Ls, Ld;
H = normalize(L + vEye);
Ld = max(0.0, dot(vNormal, L));
Ls = max(0.0, dot(vNormal, H));
if (Ld == 0.0)
Ls = 0.0;
else
Ls = pow(Ls, material.shininess);
return color(material.emission)
+ light * color(material.diffuse) * Ld
+ light * color(material.specular) * Ls;
}
subroutine(technique)
vec4
lambert(vec4 light, vec3 L) {
float Ld;
Ld = max(0.0, dot(vNormal, L));
return color(material.emission)
+ light * color(material.diffuse) * Ld;
}
subroutine(technique)
vec4
constant(vec4 light, vec3 L) {
return color(material.emission);
}
// lights
float
point(Light light, inout vec3 L) {
float dist;
L = light.position - vPosition;
dist = length(L);
L = L / dist;
return 1.0 / (light.constAttn
+ light.linAttn * dist
+ light.quadAttn * dist * dist);
}
float
spot(Light light, inout vec3 L) {
float dist;
float spotCos;
L = light.position - vPosition;
dist = length(L);
L = L / dist;
spotCos = dot(light.direction, -L);
if (spotCos < light.cutoffCos)
return 0.0;
return pow(spotCos, light.cutoffExp) / (light.constAttn
+ light.linAttn * dist
+ light.quadAttn * dist * dist);
}
float
directional(Light light, inout vec3 L) {
L = -light.direction;
return 1.0;
}
out vec4 fragColor;
void main() {
vec4 final, al, amb;
vec3 L, H;
float a, Ld, Ls;
al = vec4(0.0, 0.0, 0.0, 1.0);
final = vec4(0.0);
for (int i = 0; i < lightCount; i++) {
if (lights[i].enabled == 0)
continue;
switch (lights[i].type) {
case SpotLight:
a = spot(lights[i], L);
break;
case PointLight:
a = point(lights[i], L);
break;
case DirectionalLight:
a = directional(lights[i], L);
break;
default:
discard;
return;
}
al += lights[i].ambient * a;
final += effect(lights[i].color * a, L);
}
fragColor = material.ambient.color * al + final;
}
@recp
Copy link
Author

recp commented Nov 16, 2017

This shader is not used by libgk anymore!
Now there is a shader generator and shader manager for specific purposes/tasks

Check new version at: https://github.com/recp/libgk/tree/master/src/shader/glsl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment