Jump to content
  • Advertisement
Sign in to follow this  

clamping values

This topic is 4341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
[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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!