1 #ifndef INCLUDE_AL_GRAPHICS_DEFAULT_SHADERS_HPP
2 #define INCLUDE_AL_GRAPHICS_DEFAULT_SHADERS_HPP
50 #include "al/graphics/al_Shader.hpp"
52 inline constexpr
int al_max_num_lights() {
return 8; }
72 int global_ambient = -1;
73 int normal_matrix = -1;
76 std::vector<per_light_uniform_locations> lights;
77 bool has_material =
false;
81 std::string name =
"shader");
86 inline std::string al_default_shader_version_string() {
87 return R
"(#version 330
91 inline std::string al_default_vert_shader_stereo_functions(
bool is_omni) {
94 vec4 stereo_displace(vec4 v, float e, float f) {
95 // eye to vertex distance
96 float l = sqrt((v.x - e) * (v.x - e) + v.y * v.y + v.z * v.z);
97 // absolute z-direction distance
99 // x coord of projection of vertex on focal plane when looked from eye
100 float t = f * (v.x - e) / z;
101 // x coord of displaced vertex to make displaced vertex be projected on focal plane
102 // when looked from origin at the same point original vertex would be projected
103 // when looked form eye
104 v.x = z * (e + t) / f;
105 // set distance fromr origin to displaced vertex same as eye to original vertex
106 v.xyz = normalize(v.xyz);
112 vec4 stereo_displace(vec4 v, float e, float r) {
113 vec3 OE = vec3(-v.z, 0.0, v.x); // eye direction, orthogonal to vertex vector
114 OE = normalize(OE); // but preserving +y up-vector
115 OE *= e; // set mag to eye separation
116 vec3 EV = v.xyz - OE; // eye to vertex
117 float ev = length(EV); // save length
118 EV /= ev; // normalize
120 // coefs for polynomial t^2 + 2bt + c = 0
121 // derived from cosine law r^2 = t^2 + e^2 + 2tecos(theta)
122 // where theta is angle between OE and EV
123 // t is distance to sphere surface from eye
124 float b = -dot(OE, EV); // multiply -1 to dot product because
125 // OE needs to be flipped in direction
126 float c = e * e - r * r;
127 float t = -b + sqrt(b * b - c); // quadratic formula
129 v.xyz = OE + t * EV; // direction from origin to sphere surface
130 v.xyz = ev * normalize(v.xyz); // normalize and set mag to eye-to-v distance
137 inline std::string al_mesh_vert_shader(
bool is_omni =
false) {
138 using namespace std::string_literals;
139 return al_default_shader_version_string() +
140 al_default_vert_shader_stereo_functions(is_omni) + R
"(
141 uniform mat4 al_ModelViewMatrix;
142 uniform mat4 al_ProjectionMatrix;
143 layout (location = 0) in vec3 position;
144 layout (location = 1) in vec4 color;
145 uniform float eye_sep;
146 uniform float foc_len;
150 gl_Position = al_ProjectionMatrix * al_ModelViewMatrix * vec4(position, 1.0);
153 gl_Position = al_ProjectionMatrix * stereo_displace(al_ModelViewMatrix * vec4(position, 1.0), eye_sep, foc_len);
160 inline std::string al_mesh_frag_shader() {
167 frag_color = color_ * tint;
174 inline std::string al_tex_vert_shader(
bool is_omni =
false) {
175 using namespace std::string_literals;
176 return al_default_shader_version_string() +
177 al_default_vert_shader_stereo_functions(is_omni) + R
"(
178 uniform mat4 al_ModelViewMatrix;
179 uniform mat4 al_ProjectionMatrix;
180 layout (location = 0) in vec3 position;
181 layout (location = 2) in vec2 texcoord;
182 uniform float eye_sep;
183 uniform float foc_len;
187 gl_Position = al_ProjectionMatrix * al_ModelViewMatrix * vec4(position, 1.0);
190 gl_Position = al_ProjectionMatrix * stereo_displace(al_ModelViewMatrix * vec4(position, 1.0), eye_sep, foc_len);
192 texcoord_ = texcoord;
197 inline std::string al_tex_frag_shader() {
200 uniform sampler2D tex0;
205 frag_color = texture(tex0, texcoord_) * tint;
212 inline std::string al_color_vert_shader(
bool is_omni =
false) {
213 using namespace std::string_literals;
214 return al_default_shader_version_string() +
215 al_default_vert_shader_stereo_functions(is_omni) + R
"(
216 uniform mat4 al_ModelViewMatrix;
217 uniform mat4 al_ProjectionMatrix;
218 layout (location = 0) in vec3 position;
219 uniform float eye_sep;
220 uniform float foc_len;
223 gl_Position = al_ProjectionMatrix * al_ModelViewMatrix * vec4(position, 1.0);
226 gl_Position = al_ProjectionMatrix * stereo_displace(al_ModelViewMatrix * vec4(position, 1.0), eye_sep, foc_len);
232 inline std::string al_color_frag_shader() {
239 frag_color = col0 * tint;
248 enum class ShaderType : unsigned char {
259 void compileDefaultShader(ShaderProgram& s, ShaderType type,
260 bool is_omni =
false);
263 std::string multilight_vert_shader(ShaderType type,
int num_lights,
264 bool is_omni =
false);
267 std::string multilight_frag_shader(ShaderType type,
int num_lights);
272 void compileMultiLightShader(ShaderProgram& s, ShaderType type,
int num_lights,
273 bool is_omni =
false);