GLSL is a language for creating high-level shaders. It is based on C, so it has a very similar syntax.
GLSL is the main shading language for OpenGL, and is widely used by programmers and artists, i.e., code for creating art and vice versa.
▶️ Watch the Video
👀 Codes created in the Video
main.cpp
#include <SFML/Graphics.hpp> #include <iostream> int main(){ sf::RenderWindow window(sf::VideoMode(1280,720), "SFML GLSL Shaders"); sf::Shader shader; sf::Clock clock; sf::RectangleShape rect( sf::Vector2f( static_cast<float>(window.getSize().x), static_cast<float>(window.getSize().y) ) ); if(!shader.loadFromFile("shader.frag", sf::Shader::Fragment)){ std::cerr << "Failed to load file.\n"; return EXIT_FAILURE; } while( window.isOpen() ){ sf::Event event; while( window.pollEvent(event)){ if( event.type == sf::Event::Closed ){ window.close(); } } float time = clock.getElapsedTime().asSeconds(); shader.setUniform("iTime", time); shader.setUniform("iResolution", sf::Glsl::Vec3( window.getSize().x, window.getSize().y, 1.0 )); window.clear(); window.draw(rect, &shader); window.display(); } return EXIT_SUCCESS; } // g++ main.cpp -lsfml-graphics -lsfml-window -lsfml-system
shader.frag
#version 150 core uniform float iTime; uniform vec3 iResolution; out vec4 fragColor; vec3 rand_colors(float f){ vec3 a = vec3(0.5f, 0.5f, 0.5f); vec3 b = vec3(0.5f, 0.5f, 0.5f); vec3 c = vec3(1.0f, 1.0f, 1.0f); vec3 d = vec3(0.123f, 0.456f, 0.567f); return a + b * cos(6.5 * (c * f + d)); } void main(){ vec2 fragCoord = gl_FragCoord.xy; vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y; for(int i = 0; i < 4; ++i){ uv = fract(uv); uv -= 0.5; float d = length(uv); vec3 color = rand_colors(d + iTime); d = sin(d * 8.f + iTime * 3.f) / 6.f; d = abs(d); //d -= 0.5; d = 0.02f / d; color *= d; fragColor = vec4(color, 1.0); } }
Código básico
.frag
para criar um projeto do zero:
#version 150 core uniform float iTime; uniform vec3 iResolution; out vec4 fragColor; void main(){ vec2 fragCoord = gl_FragCoord.xy; }
🔗 Links mentioned in the Video
- https://www.shadertoy.com/
- Shader created in the video: https://www.shadertoy.com/view/33sSzM
- Shader used at the end of the video: https://www.shadertoy.com/view/w3lSzN
- https://thebookofshaders.com/
- Doc function
length
: https://thebookofshaders.com/glossary/?search=length - OpenGL Tutorial for Beginners
Links to courses:
- 👑 Learn to create your own programming language
- ✅ Learn Game Creation with C++ and SFML
- ✅ C++ Promotional Package
- ✅ Learn C++ and Qt
- ✅ Discover our Courses
- 🎁 All Courses on Udemy
Additional links:
Top comments (1)
Apply your GLSL skills to the V Shader Hackathon, running until 22 May 2025!
Create unique Shader art to win up to $1000 :)
How to join: medium.com/vsystems/13-26-april-cr...
Any questions? Join our community!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.