Wireframes
A lightweight 3D graphics rendering engine in Clojure & ClojureScript.
Install / Use
/learn @rm-hull/WireframesREADME
Wireframes 
A lightweight 3D rendering engine in Clojure & ClojureScript.

Adapted and extended from a javascript demo (originally by Kragen Javier Sitaker, see references below) into a Clojure/ClojureScript library (that renders to SVG, an HTML5 Canvas or a Graphics2D object - depending on the runtime environment, obviously).
This started out as a experiment to plot Lorenz attractors in 3D space, but it turns out to be a really simple way to programmatically generate three dimensional geometric shapes - basically a programmable CAD system - I'm pretty sure that AutoCAD could already do this (and much quicker too), but where I would really like to go with this is:
- build up a robust and idiomatic Clojure API for generating shapes
- implement a wide variety of output renderers - potentially even a GLSL cross-compiler and certainly a gcode output formatter suitable for 3D printers
- maintain 100% compatibility with ClojureScript
As this is a software renderer, please don't expect OpenGL levels of performance (or until WebGL and OpenGL renderers have been written).
A variety of (in-progress) generated shapes and demos:
- A gallery of some shapes and imports from common 3D formats,
- An animated torus tumbling in 3D space, and other shapes,
- A taxonomy of 3D polyhedra.
Pre-requisites
You will need Leiningen 2.3.4 or above installed.
Building
To build and install the library locally, run:
$ lein test
$ lein install
To re-generate the examples in the doc/gallery directory, run:
$ lein test :examples
Including in your project
There is an 'alpha-quality' version hosted at Clojars. For leiningen include a dependency:
[rm-hull/wireframes "0.0.1-SNAPSHOT"]
For maven-based projects, add the following to your pom.xml:
<dependency>
<groupId>rm-hull</groupId>
<artifactId>wireframes</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Basic Usage
See the API Documentation.
Creating shapes
There are some drawing primitives in the wireframes.shapes namespace to create
objects such as lines, bezier curves, polygons, circles, cuboids, cylinders and torus shapes.
The basic mechanism for building shapes is extrusion. For example, to create a cube, start with a square polygon in the X-Y plane, and extrude that shape into a line along the Z-axis, as per the following code:
(use 'wireframes.shapes.primitives)
(->
(make-polygon
(make-point 0 0 0)
(make-point 0 1 0)
(make-point 1 1 0)
(make-point 1 0 0))
(extrude (translate 0 0 1) 1))
Drawing shapes
There are various software renderers in the wireframes.renderer namespace for
output to java images, SVG or HTML5 canvas (the availability of which combinations
depends on whether you are executing the code in Clojure or ClojureScript).
Clojure
For example, in Clojure, to generate a torus angled in the Y- and Z-axles, written out to a PNG file:
(use 'wireframes.shapes.curved-solids)
(use 'wireframes.transform)
(use 'wireframes.renderer.bitmap)
(use 'wireframes.renderer.color)
(write-png
(->img
#(draw-solid
{:focal-length 3
:color-fn (wireframe :white :transparent)
:style :transparent
:transform (combine
(rotate :z (degrees->radians 65))
(rotate :y (degrees->radians -30))
(translate 0 0 16))
:shape (make-torus 1 3 60 60)} %)
[400 400])
"torus-65.png")
Produces:

Surfaces and other primitives
MATLAB-style function plots can be generated thus:
(use 'wireframes.shapes.primitives)
(use 'wireframes.transform)
(use 'wireframes.renderer.bitmap)
(use 'wireframes.renderer.lighting)
(use 'wireframes.renderer.color)
(defn sqr [x]
(* x x))
(defn sinc
"Unnormalized/cardinal sine function"
[x] (if (zero? x)
1.0
(/ (Math/sin x) x)))
(defn sombrero [x y]
(* 15 (sinc (Math/sqrt (+ (sqr x) (sqr y ))))))
(write-png
(->img
#(draw-solid
{:focal-length 30
:color-fn (comp
black-edge ; [1]
(positional-lighting-decorator ; [2]
default-position
(spectral-z -6.5 15))) ; [3]
:style :shaded
:transform (combine
(rotate :z (degrees->radians 15))
(rotate :x (degrees->radians 135))
(scale 0.05)
(translate 0 0 10))
:shape (make-surface ; [4]
(range -22 22 0.25)
(range -22 22 0.25)
sombrero)} %) ; [5]
[600 600])
"sinc3D.png")
Results in:

Program Notes
TODO
Loading common 3D shape files
The defacto/clichéd Utah teapot (or any patch/vertex 3D file) can be loaded in with the following code sample:
(use 'wireframes.shapes.patch-loader)
(use 'wireframes.renderer.bitmap)
(use 'wireframes.transform)
(write-png
(->img
#(draw-solid
{:focal-length 10
:fill-color Color/WHITE
:style :translucent
:transform (combine
(rotate :z (degrees->radians 35))
(rotate :x (degrees->radians -70))
(translate 0 -1 40))
:shape (load-shape "resources/newell-teapot/teapot")} %)
[1000 900])
"teapot.png")
which generates:

The following file formats support loading:
- Patch files in the
wireframes.shapes.patch-loadernamespace, - Wavefront .obj files in the
wireframes.shapes.wavefront-loadernamespace, - Stereolithography .stl files in the
wireframes.shapes.stl-loadernamespace
Saving 3D shapes to STL-format files
Once generated or loaded by whatever means, a shape may be persisted in STL format with the following code sample:
(use 'wireframes.shapes.stl-loader)
(use 'wireframes.shapes.curved-solids)
(save-shape
(make-torus 1 3 60 60)
"a description which will get truncated to 80 chars"
"doc/gallery/torus.stl")
This specific torus, the teapot and a wineglass can then be viewed using the GitHub 3D viewer.
TODO
- Investigate using primitive arrays (see array branch)
- Use/implement a vector library
- Geometric extension with Minkowski addition (see http://projecteuler.net/problem=228)
- ~~Efficiently calculate polygons on extruded shapes~~
- ~~Rewrite/rename wireframes.transform/concat - unroll loops for performance~~
- ~~Complete Bezier patch code~~
- ~~Rectilinear perspective mapping~~
- ~~Stitch adjacent surface panels together~~
- Renderer implementations:
- ~~Graphics2D~~
- ~~SVG~~
- ~~Canvas~~
- WebGL
- OpenGL
- ~~Simple flat shading / lighting~~
- ~~Configurable lighting position(s)~~
- ~~Colours~~
- Gourand shading
- Texture mapping
- ~~Backface removal~~
- Z-buffer
- Polygon inflation
- ~~Compute shape bounds~~
- ~~Center shape at origin function~~
- gcode generation for 3D printers
- ~~Support loading from & saving to .stl files~~
- ~~Support loading from Wavefront .obj files~~
- ~~Deprecate
:lines- no longer used except in platonic solids~~ - Improve documentation
- Examples
- ~~MATLAB style surface functions~~
- ~~Integrate Inkspot & custom vertex/fragment shader~~
- Constructive Solid Geometry (CSG) boolean operators
Known Bugs
See open issues.
References
- http://lists.canonical.org/pipermail/kragen-hacks/2007-February/000448.html
- http://www.sjbaker.org/wiki/index.php?title=The_History_of_The_Teapot
- http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-11-rendering-the-teapot-bezier-surfaces/b-zier-surface/
- https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
- https://www.mathdisk.com/pub/safi/worksheets/Perspective_Projection
- http://www.cs.berkeley.edu/~jrs/mesh/
- http://www.victoriakirst.com/beziertool/
- https://en.wikipedia.org/wiki/Wavefront_.obj_file
- https://en.wikipedia.org/wiki/STL_(file_format)
- https://github.com/colah/ImplicitCAD
- http://evanw.github.io/csg.js/
- http://www.opengl.org/wiki/Calculating_a_Surface_Normal
- http://www.cs.utah.edu/~xchen/columbia/session1/lec24/html/
- http://www.3dcadbrowser.com/3dmodels.aspx?word=star%20wars
- http://derek.troywest.com/articles/by-example-gloss/
- http://doc.cgal.org/latest/Manual/packages.html
- http://stackoverflow.com/questions/3749678/expand-fill-of-convex-polygon
- http://stackoverflow.com/questions/1109536/an-algorithm-for-inflating-deflating-offsetting-buffering-polygons
- https://github.com/tsaastam/cljs-webgl-example
- http://www.cs.arizona.edu/classes/cs437/fall11/ch3d.prn.pdf
- https://github.com/rm-hull/project-euler/blob/master/src/util/geometry.clj#L55
Contr
Related Skills
node-connect
339.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
