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

Author Topic: Implementing VAO in SFML/OpenGL with shaders  (Read 2126 times)

0 Members and 1 Guest are viewing this topic.

StackDanny

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Implementing VAO in SFML/OpenGL with shaders
« on: January 21, 2019, 02:16:36 pm »
Hello, I am following a OpenGL tutorial series on YouTube and they use SDL for the window. I, however, would like to use SFML as I am very familiar with it. Well, except for all the OpenGL stuff. So I am trying to load my data to the gpu as a VAO. As far as I understand, These data points can be accessed in the shaders with
layout(location = 0)
I have never worked with this layout keyword and the in and out keywords, but I hope I understand them so far. The problem is that my program only provides me a white screen (no triangle). My system has occasionally crashed from time to time while I try to debug this Code (there seems to be a serious problem). I've read the SFML examples but their OpenGL example doesn't involve shaders. I've also searched on forums but they all seem to discuss problems beyond this "hello world" introduction-like program. It's really demotivating  :-\ after hours and hours not beeing able to get this simple program to run. I'd be so relieved if someone could explain my error(s). Thank you! :)

simple.vert
#version 330 core

layout(location = 0) in vec3 a_position;

void main(){
   gl_Position = vec4(a_position, 1.0f);
}

simple.frag
#version 330 core

out vec4 fragColor;

void main(){
        fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

main.cpp
#define GLEW_STATIC
#include <GL/glew.h>

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


struct Vertex {
        float x;
        float y;
        float z;
};

struct VertexBuffer {

        VertexBuffer(Vertex* data, uint32_t numVertices) {
                glGenVertexArrays(1, &vao);
                this->bind();

                glGenBuffers(1, &bufferId);
                glBindBuffer(GL_ARRAY_BUFFER, bufferId);
                glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), data, GL_STATIC_DRAW);

                glEnableVertexAttribArray(0);
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), data);
                this->unbind();
        }

        virtual ~VertexBuffer() {
                glDeleteBuffers(1, &bufferId);
        }

        void bind() {
                glBindVertexArray(vao);
        }

        void unbind() {
                glBindVertexArray(0);
        }
private:
        GLuint bufferId;
        GLuint vao;
};
int main(){
        sf::ContextSettings settings;
        settings.antialiasingLevel = 4;
        settings.depthBits = 24;
        settings.majorVersion = 3;
        settings.minorVersion = 3;

        sf::RenderWindow window;
        window.create(sf::VideoMode(1280, 720), "GLSFML", sf::Style::Default, settings);
        window.setVerticalSyncEnabled(true);

        glewInit();


        sf::Shader shader;
        shader.loadFromFile("resources/simple.vert", "resources/simple.frag");
        sf::Shader::bind(&shader);


        Vertex vertices[] = {
                Vertex{-0.5f, -0.5f, 0.0f},
                Vertex{0.0f, 0.5f, 0.0f},
                Vertex{0.5f, -0.5f, 0.0f}
        };
        uint32_t numVertices = 3;

        VertexBuffer vb(vertices, numVertices);

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

                        window.setActive(true);
                        glClearColor(0.5, 0.5, 0.0, 1.0);
                        glClear(GL_COLOR_BUFFER_BIT);

                        vb.bind();
                        glDrawArrays(GL_TRIANGLES, 0, numVertices);
                        vb.unbind();

                        window.setActive(false);

                        window.display();
                }
        }
        catch (std::exception& any) {
                std::cerr << any.what() << '\n';
                std::cin.get();
        }

        return EXIT_SUCCESS;
}
 

Edit: I just found this example file: https://gist.github.com/gamepopper/1931ca297f3decdee90e785f12762192 which might be what I need here.
« Last Edit: January 22, 2019, 09:41:53 am by StackDanny »

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Implementing VAO in SFML/OpenGL with shaders
« Reply #1 on: January 31, 2019, 07:31:33 am »
Hi,

the SFML currently only support until version 120 (#version 120) of glsl. That means that instead of using in and out keywords for the variables, you will have to use the "uniform" keyword, along with the "setUniform" function of an SFML shader.

There are probably good tutorial out there for glsl 120, btw.

Hope I helped!

StackDanny

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Implementing VAO in SFML/OpenGL with shaders
« Reply #2 on: February 06, 2019, 02:01:48 pm »
Hello,

As far as I understand it binding the data with glew fills the layouted variables? So do I have to just update the version to allow layout variables?