11#version 130
22
33#pragma include "../utils/ p3d_light_sources.glsl"
4-
5- #define MAX_ITERATION 20
4+ #pragma include "../utils/ p3d_material.glsl"
65
76// Input from vertex shader
8- in vec2 texcoord;
7+ in vec3 viewspacePos;
8+ in vec4 fragPos;
99in vec3 vertexNormal;
1010in vec3 primNormal;
11- in vec3 FragPos;
1211in float num;
1312
14- in vec3 pospos;
15-
1613in vec3 cam_pos;
1714in vec3 cam_dir;
1815
1916out vec4 outputColor;
20- const float atten = 0.5 ;
21- const float shininess = 0.5 ;
22-
23- const float specularStrength = 0.9 ;
24- const vec3 specColor = vec3 (1 ,1 ,1 );
25- const vec3 matColor = vec3 (0 ,0.2 ,0 );
2617
2718vec3 remap(vec3 iMin, vec3 iMax, vec3 oMin, vec3 oMax, vec3 v) {
2819 vec3 t = smoothstep (iMin, iMax, v);
@@ -35,36 +26,49 @@ float remap(float iMin, float iMax, float oMin, float oMax, float v) {
3526}
3627
3728void main() {
38- vec3 rgb_normal = (normalize (primNormal) + 0.5 ) * 0.5 ;
39-
40- // outputColor = vec4(rgb_normal, 1)*clamp(vec4(dot(primNormal, )),0,1);
41- // outputColor = vec4(rgb_normal, 1);//*clamp(primNormal, 0, 1).x;
42- vec3 lightDir = p3d_LightSource[0 ].position.xyz;
43- lightDir = vec3 (0 ,- 0.5 ,0.5 );
44-
45- float diff = max (dot (primNormal, lightDir), 0.0 );
46- vec3 diffuse = diff * vec3 (0.1 );// p3d_LightSource[0].color.xyz;
47-
48- vec3 viewDir = normalize (cam_pos - FragPos);
49- vec3 reflectDir = reflect (lightDir, normalize (primNormal));
50-
51- float spec = pow (max (dot (- viewDir, reflectDir), 0.0 ), 32 );
52- vec3 specular = specularStrength * spec * specColor;
53-
54- vec3 light = diffuse + specular;
55- // if (any(lessThan(normalize(pospos), vec3(0)))) light = vec3(0);
56-
57- vec3 result = (p3d_LightModel.ambient.xyz+ light) * vec3 (remap(0.5 , 10 , 0 , 1 , dot (pospos,vec3 (1 ))));
58- outputColor = vec4 (result, 1 );
59-
60- // outputColor = vec4()
61- // outputColor = vec4(rgb_normal, 1);// * smoothstep(0,1, dot(vertexNormal, lightDir)+0.9*dot(p3d_LightModel.ambient.xyz, vec3(1)));
62- // vec3 diffuseReflection = atten * p3d_LightSource[0].color.xyz * max(normalize(primNormal), lightDir);
63- // vec3 lightReflectDir = reflect(-lightDir, normalize(primNormal));
64- // float lightSeeDir = max(0.0, dot(lightReflectDir, cam_dir));
65- // vec3 shininessPower = vec3(pow(lightSeeDir.x, shininess), pow(lightSeeDir.y, shininess), pow(lightSeeDir.z, shininess));
66- // float shininessPower = pow(lightSeeDir, (shininess));
67- // vec3 specularReflect = atten * specColor * shininessPower;
68- //
69- // outputColor = vec4(matColor*(diffuseReflection+p3d_LightModel.ambient.xyz), 1);
29+ int mode = 1 ;
30+ // vec3 viewDir = normalize(viewspacePos.xyz-cam_pos);
31+ vec3 illumLightSum = vec3 (0 );
32+ vec3 normal = normalize (primNormal);
33+
34+ for (int i = 0 ; i < p3d_LightSource.length ; i++ ) {
35+ vec3 lightDir = p3d_LightSource[i].position.xyz;
36+ if (p3d_LightSource[i].position.w != 0 ) lightDir -= viewspacePos;
37+
38+ float distance = length (lightDir);
39+ distance = distance * distance ;
40+ lightDir = normalize (lightDir);
41+
42+ float lambertian = max (dot (lightDir, normal), 0.0 );
43+ float specular = 0.0 ;
44+
45+ if (lambertian > 0.0 ) {
46+ vec3 viewDir = normalize (- viewspacePos);
47+
48+ // this is blinn phong
49+ vec3 halfDir = normalize (lightDir + viewDir);
50+ float specAngle = max (dot (halfDir, normal), 0.0 );
51+ specular = pow (specAngle, p3d_Material.shininess);
52+
53+ // this is phong (for comparison)
54+ if (mode == 2 ) {
55+ vec3 reflectDir = reflect (- lightDir, normal);
56+ specAngle = max (dot (reflectDir, viewDir), 0.0 );
57+ // note that the exponent is different here
58+ specular = pow (specAngle, p3d_Material.shininess/ 4.0 );
59+ }
60+ }
61+ vec3 illumDiffuse = (p3d_Material.diffuse.xyz) * lambertian * p3d_LightSource[i].color.xyz * 1 / distance ;
62+ vec3 illumSpecular = p3d_Material.diffuse.xyz* p3d_Material.specular * specular * p3d_LightSource[i].color.xyz * 1 / distance ;
63+
64+ illumLightSum += illumDiffuse+ illumSpecular;
65+ }
66+
67+ // vec3 reflectDir = reflect(-lightDir, normalize(primNormal));
68+ // float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64);
69+ // vec3 specular = specularStrength * spec * specColor;
70+
71+ vec3 colorGammaCorrected = pow (p3d_LightModel.ambient.xyz* p3d_Material.ambient.xyz+ illumLightSum, vec3 (0.49504950495 ));
72+ // use the gamma corrected color in the fragment0
73+ outputColor = vec4 (colorGammaCorrected, 1.0 );
7074}
0 commit comments