235 {
236 View default_view;
237 default_view.SetCenter(float(width_) / 2.F, float(height_) / 2.F);
238 default_view.SetSize(float(width_), float(height_));
240
242 layout(location = 0) in vec2 space_position;
243 layout(location = 1) in vec2 texture_position;
244
245 uniform mat4 projection;
246 uniform mat4 view;
247
248 out vec2 f_texture_position;
249
250 void main() {
251 f_texture_position = texture_position;
252 gl_Position = projection * view * vec4(space_position, 0.F, 1.F);
253 }
254 )",
255 GL_VERTEX_SHADER);
256
258 in vec2 f_texture_position;
259 uniform sampler2D texture_0;
260 uniform vec4 color;
261 out vec4 out_color;
262
263 void main() {
264 out_color = texture(texture_0, f_texture_position) * color;
265 }
266 )",
267 GL_FRAGMENT_SHADER);
268
269 shader_program_2d_.AddShader(vertex_shader_2d_);
270 shader_program_2d_.AddShader(fragment_shader_2d_);
271 shader_program_2d_.Link();
272
274 layout(location = 0) in vec3 space_position;
275 layout(location = 1) in vec3 normal;
276 layout(location = 2) in vec2 texture_position;
277
278 uniform mat4 projection;
279 uniform mat4 view;
280
281 out vec4 fPosition;
282 out vec2 fTexture;
283 out vec3 fNormal;
284
285 void main() {
286 fTexture = texture_position;
287 fPosition = view * vec4(space_position,1.F);
288 fNormal = vec3(view * vec4(normal,0.F));
289
290 gl_Position = projection * fPosition;
291 }
292 )",
293 GL_VERTEX_SHADER);
294
296 uniform sampler2D texture_0;
297 uniform vec4 color;
298
299 uniform vec4 light_position;
300 uniform float ambient;
301 uniform float diffuse;
302 uniform float specular;
303 uniform float specular_power;
304
305 in vec4 fPosition;
306 in vec2 fTexture;
307 in vec3 fNormal;
308
309 out vec4 out_color;
310
311 void main(void)
312 {
313 vec3 object_dir =-normalize(fPosition.xyz);
314 vec3 normal_dir = normalize(fNormal);
315 vec3 light_dir = normalize(light_position.xyz-fPosition.xyz);
316 vec3 reflect_dir = -reflect(object_dir,normal_dir);
317
318 float diffuse_strength = max(0.F, dot(normal_dir, light_dir));
319 float specular_strength = pow(max(0.F, dot(reflect_dir, light_dir)),
320 specular_power);
321
322 out_color = texture(texture_0, fTexture);
323 out_color.rgb *= ambient +
324 diffuse * diffuse_strength +
325 specular * specular_strength;
326 out_color *= color;
327 }
328 )",
329 GL_FRAGMENT_SHADER);
330
331 shader_program_3d_.AddShader(vertex_shader_3d_);
332 shader_program_3d_.AddShader(fragment_shader_3d_);
333 shader_program_3d_.Link();
334
335 shader_program_3d_.Use();
336 constexpr auto default_light_position = glm::vec4(0.F, 5.F, 0.F, 1.F);
337 constexpr auto default_ambient = 0.3F;
338 constexpr auto default_diffuse = 0.5F;
339 constexpr auto default_specular = 0.5F;
340 constexpr auto default_specular_power = 4.F;
341 shader_program_3d_.
SetUniform(
"light_position", default_light_position);
342 shader_program_3d_.
SetUniform(
"ambient", default_ambient);
343 shader_program_3d_.
SetUniform(
"diffuse", default_diffuse);
344 shader_program_3d_.
SetUniform(
"specular", default_specular);
345 shader_program_3d_.
SetUniform(
"specular_power", default_specular_power);
346
348}
void SetView(const View &view)
Set the View to use.
void SetShaderProgram(ShaderProgram &shader_program)
Set the ShaderProgram to be used.
void SetUniform(const std::string &name, float x, float y, float z)
Assign shader vec3 uniform.
void AddShader(const Shader &shader)
Add a Shader to the program list. This must called multiple time for each shader components before ca...
void Use() const
Bind the ShaderProgram. Future draw will use it. This unbind any previously bound ShaderProgram.
void Link() const
Add a Shader to the program list.
static Shader FromString(const std::string &content, GLenum type)
Load a shader from a std::string.