Screen gamma changing (WinAPI)

A forum to store posts deemed exceptionally wise and useful
Post Reply
puh
Posts: 356
Joined: Tue Aug 26, 2003 3:53 pm

Screen gamma changing (WinAPI)

Post by puh »

Hope someone find it usefull - http://www.irrlichtnx.mmdevel.de/phpBB2 ... c.php?t=26

Final solution to change gamma in Irrlicht application using Windows API (don't forget to add User32.lib (for GetDC) and Gdi32.lib (for SetDeviceGammaRamp and GetDeviceGammaRamp) into Extra libraries)

Code: Select all

bool SetGamma(float gamma) 
{ 
	WORD *ramp = new WORD[256 * 3]; 
	for (int i = 0; i < 3; i++) 
	{ 
		int min = 256 * i; 
		int max = min + 256; 
		for (int j = min; j < max; j++) 
		ramp[j] = pow((float)((j % 256) / 256.0), gamma) * 65536; 
	} 
	if (SetDeviceGammaRamp(GetDC(NULL), ramp)) return true; 
	else return false; 
} 

float GetGamma() 
{ 
	WORD *ramp = new WORD[256 * 3]; 
	GetDeviceGammaRamp(GetDC(NULL), ramp); 
	float gamma = 1.0; 
	float Csum = 0.0; 
	int Ccount = 0; 
	int min = 256; 
	int max = min + 256; 
		for (int j = min; j < max; j++) 
		{ 
			if (j != 0 && ramp[j] != 0 && ramp[j] != 65536) 
			{ 
				double B = (j % 256) / 256.0; 
				double A = ramp[j] / 65536.0; 
				float C = (float) ( log(A) / log(B) ); 
				Csum += C; 
				Ccount++; 
			} 
		} 
		gamma = Csum / Ccount; 
	return gamma; 
}
Using: just copy into your .cpp file. First call: float oldGamma = GetGamma(); before any changing and SetGamma(oldGamma); at the end of the program to restore old value.

Big thanks to zola for showing the way! God bless you! :)
Code GetGamma() partially from http://www.monkeybreadsoftware.de/realb ... 03_1.shtml
Guest

?

Post by Guest »

...and where do I find User32.lib and Gdi32.lib?
puh
Posts: 356
Joined: Tue Aug 26, 2003 3:53 pm

Post by puh »

Windows API SDK :)
Guest

=[

Post by Guest »

Doesn't work, .Net says that Gdi32.lib is full of errors, even though it came with .Net?

Really is too bad, if I could change the rgb levels I could create a actual 3D game using red/blue 3D glasses...
Post Reply