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

Author Topic: Having a problem with glGenBuffers(...)  (Read 10634 times)

0 Members and 1 Guest are viewing this topic.

jeoffrey0522

  • Newbie
  • *
  • Posts: 2
    • View Profile
Having a problem with glGenBuffers(...)
« on: December 20, 2014, 10:07:20 am »
Hello. I was just trying to do some kind of GPGPU Computing with OpenGL and OpenCL(you don't have to know about OpenCL),
and I created a VBO(vertex buffer object) before creating a window.

GLuint vbo;
glGenBuffers(1,&vbo);
 

But there was a segmentation fault and the program throwed an error.
So, I tried to create a VBO after creating window, but it doesnt' work neither. It worked only in the 'main loop', and I want to initialize everything before creating the window. What's the problem and how can I solve this?

 
int main()
{
        sf::Context context;//I think this code doesn't related with my problem.
        Fluid fluid;

        cout<<"main() - Window initalized"<<endl;
        sf::RenderWindow window(sf::VideoMode(1080, 720), "SFML works!",sf::Style::Titlebar|sf::Style::Close);

        //initialize something

    // The 'main loop'
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        //draw and update something

    }

    //exit

}


*i'm not a native english speaker, so I'm sorry for my bad English.
« Last Edit: December 20, 2014, 10:25:10 am by jeoffrey0522 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Having a problem with glGenBuffers(...)
« Reply #1 on: December 20, 2014, 10:28:15 am »
Run a debugger, find the origin of the segfault and provide the call stack.

Also provide your full build command.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jeoffrey0522

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: AW: Having a problem with glGenBuffers(...)
« Reply #2 on: December 20, 2014, 10:51:31 am »
Run a debugger, find the origin of the segfault and provide the call stack.

Also provide your full build command.
This is a runtime error(not compile-time), and this problem is related with OpenGL context creation in SFML.
Also, the segment fault was appeared at glGenBuffers().

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Having a problem with glGenBuffers(...)
« Reply #3 on: December 20, 2014, 11:13:58 am »
Debuggers are for run-time .. debugging and the build command can affect how your program runs (Certain optimizations and what-not).

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Having a problem with glGenBuffers(...)
« Reply #4 on: December 20, 2014, 11:29:13 am »
I created a VBO(vertex buffer object) before creating a window.
Why? This is not supposed to work... Are you familiar with OpenGL at all? Have you followed OpenGL tutorials in the past? Because one of the first things you would have learnt is that you do not create any OpenGL resources before you have a window (active OpenGL context). If you are unfamiliar with OpenGL, I suggest to just go learn from a tutorial rather than resort to trial and error. It will save you a lot of time.

GLuint vbo;
glGenBuffers(1,&vbo);
This code segment is completely useless to us. The fact that this has to be done all the time doesn't help us to see what is specific to your code.

But there was a segmentation fault and the program throwed an error.
This is known as a crash, and yes, as eXpl0it3r said, the offending line of code can be found with the help of a debugger. I suggest learning how to use your debugger at the same time as you learn OpenGL.

So, I tried to create a VBO after creating window, but it doesnt' work neither. It worked only in the 'main loop', and I want to initialize everything before creating the window.
This sounds like a really broken program structure to me. You are probably not describing your code to us the way you wrote it. Instead of trying to paraphrase the code you have written, why don't you simply paste it here for us to see? I am pretty sure the cause of the crash will be found relatively quickly.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Having the same problem
« Reply #5 on: January 04, 2015, 07:14:07 pm »
I have the same problem trying to use OpenGL - for learning. My program:

#include <SFML/Window.hpp>
//#include <SFML/OpenGL.hpp>
#include <GL/glew.h>

#include <stdint.h>

GLuint vbo[2]; // I want to generate two VBOs

void drawProc()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearColor(0.f, 0.5f, 1.f, 1.f); // light blue
}


int main(int argc, char **argv)
{
  sf::ContextSettings settings;
  settings.depthBits = 24;
  settings.stencilBits = 8;
  settings.antialiasingLevel = 4;
  settings.majorVersion = 3;
  settings.minorVersion = 0;
 
  sf::Window window(sf::VideoMode(800, 600), "OpenGL Test", sf::Style::Default, settings);
  window.setVerticalSyncEnabled(true);
 
 
  float triangle[9] =
  {
    -0.4f, 0.1f, 0.0f,
    0.4f, 0.1f, 0.0f,
    0.0f, 0.7f, 0.0f
  };
 
  float quad[12] =
  {
    -0.2f, -0.1f, 0.0f,
    -0.2f, -0.6f, 0.0f,
    0.2f, -0.1f, 0.0f,
    0.2f, -0.6f, 0.0f
  };
 
  glGenBuffers(2, vbo); // gdb says, that this program crashes on this line
 
  /*glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 9, triangle, GL_STATIC_DRAW);
 
  glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 12, quad, GL_STATIC_DRAW);*/

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

I already got the code working with the drawProc and not generating any VBO, my screen was cleared blue, as wished. But as soon, as I try to generate a buffer (only works, when I include GL/glew.h, it is not available with SFML/OpenGL.hpp...), my program crashes.

I am using linux (64-bit) and compiling the program above with the following command:
Code: [Select]
g++ main.cpp -o opengl-test -g $(pkg-config --cflags --libs sfml-window gl glew)

EDIT: glewInit() right after the window creation solves the problem in my case!

sf::Window window(sf::VideoMode(800, 600), "OpenGL Test", sf::Style::Default, settings);
glewInit();
« Last Edit: January 04, 2015, 07:16:17 pm by Tenry »
Please note that my previous display name was "Shy Guy".

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Having a problem with glGenBuffers(...)
« Reply #6 on: January 05, 2015, 11:19:07 am »
Tenry, you should be using glGenBuffers(2, &vbo).

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Re: Having a problem with glGenBuffers(...)
« Reply #7 on: January 05, 2015, 07:38:09 pm »
Tenry, you should be using glGenBuffers(2, &vbo).
Nope. The second parameter expects a pointer to the first GLuint of an array. If I have declared a real array (using GLuint vbo[n], n being a number 1 or greater), vbo equals a pointer to vbo[0]: vbo == &(vbo[0]), that's why I don't need the ampersand.
You're are right if you do like in most tutorials, declared GLuint vbo. Here, you don't have any array and so vbo is a plain integer. By using &vbo you retreive a pointer to your only element.
Please note that my previous display name was "Shy Guy".