v / examples / sokol / 06_obj_viewer / gouraud.glsl
109 lines · 88 sloc · 3.75 KB · 3c68e78f32d95e0552f6fa496ed245c1304731d5
Raw
1//#pragma sokol @ctype mat4 hmm_mat4
2
3#pragma sokol @vs vs
4
5uniform vs_params {
6 mat4 u_MVMatrix; // A constant representing the combined model/view matrix.
7 mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix.
8 mat4 u_NMatrix; // A constant representing the Normal Matrix
9};
10
11in vec4 a_Position; // Per-vertex position information we will pass in.
12in vec3 a_Normal; // Per-vertex normal information we will pass in.
13//in vec4 a_Color; // Per-vertex color information we will pass in.
14in vec2 a_Texcoord0;
15
16out vec3 v_Position; // This will be passed into the fragment shader.
17//out vec4 v_Color; // This will be passed into the fragment shader.
18out vec3 v_Normal; // This will be passed into the fragment shader.
19out vec3 v_Normal1;
20out vec2 uv; // This will be passed into the fragment shader.
21
22// The entry point for our vertex shader.
23void main()
24{
25 // Transform the vertex into eye space.
26 v_Position = vec3(u_MVMatrix * a_Position);
27 // Pass through the color.
28 //v_Color = a_Color;
29 // calc eye space normal
30 v_Normal = vec3(u_NMatrix * vec4(a_Normal, 1.0));
31 // texture coord
32 uv = a_Texcoord0;
33
34 v_Normal1 = normalize(vec3(u_MVMatrix * vec4(a_Normal, 1.0)));
35
36 // gl_Position is a special variable used to store the final position.
37 // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
38 gl_Position = u_MVPMatrix * a_Position;
39}
40
41#pragma sokol @end
42
43#pragma sokol @fs fs
44//precision mediump float; // Set the default precision to medium. We don't need as high of a precision in the fragment shader
45uniform texture2D tex;
46uniform sampler smp;
47uniform fs_params {
48 vec4 u_LightPos; // The position of the light in eye space.
49 vec4 ambientColor;
50 vec4 diffuseColor;
51 vec4 specularColor;
52
53};
54in vec3 v_Position; // Interpolated position for this fragment.
55//in vec4 v_Color; // This is the color from the vertex shader interpolated across the triangle per fragment.
56in vec3 v_Normal; // Interpolated normal for this fragment.
57in vec3 v_Normal1;
58in vec2 uv;
59out vec4 frag_color;
60
61vec3 lightDirection = -u_LightPos.xyz;// vec3(0.0, -0.5, 0.5);
62//const vec4 ambientColor = vec4(0.094, 0.0, 0.0, 1.0);
63//const vec4 diffuseColor = vec4(0.5, 0.0, 0.0, 1.0);
64//const vec4 specularColor = vec4(1.0, 1.0, 1.0, 1.0);
65//const float shininess = 10.0;
66const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
67
68vec3 phongBRDF(vec3 lightDir, vec3 viewDir, vec3 normal, vec3 phongDiffuseCol, vec3 phongSpecularCol, float phongShininess) {
69 vec3 color = phongDiffuseCol;
70 vec3 reflectDir = reflect(-lightDir, normal);
71 float specDot = max(dot(reflectDir, viewDir), 0.0);
72 color += pow(specDot, phongShininess) * phongSpecularCol;
73 return color;
74}
75
76vec4 getPhong(in vec4 diffuseColor) {
77 vec3 lightDir = normalize(-lightDirection);
78 vec3 viewDir = normalize(-v_Position);
79 vec3 n = normalize(v_Normal);
80
81 vec3 luminance = ambientColor.rgb * 0.5;
82
83 float illuminance = dot(lightDir, n);
84 if(illuminance > 0.0) {
85 // we save specular shiness in specularColor.a
86 vec3 brdf = phongBRDF(lightDir, viewDir, n, diffuseColor.rgb, specularColor.rgb, specularColor.a * 1000);
87 luminance += brdf * illuminance * lightColor.rgb;
88 }
89
90 vec4 outColor = vec4(luminance,1.0);
91 return outColor;
92}
93
94// The entry point for our fragment shader.
95void main()
96{
97 vec4 txt = texture(sampler2D(tex, smp), uv);
98
99 // Directional light
100 float directional = dot(normalize(v_Normal1), normalize(vec3(0,0.5,1))) ;
101 directional = directional * 0.15;
102
103 // Multiply the color by the diffuse illumination level to get final output color.
104 frag_color = vec4(clamp(directional + txt.rgb * getPhong(diffuseColor).rgb,0,1), txt.a * diffuseColor.a);
105
106}
107#pragma sokol @end
108
109#pragma sokol @program gouraud vs fs
110