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

Author Topic: Integrating SFML with Modern OpenGL  (Read 5947 times)

0 Members and 6 Guests are viewing this topic.

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Integrating SFML with Modern OpenGL
« on: July 27, 2016, 07:39:07 am »
Hey all,

I'm coding on a Macbook, trying to run the following program:

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

int main() {
    sf::RenderWindow app(sf::VideoMode(640, 480), "OpenGL");

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

        static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
        glClearBufferfv(GL_COLOR, 0, red);
        app.display();
    }

    return 0;
}
 

However, I get the following error:

sb2-1.cpp:25:9: error: use of undeclared identifier 'glClearBufferfv'
        glClearBufferfv(GL_COLOR, 0, red);
        ^
1 error generated.
 

Using glClear() instead of glClearBufferfv() works perfectly well.

Is there something I need to do to make SFML play nice with newer OpenGL besides what's listed in the docs? Thanks!
Whether you think you can or you can't, you're right.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Integrating SFML with Modern OpenGL
« Reply #1 on: July 27, 2016, 09:22:15 am »
Have you checked your OpenGL header files whether they actually include that function?

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Re: Integrating SFML with Modern OpenGL
« Reply #2 on: July 27, 2016, 04:19:37 pm »
I do have the function defined in two header files located in the OpenGL framework headers:

Code: [Select]
CGLMacro.h:#define glClearBufferfv(buffer, drawbuffer, value) \
gl3.h:GLAPI void APIENTRY glClearBufferfv (GLenum buffer, GLint drawbuffer, const GLfloat *value);
Whether you think you can or you can't, you're right.

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Re: Integrating SFML with Modern OpenGL
« Reply #3 on: July 28, 2016, 05:05:07 am »
Okay, I got it compiling and working fine on Windows, which I realized I needed GLEW for. The changes I made were adding the following two lines to the very top of the C++ file, after I had installed GLEW:

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

This stuff's probably old hat to some of you, but it was tough for me to glean the information in a straightforward way from forum posts.

Now, the unfortunate thing is I did exact same on Mac, the only difference being the command string I used, which was the following:

Code: [Select]
g++ test.cpp -I/usr/local/include -F/Library/Frameworks -L/usr/local/lib -L/opt/X11/lib -lglew -framework sfml-window -framework sfml-graphics -framework sfml-system -framework opengl -o test

and installing GLEW via Homebrew, whereas on Windows I'm compiling with the static version of the library (glew32s.lib) supplied from the website and not needing framework flags, just the regular -l flags.

It compiles! ... and then it seg faults right after a white window shows up (no red from the glClearBufferfv call). :[ Anyone know why it would do so?
Whether you think you can or you can't, you're right.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Integrating SFML with Modern OpenGL
« Reply #4 on: July 28, 2016, 12:09:53 pm »
Do you init GLEW ?

This is what you need to do to use GLEW (with modern OpenGL) :
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
    std::cout << "Failed to initialize GLEW" << std::endl;
    return -1;
}

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Re: Integrating SFML with Modern OpenGL
« Reply #5 on: July 28, 2016, 03:10:31 pm »
Ah whoops yes, you're totally right; forgot that important detail, which I did remember to do on Windows before; thanks! However, the window is white instead of red; know why?
Whether you think you can or you can't, you're right.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Integrating SFML with Modern OpenGL
« Reply #6 on: July 28, 2016, 07:38:30 pm »
Can you show your code ?

Clearing is done like this :
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //Replace the values with the color :)
glClear(GL_COLOR_BUFFER_BIT);

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Re: Integrating SFML with Modern OpenGL
« Reply #7 on: July 28, 2016, 07:43:41 pm »
#define GLEW_STATIC
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>

int main() {
    sf::RenderWindow app(sf::VideoMode(640, 480), "OpenGL");
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        std::cout << "Failed to initialize GLEW!";
        return -1;
    }

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

        static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
        glClearBufferfv(GL_COLOR, 0, red);
        app.display();
    }

    return 0;
}
 

The code is basically the same as before, but with your proposed changes and the GLEW_STATIC stuff. This example does work perfectly well on my Windows machine; the glClearBufferfv is a newer function than glClear and glClearColor. Those two I'm already familiar with, and they work on my Mac without the GLEW stuff.
Whether you think you can or you can't, you're right.