Prep quadratic bezier shaders to work in 3d

This commit is contained in:
Grant Sanderson
2020-02-16 10:53:16 -08:00
parent da7864b28c
commit 673b85f129
8 changed files with 68 additions and 42 deletions

View File

@ -14,10 +14,16 @@ out vec2 v_im_coords;
out float v_opacity;
// Analog of import for manim only
#INSERT set_gl_Position.glsl
#INSERT rotate_point_for_frame.glsl
#INSERT scale_and_shift_point_for_frame.glsl
void main(){
v_im_coords = im_coords;
v_opacity = opacity;
set_gl_Position(point);
gl_Position = vec4(
rotate_point_for_frame(
scale_and_shift_point_for_frame(point)
),
1.0
);
}

View File

@ -1,11 +0,0 @@
// Assumes theese uniforms exist in the surrounding context
// uniform float scale;
// uniform float aspect_ratio;
// uniform float frame_center;
vec4 point_to_gl_Position(vec3 p){
vec3 result = p / scale;
result.x /= aspect_ratio;
result -= frame_center;
gl_Position = vec4(result, 1.0);
}

View File

@ -8,7 +8,7 @@ uniform float aspect_ratio;
uniform float anti_alias_width;
uniform vec3 frame_center;
in vec2 bp[3];
in vec3 bp[3];
in vec4 v_color[3];
in float v_fill_all[3];
in float v_orientation[3];
@ -36,7 +36,7 @@ const float SQRT5 = 2.236068;
// so to share functionality between this and others, the caller
// replaces this line with the contents of named file
#INSERT quadratic_bezier_geometry_functions.glsl
#INSERT set_gl_Position.glsl
#INSERT scale_and_shift_point_for_frame.glsl
mat3 get_xy_to_wz(vec2 b0, vec2 b1, vec2 b2){
@ -65,7 +65,10 @@ mat3 get_xy_to_wz(vec2 b0, vec2 b1, vec2 b2){
void emit_simple_triangle(){
for(int i = 0; i < 3; i++){
color = v_color[i];
set_gl_Position(vec3(bp[i], 0));
gl_Position = vec4(
scale_and_shift_point_for_frame(bp[i]),
1.0
);
EmitVertex();
}
EndPrimitive();
@ -118,12 +121,25 @@ void emit_pentagon(vec2 bp0, vec2 bp1, vec2 bp2, float orientation){
vec2 corner = corners[coords_index_map[i]];
uv_coords = (xy_to_uv * vec3(corner, 1)).xy;
wz_coords = (xy_to_wz * vec3(corner, 1)).xy;
float z;
// I haven't a clue why an index map doesn't work just
// as well here, but for some reason it doesn't.
if(i < 2) color = v_color[0];
else if(i == 2) color = v_color[1];
else color = v_color[2];
set_gl_Position(vec3(corner, 0));
if(i < 2){
color = v_color[0];
z = bp[0].z;
}
else if(i == 2){
color = v_color[1];
z = bp[1].z;
}
else{
color = v_color[2];
z = bp[2].z;
}
gl_Position = vec4(
scale_and_shift_point_for_frame(vec3(corner, z)),
1.0
);
EmitVertex();
}
EndPrimitive();
@ -138,7 +154,7 @@ void main(){
emit_simple_triangle();
}else{
vec2 new_bp[3];
int n = get_reduced_control_points(bp[0], bp[1], bp[2], new_bp);
int n = get_reduced_control_points(bp[0].xy, bp[1].xy, bp[2].xy, new_bp);
bezier_degree = float(n);
float orientation = v_orientation[0];

View File

@ -7,14 +7,17 @@ in float fill_all;
// orientation is +1 for counterclockwise curves, -1 otherwise
in float orientation;
out vec2 bp; // Bezier control point
out vec3 bp; // Bezier control point
out vec4 v_color;
out float v_fill_all;
out float v_orientation;
#INSERT rotate_point_for_frame.glsl
void main(){
bp = point.xy; // TODO
bp = rotate_point_for_frame(point);
v_color = color;
v_fill_all = fill_all;
v_orientation = orientation;

View File

@ -8,9 +8,9 @@ uniform float aspect_ratio;
uniform float anti_alias_width;
uniform vec3 frame_center;
in vec2 bp[3];
in vec2 prev_bp[3];
in vec2 next_bp[3];
in vec3 bp[3];
in vec3 prev_bp[3];
in vec3 next_bp[3];
in vec4 v_color[3];
in float v_stroke_width[3];
@ -43,7 +43,7 @@ const float MITER_JOINT = 3;
// so to share functionality between this and others, the caller
// replaces this line with the contents of named file
#INSERT quadratic_bezier_geometry_functions.glsl
#INSERT set_gl_Position.glsl
#INSERT scale_and_shift_point_for_frame.glsl
float angle_between_vectors(vec2 v1, vec2 v2){
@ -250,7 +250,7 @@ void set_previous_and_next(vec2 controls[3], int degree){
vec2 tangent = controls[1] - controls[0];
set_adjascent_info(
controls[0], tangent, degree, 1, 1,
vec2[3](prev_bp[0], prev_bp[1], prev_bp[2]),
vec2[3](prev_bp[0].xy, prev_bp[1].xy, prev_bp[2].xy),
has_prev, bevel_start, angle_from_prev
);
}
@ -258,7 +258,7 @@ void set_previous_and_next(vec2 controls[3], int degree){
vec2 tangent = controls[degree - 1] - controls[degree];
set_adjascent_info(
controls[degree], tangent, degree, 0, -1,
vec2[3](next_bp[0], next_bp[1], next_bp[2]),
vec2[3](next_bp[0].xy, next_bp[1].xy, next_bp[2].xy),
has_next, bevel_end, angle_to_next
);
}
@ -267,7 +267,7 @@ void set_previous_and_next(vec2 controls[3], int degree){
void main() {
vec2 controls[3];
int degree = get_reduced_control_points(bp[0], bp[1], bp[2], controls);
int degree = get_reduced_control_points(bp[0].xy, bp[1].xy, bp[2].xy, controls);
bezier_degree = float(degree);
// Null curve or linear with higher index than needed
@ -279,7 +279,7 @@ void main() {
mat3 xy_to_uv = get_xy_to_uv(controls[0], controls[1]);
float scale_factor = length(controls[1] - controls[0]);
uv_anti_alias_width = anti_alias_width / scale_factor;
uv_b2 = (xy_to_uv * vec3(bp[2], 1.0)).xy;
uv_b2 = (xy_to_uv * vec3(controls[degree], 1.0)).xy;
// Corners of a bounding region around curve
vec2 corners[5];
@ -288,12 +288,14 @@ void main() {
// Get style info aligned to the corners
float stroke_widths[5];
vec4 stroke_colors[5];
float z_values[5];
int index_map[5];
if(n_corners == 4) index_map = int[5](0, 0, 2, 2, 2);
else index_map = int[5](0, 0, 1, 2, 2);
for(int i = 0; i < 5; i++){
stroke_widths[i] = v_stroke_width[index_map[i]];
stroke_colors[i] = v_color[index_map[i]];
z_values[i] = bp[index_map[i]].z; // TODO, seems clunky
}
// Emit each corner
@ -304,7 +306,10 @@ void main() {
uv_stroke_width = stroke_widths[i] / scale_factor;
color = stroke_colors[i];
set_gl_Position(vec3(corner, 0));
gl_Position = vec4(
scale_and_shift_point_for_frame(vec3(corner, z_values[i])),
1.0
);
EmitVertex();
}
EndPrimitive();

View File

@ -8,23 +8,27 @@ in float stroke_width;
in vec4 color;
in float joint_type;
out vec2 bp; // Bezier control point
out vec2 prev_bp;
out vec2 next_bp;
out vec3 bp; // Bezier control point
out vec3 prev_bp;
out vec3 next_bp;
out float v_stroke_width;
out vec4 v_color;
out float v_joint_type;
// TODO, this should maybe depent on scale
// TODO, this should maybe depend on scale
const float STROKE_WIDTH_CONVERSION = 0.01;
#INSERT rotate_point_for_frame.glsl
void main(){
v_stroke_width = STROKE_WIDTH_CONVERSION * stroke_width;
v_color = color;
v_joint_type = joint_type;
bp = point.xy; // TODO, apply some kind of 3d rotation or shift first
prev_bp = prev_point.xy;
next_bp = next_point.xy;
bp = rotate_point_for_frame(point);
prev_bp = rotate_point_for_frame(prev_point);
next_bp = rotate_point_for_frame(next_point);
}

View File

@ -0,0 +1,4 @@
vec3 rotate_point_for_frame(vec3 point){
// TODO, orient in 3d based on certain rotation matrices
return point;
}

View File

@ -3,10 +3,9 @@
// uniform float aspect_ratio;
// uniform float frame_center;
void set_gl_Position(vec3 point){
// TODO, orient in 3d based on certain rotation matrices
vec3 scale_and_shift_point_for_frame(vec3 point){
point -= frame_center;
point /= scale;
point.x /= aspect_ratio;
gl_Position = vec4(point, 1.0);
return point;
}