Re: GL Gamma Correction
Posted: Tue May 07, 2013 12:00 am
gl_brightness and gl_contrast are supposed to exist in fte, but whether they're identical to the gamma+contrast cvars is a different matter. If it ain't exact, it ain't gamma. 
Reckless, I don't think that does real gamma. But if It really does real gamma, feel free to smack me upside the head and point out how it does.reckless wrote:Hmm an interresting non shader based gamma function i have from one of mh's older engines.
uses opengl's combine extention so will not Work for the software client.Code: Select all
/* =================== SCR_SetBrightness Enables setting of brightness without having to do any mondo fancy shite. It's assumed that we're in the 2D view for this... Basically, what it does is multiply framebuffer colours by an incoming constant between 0 and 2 =================== */ void SCR_SetBrightness (float brightfactor) { // divide by 2 cos the blendfunc will sum src and dst const GLfloat brightblendcolour[4] = {0, 0, 0, 0.5f * brightfactor}; const GLfloat constantwhite[4] = {1, 1, 1, 1}; // don't trust == with floats, don;t bother if it's 1 cos it does nothing to the end result!!! if (brightfactor > 0.99 && brightfactor < 1.01) { return; } glColor4fv (constantwhite); glEnable (GL_BLEND); glDisable (GL_ALPHA_TEST); glBlendFunc (GL_DST_COLOR, GL_SRC_COLOR); // combine hack... // this is weird cos it uses a texture but actually doesn't - the parameters of the // combiner function only use the incoming fragment colour and a constant colour... // you could actually bind any texture you care to mention and get the very same result... // i've decided not to bind any at all... glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); glTexEnvf (GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT); glTexEnvf (GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_ALPHA); glTexEnvf (GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR); glTexEnvf (GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, brightblendcolour); glBegin (GL_QUADS); glTexCoord2f (0, 0); glVertex2f (0, 0); glTexCoord2f (0, 1); glVertex2f (0, GL_State.OrthoHeight); glTexCoord2f (1, 1); glVertex2f (GL_State.OrthoWidth, GL_State.OrthoHeight); glTexCoord2f (1, 0); glVertex2f (GL_State.OrthoWidth, 0); glEnd (); // restore combiner function colour to white so as not to mess up texture state glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constantwhite); glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glDisable (GL_BLEND); glEnable (GL_ALPHA_TEST); glColor4fv (constantwhite); }
Hey, my coworker wrote those! Small world.Barnes wrote:ahh yes!!!
photoshop math on glsl and hlsl (im use some things)
http://mouaif.wordpress.com/2009/01/05/ ... l-shaders/
Damn! Real small world!jitspoe wrote:Hey, my coworker wrote those! Small world.Barnes wrote:ahh yes!!!
photoshop math on glsl and hlsl (im use some things)
http://mouaif.wordpress.com/2009/01/05/ ... l-shaders/![]()

Shockingly enough, I'm only getting a 4 FPS hit off this and it works pretty well.reckless wrote:Aye its not true gammabut if someone can use it for whatever its easy to implement call the function from the same place as the old gamma and give it a cvar as argument for the bright factor.
Small note it uses negative values so if you want to use it with the standard quake menu you have to invert the menu scales. Maybe nice for a scalable overbright setting hmm?.
Code: Select all
void VID_Local_Gamma_Reset (void)
{
int i;
HDC hdc = GetDC (NULL);
WORD gammaramps[3][256];
for (i = 0;i < 256;i++)
gammaramps[0][i] = gammaramps[1][i] = gammaramps[2][i] = (i * 65535) / 255;
i = SetDeviceGammaRamp(hdc, &gammaramps[0][0]);
ReleaseDC (NULL, hdc);
}
/*
================
VID_Gamma_SetGamma -- apply gamma correction
================
*/
void VID_Local_Gamma_SetGamma (void)
{
if (wplat.draw_context && vid.gammaworks)
{
if (SetDeviceGammaRamp(wplat.draw_context, vid.gammaramp) == false)
Con_Printf ("VID_Gamma_SetGamma: failed on SetDeviceGammaRamp\n");
}
}
/*
qboolean VID_Local_Validate_Gamma (void)
{
static unsigned short checktable[768];
Con_Printf ("Gamma guardian: ");
if (GetDeviceGammaRamp (wplat.draw_context, checktable))
{
if (memcmp(&checktable, &vid.gammaramp, sizeof(checktable)) !=0 )
Con_Printf ("Gamma is wrong!\n");
else Con_Printf ("Gamma is correct!\n");
} else Con_Printf ("Couldn't get gamma table\n");
} */
/*
================
VID_Gamma_Restore -- restore system gamma
================
*/
void VID_Local_Gamma_Restore (void)
{
if (wplat.draw_context && vid.gammaworks)
{
if (SetDeviceGammaRamp(wplat.draw_context, vid.systemgammaramp) == false)
Con_Printf ("VID_Gamma_Restore: failed on SetDeviceGammaRamp\n");
}
}
/*
================
VID_Gamma_f -- callback when the cvar changes
================
*/
void VID_Local_Gamma_f (cvar_t *var)
{
int i;
if (vid_gamma.default_string == NULL || vid_gamma.default_string == NULL)
return; // Presumed to be in initialization
for (i=0; i<256; i++)
{
float inf = (255 * pow ((i + 0.5) / 255.5, vid_gamma.value) + 0.5);
inf -= 128;
inf *= vid_contrast.value;
inf += 128;
vid.gammaramp[i + 0] =
vid.gammaramp[i + 256] =
vid.gammaramp[i + 512] = CLAMP (0, (int) inf, 255) << 8;
}
VID_Gamma_SetGamma ();
}
qboolean VID_Local_Gamma_Init (void)
{
HDC hdc = GetDC (NULL);
if (GetDeviceGammaRamp (hdc, vid.systemgammaramp))
return true;
return false;
}Code: Select all
cvar_t vid_gamma = {"gamma", "1", CVAR_ARCHIVE}; //johnfitz -- moved here from view.c
cvar_t vid_contrast = {"contrast", "1", CVAR_ARCHIVE};
void VID_Gamma_Think (void)
{
if (vid.ActiveApp && !vid.Minimized && vid.gammaworks /*&& wplat.draw_context*/)
{
if (vid.gamma_checktime && realtime > vid.gamma_checktime)
{
VID_Gamma_Restore ();
VID_Gamma_SetGamma ();
vid.gamma_checktime = 0;
Con_DPrintf ("Gamma restore\n");
}
}
}
void VID_Gamma_Shutdown (void)
{
VID_Local_Gamma_Restore ();
}
void VID_Gamma_Init (void)
{
if ( (vid.gammaworks = VID_Local_Gamma_Init ()) == false)
Con_Warning ("Hardware gamma not available.\n");
Cvar_RegisterVariableWithCallback (&vid_contrast, VID_Local_Gamma_f);
Cvar_RegisterVariableWithCallback (&vid_gamma, VID_Local_Gamma_f);
}
void VID_Gamma_SetGamma (void)
{
VID_Local_Gamma_SetGamma ();
}
void VID_Gamma_Restore (void)
{
VID_Local_Gamma_Restore ();
}