Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix most problems
  • Loading branch information
SimonDanisch committed Mar 22, 2022
commit 586ab121ad272a88e87f3895f26fd5847082191f
34 changes: 17 additions & 17 deletions GLMakie/src/drawing_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ using Makie: attribute_per_char, FastPixel, el32convert, Pixel
using Makie: convert_arguments

Makie.el32convert(x::GLAbstraction.Texture) = x
Makie.convert_attribute(s::ShaderAbstractions.Sampler{RGBAf}, k::key"color") = s
function Makie.convert_attribute(s::ShaderAbstractions.Sampler{T, N}, k::key"color") where {T, N}
Makie.el32convert(x::ShaderAbstractions.Sampler) = x
Makie.convert_attribute(s::ShaderAbstractions.Sampler{RGBAf}, ::key"color") = s
function Makie.convert_attribute(s::ShaderAbstractions.Sampler{T, N}, ::key"color") where {T, N}
ShaderAbstractions.Sampler(
el32convert(s.data), minfilter = s.minfilter, magfilter = s.magfilter,
x_repeat = s.repeat[1], y_repeat = s.repeat[min(2, N)], z_repeat = s.repeat[min(3, N)],
Expand Down Expand Up @@ -449,34 +450,33 @@ function draw_atomic(screen::GLScreen, scene::Scene, x::Image)
r = to_range(x, y)
x, y = minimum(r[1]), minimum(r[2])
xmax, ymax = maximum(r[1]), maximum(r[2])
rect = Rect2f(x, y, xmax - x, ymax - y)
points = decompose(Point2f, rect)
faces = decompose(GLTriangleFace, rect)
uv = map(decompose_uv(rect)) do uv
return 1f0 .- Vec2f(uv[2], uv[1])
end
return GeometryBasics.Mesh(meta(points; uv=uv), faces)
rect = Rect2f(x, y, xmax - x, ymax - y)
return decompose(Point2f, rect)
end
gl_attributes[:texturecoordinates] = map(decompose_uv(Rect(0, 0, 1, 1))) do uv
return 1f0 .- Vec2f(uv[2], uv[1])
end
gl_attributes[:color] = x[3]
gl_attributes[:faces] = decompose(GLTriangleFace, Rect(0, 0, 1, 1))
gl_attributes[:shading] = false
gl_attributes[:vertex_color] = Vec4f(0)
gl_attributes[:image] = x[3]
if to_value(x[3]) isa AbstractMatrix{<: Colorant}
delete!(gl_attributes, :color_map)
delete!(gl_attributes, :color_norm)
end
connect_camera!(gl_attributes, scene.camera)
return mesh_inner(mesh, transform_func_obs(x), gl_attributes)
end
end

function update_positions(mesh::GeometryBasics.Mesh, positions)
points = coordinates(mesh)
attr = GeometryBasics.attributes(points)
delete!(attr, :position) # position == metafree(points)
return GeometryBasics.Mesh(meta(positions; attr...), faces(mesh))
end

function mesh_inner(vertices, transfunc, gl_attributes)
# signals not supported for shading yet
gl_attributes[:shading] = to_value(pop!(gl_attributes, :shading))
gl_attributes[:vertices] = map(vertices, transfunc) do vertices, func
return apply_transform(func, vertices)
end
# color is only kept for backwards compat (vertex_color/image in shader)
delete!(gl_attributes, :color)
return GLVisualize.draw_mesh(gl_attributes)
end

Expand Down
2 changes: 1 addition & 1 deletion MakieCore/src/basic_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ end
Plots a 3D or 2D mesh. Supported `mesh_object`s include `Mesh` types from [GeometryBasics.jl](https://github.com/JuliaGeometry/GeometryBasics.jl).

"""
@recipe(Mesh, vertices) do scene
@recipe(Mesh, _vertices) do scene
Attributes(;
default_theme(scene)...,

Expand Down
31 changes: 31 additions & 0 deletions ReferenceTests/src/tests/meshes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using GLMakie, GeometryBasics

# basic 2d
f, ax, pl = mesh(Rect2f(0, 0, 1, 1))
# basic 3d
f, ax, pl = mesh(Sphere(Point3f(0), 1f0))
# without normals
f, ax, pl = mesh(triangle_mesh(Sphere(Point3f(0), 1f0)))
f, ax, pl = mesh(Sphere(Point3f(0), 1f0), color=rand(100, 100))


heatmap(rand(32, 32))
image!(map(x -> RGBAf(x, 0.5, 0.5, 0.8), rand(32, 32)))
current_figure()


poly([(0.0, 0.0), (0.5, 1.0), (1.0, 0.0)],
color=[:red, :green, :blue],
strokecolor=:black, strokewidth=2)


points = decompose(Point2f, Circle(Point2f(50), 50f0))
fig, ax, pol = poly(points, color=:gray, strokewidth=10, strokecolor=:red)
# Optimized forms
poly!(ax, [Circle(Point2f(50 + 300), 50f0)], color=:gray, strokewidth=10, strokecolor=:red)
poly!(ax, [Circle(Point2f(50 + i, 50 + i), 10f0) for i = 1:100:400], color=:red)
poly!(ax, [Rect2f(50 + i, 50 + i, 20, 20) for i = 1:100:400], strokewidth=2, strokecolor=:green)
linesegments!(ax,
[Point2f(50 + i, 50 + i) => Point2f(i + 70, i + 70) for i = 1:100:400], linewidth=8, color=:purple
)
fig
1 change: 0 additions & 1 deletion src/basic_recipes/barplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ function Makie.plot!(p::BarPlot)
bars = lift(calculate_bars, p[1], p.fillto, p.offset, p.width, p.dodge, p.n_dodge, p.gap,
p.dodge_gap, p.stack, p.direction, p.bar_labels, p.flip_labels_at,
p.label_color, p.color_over_background, p.color_over_bar, p.label_formatter, p.label_offset)

poly!(
p, bars, color = p.color, colormap = p.colormap, colorrange = p.colorrange,
strokewidth = p.strokewidth, strokecolor = p.strokecolor, visible = p.visible,
Expand Down
61 changes: 32 additions & 29 deletions src/basic_recipes/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ $(ATTRIBUTES)
colorrange = automatic,
strokewidth = theme(scene, :patchstrokewidth),
shading = false,
# we turn this false for now, since otherwise shapes look transparent
# since we use meshes, which are drawn into a different framebuffer because of fxaa
# if we use fxaa=false, they're drawn into the same
# TODO, I still think this is a bug, since they should still use the same depth buffer!
fxaa = false,
fxaa = true,
linestyle = nothing,
overdraw = false,
transparency = false,
Expand Down Expand Up @@ -165,31 +161,38 @@ function plot!(plot::Mesh{<: Tuple{<: AbstractVector{P}}}) where P <: Union{Abst
attributes[:colormap] = get(plot, :colormap, nothing)
attributes[:colorrange] = get(plot, :colorrange, nothing)

bigmesh = if color_node[] isa AbstractVector && length(color_node[]) == length(meshes[])
# One color per mesh
lift(meshes, color_node, attributes.colormap, attributes.colorrange) do meshes, colors, cmap, crange
# Color are reals, so we need to transform it to colors first
single_colors = if colors isa AbstractVector{<:Number}
interpolated_getindex.((to_colormap(cmap),), colors, (crange,))
else
to_color.(colors)
end
real_colors = RGBAf[]
# Map one single color per mesh to each vertex
for (mesh, color) in zip(meshes, single_colors)
append!(real_colors, Iterators.repeated(RGBAf(color), length(coordinates(mesh))))
end
# real_colors[] = real_colors[]
if P <: AbstractPolygon
meshes = triangle_mesh.(meshes)
end
return pointmeta(merge(meshes), color=real_colors)
num_meshes = Observable(Int[])
map(meshes) do meshes
# TODO use change observable!
new_lengths = length.(coordinates.(meshes))
if num_meshes[] != new_lengths
num_meshes[] = new_lengths
end
else
attributes[:color] = color_node
lift(meshes) do meshes
return merge(GeometryBasics.mesh.(meshes))
end
mesh_colors = Observable{RGBColors}()
map!(mesh_colors, plot.color, num_meshes) do colors, num_meshes
# one mesh per color
c_converted = to_color(colors)
if c_converted isa AbstractVector && length(c_converted) == length(num_meshes)
result = similar(c_converted, sum(num_meshes))
i = 1
for (cs, len) in zip(c_converted, num_meshes)
for j in 1:len
result[i] = cs
i += 1
end
end
return result
else
return c_converted
end
end
mesh!(plot, attributes, bigmesh)
attributes[:color] = mesh_colors
lift(meshes) do meshes
m = merge(GeometryBasics.mesh.(meshes))
attributes[:faces] = decompose(GLTriangleFace, m)
attributes[:vertices] = decompose(Point, m)
return
end
p = mesh!(plot, attributes)
end
10 changes: 5 additions & 5 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ function convert_arguments(::Type{<:Mesh}, mesh::GeometryBasics.Mesh{N}) where {
fs = decompose(GLTriangleFace, mesh)
normals = hasproperty(mesh, :normals) ? mesh.normals : automatic
uv = hasproperty(mesh, :uv) ? mesh.uv : automatic
return PlotSpec{Mesh}(points; faces=fs, normals=normals, texturecoordinates=uv)
return PlotSpec{Mesh}(; vertices=points, faces=fs, normals=normals, texturecoordinates=uv)
end

convert_attribute(uv::AbstractVector{<:Vec2}, ::key"texturecoordinates") = convert(Vector{Vec2f}, uv)
Expand All @@ -518,11 +518,11 @@ end
function convert_arguments(MT::Type{<:Mesh}, geom::GeometryPrimitive)
# we convert to UV mesh as default, because otherwise the uv informations get lost
# - we can still drop them, but we can't add them later on
return (GeometryBasics.uv_normal_mesh(geom),)
return convert_arguments(MT, GeometryBasics.uv_normal_mesh(geom))
end

"""
convert_arguments(Mesh, x, y, z, indices)::GLNormalMesh
convert_arguments(Mesh, x, y, z, indices)

Takes real vectors x, y, z and constructs a triangle mesh out of those, using the
faces in `indices`, which can be integers (every 3 -> one triangle), or GeometryBasics.NgonFace{N, <: Integer}.
Expand All @@ -543,12 +543,12 @@ See [`to_vertices`](@ref) and [`to_triangles`](@ref) for more information about
accepted types.
"""
function convert_arguments(
::Type{<:Mesh},
MT::Type{<:Mesh},
vertices::AbstractArray,
indices::AbstractArray
)
m = normal_mesh(to_vertices(vertices), to_triangles(indices))
(m,)
return convert_arguments(MT, m)
end

################################################################################
Expand Down
14 changes: 9 additions & 5 deletions src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function color_and_colormap!(plot, intensity = plot[:color])
end

struct MeshPlot{T, N}

vertex_colors::Union{Nothing, RGBColors}
image::Union{Nothing, Sampler{T}}
colormap::Union{Nothing, Vector{RGBAf}}
Expand All @@ -44,6 +43,7 @@ struct MeshPlot{T, N}
faces::Vector{GLTriangleFace}

backlight::Float32
depth_shift::Float32
shading::Bool
fetch_pixel::Bool
uv_scale::Vec2f
Expand Down Expand Up @@ -82,7 +82,7 @@ function calculated_attributes!(::Type{<: Mesh}, plot)
return plot.shading[] ? lift(normals, plot.vertices, plot.faces) : nothing
end
replace_automatic!(plot, :texturecoordinates) do
return needs_uv ? lift(decompose_uv, plot.vertices) : nothing
return needs_uv ? lift(decompose_uv, plot.vertices) : nothing
end
return
end
Expand Down Expand Up @@ -196,7 +196,10 @@ function apply_convert!(P, attributes::Attributes, x::PlotSpec{S}) where S
# Note that kw_args in the plot spec that are not part of the target plot type
# will end in the "global plot" kw_args (rest)
for (k, v) in pairs(kwargs)
attributes[k] = v
# Don't overwrite existing attributes with automatic
if !(haskey(attributes, k) && v isa Automatic)
attributes[k] = v
end
end
return (plottype(S, P), args)
end
Expand Down Expand Up @@ -264,7 +267,6 @@ function (PlotType::Type{<: AbstractPlot{Typ}})(scene::SceneLike, attributes::At
end
# create the plot, with the full attributes, the input signals, and the final signals.
plot_obj = FinalType(scene, transformation, plot_attributes, input, seperate_tuple(args))

calculated_attributes!(plot_obj)
plot_obj
end
Expand Down Expand Up @@ -340,6 +342,9 @@ function plot!(scene::Union{Combined, SceneLike}, P::PlotFunc, attributes::Attri
# plottype will lose the argument types, so we just extract the plot func
# type and recreate the type with the argument type
PreType = Combined{plotfunc(PreType), typeof(argvalues)}
if isempty(args)
return plot!(scene, PreType, attributes, Observable(()), Observable(()))
end
convert_keys = intersect(used_attributes(PreType, argvalues...), keys(attributes))
kw_signal = if isempty(convert_keys) # lift(f) isn't supported so we need to catch the empty case
Observable(())
Expand Down Expand Up @@ -455,7 +460,6 @@ end

function plot!(scene::Combined, P::PlotFunc, attributes::Attributes, input::NTuple{N,Observable}, args::Observable) where {N}
# create "empty" plot type - empty meaning containing no plots, just attributes + arguments

plot_object = P(scene, attributes, input, args)
# call user defined recipe overload to fill the plot type
plot!(plot_object)
Expand Down
8 changes: 7 additions & 1 deletion src/layouting/data_limits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ end

point_iterator(plot::Combined) = point_iterator(plot.plots)

point_iterator(plot::Mesh) = plot.vertices[]
function point_iterator(plot::Mesh)
if isempty(plot.plots)
return plot.vertices[]
else
return point_iterator(plot.plots)
end
end

function br_getindex(vector::AbstractVector, idx::CartesianIndex, dim::Int)
return vector[Tuple(idx)[dim]]
Expand Down
6 changes: 3 additions & 3 deletions src/makielayout/layoutables/axis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ function layoutable(::Type{Axis}, fig_or_scene::Union{Figure, Scene}; bbox = not

# TODO: replace with mesh, however, CairoMakie needs a poly path for this signature
# so it doesn't rasterize the scene
background = poly!(topscene, scenearea, color = backgroundcolor, inspectable = false, shading = false, strokecolor = :transparent)
translate!(background, 0, 0, -100)
decorations[:background] = background
# background = poly!(topscene, scenearea, color = backgroundcolor, inspectable = false, shading = false, strokecolor = :transparent)
# translate!(background, 0, 0, -100)
# decorations[:background] = background

block_limit_linking = Observable(false)

Expand Down
1 change: 0 additions & 1 deletion src/makielayout/layoutables/axis3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ function projectionmatrix(viewmatrix, limits, eyepos, radius, azim, elev, angle,

if viewmode in (:fitzoom, :stretch)
points = decompose(Point3f, limits)
# @show points
projpoints = Ref(pm * viewmatrix) .* to_ndim.(Point4f, points, 1)

maxx = maximum(x -> abs(x[1] / x[4]), projpoints)
Expand Down
3 changes: 1 addition & 2 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ function RectangleZoom(f::Function, ax::Axis; kw...)
selection_vertices = lift(_selection_vertices, ax.finallimits, r.rectnode)
# manually specify correct faces for a rectangle with a rectangle hole inside
faces = [1 2 5; 5 2 6; 2 3 6; 6 3 7; 3 4 7; 7 4 8; 4 1 8; 8 1 5]
# fxaa false seems necessary for correct transparency
mesh = mesh!(ax.scene, selection_vertices, faces, color = (:black, 0.2), shading = false,
fxaa = false, inspectable = false, visible=r.active, transparency=true)
inspectable = false, visible=r.active, transparency=true)
# translate forward so selection mesh and frame are never behind data
translate!(mesh, 0, 0, 100)
return r
Expand Down