Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML GPU activity and shaders  (Read 2193 times)

0 Members and 2 Guests are viewing this topic.

SELondoner

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
SFML GPU activity and shaders
« on: April 09, 2021, 04:53:05 pm »
Hi community,

I've been really enjoying getting to grips with sfml and refreshing my c++ in the process (did a course on it many year ago). I'm playing around with a little zombie game (early days) and I got some fun particle effects going (vertex arrays) but thought i'd look into shaders.

I'm on a 2015 macbook and using xcode.
My 'system' doesn't seem to understand glEnable(GL_TEXTURE_2D), I have then read that apple has stopped supporting opengl (I knew they'd gone to metal but didn't realise that this was instead of opengl). Anyway, it appears there's nowt I can do and if I want to carry on with the full enjoyment of sfml I may have to invest in a windows machine. This may not be the place but for very good fancy shader 2D stuff should I be looking at a laptop with a dedicated graphics card or can you guys get good performance with an integrated card.

kind regards and would appreciate any advice.

SEL.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: SFML GPU activity and shaders
« Reply #1 on: April 09, 2021, 06:28:03 pm »
OpenGL is only deprecated and can still be used. SFML even runs on the latest devices with the new M1 processors just fine.

If you buy new, there's really no point in not investing a bit more to get a device with a dedicated GPU IMHO. If you buy refurbished, then your budget basically dictates what you're getting.
Intel's integrated chips on newer processors isn't too bad, but the drivers aren't always the best. For basic SFML 2D graphics, it should be fine, but it will become quickly noticeable once you fire up some bigger games.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SELondoner

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: SFML GPU activity and shaders
« Reply #2 on: April 10, 2021, 11:55:48 am »
Thanks exploiter,

You were correct.

Spent most of yesterday but my macbook does seem to have the capability to run some shaders so for now I'm going to make do.

kind regards,
SEL

SELondoner

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: SFML GPU activity and shaders
« Reply #3 on: April 10, 2021, 12:01:56 pm »
Just in case anyone else is looking for code, this is what I found at about 1.30 in the morning (https://github.com/SFML/SFML/wiki/Source%3A-Radial-Gradient-Shader, thanks Hapax).

It is as was but just changed setParameter to setUniform as it was deprecated.

#include <SFML/Graphics.hpp>
#include <iostream>

const char VertexShader[] =
"void main()"
"{"
    "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
    "gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"
    "gl_FrontColor = gl_Color;"
"}";

const char RadialGradient[] =
"uniform vec4 color;"
"uniform vec2 center;"
"uniform float radius;"
"uniform float expand;"
"uniform float windowHeight;"
"void main(void)"
"{"
"vec2 centerFromSfml = vec2(center.x, windowHeight - center.y);"
"vec2 p = (gl_FragCoord.xy - centerFromSfml) / radius;"
    "float r = sqrt(dot(p, p));"
    "if (r < 1.0)"
    "{"
        "gl_FragColor = mix(color, gl_Color, (r - expand) / (1.0 - expand));"
    "}"
    "else"
    "{"
        "gl_FragColor = gl_Color;"
    "}"
"}";

int main()
{
    if (!sf::Shader::isAvailable())
    {
        std::cerr << "Shaders are not available." << std::endl;
        return EXIT_FAILURE;
    }

    sf::RenderWindow window(sf::VideoMode(800, 600), "Radial Gradient", sf::Style::Default);
    window.setFramerateLimit(30);

    sf::CircleShape circle;
    sf::RectangleShape rectangle;
    sf::Shader shader;

    circle.setRadius(50.f);
    circle.setOrigin(circle.getRadius(), circle.getRadius());
    circle.setPosition(sf::Vector2f(window.getSize()) / 2.f);
    circle.setFillColor(sf::Color::Transparent);

    rectangle.setSize({ 300.f, 200.f });
    rectangle.setFillColor(sf::Color::Yellow);
    rectangle.setOrigin(rectangle.getLocalBounds().width / 2.f, rectangle.getLocalBounds().height / 2.f);
    rectangle.setPosition(sf::Vector2f(window.getSize()) / 4.f);

    shader.loadFromMemory(VertexShader, RadialGradient);
    shader.setUniform("windowHeight", static_cast<float>(window.getSize().y)); // this must be set, but only needs to be set once (or whenever the size of the window changes)

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if ((event.type == sf::Event::Closed) || (event.type == sf::Event::KeyPressed) || (event.key.code == sf::Keyboard::Escape))
                window.close();
        }
        sf::Glsl::Vec4 tcolor = sf::Color::Red;
        sf::Glsl::Vec4 tcolor2 = sf::Color::Blue;
        window.clear();
        shader.setUniform("color", tcolor);
        shader.setUniform("center", rectangle.getPosition() + sf::Vector2f(0.f, 0.f));
        shader.setUniform("radius", 200.f);
        shader.setUniform("expand", 0.25f);
        window.draw(rectangle, &shader);
        shader.setUniform("color", tcolor2);
        shader.setUniform("center", circle.getPosition());
        shader.setUniform("radius", circle.getRadius());
        shader.setUniform("expand", 0.f);
        window.draw(circle, &shader);
        window.display();
    }

    return EXIT_SUCCESS;
}

 

anything