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

Author Topic: Program with SFML and OpenGL not working on my machine(linux)  (Read 610 times)

0 Members and 1 Guest are viewing this topic.

MadaoBot

  • Newbie
  • *
  • Posts: 3
    • View Profile
Program with SFML and OpenGL not working on my machine(linux)
« on: September 06, 2023, 08:27:54 pm »
This code works on my friend's computer, but on mine it only shows a blue screen.

#include <glm/glm.hpp>

#include <GL/glew.h>

//#include <TGUI/TGUI.hpp>
//#include <TGUI/Backend/SFML-Graphics.hpp>

#include <iostream>
#include <array>
#include <random>

#include <SFML/Window.hpp>
#include <SFML/System.hpp>

#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>//oeo


void pauseExitErrorMessage(const std::string_view msg, int err = 1)
{
    std::cout << msg;
    std::cin.get();
    exit(err);
}

int main()
{
    sf::ContextSettings context_sets {};

    context_sets.majorVersion   = 3;
    context_sets.minorVersion   = 3;
    context_sets.depthBits      = 24;
    context_sets.stencilBits    = 0;
    context_sets.attributeFlags = sf::ContextSettings::Default;

    sf::Window window {
        sf::VideoMode{800, 600},
        "OpenGL test 1",
        sf::Style::Close,
        context_sets
    };

    window.setVerticalSyncEnabled(true);
    window.setActive(true);    

    glewInit();

    // Vertex data  
    glm::vec3 vertices[] {
        { .5f,  .5f, .0f},
        { .5f, -.5f, .0f},
        {-.5f, -.5f, .0f},
        {-.5f,  .5f, .0f}
    };
   
    // Indices
    GLuint indices[] {
        0, 1, 2,
        2, 3, 0
    };

   // Create and bind vertex array object

    GLuint VAO;

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    // Create and bind buffer

    GLuint VBO;

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    // Load data into buffer
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Element buffer object

    GLuint EBO;

    glGenBuffers(1, &EBO);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // Descrive how to interpret vertex buffer and enable its interpretation
   
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
    glEnableVertexAttribArray(0);

    // Unbind VAO
    glBindVertexArray(0);

    // Draw only lines
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    // Load shaders

    sf::Shader vert_shdr;
    sf::Shader frag_shdr;

    if(!vert_shdr.loadFromFile("../shader.vert", sf::Shader::Vertex))
    {
        pauseExitErrorMessage("Failed to load vertex shader!\n");
    }

    if(!frag_shdr.loadFromFile("../shader.frag", sf::Shader::Fragment))
    {
        pauseExitErrorMessage("Failed to load fragment shader!\n");
    }

    // Bind shaders
    sf::Shader::bind(&vert_shdr);
    sf::Shader::bind(&frag_shdr);

    bool running {true};

    bool fill_shape {true};

    while(running)
    {
        sf::Event event;

        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    running = false;
                    break;
                }
                case sf::Event::MouseMoved:
                {
                    break;
                }
                case sf::Event::KeyPressed:
                {
                    if(event.KeyPressed == sf::Keyboard::F)
                    {
                        if(fill_shape)
                        {
                            fill_shape = false;

                            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
                        } else
                        {
                            fill_shape = true;

                            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                        }
                    }
                }
            }
        }

        // Clear screen

        glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Draw
       
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        // Swap buffers
        window.display();
    }

    return 0;
}
 

#version 330 core
layout (location = 0) in vec3 pos;

void main()
{
    gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
}
 

#version 330 core
out vec4 FragColor;

void main()
{
    FragColor = vec4(0.0f, 1.0f, 0.25f, 0.0f);
}
 
« Last Edit: September 08, 2023, 04:51:25 pm by MadaoBot »

texus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
    • TGUI
    • Email
Re: Program with TGUI and OpenGL not working on my machine
« Reply #1 on: September 07, 2023, 08:41:17 am »
Does this have anything to do with TGUI? If you don't include TGUI and don't link to it, does the issue still exist? If yes then you should probably remove the TGUI include and change the title so that more people might look at the code.

Can you also share the code inside shader.vert and shader.frag, so that people can look at that as well?

sf::Shader uses legacy OpenGL, while you are using modern OpenGL (e.g. requesting GL 4.6 and using glGenVertexArrays). I don't know how well those will mix in the way you are using it. I couldn't tell if this code is supposed to run correctly or not (especially not without seeing the contents of those shader files).

There are graphics drivers that happily accept shaders with wrong GLSL versions or even GLES shaders while a desktop OpenGL context is loaded, but other graphics drivers will be more strict about following the OpenGL standard. So it is possible that the GLSL shaders you are loading will work on your friend's computer but not on yours if you have different GPUs (e.g. if he has nvidia and you have amd or intel).
TGUI: C++ SFML GUI

MadaoBot

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Program with SFML and OpenGL not working on my machine(linux)
« Reply #2 on: September 08, 2023, 04:53:12 pm »
I updated the post, thanks for your reply.

texus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
    • TGUI
    • Email
Re: Program with SFML and OpenGL not working on my machine(linux)
« Reply #3 on: September 08, 2023, 07:18:13 pm »
For me your code showed a white rectangle (or white triangles if a key is pressed), so something is definitely wrong with the code.

It turns out that you are using 2 separate shaders objects, while you should only be using 1. The code should look something like this:
sf::Shader shdr;

if(!shdr.loadFromFile("../shader.vert", "../shader.frag"))
{
    pauseExitErrorMessage("Failed to load shader!\n");
}

// Bind shaders
sf::Shader::bind(&shdr);

That makes the code work for me. Also I noticed that the fill_shape is changed no matter which key is being pressed instead of only when pressing F. You will need "event.key.code == sf::Keyboard::F" there in your code.
TGUI: C++ SFML GUI

MadaoBot

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Program with SFML and OpenGL not working on my machine(linux)
« Reply #4 on: September 08, 2023, 09:00:25 pm »
It works! Thank you very much!  :)