-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.hpp
84 lines (71 loc) · 1.76 KB
/
shader.hpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <glad/glad.h>
class Shader {
public:
GLuint handle;
std::string source;
//char *source = nullptr;
GLint success;
size_t len = 0;
char info_log[1024];
Shader(const char* filename, GLenum type) {
std::ifstream ifs(filename, std::ios::ate);
if (!ifs.is_open()) {
throw std::runtime_error("failed to open file");
}
size_t len = ifs.tellg();
ifs.seekg(0, std::ios::beg);
//ifs.read(source, len);
std::stringstream ss;
ss << ifs.rdbuf();
source = ss.str();
ifs.close();
std::cerr << std::endl << filename << ", length: " << len << std::endl;
// std::cerr << source << std::endl;
const char* source_c = source.c_str();
init(source_c, type);
}
Shader(const char* source, GLenum type, int) {
init(source, type);
}
~Shader() {
if (success == GL_TRUE) {
glDeleteShader(handle);
}
}
private:
void init(const char* source, GLenum type) {
handle = glCreateShader(type);
glShaderSource(handle, 1, &source, NULL);
glCompileShader(handle);
glGetShaderiv(handle, GL_COMPILE_STATUS, &success);
if (success != GL_TRUE) {
glGetShaderInfoLog(handle, 1024, NULL, info_log);
std::cerr << "failed to compile shader: " << std::endl;
std::cerr << info_log << std::endl;
}
}
};
class ShaderProgram {
public:
GLuint handle;
GLint success;
ShaderProgram(const Shader& vert, const Shader& frag) {
handle = glCreateProgram();
glAttachShader(handle, vert.handle);
glAttachShader(handle, frag.handle);
glLinkProgram(handle);
glGetProgramiv(handle, GL_LINK_STATUS, &success);
if (!success) {
char info_log[1024];
glGetProgramInfoLog(handle, 1024, NULL, info_log);
std::cerr << info_log << std::endl;
}
}
void use() {
glUseProgram(handle);
}
};