Skip to content

Commit 20817d1

Browse files
committed
Added color functions and coordinate shifting in highp shader
1 parent 27832a8 commit 20817d1

2 files changed

Lines changed: 79 additions & 16 deletions

File tree

src/preprocessor/string_builder.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ string declare_constant(const std::string& variable_name, const hp_vec2& value){
154154
return "const number " + variable_name + " = initialize_hp_vec2(" + convert_number_to_glsl(value.x) + "," + convert_number_to_glsl(value.y) + ");\n";
155155
}
156156

157-
string build_high_precision_shader_string(const std::string& highp_header, const std::string& highp_function_declarations, const std::string& lowp_function_declarations){
157+
string build_high_precision_shader_string(const std::string& highp_header, const std::string& highp_footer, const std::string& highp_function_declarations, const std::string& lowp_function_declarations){
158158
string out = highp_header;
159159
const number pi = compute_pi();
160160
const number e = compute_e();
@@ -164,24 +164,26 @@ string build_high_precision_shader_string(const std::string& highp_header, const
164164
const number three = number_integer(3);
165165
const number zero = null_number();
166166

167-
declare_constant("PI",pi);
168-
declare_constant("E", e);
169-
declare_constant("LN2", ln_2);
170-
declare_constant("REAL_ZERO", zero);
171-
declare_constant("REAL_ONE", number_one());
172-
declare_constant("REAL_TWO", two);
173-
declare_constant("INFINITY", infinite_number());
167+
out += declare_constant("PI",pi);
168+
out += declare_constant("E", e);
169+
out += declare_constant("LN2", ln_2);
170+
out += declare_constant("REAL_ZERO", zero);
171+
out += declare_constant("REAL_ONE", number_one());
172+
out += declare_constant("REAL_TWO", two);
173+
out += declare_constant("INFINITY", infinite_number());
174174

175-
declare_constant("TWO_PI_OVER_3", hp_div(hp_mult(two,pi),three));
176-
declare_constant("TWO_OVER_PI", hp_div(two,pi));
175+
out += declare_constant("TWO_PI_OVER_3", hp_div(hp_mult(two,pi),three));
176+
out += declare_constant("TWO_OVER_PI", hp_div(two,pi));
177177

178-
declare_constant("ZERO", hp_vec2(zero,zero));
179-
declare_constant("CPI", hp_vec2(pi,zero));
180-
declare_constant("ONE", hp_vec2(one,zero));
181-
declare_constant("MINUS_ONE", hp_vec2(hp_neg(one),zero));
182-
declare_constant("I", hp_vec2(zero,one));
178+
out += declare_constant("ZERO", hp_vec2(zero,zero));
179+
out += declare_constant("CPI", hp_vec2(pi,zero));
180+
out += declare_constant("ONE", hp_vec2(one,zero));
181+
out += declare_constant("MINUS_ONE", hp_vec2(hp_neg(one),zero));
182+
out += declare_constant("I", hp_vec2(zero,one));
183183

184184
out += transpile_to_highp_glsl(lowp_function_declarations,highp_function_declarations);
185185

186-
186+
out += highp_footer;
187+
188+
return out;
187189
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
uniform uint u_center_x_limb[LIMB_SIZE];
2+
uniform int u_center_x_sign;
3+
4+
uniform uint u_center_y_limb[LIMB_SIZE];
5+
uniform int u_center_y_sign;
6+
7+
uniform uint u_zoom_limb[LIMB_SIZE];
8+
uniform int u_zoom_sign;
9+
10+
uniform vec2 u_resolution;
11+
12+
hp_vec2 get_high_precision_coordinates(vec2 fragCoord){
13+
number center_x;
14+
center_x.limb = u_center_x_limb;
15+
center_x.sign = u_center_x_sign;
16+
center_x.is_infinite = false;
17+
18+
number center_y;
19+
center_y.limb = u_center_y_limb;
20+
center_y.sign = u_center_y_sign;
21+
center_y.is_infinite = false;
22+
23+
number zoom;
24+
zoom.limb = u_zoom_limb;
25+
zoom.sign = u_zoom_sign;
26+
zoom.is_infinite = false;
27+
28+
vec2 offset = (fragCoord.xy - 0.5 * u_resolution.xy) / u_resolution.y;
29+
number offset_x_hp = float_to_number(offset.x);
30+
number offset_y_hp = float_to_number(offset.y);
31+
32+
number final_x = hp_add(center_x, hp_mult(offset_x_hp, zoom));
33+
number final_y = hp_add(center_y, hp_mult(offset_y_hp, zoom));
34+
35+
return initialize_hp_vec2(final_x, final_y);
36+
}
37+
38+
vec3 hsl2rgb(vec3 hsl) {
39+
vec3 rgb = clamp(abs(mod(hsl.x * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
40+
return hsl.z + hsl.y * (rgb - 0.5) * (1.0 - abs(2.0 * hsl.z - 1.0));
41+
}
42+
43+
vec3 domain_color(in hp_vec2 z) {
44+
number angle = hp_atan2(z.y, z.x);
45+
number hue_hp = hp_div(angle, hp_mult(REAL_TWO, PI));
46+
number light_hp = hp_mult(TWO_OVER_PI, hp_atan(hp_length(z)));
47+
48+
float hue = number_to_float(hue_hp);
49+
float light = number_to_float(light_hp);
50+
51+
return vec3(hue, 1.0, light);
52+
}
53+
54+
void main(){
55+
hp_vec2 z = get_high_precision_coordinates(gl_FragCoord.xy);
56+
hp_vec2 func_value;
57+
#define INJECTION_POINT HERE
58+
59+
vec3 hsl = domain_color(func_value);
60+
61+
}

0 commit comments

Comments
 (0)