我正在尝试使用sfml2设置OpenGL上下文。一切正常,我能够绘制一个三角形。在下一步中,我想在GL东西上用SFML绘制一些2D元素。因此,我首先将所有“窗口”条目更改为“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);
}

最佳答案

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

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

要摆脱此错误,您必须禁用所有不与glDisableClientState()一起使用的数组。然后,一切应该正常。

10-07 12:20