Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string readFromFile(const std::string &filename) {
- std::string res;
- std::string line;
- std::ifstream file(filename);
- if (file.is_open()) {
- while (std::getline(file, line)) {
- res += line + "\n";
- }
- }
- else {
- throw std::logic_error("Can't open file: " + filename);
- }
- return res;
- }
- Shader::Shader(const std::string &vertexPath, const std::string &fragPath) {
- unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
- std::string vShader = readFromFile(vertexPath);
- GLchar const* vSh[] = { vShader.c_str() };
- glShaderSource(vertexShader, 1, vSh, NULL);
- glCompileShader(vertexShader);
- checkShaderCompilation(vertexShader);
- unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- std::string fShader = readFromFile(fragPath);
- GLchar const* fSh[] = { fShader.c_str() };
- glShaderSource(fragmentShader, 1, fSh, NULL);
- glCompileShader(fragmentShader);
- checkShaderCompilation(fragmentShader);
- programID = glCreateProgram();
- glAttachShader(programID, vertexShader);
- glAttachShader(programID, fragmentShader);
- glLinkProgram(programID);
- checkProgramLinking();
- glDeleteShader(fragmentShader);
- glDeleteShader(vertexShader);
- }
- void Shader::Use() {
- glUseProgram(programID);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement