Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
added rounded cube mesh, not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Nov 29, 2015
1 parent cf1cca1 commit b193e85
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/primitives.jl
Expand Up @@ -89,3 +89,55 @@ function call{MT <: AbstractMesh}(::Type{MT}, s::Sphere, facets=12)
end
MT(vertices, indexes)
end


signedpower(v, n) = sign(v)*abs(v)^n

immutable RoundedCube{T}
power::T
end
export RoundedCube
function call{M <: AbstractMesh}(::Type{M}, c::RoundedCube, N=128)
(N < 3) && error("Usage: $N nres rpower\n")
# Create vertices
L = (N+1)*(N÷2+1)
has_texturecoordinates = true
p = Array(vertextype(M), L)
texture_coords = Array(UV{Float32}, L)
faces = Array(facetype(M), N*2)
NH = N÷2
# Pole is along the z axis
for j=0:NH
for i=0:N
index = j * (N+1) + i
theta = i * 2 * pi / N
phi = -0.5*pi + pi * j / NH
# Unit sphere, power determines roundness
x,y,z = (
signedpower(cos(phi), c.power) * signedpower(cos(theta), c.power),
signedpower(cos(phi), c.power) * signedpower(sin(theta), c.power),
signedpower(sin(phi), c.power)
)
if has_texturecoordinates
u = abs(atan2(y,x) / (2*pi))
texture_coords[index+1] = (u, 0.5 + atan2(z, sqrt(x*x+y*y)) / pi)
end
# Seams
if j == 0; x,y,z = 1,1,2; end
if j == NH; x,y,z = 1,1,0; end
if i == N; x,y = p[(j*(N+1)+i-N)+1]; end
p[index+1] = (1-x,1-y,1-z)
end
end
for j=0:(NH-1)
for i=0:(N-1)
i1 = j * (N+1) + i
i2 = j * (N+1) + (i + 1)
i3 = (j+1) * (N+1) + (i + 1)
i4 = (j+1) * (N+1) + i
faces[i*2+1] = (i1,i3,i4)
faces[i*2+2] = (i1,i2,i3)
end
end
M(p, faces)
end

0 comments on commit b193e85

Please sign in to comment.