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

Author Topic: Trying to render a texture with opengl in a SFML application  (Read 2427 times)

0 Members and 1 Guest are viewing this topic.

beamlight

  • Newbie
  • *
  • Posts: 2
    • View Profile
I am trying to render a texture through a fragment shader to a SFML rectangle, but all I get is white color instead of the rendered texture.

here is the main file, which sets up a sfml window and prepare a sfml rectangle and shader:

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

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Shader Test");
    window.setFramerateLimit(60);

    // Load texture
    sf::Texture texture;
    if (!texture.loadFromFile("n10249.jpg")) {
        std::cerr << "Failed to load texture" << std::endl;
        return -1;
    }

    // Load shader
    sf::Shader shader;
    if (!shader.loadFromFile("fragment_shader.frag", sf::Shader::Fragment)) {
        std::cerr << "Failed to load shader." << std::endl;
        return -1;
    }

    shader.setUniform("texture0", texture); // Set the texture uniform

    // Create rectangle and set texture
    sf::RectangleShape rect(sf::Vector2f(window.getSize().x, window.getSize().y));
    rect.setTexture(&texture);  // Set texture to rectangle

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(rect, &shader); // Draw rectangle with shader
        window.display();
    }

    return 0;
}

and here is the fragment shader:

#version 330 core
in vec2 TexCoords;
out vec4 FragColor;

uniform sampler2D texture0;

void main() {
    vec4 texColor = texture(texture0, TexCoords);
    FragColor = texColor; // Directly output the texture color
}


fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Trying to render a texture with opengl in a SFML application
« Reply #1 on: June 30, 2024, 02:51:53 pm »
Unless things have changed on the development branch, SFML 2 uses GLSL 1.2

Try:
#version 120
uniform sampler2D u_texture;

void main()
{
    gl_FragColor = texture2D(u_texture, gl_TexCoord[0].xy);
}

 

https://www.sfml-dev.org/tutorials/2.6/graphics-shader.php#minimal-shaders

beamlight

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Trying to render a texture with opengl in a SFML application
« Reply #2 on: June 30, 2024, 04:58:26 pm »
Thank you fallahn! It worked, do you have any suggesions for an aspiring shader programmer who wants to learn the basics? I have previous knowledge about HLSL but GLSL is brand new to me

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Trying to render a texture with opengl in a SFML application
« Reply #3 on: June 30, 2024, 05:50:33 pm »
I think the most important thing when starting out is to know that there *are* different versions of OpenGL/GLSL... and that they're kind of a mess 😅

Mixing OpenGL versions is not particularly recommended - it *might* work on Windows, *kinda* works on Linux and definitely doesn't work on macOS.

Once you get your head around that just make sure to stick with one particular version and use an appropriate library. There's the "old" style, which SFML uses (as does WebGL and GLES2 if memory serves, but don't quote me on that) or the "new" style, as taught by most OpenGL learning resources.

"Old" style OpenGL versions are up to 2.1, which uses GLSL up to 1.2
OpenGL3, 3.1 and 3.2 use GLSL 1.3, 1.4 and 1.5 - though you rarely see these anywhere
"New" style OpenGL is 3.3+ and the GLSL version numbers match (3.3, 4.0, 4.1 etc)

When you have the basics down in either version from there it's actually pretty easy to see the differences and swap between the various versions as you need to.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Trying to render a texture with opengl in a SFML application
« Reply #4 on: July 01, 2024, 10:16:08 am »
GLES2 is also the "new" style, which is why SFML can't use it as is.

Versions can somewhat be mixed with a compatibility context, but as fallahn said those are not supported on macOS.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Trying to render a texture with opengl in a SFML application
« Reply #5 on: July 01, 2024, 11:03:08 am »
I had to double check - according to this GLES2 is a mix of both 😅
https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf

It uses varyings rather than in/out and has the gl_FragColor constant (old style), however there's no gl_TexCoord array, texture coords need to be passed via a varying instead (new style)... It also uses the old style texture2D() sampler functions.

So, uh.. probably best to forget I mentioned GLES 😅