Problems with GLMakie

I’m trying to learn how to use GLMakie having had a lot of success with CairoMakie. But I immediately ran into issues. VSCode is my main text editor, and whereas I’ve never had an issue with CairoMakie, I can’t say the same here. This example comes from the mesh section on the Beautiful Makie website, but I cannot run it. From the closeall() function to the basic geometry types, I keep getting that the functions are undefined. Any help would be greatly appreciated.

using GLMakie, Random, Colors, LinearAlgebra
using GeometryBasics: Cylinder, Pyramid
using Makie
import GeometryBasics
GLMakie.closeall() # close any open screen

Random.seed!(3)

cyl = Cylinder(Point{3, Float64}(1,2,3), Point{3, Float64}(2,3,4), 1.0)
pyr = Pyramid(Point3f(0), 1f0, 1f0)
rectmesh = Rect3(Point3f(-0.5), Vec3f(1))
rectthin = Rect3(Point3f(-1), Vec3f(2,2,0.25))
sphere = Sphere(Point3f(-0.5), 1)
Cone(; quality = 10) = merge([
    Makie._circle(Point3f(0), 0.5f0, Vec3f(0,0,-1), quality),
    Makie._mantle(Point3f(0), Point3f(0,0,1), 0.5f0, 0f0, quality)])
cone = Cone()
rectMesh = GeometryBasics.mesh(rectmesh)
rectThin = GeometryBasics.mesh(rectthin)
cyL = GeometryBasics.mesh(cyl)

cmap = resample_cmap(:Spectral_11, 3*length(rectMesh.position))
colors1 = [cmap[i] for i in 1:3*length(rectMesh.position)]
colors2 = repeat([RGBA(rand(4)...) for v in rectThin.position], 3)
colors3 = repeat([norm(v) for v in cyL.position], 2)[1:62]
markers = [sphere, rectmesh, cyl, pyr, cone]

with_theme(theme_dark()) do
    fig = Figure(size = (1200,800))
    axs = [Axis3(fig[i,j]; aspect = :data, perspectiveness = 0.5)
        for j in 1:3, i in 1:2]
    mesh!(axs[1], sphere, color = :white)
    mesh!(axs[2], rectmesh, color = colors1)
    mesh!(axs[3], pyr; color = (:dodgerblue, 0.85))
    wireframe!(axs[3], pyr; color = :grey90)
    mesh!(axs[4], cyl; color = colors3,
        colormap = :diverging_tritanopic_cwr_75_98_c20_n256)
    mesh!(axs[5], cone; transparency = true)
    wireframe!(axs[5], cone; color = :grey90, linewidth = 0.5)
    mesh!(axs[6], rectthin; color = colors2, shading = NoShading)
    [meshscatter!(axs[6], Point3f(1.5rand(3) .- 0.5); marker = markers[i],
        markersize = 0.25) for i in 1:5]
    fig
end

Makie._circle and Makie._mantle are the culprits. I’m not sure what, if anything, was supposed to replace them. The figure can be reproduced with:

using GeometryBasics
using LinearAlgebra

function Cone(; quality = 10)
    # Create base circle points
    base_radius = 0.5f0
    base_points = Point3f[]
    for i in 0:quality-1
        angle = 2π * i / quality
        x = base_radius * cos(angle)
        y = base_radius * sin(angle)
        push!(base_points, Point3f(x, y, 0))
    end
    
    # Apex point
    apex = Point3f(0, 0, 1)
    
    # Create faces connecting base to apex
    faces = TriangleFace{Int}[]
    for i in 1:quality
        next_i = (i % quality) + 1
        # Triangle: base point i, base point next, apex
        push!(faces, TriangleFace(i, next_i, quality + 1))
    end
    
    # Base circle face
    base_face_indices = collect(1:quality)
    for i in 2:quality-1
        push!(faces, TriangleFace(1, i, i + 1))
    end
    
    # Combine all points
    all_points = vcat(base_points, [apex])
    
    return GeometryBasics.Mesh(all_points, faces)
end

3 Likes

That tidy bit of code goes to replace the Cone function, right? I run into issues before this juncture. I should point out that when I defined the function in its own script and ran it, it seemed to compile fine. No errors were thrown and the function appeared defined with one function. Here’s what I’m seeing on my end:

Yeah, it was supposed to be a drop-in. In re-running this, I ran into a namespace conflict with Cone, so I’ve changed it to Kone. I also had to import TriangleFact this time. Here’s the whole script:

using GLMakie, Random, Colors, LinearAlgebra
using GeometryBasics: Cylinder, Pyramid, TriangleFace
using Makie
import GeometryBasics
GLMakie.closeall() # close any open screen

Random.seed!(3)

cyl = Cylinder(Point{3, Float64}(1,2,3), Point{3, Float64}(2,3,4), 1.0)
pyr = Pyramid(Point3f(0), 1f0, 1f0)
rectmesh = Rect3(Point3f(-0.5), Vec3f(1))
rectthin = Rect3(Point3f(-1), Vec3f(2,2,0.25))
sphere = Sphere(Point3f(-0.5), 1)
function Kone(; quality = 10)
    # Create base circle points
    base_radius = 0.5f0
    base_points = Point3f[]
    for i in 0:quality-1
        angle = 2π * i / quality
        x = base_radius * cos(angle)
        y = base_radius * sin(angle)
        push!(base_points, Point3f(x, y, 0))
    end
    
    # Apex point
    apex = Point3f(0, 0, 1)
    
    # Create faces connecting base to apex
    faces = TriangleFace{Int}[]
    for i in 1:quality
        next_i = (i % quality) + 1
        # Triangle: base point i, base point next, apex
        push!(faces, TriangleFace(i, next_i, quality + 1))
    end
    
    # Base circle face
    base_face_indices = collect(1:quality)
    for i in 2:quality-1
        push!(faces, TriangleFace(1, i, i + 1))
    end
    
    # Combine all points
    all_points = vcat(base_points, [apex])
    
    return GeometryBasics.Mesh(all_points, faces)
end

cone = Kone()
rectMesh = GeometryBasics.mesh(rectmesh)
rectThin = GeometryBasics.mesh(rectthin)
cyL = GeometryBasics.mesh(cyl)

cmap = resample_cmap(:Spectral_11, 3*length(rectMesh.position))
colors1 = [cmap[i] for i in 1:3*length(rectMesh.position)]
colors2 = repeat([RGBA(rand(4)...) for v in rectThin.position], 3)
colors3 = repeat([norm(v) for v in cyL.position], 2)[1:62]
markers = [sphere, rectmesh, cyl, pyr, cone]

with_theme(theme_dark()) do
    fig = Figure(size = (1200,800))
    axs = [Axis3(fig[i,j]; aspect = :data, perspectiveness = 0.5)
        for j in 1:3, i in 1:2]
    mesh!(axs[1], sphere, color = :white)
    mesh!(axs[2], rectmesh, color = colors1)
    mesh!(axs[3], pyr; color = (:dodgerblue, 0.85))
    wireframe!(axs[3], pyr; color = :grey90)
    mesh!(axs[4], cyl; color = colors3,
        colormap = :diverging_tritanopic_cwr_75_98_c20_n256)
    mesh!(axs[5], cone; transparency = true)
    wireframe!(axs[5], cone; color = :grey90, linewidth = 0.5)
    mesh!(axs[6], rectthin; color = colors2, shading = NoShading)
    [meshscatter!(axs[6], Point3f(1.5rand(3) .- 0.5); marker = markers[i],
        markersize = 0.25) for i in 1:5]
    fig
end

2 Likes

In GeometryBasics.jl v0.5 (the newest version) there is a native Cone type:

which I’m pretty sure will just work as a replacement.

cc @lazarusA

2 Likes

yeah, not sure about the new Cone from Geometrics, I do like to control the level of quality, for now I will update the script with @technocrat suggestions.

You can control quality by wrapping primitives in Tessellation(primitive, quality) in GeometryBasics. GeometryBasics.mesh(Tessellation(Cone(...), 2 * quality)) should be equivalent to Kone(; quality). (The 2x quality is because Cylinder equates quality with the total number of vertices and Cone is written to match up with the same quality setting.)

1 Like