Brenta Engine 1.2
Loading...
Searching...
No Matches
phong_fs.c
1static const char* phong_fs = "#version 330 core\n"
2"\n"
3"//\n"
4"// Phong uses two textures, diffuse and specular, and an ambient\n"
5"// color to light the object.\n"
6"//\n"
7"\n"
8"// Structs and uniforms\n"
9"\n"
10"struct Material\n"
11"{\n"
12" // These values as specific to phong\n"
13" sampler2D texture_diffuse1;\n"
14" sampler2D texture_specular1;\n"
15" float shininess;\n"
16"};\n"
17"uniform Material material;\n"
18"\n"
19"struct DirLight\n"
20"{\n"
21" vec3 direction;\n"
22" vec3 ambient;\n"
23" vec3 diffuse;\n"
24" vec3 specular;\n"
25" float strength;\n"
26"};\n"
27"uniform DirLight dir_light;\n"
28"uniform bool use_dir_light = false; // Set this to true to enable directional light\n"
29"\n"
30"struct PointLight\n"
31"{\n"
32" vec3 position;\n"
33" float strength;\n"
34"\n"
35" vec3 ambient;\n"
36" vec3 diffuse;\n"
37" vec3 specular;\n"
38"\n"
39" // Light attenuation\n"
40" float constant;\n"
41" float linear;\n"
42" float quadratic;\n"
43"};\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"
47"\n"
48"uniform vec3 view_pos;\n"
49"uniform float transparency = 1.0;\n"
50"\n"
51"// inputs\n"
52"in vec3 Normal;\n"
53"in vec3 FragPos;\n"
54"in vec2 TexCoords;\n"
55"\n"
56"// Outputs\n"
57"out vec4 FragColor; \n"
58"\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"
62"\n"
63"void main()\n"
64"{\n"
65" vec4 textColor = texture(material.texture_diffuse1, TexCoords);\n"
66" if (textColor.a < 0.1) discard;\n"
67"\n"
68" vec3 norm = normalize(Normal);\n"
69" vec3 viewDir = normalize(view_pos - FragPos);\n"
70"\n"
71" vec3 result = vec3(0.0);\n"
72"\n"
73" // Directional light\n"
74" if (use_dir_light)\n"
75" {\n"
76" result = calc_dir_light(dir_light, norm, viewDir);\n"
77" }\n"
78" else\n"
79" {\n"
80" // If we don't have a directional light, just use the texture\n"
81" result = vec3(textColor);\n"
82" }\n"
83"\n"
84" // Point lights\n"
85" if (n_point_lights > 0)\n"
86" {\n"
87" for (int i = 0; i < NR_POINT_LIGHTS; i++)\n"
88" {\n"
89" if (i >= n_point_lights) break;\n"
90" result += calc_point_light(point_lights[i], norm, FragPos, viewDir);\n"
91" }\n"
92" }\n"
93"\n"
94" // Output result\n"
95" FragColor = vec4(result, transparency);\n"
96"}\n"
97"\n"
98"vec3 calc_dir_light(DirLight light, vec3 normal, vec3 viewDir) \n"
99"{\n"
100" vec3 lightDir = normalize(-light.direction);\n"
101"\n"
102" // diffuse shading\n"
103" float diff = max(dot(normal, lightDir), 0.0);\n"
104"\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"
108"\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"
114"}\n"
115"\n"
116"vec3 calc_point_light(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)\n"
117"{\n"
118" vec3 lightDir = normalize(light.position - fragPos);\n"
119"\n"
120" // diffuse shading\n"
121" float diff = max(dot(normal, lightDir), 0.0);\n"
122"\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"
126"\n"
127" // attenuation\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"
131"\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"
140"}\n"
141;