3

Well, I'm trying to get out of the glBegin/glEnd thing, so I'm trying to get VBO working. I'm actually getting a segfault at the glDrawArrays call and I don't know what to do.

If you need any details, please ask.

This is my code:

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

int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML and VBO", sf::Style::Default);
window.setVerticalSyncEnabled(true);
window.setActive(true);
window.resetGLStates();

GLfloat m_vData[12];
GLuint m_uiVBO;

m_vData[0] = -0.2f; m_vData[1] = -0.1f; m_vData[2] = 0.0f;
m_vData[3] = -0.2f; m_vData[4] = -0.6f; m_vData[5] = 0.0f;
m_vData[6] = 0.2f; m_vData[7] = -0.1f; m_vData[8] = 0.0f;
m_vData[9] = 0.2f; m_vData[10] = -0.6f; m_vData[11] = 0.0f;

glGenBuffers(1, &m_uiVBO);

glBindBuffer(GL_ARRAY_BUFFER, m_uiVBO);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), m_vData, GL_STATIC_DRAW);

float rotation = 0;

bool running = true;
while(running)
{
    sf::Event event;
    while(window.pollEvent(event))
    {
        switch(event.type)
        {
        case sf::Event::KeyPressed:
            if(event.key.code != sf::Keyboard::Key::Return) { break; }
        case sf::Event::Closed:
            running = false;
            break;
        default:
            break;
        }
    }

    glClearColor(0.3f, 0.3f, 0.3f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, m_uiVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Segfault

    window.display();
}
return 0;
}
3
  • 1
    glDisableVertexAttribArray(0); seems to be missing. It is possible this is causing your problem. Also here is a nice tutorial on what you trying to do i highly suggest you take a look at: opengl-tutorial.org/beginners-tutorials/…
    – apoiat
    Jul 26, 2013 at 13:08
  • Also make sure you are placing this after your gldrawarrays call
    – apoiat
    Jul 26, 2013 at 13:16
  • Thanks a lot, this link helped me.
    – Iago Caran
    Jul 26, 2013 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.