Archive for the ‘tutorial’ tag
Android Maps and Routing
Very quick one here.
I’ve been trying to mapping, especially routing, working on an android application I’m developing. I will save you a lot of trouble and tell you to use the inbuilt Google services.
In fact, I found a gem of a post at http://smartandroidians.blogspot.co.uk/2010/06/showing-route-through-google-map-in.html which shows you how to open an intent for routing. I’ll add in some handy extras.
// Uri to open - this will work in your browser and is actually the uri that maps generates itself
Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=" + userLocOverlayItem.routableAddress() +
"&daddr=" + destinationOverlayItem.routableAddress() +
"&dirflg=w");
// Create an intent to view the URI
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// [Bonus] Set the intent class as a map, so you can avoid an annoying pop up asking whether to
// open in the browser or maps
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
// Start the activity and close your launching activity
startActivity(intent);
finish();
I added a couple of things, as my application wanted walking directions and there was an annoying pop up asking what to open the URI with.
First, adding ...&dirflg=w to the URI forced walking directions.
Second, setting the intent’s class name as in the snippet set a hint to android for what to open the URI with [2].
This isn’t new, but it was hard to track down something clear via searching, so I’m consolidating.
References
[1] http://smartandroidians.blogspot.co.uk/2010/06/showing-route-through-google-map-in.html
[2] http://stackoverflow.com/questions/8132069/calling-google-map-using-intent?lq=1
SlimDX and Shaders – Constant Buffers
Setting up
Having played with a few GLSL shaders in C++, I thought that moving to a DirectX/HLSL solution sould be fairly simple.
Creating the most simple pixel shader was easy enough, and SlimDX is a decent wrapper for DX in C# – so after an hour or so I had the standard working triangle floating in space. I wanted to go further (obviously), and having spent the last few days delving into the demoscene and finding the most amazing site for pixelshader techniques (http://www.iquilezles.org/www/index.htm) I wanted to have a shader to work with (x,y) coordinates that were between (-1,1) rather than absolute screen coordinates.
This requires passing the current render area width and height to the shader to normalise with. This is done using Constant Buffers and on the shader side looks like:
cbuffer ConstBuffer : register(c0)
{
float2 resolution;
}
// Simple vertex shader
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}
// Pixel shader
float4 PShader(float4 position : SV_POSITION) : SV_Target
{
// Get normalised (x,y)
float2 p = -1 + 2 * (position.xy/resolution);
// Spikes in the corner
float c = abs(p.x*p.y);
return float4(c,c,c,1.0f);
}
To get the resolution variable into the register for the shader to use, you must create a ConstantBuffer in the program code and assign it to the shade. The Constant Buffer must be a size which is divisible by 16, so if your data is too small, just put it in a bigger buffer.
// Create data stream, we only need 8 bytes, but round up to 16
var resolution = new DataStream(16, true, true);
// Fill the stream with width/height info - I'm using a renderform
resolution.Write(new Vector2(form.ClientSize.Width, form.ClientSize.Height));
// Rewind the stream
resolution.Position = 0;
// Create and bind a buffer
context.PixelShader.SetConstantBuffer(new Buffer(device, //Device
resolution, //Stream
16, // Size
// Flags
ResourceUsage.Dynamic,
BindFlags.ConstantBuffer,
CpuAccessFlags.Write,
ResourceOptionFlags.None,
4),
0); // Register
This lets us run the above shader, giving us:

Bonus
Having played on shader toy, I repurposed the Deform effect for a static image.
Shader:
cbuffer ConstBuffer : register(c0)
{
float2 resolution;
}
// Simple vertex shader
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}
// Pixel shader
float4 PShader(float4 position : SV_POSITION) : SV_Target
{
// Get normalised (x,y)
float2 p = -1 + 2 * (position.xy/resolution);
// Deform focus
float2 m = float2(0.2f, 0.1f);
// Deform
float a1 = atan((p.y-m.y)/(p.x-m.x));
float r1 = sqrt(dot(p-m,p-m));
float a2 = atan((p.y+m.y)/(p.x+m.x));
float r2 = sqrt(dot(p+m,p+m));
float2 uv;
uv.x = 0.2 + (r1-r2)*0.25;
uv.y = sin(2.0*(a1-a2));
float w = r1*r2*0.8;
float c2 = abs(uv.x*uv.y);
float4 col = float4(c2,c2,c2,1.0f);
return float4(col.xyz/(0.1+w),1.0f);
}
Result:

Rails 3 and lighttpd
This was performed on Archlinux with lighttpd 1.4.28 and rails 3.0.3
Prerequisites
Required packages:
- lighttpd,
- fcgi,
- ruby,
- and their dependencies…
Ruby Setup
Required gems:
- fcgi,
- bundler
(if you are behind a proxy, the magic gem command is :
<code># gem install GEM -r -p "http://[PROXY_URL]:[PROXY_PORT]"</code>
)
Once you have that you need to create a “dispatch.fcgi” script to do all the rails magic. I found an example one at http://stackoverflow.com/questions/3296206/rails-3-and-fcgi .
<code>#!/usr/bin/ruby
require 'rubygems'require 'fcgi'
require_relative '../config/environment'
class Rack::PathInfoRewriter
def initialize(app)
@app = app
end
def call(env)
env.delete('SCRIPT_NAME')
parts = env['REQUEST_URI'].split('?')
env['PATH_INFO'] = parts[0]
env['QUERY_STRING'] = parts[1].to_s
@app.call(env)
end
end
Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(YOUR_APP_NAME::Application)
</code>
Running a “bundle install” from your app root will make sure all the necessary gems are available for local use. Follow these instructions and run “ruby public/dispatch.fcgi”, if you get no errors, voila!
Lighttpd Setup
Now, to set up lighttpd you need to merge this with your config:
<code>server.modules += ( "mod_fastcgi", "mod_rewrite" )
$HTTP["host"] == "localhost" {
server.document-root = "/path/to/your/app/public/"
server.dir-listing = "disable"
server.error-handler-404 = "/dispatch.fcgi"
fastcgi.server = (
".fcgi" => (
"localhost" => (
"min-procs" => 1,
"max-procs" => 1,
"socket" => "/tmp/ruby-beholder.socket",
"bin-path" => "/path/to/your/app/public/dispatch.fcgi",
"bin-environment" => ( "RAILS_ENV" => "development" )
)
)
)
}</code>
A quick “sudo /etc/rc.d/lighttpd restart” and a check of the error logs will tell you if it has worked