Loading...
Searching...
No Matches
1static const char* phong_fs =
"#version 330 core\n"
4"// Phong uses two textures, diffuse and specular, and an ambient\n"
5"// color to light the object.\n"
8"// Structs and uniforms\n"
12" // These values as specific to phong\n"
13" sampler2D texture_diffuse1;\n"
14" sampler2D texture_specular1;\n"
17"uniform Material material;\n"
27"uniform DirLight dir_light;\n"
28"uniform bool use_dir_light = false; // Set this to true to enable directional light\n"
39" // Light attenuation\n"
44"#define NR_POINT_LIGHTS 4\n"
45"uniform PointLight point_lights[NR_POINT_LIGHTS];\n"
46"uniform int n_point_lights = 0; // Set this to the number of point lights you have\n"
48"uniform vec3 view_pos;\n"
49"uniform float transparency = 1.0;\n"
57"out vec4 FragColor; \n"
59"// Function prototypes\n"
60"vec3 calc_dir_light(DirLight light, vec3 normal, vec3 viewDir);\n"
61"vec3 calc_point_light(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);\n"
65" vec4 textColor = texture(material.texture_diffuse1, TexCoords);\n"
66" if (textColor.a < 0.1) discard;\n"
68" vec3 norm = normalize(Normal);\n"
69" vec3 viewDir = normalize(view_pos - FragPos);\n"
71" vec3 result = vec3(0.0);\n"
73" // Directional light\n"
74" if (use_dir_light)\n"
76" result = calc_dir_light(dir_light, norm, viewDir);\n"
80" // If we don't have a directional light, just use the texture\n"
81" result = vec3(textColor);\n"
85" if (n_point_lights > 0)\n"
87" for (int i = 0; i < NR_POINT_LIGHTS; i++)\n"
89" if (i >= n_point_lights) break;\n"
90" result += calc_point_light(point_lights[i], norm, FragPos, viewDir);\n"
95" FragColor = vec4(result, transparency);\n"
98"vec3 calc_dir_light(DirLight light, vec3 normal, vec3 viewDir) \n"
100" vec3 lightDir = normalize(-light.direction);\n"
102" // diffuse shading\n"
103" float diff = max(dot(normal, lightDir), 0.0);\n"
105" // specular shading\n"
106" vec3 reflectDir = reflect(-lightDir, normal);\n"
107" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess );\n"
109" // combine results\n"
110" vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords));\n"
111" vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));\n"
112" vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords));\n"
113" return (ambient + diffuse + specular) * light.strength;\n"
116"vec3 calc_point_light(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
118" vec3 lightDir = normalize(light.position - fragPos);\n"
120" // diffuse shading\n"
121" float diff = max(dot(normal, lightDir), 0.0);\n"
123" // specular shading\n"
124" vec3 reflectDir = reflect(-lightDir, normal);\n"
125" float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);\n"
128" float distance = length(light.position - fragPos);\n"
129" float attenuation = 1.0 / (light.constant + light.linear * distance + \n"
130" light.quadratic * (distance * distance));\n"
132" // combine results\n"
133" vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords));\n"
134" vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));\n"
135" vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords));\n"
136" ambient *= attenuation;\n"
137" diffuse *= attenuation;\n"
138" specular *= attenuation;\n"
139" return (ambient + diffuse + specular) * light.strength;\n"