“无法创建上下文。” OpenGL和SFML

时间:2015-02-14 10:16:05

标签: c++ opengl glsl sfml

所以我试图学习SFML和OpenGL,但我遇到了这个错误:

"错误。无法创建上下文。没有共享上下文的重试。 警告。在没有共享上下文的情况下创建新上下文。"

继承我的源代码:

MainComponenent.cpp

#include "MainComponent.h"
#include "Error.h"

MainComponent::MainComponent() {
    _screenWidth = 1200;
    _screenHeight = 800;

    _gameState = GameState::EXIT;
}


void MainComponent::run() {
    initialize();

    _sprite.init(-1.0f, -1.0f, 1.0f, 1.0f);

    gameLoop();
}

void MainComponent::initialize() {
    sf::ContextSettings settings;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 4;
    settings.majorVersion = 4;
    settings.minorVersion = 0;

    _window.create(sf::VideoMode(_screenWidth, _screenHeight), "SFML Window", sf::Style::Titlebar | sf::Style::Close, settings);
    if(_window.isOpen()) {
        _gameState = GameState::PLAY;
    } else {
        fatalError("The SFML Window Failed to be Created.");
    }

    //set background color
    glClearColor(0.0f, 0.15f, 0.30f, 1.0f);

    //initialize shaders
    initShaders();
}

void MainComponent::initShaders() {
    _colorProgram.compileShaders("colorShading.vert", "colorShading.frag");
    _colorProgram.addAttribute("vertexPosition");
    _colorProgram.linkShaders();
}

void MainComponent::gameLoop() {
    while(_gameState != GameState::EXIT) {
        processInput();
        drawGame();
    }
    _window.close();
}

void MainComponent::processInput() {
    //handle events
    sf::Event event;
    while(_window.pollEvent(event)) {
        if(event.type == sf::Event::Closed)
            _gameState = GameState::EXIT;
    }
}

void MainComponent::drawGame() {
    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    _colorProgram.use();

    _sprite.draw();

    _colorProgram.unuse();

    _window.display();

}

GLSLProgram.cpp

#include "GLSLProgram.h"
#include "Error.h"

#include <fstream>
#include <vector>

GLSLProgram::GLSLProgram() : _programID(0), _vertexShaderID(0),  _fragmentShaderID(0), _numAttributes(0) {

}

GLSLProgram::~GLSLProgram() {

}

void GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath) {
    _programID = glCreateProgram();
    _vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    if(_vertexShaderID == 0) {
        fatalError("The Vertex Shader Failed to be Created.");
    }
    _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    if(_fragmentShaderID == 0) {
        fatalError("The Fragment Shader Failed to be Created.");
    }

    compileShader(vertexShaderFilePath, _vertexShaderID);
    compileShader(fragmentShaderFilePath, _fragmentShaderID);
}

void GLSLProgram::linkShaders() {
    glAttachShader(_programID, _vertexShaderID);
    glAttachShader(_programID, _fragmentShaderID);

    glLinkProgram(_programID);

    GLuint isLinked = 0;
    glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
    if(isLinked == GL_FALSE) {
        glDeleteProgram(_programID);
        glDeleteShader(_vertexShaderID);
        glDeleteShader(_fragmentShaderID);

        fatalError("Shaders failed to link.");
    }

    glDetachShader(_programID, _vertexShaderID);
    glDetachShader(_programID, _fragmentShaderID);
    glDeleteShader(_vertexShaderID);
    glDeleteShader(_fragmentShaderID);
}

void GLSLProgram::addAttribute(const std::string& attributeName) {
    glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
}

void GLSLProgram::use() {
    glUseProgram(_programID);
    for(int i = 0; i < _numAttributes; i++) {
        glEnableVertexAttribArray(i);
    }
}

void GLSLProgram::unuse() {
    glUseProgram(0);
    for(int i = 0; i < _numAttributes; i++) {
        glDisableVertexAttribArray(i);
    }
}

void GLSLProgram::compileShader(const std::string& filePath, GLuint id) {

    std::ifstream shaderFile(filePath);
    if (shaderFile.fail()) {
        perror(filePath.c_str());
        fatalError("Failed to open " + filePath);
    }

    std::string fileContents = "";
    std::string line;

    while (std::getline(shaderFile, line)) {
        fileContents += line + "\n";
    }

    shaderFile.close();

    const char* contentsPtr = fileContents.c_str();

    glShaderSource(id, 1, &contentsPtr, nullptr);

    glCompileShader(id);

    GLint success = 0;
    glGetShaderiv(id, GL_COMPILE_STATUS, &success);

    if (success == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);

        std::vector<char> errorLog(maxLength);
        glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);

        glDeleteShader(id);

        std::printf("%s\n", &(errorLog[0]));
        fatalError("Shader " + filePath + " failed to compile");
    }
}

感谢您的帮助。

0 个答案:

没有答案