Exemple #1
0
Program createProgram(const std::vector<GLuint> &shaders) {
    Program program(glCreateProgram());
    if (!program) {
        throw OpenGLException("Program creation failed", glGetError());
    }
    
    for (GLuint shader : shaders) {
        glAttachShader(program.get(), shader);
        GLenum error = glGetError();
        if (error != GL_NO_ERROR) {
            throw OpenGLException("Shader attachment failed", error);
        }
    }
    
    glLinkProgram(program.get());
    
    GLint infoLogLength;
    glGetProgramiv(program.get(), GL_INFO_LOG_LENGTH, &infoLogLength);
    if (infoLogLength > 0) {
        std::vector<GLchar> infoLog(infoLogLength);
        glGetProgramInfoLog(program.get(), infoLogLength, nullptr, infoLog.data());
        mwarning(M_DISPLAY_MESSAGE_DOMAIN,
                 "OpenGL: Program linking produced the following information log:\n%s",
                 infoLog.data());
    }
    
    GLint linkStatus;
    glGetProgramiv(program.get(), GL_LINK_STATUS, &linkStatus);
    if (linkStatus != GL_TRUE) {
        throw OpenGLException("Program linking failed");
    }
    
    return program;
}
Exemple #2
0
Shader createShader(GLenum shaderType, const std::string &glslVersion, const std::string &shaderSource) {
    Shader shader(glCreateShader(shaderType));
    if (!shader) {
        throw OpenGLException("Shader creation failed", glGetError());
    }
    
    const std::array<const GLchar *, 5> strings = {
        "#version ", glslVersion.data(), "\n",
        "precision highp float;\n",
        shaderSource.data()
    };
    glShaderSource(shader.get(), strings.size(), strings.data(), nullptr);
    glCompileShader(shader.get());
    
    GLint infoLogLength;
    glGetShaderiv(shader.get(), GL_INFO_LOG_LENGTH, &infoLogLength);
    if (infoLogLength > 0) {
        std::vector<GLchar> infoLog(infoLogLength);
        glGetShaderInfoLog(shader.get(), infoLogLength, nullptr, infoLog.data());
        mwarning(M_DISPLAY_MESSAGE_DOMAIN,
                 "OpenGL: Shader compilation produced the following information log:\n%s",
                 infoLog.data());
    }
    
    GLint compileStatus;
    glGetShaderiv(shader.get(), GL_COMPILE_STATUS, &compileStatus);
    if (compileStatus != GL_TRUE) {
        throw OpenGLException("Shader compilation failed");
    }
    
    return shader;
}
void OpenGL::CheckError()
{
#ifdef DEBUG
    int error = glGetError();

    if (error != GL_NO_ERROR)
    {
        throw OpenGLException(error);
    }
#endif
}
TextureUnit::TextureUnit() : unitEnum_(0), unitNumber_(0) {
    ivwAssert(!textureUnits_.empty(), "Texture unit handler not initialized.");

    // check which texture unit is available
    for (size_t i = 1; i < textureUnits_.size(); i++) {
        if (textureUnits_[i] == false) {
            // unit previously unused, mark as used now
            textureUnits_[i] = true;
            unitNumber_ = (GLint)i;
            unitEnum_ = GL_TEXTURE0 + unitNumber_;
            return;
        }
    }

    throw OpenGLException("Exceeding number of available texture units.", IvwContext);
}