07
Oct
09

OpenCL Radial Displace Code

Here’s the code for the below.

const float PI = 3.14159265359;
const float TWOPI = 6.28318530718;

float4 makeSphere(float4 pos, float radius) {
	
	float u = pos.x * TWOPI * 1.01;
	float v = pos.y * PI;
	
	float4 sphere;
	sphere.x = radius * cos(u) * sin(v);
	sphere.y = radius * sin(u) * sin(v);
	sphere.z = radius * cos(v);
	sphere.w = 1.0;
	
	return sphere;
}


__kernel void tb_vdm_sphere(__rd image2d_t BaseImg, float Width, float Height, float Displacement,
						__global float4 *Mesh, __global float4 *Colors)
{
	// Get values for current iteration
	int		tid_x = get_global_id(0),
			tid_y = get_global_id(1),
			indx = tid_y * get_global_size(0) + tid_x;
	
	// Init variables		
	float4 color, vertex;
	//int2   pos = (int2)(tid_x, tid_y);
	// Floating-point position in 0 > 1 range
	float4 posFloat = (float4)((float)tid_x / Width, (float)tid_y / Height, 0.0, 1.0); 
	
	// Sample colour from base image
	sampler_t smp = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
	color = read_imagef(BaseImg, smp, (int2)(tid_x, tid_y));
	
	// Write color at index in Colors output array
	Colors[indx] = (float4)(1.0,1.0,1.0,1.0);
	
	// Displacement is determined by the displacement amount * the "length" of the color vector (ie approximate brightness)
	/*vertex =	(float4)
				((float)tid_x / get_global_size(0) - 0.5,
				1.0 - (float)tid_y / get_global_size(1) - 0.5,
				Displacement * length(color.xyzw), 1.0);*/

	
	// Write mesh value at index of Mesh output array
	Mesh[indx] = makeSphere(posFloat, Displacement * length(color.xyzw));
}

It’s just a doctored version of the OpenCL Extruded Text developer example (which is very nice, incidentally).

OpenCL is another ‘C-like’ language, very like GLSL, in a lot of ways. There are some slight syntax differences which have been tripping me up just while working on this little project. I’m sure I’ll get used to it though. There’s so much you can do with it, I’m sure I’ll be using it a lot. It’s just a shame relatively few Mac models/GPUs currently support it. OpenCL is actually one of the reasons I decided to upgrade my ageing laptop this Summer. I’m glad I did, now.

Advertisements

2 Responses to “OpenCL Radial Displace Code”


  1. 2 toneburst
    October 7, 2009 at 5:04 pm

    No prob. Happy crashing! 😉

    a|x


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


Twitter

October 2009
M T W T F S S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Links

Blog Stats

  • 475,892 hits

%d bloggers like this: