How to Create Artistic Codes with C++ and GLSL (Shaders)

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.


This content originally appeared on DEV Community and was authored by Marcos Oliveira

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

How to Create Artistic Codes with C++ and GLSL (Shaders)

👀 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

Links to courses:

Additional links:


This content originally appeared on DEV Community and was authored by Marcos Oliveira


Print Share Comment Cite Upload Translate Updates
APA

Marcos Oliveira | Sciencx (2025-04-18T23:40:47+00:00) How to Create Artistic Codes with C++ and GLSL (Shaders). Retrieved from https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/

MLA
" » How to Create Artistic Codes with C++ and GLSL (Shaders)." Marcos Oliveira | Sciencx - Friday April 18, 2025, https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/
HARVARD
Marcos Oliveira | Sciencx Friday April 18, 2025 » How to Create Artistic Codes with C++ and GLSL (Shaders)., viewed ,<https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/>
VANCOUVER
Marcos Oliveira | Sciencx - » How to Create Artistic Codes with C++ and GLSL (Shaders). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/
CHICAGO
" » How to Create Artistic Codes with C++ and GLSL (Shaders)." Marcos Oliveira | Sciencx - Accessed . https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/
IEEE
" » How to Create Artistic Codes with C++ and GLSL (Shaders)." Marcos Oliveira | Sciencx [Online]. Available: https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/. [Accessed: ]
rf:citation
» How to Create Artistic Codes with C++ and GLSL (Shaders) | Marcos Oliveira | Sciencx | https://www.scien.cx/2025/04/18/how-to-create-artistic-codes-with-c-and-glsl-shaders/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.