Beispiel #1
0
GLint ShaderProgram::loadShader(const std::string &shader_file, GLint type) {
    // Create shaders
    auto shader_id = glCreateShader(type);
    auto result = GL_FALSE;
    auto info_length = 0;

    // Load shader code
    std::ifstream shader_stream(shader_file);
    std::string shader_code((std::istreambuf_iterator<char>(shader_stream)), std::istreambuf_iterator<char>());

    // Compile shader
    std::cout << "Compiling Shader ..." << shader_file << std::endl;
    auto shader_code_ptr = shader_code.c_str();
    glShaderSource(shader_id, 1, &shader_code_ptr, NULL);
    glCompileShader(shader_id);

    // Check vertex shader log
    glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE) {
        glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &info_length);
        std::string vertex_shader_log((unsigned int)info_length, ' ');
        glGetShaderInfoLog(shader_id, info_length, NULL, &vertex_shader_log[0]);
        std::cout << vertex_shader_log << std::endl;
    }

    return shader_id;
}
Beispiel #2
0
Shader::Shader(const std::string &vertex_shader_code, const std::string &fragment_shader_code) {
  // Create shaders
  auto vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
  auto fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);
  auto result = GL_FALSE;
  auto info_length = 0;

  // Compile vertex shader
  auto vertex_shader_code_ptr = vertex_shader_code.c_str();
  glShaderSource(vertex_shader_id, 1, &vertex_shader_code_ptr, NULL);
  glCompileShader(vertex_shader_id);

  // Check vertex shader log
  glGetShaderiv(vertex_shader_id, GL_COMPILE_STATUS, &result);
  if (result == GL_FALSE) {
    glGetShaderiv(vertex_shader_id, GL_INFO_LOG_LENGTH, &info_length);
    std::string vertex_shader_log((unsigned int) info_length, ' ');
    glGetShaderInfoLog(vertex_shader_id, info_length, NULL,
                       &vertex_shader_log[0]);
    std::cout << "Error Compiling Vertex Shader ..." << std::endl;
    std::cout << vertex_shader_log << std::endl;
  }

  // Compile fragment shader
  auto fragment_shader_code_ptr = fragment_shader_code.c_str();
  glShaderSource(fragment_shader_id, 1, &fragment_shader_code_ptr, NULL);
  glCompileShader(fragment_shader_id);

  // Check fragment shader log
  glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS, &result);
  if (result == GL_FALSE) {
    glGetShaderiv(fragment_shader_id, GL_INFO_LOG_LENGTH, &info_length);
    std::string fragment_shader_log((unsigned long) info_length, ' ');
    glGetShaderInfoLog(fragment_shader_id, info_length, NULL,
                       &fragment_shader_log[0]);
    std::cout << "Error Compiling Fragment Shader ..." << std::endl;
    std::cout << fragment_shader_log << std::endl;
  }

  // Create and link the program
  auto program_id = glCreateProgram();
  glAttachShader(program_id, vertex_shader_id);
  glAttachShader(program_id, fragment_shader_id);
  glBindFragDataLocation(program_id, 0, "FragmentColor");
  glLinkProgram(program_id);

  // Check program log
  glGetProgramiv(program_id, GL_LINK_STATUS, &result);
  if (result == GL_FALSE) {
    glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &info_length);
    std::string program_log((unsigned long) info_length, ' ');
    glGetProgramInfoLog(program_id, info_length, NULL, &program_log[0]);
    std::cout << "Error Linking Shader Program ..." << std::endl;
    std::cout << program_log << std::endl;
  }
  glDeleteShader(vertex_shader_id);
  glDeleteShader(fragment_shader_id);

  program = program_id;
}