Almost working blending, stroke broken

This commit is contained in:
Tristan Schulz 2023-09-12 22:36:32 +02:00
commit 46ba43cc97
5 changed files with 55 additions and 39 deletions

View file

@ -21,13 +21,18 @@ if __name__ == "__main__":
with tempconfig({"renderer": "opengl"}):
renderer = OpenGLRenderer(1920, 1080, background_color=col.GRAY)
# vm = OpenGLVMobject([col.RED, col.GREEN])
vm = Circle(
radius=1, stroke_color=col.YELLOW, fill_opacity=1, fill_color=col.RED
).shift(RIGHT)
vm = (
Circle(
radius=1,
stroke_color=col.YELLOW,
)
.shift(RIGHT)
.set_opacity(0.5)
)
vm2 = Square(stroke_color=col.GREEN, fill_opacity=0, stroke_opacity=1).move_to(
(0, 0, -0.5)
)
vm3 = ManimBanner()
vm3 = ManimBanner().set_opacity(0.5)
# vm.set_points_as_corners([[-1920/2, 0, 0], [1920/2, 0, 0], [0, 1080/2, 0]])
# print(vm.color)
# print(vm.fill_color)
@ -46,7 +51,7 @@ if __name__ == "__main__":
width=1920,
height=1080,
vsync=True,
config=Config(double_buffer=True, samples=4),
config=Config(double_buffer=True, samples=0),
)
renderer.use_window()

View file

@ -68,16 +68,13 @@ bounding_box:
# TODO: Move into GLVMobjectManager
def get_triangulation(self, normal_vector=None):
def get_triangulation(self: OpenGLVMobject, normal_vector=None):
# Figure out how to triangulate the interior to know
# how to send the points as to the vertex shader.
# First triangles come directly from the points
if normal_vector is None:
normal_vector = self.get_unit_normal()
if not self.needs_new_triangulation:
return self.triangulation
points = self.points
if len(points) <= 1:
@ -204,7 +201,7 @@ class OpenGLRenderer(Renderer):
self,
pixel_width: int = config.pixel_width,
pixel_height: int = config.pixel_height,
samples=4,
samples=0,
background_color: c.ManimColor = color.BLACK,
background_opacity: float = 1.0,
background_image: str | None = None,
@ -228,19 +225,26 @@ class OpenGLRenderer(Renderer):
(self.pixel_width, self.pixel_height), components=1, samples=0, dtype="f1"
)
self.stencil_buffer = self.ctx.renderbuffer(
(self.pixel_width, self.pixel_height), components=1, samples=0, dtype="f1"
(self.pixel_width, self.pixel_height),
components=1,
samples=samples,
dtype="f1",
)
self.color_buffer = self.ctx.renderbuffer(
(self.pixel_width, self.pixel_height), components=4, samples=0, dtype="f1"
(self.pixel_width, self.pixel_height),
components=4,
samples=samples,
dtype="f1",
)
self.depth_buffer = self.ctx.depth_renderbuffer(
(self.pixel_width, self.pixel_height), samples=samples
)
# Here we create different fbos that can be reused which are basically just targets to use for rendering and copy
# render_target_fbo is used for rendering it can write to color and stencil
self.render_target_fbo = self.ctx.framebuffer(
color_attachments=[self.color_buffer, self.stencil_buffer],
depth_attachment=self.ctx.depth_renderbuffer(
(self.pixel_width, self.pixel_height), samples=0
),
depth_attachment=self.depth_buffer,
)
# this is used as source for stencil copy
@ -361,6 +365,21 @@ class OpenGLRenderer(Renderer):
self.render_target_fbo.use()
# Setting camera uniforms
self.ctx.enable(gl.BLEND) # type: ignore
# TODO: Because the Triangulation is messing up the normals this won't work
self.ctx.blend_func = ( # type: ignore
gl.SRC_ALPHA,
gl.ONE_MINUS_SRC_ALPHA,
gl.ONE,
gl.ONE,
)
def enable_depth(mob):
if sub.depth_test:
self.ctx.enable(gl.DEPTH_TEST) # type: ignore
else:
self.ctx.disable(gl.DEPTH_TEST) # type: ignore
for sub in mob.family_members_with_points():
if sub.renderer_data is None:
# Initialize
@ -377,24 +396,12 @@ class OpenGLRenderer(Renderer):
# if(mob.has_fill()):
# mob.renderer_data.mesh = ... # Triangulation todo
# self.ctx.enable(gl.CULL_FACE)
self.ctx.enable(gl.BLEND) # type: ignore
# TODO: Because the Triangulation is messing up the normals this won't work
# self.ctx.blend_func = ( #type: ignore
# gl.SRC_ALPHA,
# gl.ONE_MINUS_SRC_ALPHA,
# gl.ONE,
# gl.ONE,
# )
if sub.depth_test:
self.ctx.enable(gl.DEPTH_TEST) # type: ignore
else:
self.ctx.disable(gl.DEPTH_TEST) # type: ignore
num_mobs = len(mob.family_members_with_points())
for counter, sub in enumerate(mob.family_members_with_points()):
if not isinstance(sub.renderer_data, GLRenderData):
return
enable_depth(sub)
self.ctx.enable(gl.DEPTH_TEST)
uniforms = GLVMobjectManager.read_uniforms(sub)
# uniforms['z_shift'] = counter/9
uniforms["index"] = (counter + 1) / num_mobs
@ -412,6 +419,8 @@ class OpenGLRenderer(Renderer):
for counter, sub in enumerate(mob.family_members_with_points()):
if not isinstance(sub.renderer_data, GLRenderData):
return
enable_depth(sub)
self.ctx.enable(gl.DEPTH_TEST)
uniforms = GLVMobjectManager.read_uniforms(sub)
uniforms["index"] = (counter + 1) / num_mobs
# uniforms['z_shift'] = counter/9 + 1/20
@ -423,7 +432,7 @@ class OpenGLRenderer(Renderer):
self.render_program(
self.vmobject_stroke_program,
self.get_stroke_shader_data(sub),
np.array(range(len(sub.points)))[::-1],
np.array(range(len(sub.points))),
)
counter += 1
@ -468,6 +477,7 @@ class GLVMobjectManager:
uniforms["shadow"] = mob.shadow
uniforms["flat_stroke"] = float(mob.flat_stroke)
uniforms["joint_type"] = float(mob.joint_type.value)
uniforms["flat_stroke"] = float(mob.flat_stroke)
return uniforms

View file

@ -2,7 +2,7 @@
// uniform vec2 frame_shape;
// uniform float focal_distance;
// uniform float is_fixed_in_frame;
uniform float z_shift;
// uniform float z_shift;
const vec2 DEFAULT_FRAME_SHAPE = vec2(8.0 * 16.0 / 9.0, 8.0);
float perspective_scale_factor(float z, float focal_distance)
@ -24,7 +24,7 @@ vec4 get_gl_Position(vec3 point)
// TODO, what's the better way to do this?
// This is to keep vertices too far out of frame from getting cut.
// TODO This will be done by the clipping plane in the future with the transformation matrix
result.z += z_shift;
// result.z += z_shift;
result.z *= (1.0 / 100.0);
}
}

View file

@ -15,7 +15,7 @@ in float bezier_degree;
uniform sampler2D stencil_texture;
layout(location = 0) out vec4 frag_color;
layout(location = 1) out float stencil_value;
layout(location = 1) out vec4 stencil_value;
#define ANTI_ALIASING
@ -38,7 +38,8 @@ float sdf(){
void main() {
gl_FragDepth = gl_FragCoord.z;
if (color.a == 0) discard;
stencil_value = index;
stencil_value.xyz = vec3(index);
stencil_value.a = 1.0;
float previous_index =
texture2D(stencil_texture, vec2(gl_FragCoord.x / pixel_shape.x, gl_FragCoord.y / pixel_shape.y)).r;
@ -61,7 +62,7 @@ void main() {
#ifndef ANTI_ALIASING
frag_color.a *= float(sdf() > 0); // No anti-aliasing
#endif
if (frag_color.a <= 0.0)
if (frag_color.a < 0.0)
{
discard;
}

View file

@ -23,7 +23,7 @@ uniform sampler2D stencil_texture;
in float bezier_degree;
layout(location = 0) out vec4 frag_color;
layout(location = 1) out float stencil_value;
layout(location = 1) out vec4 stencil_value;
float cross2d(vec2 v, vec2 w){
return v.x * w.y - w.x * v.y;
@ -88,6 +88,8 @@ float modify_distance_for_endpoints(vec2 p, float dist, float t){
void main() {
// Use the default value as standard output
stencil_value.xyz = vec3(index);
stencil_value.a = 1.0;
gl_FragDepth = gl_FragCoord.z;
// Get the previous index that was written to this fragment
float previous_index =
@ -108,7 +110,6 @@ void main() {
{
discard;
}
stencil_value = index;
if (uv_stroke_width == 0)
discard;
float dist_to_curve = min_dist_to_curve(uv_coords, uv_b2, bezier_degree);
@ -117,9 +118,8 @@ void main() {
frag_color = color;
frag_color.a *= smoothstep(0.5, -0.5, signed_dist / uv_anti_alias_width);
if (frag_color.a <= 0.0)
if (frag_color.a < 0.0)
{
discard;
}
// stencil_value = 1;
}