Beispiel #1
0
void initialize() {
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint vbo;
    glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    std::string vertexShaderSource;
    if (!contentsOfFile("vertex.glsl", vertexShaderSource)) {
        exit(1);
    }

    std::string fragmentShaderSource;
    if (!contentsOfFile("fragment.glsl", fragmentShaderSource)) {
        exit(1);
    }

    Shader vertexShader(GL_VERTEX_SHADER);
    if (!vertexShader.compile(vertexShaderSource)) {
        debugPrint("Failed to compile vertex shader: %s", vertexShader.getCompileStatus().c_str());
        exit(1);
    }

    Shader fragmentShader(GL_FRAGMENT_SHADER);
    if (!fragmentShader.compile(fragmentShaderSource)) {
        debugPrint("Failed to compile fragment shader: %s", fragmentShader.getCompileStatus().c_str());
        exit(1);
    }

    Program shaderProgram(vertexShader, fragmentShader);
    if (!shaderProgram.link()) {
        debugPrint("Failed to link shader program: %s", shaderProgram.getLinkStatus().c_str());
        exit(1);
    }

    GLuint programID = shaderProgram.getGLID();
    glUseProgram(programID);

    GLint posAttrib = glGetAttribLocation(programID, "position");
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);

    GLint colorAttrib = glGetAttribLocation(programID, "color");
    glEnableVertexAttribArray(colorAttrib);
    glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));

    glEnableVertexAttribArray(posAttrib);

    ball.setLeftPlayer(&leftPlayer);
    ball.setRightPlayer(&rightPlayer);
}
Beispiel #2
0
GLuint Shaders::loadShadersFromFile(const std::string& vertexFilePath, const std::string& fragmentFilePath)
{
    try {
        return loadShaderFromString(contentsOfFile(vertexFilePath),
                                    contentsOfFile(fragmentFilePath));
    } catch (std::exception& e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}