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

Author Topic: Using OpenGL 4.5 and the SFML graphics module  (Read 3087 times)

0 Members and 1 Guest are viewing this topic.

Xemerau

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Using OpenGL 4.5 and the SFML graphics module
« on: September 10, 2016, 10:41:23 pm »
I'm trying to build a program that utilises both OpenGL 4.5 and SFML graphics module. I've been following along with the tutorial but I can't seem to get any of the graphics module code rendering too.

I'm not sure if it's a profile related issue or not, but any help would be greatly appreciated. I'll post the Shader class as an attatchment, since I'm sure the issue is in this example:

#include <GL/glew.h>
#include <SFML/OpenGL.hpp>
#include "Shader.h"
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RectangleShape.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(500,500), "sfOpengl", sf::Style::Default);
        sf::RectangleShape rect(sf::Vector2f(50, 50));
        rect.setFillColor(sf::Color::Magenta);

        GLuint VBO;
        GLuint VAO;
        Shader* shader = NULL;
        shader = new Shader("shaders/triangle.vs", "shaders/triangle.fs");
        GLfloat vertices[9] = {
                -0.5f, -0.5, 0.f,
                0.5f, -0.5f, 0.f,
                0.f, 0.5f, 0.f };

        glewInit();
        glewExperimental = GL_TRUE;
        bool running = true;


        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);
        glBindVertexArray(VAO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        // Position attribute
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
        glEnableVertexAttribArray(0);

        // Colour attribute
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
        glEnableVertexAttribArray(1);

        glBindVertexArray(0);

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

                window.pushGLStates();

                glClearColor(0.2f, 0.3f, 0.3f, 0.1f);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                // Drawing the triangle
                shader->use();
                glBindVertexArray(VAO);
                glDrawArrays(GL_TRIANGLES, 0, 3);
                glBindVertexArray(0);
                glDisableVertexAttribArray(0);

                window.popGLStates();
                window.resetGLStates();
                window.draw(rect);
                window.display();
        }

        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);
        delete shader;
        return 0;
}
 

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Using OpenGL 4.5 and the SFML graphics module
« Reply #1 on: September 10, 2016, 11:32:00 pm »
SFML is not aware of non-legacy OpenGL state. Try to comment out your own OpenGL lines one by one until sfml-graphics rendering functions properly. This way you can find out which line is the source of the problems.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Xemerau

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Using OpenGL 4.5 and the SFML graphics module
« Reply #2 on: September 11, 2016, 12:00:06 am »
The sf::RectangleShape seems to be rendering properly until the VAO and VBO are bound, only calling
glGenVertexArrays
and
glGenBuffers
results in it being rendered correctly.

Is there any way to bind the buffer and vertex array while still having SFML graphics rendering correctly?
« Last Edit: September 11, 2016, 12:35:27 am by Xemerau »