Skip to content

Commit

Permalink
Updates for GeometryBasics (#22)
Browse files Browse the repository at this point in the history
* rely on decompose interface

* allow uvs to be nothing/undefined

* fix Shape constructor for AbstractGeometry

* remove type constraint again

---------

Co-authored-by: Simon <[email protected]>
  • Loading branch information
ffreyer and SimonDanisch authored Oct 10, 2024
1 parent b9cc14a commit ed44dd6
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/highlevel-api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ GeometryTypes (Meshes and geometry primitives alike).
"""
function Shape(context::Context, meshlike; kw...)
m = uv_normal_mesh(meshlike; kw...)
v, n, fs, uv = decompose(Point3f, m), normals(m), faces(m), texturecoordinates(m)
if isnothing(n)
n = normals(v, fs)
end
return Shape(context, v, n, fs, uv)
return Shape(context, decompose(Point3f, m), decompose_normals(m), faces(m), decompose_uv(m))
end

#=
Expand All @@ -297,23 +293,37 @@ https://gist.github.com/SimonDanisch/475064ae102141554f65e926f3070630
=#
function Shape(context::Context, vertices, normals, faces, uvs)
@assert length(vertices) == length(normals)
@assert length(vertices) == length(uvs)
@assert isnothing(uvs) || (length(vertices) == length(uvs))

vraw = decompose(Point3f, vertices)
@assert eltype(vraw) == Point3f

nraw = decompose(Vec3f, normals)
uvraw = map(uv -> Vec2f(1 - uv[2], 1 - uv[1]), uvs)
@assert eltype(nraw) == Vec3f

if isnothing(uvs)
uvraw = C_NULL
uvlength = 0
uvbytesize = 0
else
uvraw = map(uv -> Vec2f(1 - uv[2], 1 - uv[1]), uvs)
@assert eltype(uvraw) == Vec2f
uvlength = length(uvs)
uvbytesize = sizeof(Vec2f)
end

f = decompose(TriangleFace{OffsetInteger{-1,rpr_int}}, faces)
iraw = collect(reinterpret(rpr_int, f))
facelens = fill(rpr_int(3), length(faces))

@assert eltype(vraw) == Point3f
@assert eltype(nraw) == Vec3f
@assert eltype(uvraw) == Vec2f

foreach(i -> checkbounds(vertices, i + 1), iraw)
rpr_mesh = rprContextCreateMesh(context, vraw, length(vertices), sizeof(Point3f), nraw, length(normals),
sizeof(Vec3f), uvraw, length(uvs), sizeof(Vec2f), iraw, sizeof(rpr_int),
iraw, sizeof(rpr_int), iraw, sizeof(rpr_int), facelens, length(faces))
rpr_mesh = rprContextCreateMesh(context,
vraw, length(vertices), sizeof(Point3f),
nraw, length(normals), sizeof(Vec3f),
uvraw, uvlength, uvbytesize,
iraw, sizeof(rpr_int), iraw, sizeof(rpr_int), iraw, sizeof(rpr_int), facelens, length(faces)
)

jl_references = (vraw, nraw, uvraw, iraw, facelens)
shape = Shape(rpr_mesh, context, jl_references)
Expand Down

0 comments on commit ed44dd6

Please sign in to comment.