From b193e85a38e20ed65f6b4914c0be60eadec2cf6e Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Sun, 29 Nov 2015 14:36:59 +0100 Subject: [PATCH] added rounded cube mesh, not working yet --- src/primitives.jl | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/primitives.jl b/src/primitives.jl index 50e1516..942c393 100644 --- a/src/primitives.jl +++ b/src/primitives.jl @@ -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