Skip to main content
gamedev.net
🔒 Locked

clamping values

Started by Endar 0 replies 700+ views
Original Post
Endar
Endar
I'm looking at dot3 bumpmapping and at one point during the info I'm looking at it says that an integer between [0,255] must be clamped to [-1.0,1.0].

float clamp(unsigned char i)
{
	float temp;

	temp = (i / 255.0f);	// first, clamp the value to [0,1]
	temp *= 2;		// multiply by 2
	temp -= 1.0f;		// subtract 1

	return temp;
}
This is what I did, and it initially looks to be fine (works with values 0,65,127,191,255). I was wondering if there was a generalised forumla for clamping a value between [a,b] to [c,d]. I took a look on mathworld, but I'm not really sure what to search for.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
deffer
deffer
[a,b] ->f [c,d]
f(x) = (x-a)       // move [a,b] to [0,b-a] so we may scale by means of multiplication     * (d-c)/(b-a) // scale [0,b-a] to [0,d-c]     + c           // move from [0,d-c] to [c,d]


I'm not sure word "clamp" fits here. It's more like "remap".
Unless you're changing data type in the meantime, so it's clamped to some min/max automatically.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.