# Triangulated surfaces in GR: ray-tracing

**URL:** https://discourse.julialang.org/t/triangulated-surfaces-in-gr-ray-tracing/6348
**Category:** Visualization
**Tags:** plotting
**Created:** [October 9, 2017, 1:53pm UTC](https://discourse.julialang.org/t/triangulated-surfaces-in-gr-ray-tracing/6348 "2017-10-09T13:53:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)
#### Post date: [October 9, 2017, 1:53pm UTC](https://discourse.julialang.org/t/triangulated-surfaces-in-gr-ray-tracing/6348/1 "2017-10-09T13:53:05Z")

</div>

I’m looking to use GR along with Interact.jl for a 3D visualization in IJulia, and I noticed that `GR.trisurface` seems to have some difficulties with overlapping polygons ([as matplotlib does](https://matplotlib.org/2.0.2/mpl_toolkits/mplot3d/faq.html?highlight=mayavi)).

Is there currently a way to achieve a ray-traced surface plot in Julia? I have been using Asymptote to render such figures statically, and it does a terrific job, but I would prefer a Julia-based solution.

```julia
import GR
GR.inline("png")
f(x,y) = x^2+y^2 < 1e-3 ? 0 : x*y/(x^2+y^2)
rad = 1
rv = linspace(0,rad,5)
θv = linspace(0,2π,12)
xv = [r*cos(θ) for r=rv, θ=θv]
yv = [r*sin(θ) for r=rv, θ=θv]
zv = [f(r*cos(θ),r*sin(θ)) for r=rv, θ=θv];
xv, yv, zv = map(a->vcat(a...),(xv,yv,zv))
GR.setviewport(0.1, 0.95, 0.1, 0.95)
GR.setwindow(-2, 2, -2, 2)
GR.setspace(-2, 2, 85, 35)
GR.setmarkersize(0.1)
GR.trisurface(xv,yv,zv)
GR.polyline3d([0,0],[0,0],[0,2])
GR.polyline3d([0,0],[0,2],[0,0])
GR.polyline3d([0,2],[0,0],[0,0])
GR.show()

```

 ![trisurf2](https://global.discourse-cdn.com/julialang/original/3X/f/f/ff94c6870bdc49f22afb2f74bcd94749135eba6e.png)

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [October 9, 2017, 2:41pm UTC](https://discourse.julialang.org/t/triangulated-surfaces-in-gr-ray-tracing/6348/2 "2017-10-09T14:41:00Z")

</div>

With GLVisualize you can do something like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/7/d749a6a41779d9bee382ec997833fa05b85bd004.jpeg)

```julia
using GLVisualize, GeometryTypes, Colors, GLAbstraction
f(x,y) = x^2+y^2 < 1e-3 ? 0 : x*y/(x^2+y^2)
rad = 1
rv = linspace(0,rad,5)
θv = linspace(0,2π,12)
xv = Float32[r*cos(θ) for r=rv, θ=θv]
yv = Float32[r*sin(θ) for r=rv, θ=θv]
zv = Float32[f(r*cos(θ),r*sin(θ)) for r = rv, θ = θv]

w = glscreen(); @async renderloop(w)
x = visualize(
    (xv, yv, zv), :surface,
    color_map = GLVisualize.default(Vector{RGBA}, Style(:default)),
    wireframe = true, color = nothing, color_norm = Vec2f0(-0.4, 0.4),
)
x2 = copy(x).children[].children[]
x2[:model] = translationmatrix(Vec3f0(2, 0, 0)) * rotationmatrix_x(0.5f0*pi)
x3 = copy(x).children[].children[]
x3[:model] = translationmatrix(Vec3f0(4, 0, 0)) * rotationmatrix_z(0.5f0*pi)

_view(x)
_view(x2)
_view(x3)

```

If you manage to create a dense volume of your function, you could also ray trace that with GLVisualize.  
Or if you can create the triangles (not the one being on a surface), you could also visualize those.

The current GLVisualize API is a bit messy as you can see, but I’m working on a much nicer API right now and will release it soon ([MakiE.jl](https://github.com/SimonDanisch/MakiE.jl))…
