Skip to content

Commit cb05945

Browse files
committed
add missing shaders
1 parent 2910423 commit cb05945

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
#endif
4+
5+
// Input from vertex shader
6+
varying vec3 fragPosition;
7+
varying vec4 fragColor;
8+
varying vec3 fragNormal;
9+
10+
// Uniforms
11+
uniform vec4 colDiffuse;
12+
uniform vec4 ambient;
13+
uniform vec3 viewPos;
14+
15+
#define MAX_LIGHTS 4
16+
#define LIGHT_DIRECTIONAL 0
17+
#define LIGHT_POINT 1
18+
19+
struct Light {
20+
int enabled;
21+
int type;
22+
vec3 position;
23+
vec3 target;
24+
vec4 color;
25+
};
26+
27+
uniform Light lights[MAX_LIGHTS];
28+
29+
void main()
30+
{
31+
vec3 lightDot = vec3(0.0);
32+
vec3 normal = normalize(fragNormal);
33+
vec3 viewD = normalize(viewPos - fragPosition);
34+
vec3 specular = vec3(0.0);
35+
36+
for (int i = 0; i < MAX_LIGHTS; i++)
37+
{
38+
if (lights[i].enabled == 1)
39+
{
40+
vec3 light = vec3(0.0);
41+
42+
if (lights[i].type == LIGHT_DIRECTIONAL)
43+
light = -normalize(lights[i].target - lights[i].position);
44+
45+
if (lights[i].type == LIGHT_POINT)
46+
light = normalize(lights[i].position - fragPosition);
47+
48+
float NdotL = max(dot(normal, light), 0.0);
49+
lightDot += lights[i].color.rgb * NdotL;
50+
51+
if (NdotL > 0.0)
52+
{
53+
float specCo = pow(max(0.0, dot(viewD, reflect(-light, normal))), 16.0);
54+
specular += specCo;
55+
}
56+
}
57+
}
58+
59+
vec4 finalColor = (fragColor * ((colDiffuse + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
60+
finalColor += fragColor * (ambient / 10.0) * colDiffuse;
61+
62+
finalColor = pow(finalColor, vec4(1.0/2.2)); // gamma correction
63+
64+
gl_FragColor = finalColor;
65+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
#endif
4+
5+
// Input vertex attributes
6+
attribute vec3 vertexPosition;
7+
attribute vec3 vertexNormal;
8+
attribute vec4 vertexColor;
9+
// attribute vec2 vertexTexCoord;
10+
11+
// Input uniform values
12+
uniform mat4 mvp;
13+
uniform mat4 matModel;
14+
uniform mat4 matNormal;
15+
16+
// Output to fragment shader
17+
varying vec3 fragPosition;
18+
varying vec4 fragColor;
19+
varying vec3 fragNormal;
20+
21+
void main()
22+
{
23+
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
24+
fragColor = vertexColor;
25+
fragNormal = normalize(vec3(matNormal * vec4(vertexNormal, 1.0)));
26+
27+
gl_Position = mvp * vec4(vertexPosition, 1.0);
28+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Input from vertex shader
2+
varying vec3 fragPosition;
3+
varying vec4 fragColor;
4+
varying vec3 fragNormal;
5+
6+
// Uniforms
7+
uniform vec4 colDiffuse;
8+
uniform vec4 ambient;
9+
uniform vec3 viewPos;
10+
11+
#define MAX_LIGHTS 4
12+
#define LIGHT_DIRECTIONAL 0
13+
#define LIGHT_POINT 1
14+
15+
struct Light {
16+
int enabled;
17+
int type;
18+
vec3 position;
19+
vec3 target;
20+
vec4 color;
21+
};
22+
23+
uniform Light lights[MAX_LIGHTS];
24+
25+
void main()
26+
{
27+
vec3 lightDot = vec3(0.0);
28+
vec3 normal = normalize(fragNormal);
29+
vec3 viewD = normalize(viewPos - fragPosition);
30+
vec3 specular = vec3(0.0);
31+
32+
for (int i = 0; i < MAX_LIGHTS; i++)
33+
{
34+
if (lights[i].enabled == 1)
35+
{
36+
vec3 light = vec3(0.0);
37+
38+
if (lights[i].type == LIGHT_DIRECTIONAL)
39+
light = -normalize(lights[i].target - lights[i].position);
40+
41+
if (lights[i].type == LIGHT_POINT)
42+
light = normalize(lights[i].position - fragPosition);
43+
44+
float NdotL = max(dot(normal, light), 0.0);
45+
lightDot += lights[i].color.rgb * NdotL;
46+
47+
if (NdotL > 0.0)
48+
{
49+
float specCo = pow(max(0.0, dot(viewD, reflect(-light, normal))), 16.0);
50+
specular += specCo;
51+
}
52+
}
53+
}
54+
55+
vec4 finalColor = (fragColor * ((colDiffuse + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
56+
finalColor += fragColor * (ambient / 10.0) * colDiffuse;
57+
58+
finalColor = pow(finalColor, vec4(1.0/2.2)); // gamma correction
59+
60+
gl_FragColor = finalColor;
61+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Input vertex attributes
2+
attribute vec3 vertexPosition;
3+
attribute vec3 vertexNormal;
4+
attribute vec4 vertexColor;
5+
6+
// Uniforms
7+
uniform mat4 mvp;
8+
uniform mat4 matModel;
9+
uniform mat4 matNormal;
10+
11+
// Output to fragment shader
12+
varying vec3 fragPosition;
13+
varying vec4 fragColor;
14+
varying vec3 fragNormal;
15+
16+
void main()
17+
{
18+
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
19+
fragColor = vertexColor;
20+
fragNormal = normalize(vec3(matNormal * vec4(vertexNormal, 1.0)));
21+
22+
gl_Position = mvp * vec4(vertexPosition, 1.0);
23+
}

0 commit comments

Comments
 (0)