Spherical Heightmap Modulation CIFilter Code

This code goes in a CIFilter that takes as its inputs the output of the filter in the previous post (or a static image of it, which kind of works), and modulates it with the luminosity of a second image. The offsets and rescaling ensure that the centre of the radial distortion effect remains in the correct place.

Modulates HeightMap image with luminosity of ModImg input.
For creating radially-distorted spherical 3D meshes from
VBO-based heightmap.

Input images must have same dimensions!

toneburst 2008

// Constants (thanks to vade for pointing out I can declare constants using the 'const' keyword).
const vec4 lumCoeff = vec4(0.299,0.587,0.114,0.0);

////	MAIN LOOP    ////

kernel vec4 heightMapModulate(sampler HeightMap, sampler ModImg, float ModAmt)
	// Heightmap pixel
	vec4 mapPix = sample(HeightMap, samplerCoord(HeightMap));

	// Shift mapPix to -1.0 > 1.0 range
	// Required or distortion center will be incorrect
	mapPix = 2.0 * mapPix - 1.0;

	// Modulating image pixel
	vec4 modPix = sample(ModImg, samplerCoord(ModImg));

	// Luminosity of mod image pixel
	float lum = dot(modPix,lumCoeff);

	// Displacement amount
	lum = mix(1.0,lum,ModAmt);

	// Remap range to -1.0 > 1.0
	lum = 2.0 * lum - 1.0; 

	// Modulate heightmap image with mod image
	vec4 outPix = mapPix * lum;
	outPix.a = 1.0;

	// Remap outPix to 0.0 > 1.0 range
	outPix = 0.5 * outPix + 0.5;

	// Output modulated displacement map
	return outPix;

As usual, I’ve probably sacrificed efficiency for readability, and I’m sure the same thing could be done with far fewer instructions.


3 Responses to “Spherical Heightmap Modulation CIFilter Code”

  1. April 13, 2008 at 6:37 am

    You should name your variables outside of the main loop that are not uniform/varying as const, especially the luma coefficients, since for every texel/fragment you are assining a new vec4 rather than defining it once for every fragment/texel.

    oconst vec4 lumacoeff = vec4(blah);


    This also will help a bit with speed, but not much.

  2. 2 toneburst
    April 13, 2008 at 12:56 pm

    Good point vade.

    don’t think you can define constants in CIKernel code though (one of themany annoying limitations).
    It is possible to define the vec4 outside the main loop though, which might have the same effect.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

April 2008
« Mar   May »


Blog Stats

  • 476,450 hits

%d bloggers like this: