sfml2 RenderWindow + OpenGL

时间:2012-02-10 08:57:34

标签: opengl crash sfml

我正在尝试使用sfml2设置OpenGL上下文。 Everthing工作正常,我能够绘制一个三角形。在下一步中,我想在GL的东西上绘制一些带有SFML的2D元素。所以我首先将所有“Window”条目更改为“RenderWindow”以便能够绘制一些东西。有arno错误,但是当我编译程序时,它总是在绘制之前崩溃,我不知道为什么。使用sfml1.6,它可以使用RenderWindow。是什么让它崩溃?

这是我的代码:

#include <iostream>
#include <glew.h>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>


using namespace std;
using namespace sf;

void handleEvents(RenderWindow*);
void fillVBO();
void drawGL();

bool running = true;
GLuint vertexBuffer;

static const GLfloat vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
};

int main() {
    VideoMode mode;
    mode.BitsPerPixel = 32;
    mode.Width = 1024;
    mode.Height = 768;
    ContextSettings cs;
    cs.AntialiasingLevel = 4;
    cs.DepthBits = 32;
    cs.StencilBits = 16;
    cs.MajorVersion = 3;
    cs.MinorVersion = 3;

    RenderWindow App(mode, "SFML window", Style::Close|Style::Resize, cs);

    cout << "Window OK" << endl;

    if(glewInit() != GLEW_OK) {
        cout << "Unable it initialize Glew!" << endl;
        return -1;
    }
    else {
        cout << "GLEW initialization OK" << endl;
    }
    glClearColor(0.0f, 0.0f, 0.3f, 0.0f);

    fillVBO();

    cout << "Fill VBO OK" << endl;

    while(running) {
        App.SetActive();
        handleEvents(&App);
        cout << "Handle Events OK" << endl;
        drawGL();
        cout << "Draw GL OK" << endl;

        App.Display();
    }
    return EXIT_SUCCESS;
}

void handleEvents(RenderWindow* wnd) {
    Event ev;
    while(wnd->PollEvent(ev)) {
        switch(ev.Type) {
        case Event::Closed:
            running = false;
            break;
        case Event::KeyPressed:
            switch(ev.Key.Code) {
            case Keyboard::Escape:
                running = false;
                break;
            default:
                break;
            }
            break;
        case Event::Resized:
            glViewport(0, 0, ev.Size.Width, ev.Size.Height);
            break;
        default:
            break;
        }
    }
}

void fillVBO() {
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
}

void drawGL() {
    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(
       0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
       3,                  // size
       GL_FLOAT,           // type
       GL_FALSE,           // normalized?
       0,                  // stride
       (void*)0            // array buffer offset
    );

    // Draw the triangle !
    glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

    glDisableVertexAttribArray(0);
}

1 个答案:

答案 0 :(得分:0)

我在sfml论坛上找到了帮助,并认为我应该在这里发布解决方案。

http://www.sfml-dev.org/forum/viewtopic.php?p=46348#46348

要消除此错误,您必须禁用不使用glDisableClientState()的所有数组。一切都应该工作正常。

相关问题