I’ve been tinkering with the GL Height Field plugin from the Developer/Examples/Quartz Composer/Plugins folder. More specifically, I’ve been attempting to create an image to bend the height field around into a sphere shape. The idea is to them mix this image with another image, to produce radially-distorted meshes. I’ve done this before with GLSL-vertex-shader-based vertex displacement mapping, but I was intrigued to know if this method would be faster.

Here’s the CIFilter code to produce the heightmap:

/* Generates spherical and planar displacement maps for VBO-based 3D heightfield. toneburst 2008 https://machinesdontcare.wordpress.com */ //////////////////////////// //// CONSTANTS //// //////////////////////////// const float PI = 3.14159265359; const float TWOPI = 6.28318530718; //////////////////////////// //// MAIN LOOP //// //////////////////////////// kernel vec4 heightMapSphere(sampler Image) { // Normalised pixel coordinates vec2 xyNorm = samplerCoord(Image) / samplerSize(Image); //////////////////////////// //// Spherical Map //// //////////////////////////// // Parametric UV coordinates float u = xyNorm.x * PI; float v = xyNorm.y * TWOPI; // Spherical map values vec3 spherical; spherical.r = cos(v) * sin(u); spherical.g = sin(v) * sin(u); spherical.b = cos(u); // Scale and offset color channels to 0.0 > 1.0 spherical = spherical * 0.5 + 0.5; // Output displacement map // RGB channels mapped to height field XYZ axes return vec4(spherical,1.0); }

The CIFilter should be fed with an image of 256x256px. The content of the image is ignored, and it will produce an output looking like this (click for full-size version):

Pipe this image into the GL Height Field plugin, and you’ll get a spherical mesh. That’s not very exciting on its own, but if you then take this image, and combine it with another image (say a live video input) before sending it to the Height Field patch, you’ll get all sorts of nice mesh distortions.

Incidentally, this filter uses exactly the same formula as the one to produce a spherical mesh from a flat GLSL Grid using GLSL Shader patch vertex shader code. It’s a basic parametric surface equation. The only difference is we’re producing variations on the levels across the RGB channels, rather than variation in the positions of vertices in 3D space. Because we’re dealing with color channels, which are clamped to the range 0.0 > 1.0, we have to scale and offset the results of our parametric equation so it fits into that range (results are initially in the range -1.0 > 1.0). This is what confused me yesterday. Big thanks once again to -NiCo- from the OpenGL GLSL forum for pointing out where I was going wrong.

## 0 Responses to “Spherical Heightmap for GL Height Field Plugin”