]> git.localhorst.tv Git - blobs.git/blob - src/graphics/shader.cpp
concerning orbits
[blobs.git] / src / graphics / shader.cpp
1 #include "PlanetSurface.hpp"
2 #include "Program.hpp"
3 #include "Shader.hpp"
4
5 #include "ArrayTexture.hpp"
6 #include "../app/init.hpp"
7
8 #include <algorithm>
9 #include <iostream>
10 #include <memory>
11 #include <ostream>
12 #include <stdexcept>
13 #include <string>
14 #include <glm/gtc/type_ptr.hpp>
15
16
17 namespace {
18
19 void gl_error(std::string msg) {
20         const GLubyte *errBegin = gluErrorString(glGetError());
21         if (errBegin && *errBegin != '\0') {
22                 const GLubyte *errEnd = errBegin;
23                 while (*errEnd != '\0') {
24                         ++errEnd;
25                 }
26                 msg += ": ";
27                 msg.append(errBegin, errEnd);
28         }
29         throw std::runtime_error(msg);
30 }
31
32 }
33
34 namespace blobs {
35 namespace graphics {
36
37 Shader::Shader(GLenum type)
38 : handle(glCreateShader(type)) {
39         if (handle == 0) {
40                 gl_error("glCreateShader");
41         }
42 }
43
44 Shader::~Shader() {
45         if (handle != 0) {
46                 glDeleteShader(handle);
47         }
48 }
49
50 Shader::Shader(Shader &&other) noexcept
51 : handle(other.handle) {
52         other.handle = 0;
53 }
54
55 Shader &Shader::operator =(Shader &&other) noexcept {
56         std::swap(handle, other.handle);
57         return *this;
58 }
59
60
61 void Shader::Source(const GLchar *src) noexcept {
62         const GLchar* src_arr[] = { src };
63         glShaderSource(handle, 1, src_arr, nullptr);
64 }
65
66 void Shader::Compile() noexcept {
67         glCompileShader(handle);
68 }
69
70 bool Shader::Compiled() const noexcept {
71         GLint compiled = GL_FALSE;
72         glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
73         return compiled == GL_TRUE;
74 }
75
76 void Shader::Log(std::ostream &out) const {
77         int log_len = 0, max_len = 0;
78         glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
79         std::unique_ptr<char[]> log(new char[max_len]);
80         glGetShaderInfoLog(handle, max_len, &log_len, log.get());
81         out.write(log.get(), log_len);
82 }
83
84
85 void Shader::AttachToProgram(GLuint id) const noexcept {
86         glAttachShader(id, handle);
87 }
88
89
90 Program::Program()
91 : handle(glCreateProgram()) {
92         if (handle == 0) {
93                 gl_error("glCreateProgram");
94         }
95 }
96
97 Program::~Program() {
98         if (handle != 0) {
99                 glDeleteProgram(handle);
100         }
101 }
102
103
104 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
105         shaders.emplace_back(type);
106         Shader &shader = shaders.back();
107         shader.Source(src);
108         shader.Compile();
109         if (!shader.Compiled()) {
110                 shader.Log(std::cerr);
111                 throw std::runtime_error("compile shader");
112         }
113         Attach(shader);
114         return shader;
115 }
116
117 void Program::Attach(Shader &shader) noexcept {
118         shader.AttachToProgram(handle);
119 }
120
121 void Program::Link() noexcept {
122         glLinkProgram(handle);
123 }
124
125 bool Program::Linked() const noexcept {
126         GLint linked = GL_FALSE;
127         glGetProgramiv(handle, GL_LINK_STATUS, &linked);
128         return linked == GL_TRUE;
129 }
130
131 void Program::Log(std::ostream &out) const {
132         int log_len = 0, max_len = 0;
133         glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
134         std::unique_ptr<char[]> log(new char[max_len]);
135         glGetProgramInfoLog(handle, max_len, &log_len, log.get());
136         out.write(log.get(), log_len);
137 }
138
139
140 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
141         return glGetAttribLocation(handle, name);
142 }
143
144 GLint Program::UniformLocation(const GLchar *name) const noexcept {
145         return glGetUniformLocation(handle, name);
146 }
147
148
149 void Program::Uniform(GLint loc, GLint val) noexcept {
150         glUniform1i(loc, val);
151 }
152
153 void Program::Uniform(GLint loc, float val) noexcept {
154         glUniform1f(loc, val);
155 }
156
157 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
158         glUniform3fv(loc, 1, glm::value_ptr(val));
159 }
160
161 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
162         glUniform4fv(loc, 1, glm::value_ptr(val));
163 }
164
165 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
166         glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
167 }
168
169
170 PlanetSurface::PlanetSurface()
171 : prog() {
172         prog.LoadShader(
173                 GL_VERTEX_SHADER,
174                 "#version 330 core\n"
175
176                 "layout(location = 0) in vec3 vtx_position;\n"
177                 "layout(location = 1) in vec3 vtx_tex_uv;\n"
178
179                 "uniform mat4 M;\n"
180                 "uniform mat4 MV;\n"
181                 "uniform mat4 MVP;\n"
182
183                 "out vec3 frag_tex_uv;\n"
184                 "out vec3 vtx_viewspace;\n"
185
186                 "void main() {\n"
187                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
188                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
189                         "frag_tex_uv = vtx_tex_uv;\n"
190                 "}\n"
191         );
192         prog.LoadShader(
193                 GL_FRAGMENT_SHADER,
194                 "#version 330 core\n"
195
196                 "struct LightSource {\n"
197                         "vec3 position;\n"
198                         "vec3 color;\n"
199                         "float strength;\n"
200                 "};\n"
201
202                 "in vec3 vtx_viewspace;\n"
203                 "in vec3 frag_tex_uv;\n"
204
205                 "uniform sampler2DArray tex_sampler;\n"
206                 "uniform vec3 normal;\n"
207                 "uniform LightSource light;\n"
208
209                 "out vec3 color;\n"
210
211                 "void main() {\n"
212                         "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
213                         "vec3 to_light = light.position - vtx_viewspace;\n"
214                         "float distance = length(to_light);\n"
215                         "vec3 light_dir = normalize(to_light);\n"
216                         "float attenuation = light.strength / (distance * distance);\n"
217                         "vec3 ambient = tex_color * vec3(0.01, 0.01, 0.01);\n"
218                         "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light.color * tex_color;\n"
219                         "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
220                         "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
221                         "if (dot(normal, light_dir) >= 0.0) {\n"
222                                 "attenuation * light.color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
223                         "}\n"
224                         "color = ambient + diffuse + specular;\n"
225                 "}\n"
226         );
227         prog.Link();
228         if (!prog.Linked()) {
229                 prog.Log(std::cerr);
230                 throw std::runtime_error("link program");
231         }
232         m_handle = prog.UniformLocation("M");
233         mv_handle = prog.UniformLocation("MV");
234         mvp_handle = prog.UniformLocation("MVP");
235         sampler_handle = prog.UniformLocation("tex_sampler");
236         normal_handle = prog.UniformLocation("normal");
237         light_position_handle = prog.UniformLocation("light.position");
238         light_color_handle = prog.UniformLocation("light.color");
239         light_strength_handle = prog.UniformLocation("light.strength");
240 }
241
242 PlanetSurface::~PlanetSurface() {
243 }
244
245 void PlanetSurface::Activate() noexcept {
246         prog.Use();
247         glEnable(GL_DEPTH_TEST);
248         glDepthFunc(GL_LESS);
249         glEnable(GL_CULL_FACE);
250         glDisable(GL_BLEND);
251 }
252
253 void PlanetSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
254         m = mm;
255         v = vv;
256         p = pp;
257         mv = v * m;
258         mvp = p * mv;
259         prog.Uniform(m_handle, m);
260         prog.Uniform(mv_handle, mv);
261         prog.Uniform(mvp_handle, mvp);
262 }
263
264 void PlanetSurface::SetNormal(const glm::vec3 &n) noexcept {
265         prog.Uniform(normal_handle, n);
266 }
267
268 void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept {
269         glActiveTexture(GL_TEXTURE0);
270         tex.Bind();
271         prog.Uniform(sampler_handle, GLint(0));
272 }
273
274 void PlanetSurface::SetLight(const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
275         prog.Uniform(light_position_handle, pos);
276         prog.Uniform(light_color_handle, color);
277         prog.Uniform(light_strength_handle, strength);
278 }
279
280 }
281 }