-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
46 lines (37 loc) · 1.09 KB
/
shader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include "shader.hpp"
Shader::Shader(int shader_type, std::string shader_name_arg) : type(shader_type), name(shader_name_arg){}
void Shader::compile()
{
id = glCreateShader(type);
std::string string_source = source.str();
const char *cc_source = string_source.c_str();
glShaderSource(id, 1, &cc_source, NULL);
glCompileShader(id);
int success;
char infoLog[512];
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(id, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::" << name << "::COMPILATION_FAILED\n" << infoLog << std::endl;
std::exit(1);
}
}
void Shader::read_file(std::string filename)
{
std::ifstream shader_file(filename);
if (shader_file.good()) {
source << shader_file.rdbuf();
} else {
std::cerr << shader_file.rdstate() << std::endl;
std::cerr << "failed to open file for shader " << name << std::endl;
std::exit(-2);
}
}
void Shader::set_version(std::string version){
source << "#version " << version << std::endl;
}