-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
94 lines (80 loc) · 2.69 KB
/
shader.cpp
File metadata and controls
94 lines (80 loc) · 2.69 KB
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
85
86
87
88
89
90
91
92
93
94
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
std::string read_file(const char *path) {
std::ifstream ifs(path);
std::string str((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
return str;
}
void check_error(const GLuint id, GLenum type) {
void (*query)(GLuint, GLenum, GLint *);
void (*get_info)(GLuint, GLsizei, GLsizei *, GLchar *);
std::string msg;
switch (type) {
case GL_COMPILE_STATUS:
query = glGetShaderiv;
get_info = glGetShaderInfoLog;
msg = "shader compilation failed: ";
break;
case GL_LINK_STATUS:
query = glGetProgramiv;
get_info = glGetProgramInfoLog;
msg = "program linkage failed: ";
break;
}
GLint success{0};
query(id, type, &success);
if (success == GL_FALSE) {
GLint log_size{0};
query(id, GL_INFO_LOG_LENGTH, &log_size);
std::vector<GLchar> log(log_size);
get_info(id, log_size, &log_size, log.data());
std::string err = std::string(log.begin(), log.end());
std::cerr << msg << err << std::endl;
}
}
GLuint load_shader(GLenum type, const char *path) {
const std::string src{read_file(path)};
const char *csrc{src.c_str()};
const GLuint id{glCreateShader(type)};
glShaderSource(id, 1, &csrc, NULL);
glCompileShader(id);
check_error(id, GL_COMPILE_STATUS);
return id;
}
Shader::Shader(const char *path_vert, const char *path_frag) {
id = glCreateProgram();
const GLuint vsh = load_shader(GL_VERTEX_SHADER, path_vert);
const GLuint fsh = load_shader(GL_FRAGMENT_SHADER, path_frag);
glAttachShader(id, vsh);
glAttachShader(id, fsh);
glBindFragDataLocation(id, 0, "outColor");
glLinkProgram(id);
check_error(id, GL_LINK_STATUS);
glDetachShader(id, vsh);
glDetachShader(id, fsh);
glDeleteShader(vsh);
glDeleteShader(fsh);
}
void Shader::use() const { glUseProgram(id); }
Shader::~Shader() { glDeleteProgram(id); }
void Shader::set_int(const std::string &name, GLint value) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), value);
}
void Shader::set_float(const std::string &name, GLfloat value) const {
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
}
void Shader::set_mat4fv(const std::string &name, const glm::mat4 &mat) const {
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE,
glm::value_ptr(mat));
}
void Shader::attr(const std::string &name, GLint size, GLenum type,
GLsizei stride, const void *pointer) const {
GLint index = glGetAttribLocation(id, name.c_str());
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, type, GL_FALSE, stride, pointer);
}