]> git.localhorst.tv Git - blank.git/blob - src/shader.cpp
move controller from camera to world
[blank.git] / src / shader.cpp
1 #include "shader.hpp"
2
3 #include "init.hpp"
4
5 #include <algorithm>
6 #include <iostream>
7 #include <memory>
8 #include <ostream>
9 #include <stdexcept>
10 #include <string>
11
12
13 namespace {
14
15 void gl_error(std::string msg) {
16         const GLubyte *errBegin = gluErrorString(glGetError());
17         if (errBegin && *errBegin != '\0') {
18                 const GLubyte *errEnd = errBegin;
19                 while (*errEnd != '\0') {
20                         ++errEnd;
21                 }
22                 msg += ": ";
23                 msg.append(errBegin, errEnd);
24         }
25         throw std::runtime_error(msg);
26 }
27
28 }
29
30 namespace blank {
31
32 Shader::Shader(GLenum type)
33 : handle(glCreateShader(type)) {
34         if (handle == 0) {
35                 gl_error("glCreateShader");
36         }
37 }
38
39 Shader::~Shader() {
40         if (handle != 0) {
41                 glDeleteShader(handle);
42         }
43 }
44
45 Shader::Shader(Shader &&other)
46 : handle(other.handle) {
47         other.handle = 0;
48 }
49
50 Shader &Shader::operator =(Shader &&other) {
51         std::swap(handle, other.handle);
52         return *this;
53 }
54
55
56 void Shader::Source(const GLchar *src) {
57         const GLchar* src_arr[] = { src };
58         glShaderSource(handle, 1, src_arr, nullptr);
59 }
60
61 void Shader::Compile() {
62         glCompileShader(handle);
63 }
64
65 bool Shader::Compiled() const {
66         GLint compiled = GL_FALSE;
67         glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
68         return compiled == GL_TRUE;
69 }
70
71 void Shader::Log(std::ostream &out) const {
72         int log_len = 0, max_len = 0;
73         glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
74         std::unique_ptr<char[]> log(new char[max_len]);
75         glGetShaderInfoLog(handle, max_len, &log_len, log.get());
76         out.write(log.get(), log_len);
77 }
78
79
80 void Shader::AttachToProgram(GLuint id) const {
81         glAttachShader(id, handle);
82 }
83
84
85 Program::Program()
86 : handle(glCreateProgram()) {
87         if (handle == 0) {
88                 gl_error("glCreateProgram");
89         }
90 }
91
92 Program::~Program() {
93         if (handle != 0) {
94                 glDeleteProgram(handle);
95         }
96 }
97
98
99 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
100         shaders.emplace_back(type);
101         Shader &shader = shaders.back();
102         shader.Source(src);
103         shader.Compile();
104         if (!shader.Compiled()) {
105                 shader.Log(std::cerr);
106                 throw std::runtime_error("compile shader");
107         }
108         Attach(shader);
109         return shader;
110 }
111
112 void Program::Attach(Shader &shader) {
113         shader.AttachToProgram(handle);
114 }
115
116 void Program::Link() {
117         glLinkProgram(handle);
118 }
119
120 bool Program::Linked() const {
121         GLint linked = GL_FALSE;
122         glGetProgramiv(handle, GL_LINK_STATUS, &linked);
123         return linked == GL_TRUE;
124 }
125
126 void Program::Log(std::ostream &out) const {
127         int log_len = 0, max_len = 0;
128         glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
129         std::unique_ptr<char[]> log(new char[max_len]);
130         glGetProgramInfoLog(handle, max_len, &log_len, log.get());
131         out.write(log.get(), log_len);
132 }
133
134
135 GLint Program::UniformLocation(const GLchar *name) const {
136         return glGetUniformLocation(handle, name);
137 }
138
139
140 DirectionalLighting::DirectionalLighting()
141 : program()
142 , light_direction(1.0f, 3.0f, 2.0f)
143 , light_color(0.9f, 0.9f, 0.9f)
144 , vp(1.0f)
145 , m_handle(0)
146 , mvp_handle(0)
147 , light_direction_handle(0)
148 , light_color_handle(0) {
149         program.LoadShader(
150                 GL_VERTEX_SHADER,
151                 "#version 330 core\n"
152                 "layout(location = 0) in vec3 vtx_position;\n"
153                 "layout(location = 1) in vec3 vtx_color;\n"
154                 "layout(location = 2) in vec3 vtx_normal;\n"
155                 "uniform mat4 M;\n"
156                 "uniform mat4 MVP;\n"
157                 "out vec3 frag_color;\n"
158                 "out vec3 normal;\n"
159                 "void main() {\n"
160                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
161                         "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
162                         "frag_color = vtx_color;\n"
163                 "}\n"
164         );
165         program.LoadShader(
166                 GL_FRAGMENT_SHADER,
167                 "#version 330 core\n"
168                 "in vec3 frag_color;\n"
169                 "in vec3 normal;\n"
170                 "uniform vec3 light_direction;\n"
171                 "uniform vec3 light_color;\n"
172                 "out vec3 color;\n"
173                 "void main() {\n"
174                         "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
175                         "vec3 n = normalize(normal);\n"
176                         "vec3 l = normalize(light_direction);\n"
177                         "float cos_theta = clamp(dot(n, l), 0, 1);\n"
178                         "color = ambient + frag_color * light_color * cos_theta;\n"
179                 "}\n"
180         );
181         program.Link();
182         if (!program.Linked()) {
183                 program.Log(std::cerr);
184                 throw std::runtime_error("link program");
185         }
186
187         m_handle = program.UniformLocation("M");
188         mvp_handle = program.UniformLocation("MVP");
189         light_direction_handle = program.UniformLocation("light_direction");
190         light_color_handle = program.UniformLocation("light_color");
191 }
192
193
194 void DirectionalLighting::Activate() {
195         GLContext::EnableDepthTest();
196         GLContext::EnableBackfaceCulling();
197         program.Use();
198
199         glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
200         glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z);
201 }
202
203 void DirectionalLighting::SetM(const glm::mat4 &m) {
204         glm::mat4 mvp(vp * m);
205         glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
206         glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
207 }
208
209 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) {
210         light_direction = -dir;
211         glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
212 }
213
214 void DirectionalLighting::SetProjection(const glm::mat4 &p) {
215         projection = p;
216         vp = p * view;
217 }
218
219 void DirectionalLighting::SetView(const glm::mat4 &v) {
220         view = v;
221         vp = projection * v;
222 }
223
224 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) {
225         projection = p;
226         view = v;
227         vp = p * v;
228 }
229
230 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) {
231         SetVP(v, p);
232         SetM(m);
233 }
234
235 }