OpenGL: rename simple shader to basic shader.
[blender.git] / source / blender / gpu / shaders / gpu_shader_basic_frag.glsl
1
2 /* Options:
3  *
4  * USE_COLOR: use glColor for diffuse colors
5  * USE_TEXTURE: use texture for diffuse colors
6  * USE_SCENE_LIGHTING: use lights (up to 8)
7  * USE_SOLID_LIGHTING: assume 3 directional lights for solid draw mode
8  * USE_TWO_SIDED: flip normal towards viewer
9  * NO_SPECULAR: use specular component
10  */
11
12 #define NUM_SOLID_LIGHTS 3
13 #define NUM_SCENE_LIGHTS 8
14
15 #if defined(USE_SOLID_LIGHTING) || defined(USE_SCENE_LIGHTING)
16 varying vec3 varying_normal;
17
18 #ifndef USE_SOLID_LIGHTING
19 varying vec3 varying_position;
20 #endif
21 #endif
22
23 #ifdef USE_COLOR
24 varying vec4 varying_vertex_color;
25 #endif
26
27 #ifdef USE_TEXTURE
28 varying vec2 varying_texture_coord;
29 uniform sampler2D texture_map;
30 #endif
31
32 void main()
33 {
34 #if defined(USE_SOLID_LIGHTING) || defined(USE_SCENE_LIGHTING)
35         /* compute normal */
36         vec3 N = normalize(varying_normal);
37
38 #ifdef USE_TWO_SIDED
39         if (!gl_FrontFacing)
40                 N = -N;
41 #endif
42
43         /* compute diffuse and specular lighting */
44         vec3 L_diffuse = vec3(0.0);
45 #ifndef NO_SPECULAR
46         vec3 L_specular = vec3(0.0);
47 #endif
48
49 #ifdef USE_SOLID_LIGHTING
50         /* assume 3 directional lights */
51         for (int i = 0; i < NUM_SOLID_LIGHTS; i++) {
52                 vec3 light_direction = gl_LightSource[i].position.xyz;
53
54                 /* diffuse light */
55                 vec3 light_diffuse = gl_LightSource[i].diffuse.rgb;
56                 float diffuse_bsdf = max(dot(N, light_direction), 0.0);
57                 L_diffuse += light_diffuse*diffuse_bsdf;
58
59 #ifndef NO_SPECULAR
60                 /* specular light */
61                 vec3 light_specular = gl_LightSource[i].specular.rgb;
62                 vec3 H = gl_LightSource[i].halfVector.xyz;
63
64                 float specular_bsdf = pow(max(dot(N, H), 0.0), gl_FrontMaterial.shininess);
65                 L_specular += light_specular*specular_bsdf;
66 #endif
67         }
68 #else
69         /* all 8 lights, makes no assumptions, potentially slow */
70
71 #ifndef NO_SPECULAR
72         /* view vector computation, depends on orthographics or perspective */
73         vec3 V = (gl_ProjectionMatrix[3][3] == 0.0) ? normalize(varying_position): vec3(0.0, 0.0, -1.0);
74 #endif
75
76         for (int i = 0; i < NUM_SCENE_LIGHTS; i++) {
77                 /* todo: this is a slow check for disabled lights */
78                 if (gl_LightSource[i].specular.a == 0.0)
79                         continue;
80
81                 float intensity = 1.0;
82                 vec3 light_direction;
83
84                 if (gl_LightSource[i].position.w == 0.0) {
85                         /* directional light */
86                         light_direction = gl_LightSource[i].position.xyz;
87                 }
88                 else {
89                         /* point light */
90                         vec3 d = gl_LightSource[i].position.xyz - varying_position;
91                         light_direction = normalize(d);
92
93                         /* spot light cone */
94                         if (gl_LightSource[i].spotCutoff < 90.0) {
95                                 float cosine = max(dot(light_direction, -gl_LightSource[i].spotDirection), 0.0);
96                                 intensity = pow(cosine, gl_LightSource[i].spotExponent);
97                                 intensity *= step(gl_LightSource[i].spotCosCutoff, cosine);
98                         }
99
100                         /* falloff */
101                         float distance = length(d);
102
103                         intensity /= gl_LightSource[i].constantAttenuation +
104                                 gl_LightSource[i].linearAttenuation * distance +
105                                 gl_LightSource[i].quadraticAttenuation * distance * distance;
106                 }
107
108                 /* diffuse light */
109                 vec3 light_diffuse = gl_LightSource[i].diffuse.rgb;
110                 float diffuse_bsdf = max(dot(N, light_direction), 0.0);
111                 L_diffuse += light_diffuse*diffuse_bsdf*intensity;
112
113 #ifndef NO_SPECULAR
114                 /* specular light */
115                 vec3 light_specular = gl_LightSource[i].specular.rgb;
116                 vec3 H = normalize(light_direction - V);
117
118                 float specular_bsdf = pow(max(dot(N, H), 0.0), gl_FrontMaterial.shininess);
119                 L_specular += light_specular*specular_bsdf*intensity;
120 #endif
121         }
122 #endif
123
124         /* compute diffuse color, possibly from texture or vertex colors */
125         float alpha;
126
127 #if defined(USE_TEXTURE) && defined(USE_COLOR)
128         vec4 texture_color = texture2D(texture_map, varying_texture_coord);
129
130         L_diffuse *= texture_color.rgb * varying_vertex_color.rgb;
131         alpha = texture_color.a * varying_vertex_color.a;
132 #elif defined(USE_TEXTURE)
133         vec4 texture_color = texture2D(texture_map, varying_texture_coord);
134
135         L_diffuse *= texture_color.rgb;
136         alpha = texture_color.a;
137 #elif defined(USE_COLOR)
138         L_diffuse *= varying_vertex_color.rgb;
139         alpha = varying_vertex_color.a;
140 #else
141         L_diffuse *= gl_FrontMaterial.diffuse.rgb;
142         alpha = gl_FrontMaterial.diffuse.a;
143 #endif
144
145         /* sum lighting */
146         vec3 L = gl_FrontLightModelProduct.sceneColor.rgb + L_diffuse;
147
148 #ifndef NO_SPECULAR
149         L += L_specular*gl_FrontMaterial.specular.rgb;
150 #endif
151
152         /* write out fragment color */
153         gl_FragColor = vec4(L, alpha);
154 #else
155
156         /* no lighting */
157 #if defined(USE_TEXTURE) && defined(USE_COLOR)
158         gl_FragColor = texture2D(texture_map, varying_texture_coord) * varying_vertex_color;
159 #elif defined(USE_TEXTURE)
160         gl_FragColor = texture2D(texture_map, varying_texture_coord);
161 #elif defined(USE_COLOR)
162         gl_FragColor = varying_vertex_color;
163 #else
164         gl_FragColor = gl_FrontMaterial.diffuse;
165 #endif
166
167 #endif
168 }
169