parent
ffba674dbf
commit
2630d4968c
16 changed files with 647 additions and 31 deletions
@ -0,0 +1,115 @@ |
|||||||
|
//========================================================================
|
||||||
|
// GLFW - An OpenGL framework
|
||||||
|
// Platform: Any
|
||||||
|
// API version: 3.0
|
||||||
|
// WWW: http://www.glfw.org/
|
||||||
|
//------------------------------------------------------------------------
|
||||||
|
// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
|
||||||
|
//
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
//
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
//
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgment in the product documentation would
|
||||||
|
// be appreciated but is not required.
|
||||||
|
//
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not
|
||||||
|
// be misrepresented as being the original software.
|
||||||
|
//
|
||||||
|
// 3. This notice may not be removed or altered from any source
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
#include "internal.h" |
||||||
|
|
||||||
|
#include <math.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
////// GLFW public API //////
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Calculate the gamma ramp table from specified values
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
GLFWAPI void glfwSetGammaFormula(float gamma, float blacklevel, float gain) |
||||||
|
{ |
||||||
|
int i, size = 256; |
||||||
|
GLFWgammaramp ramp; |
||||||
|
|
||||||
|
if (!_glfwInitialized) |
||||||
|
{ |
||||||
|
_glfwSetError(GLFW_NOT_INITIALIZED); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
for (i = 0; i < size; i++) |
||||||
|
{ |
||||||
|
float value = (float) i / ((float) (size - 1)); |
||||||
|
|
||||||
|
// Apply gamma
|
||||||
|
value = pow(value, gamma) * 65535.f + 0.5f; |
||||||
|
|
||||||
|
// Apply gain
|
||||||
|
value = gain * (value - 32767.5f) + 32767.5f; |
||||||
|
|
||||||
|
// Apply black-level
|
||||||
|
value += blacklevel * 65535.f; |
||||||
|
|
||||||
|
// Clamp values
|
||||||
|
if (value < 0.f) |
||||||
|
value = 0.f; |
||||||
|
else if (value > 65535.f) |
||||||
|
value = 65535.f; |
||||||
|
|
||||||
|
// Set the gamma ramp values
|
||||||
|
ramp.red[i] = (unsigned short) value; |
||||||
|
ramp.green[i] = (unsigned short) value; |
||||||
|
ramp.blue[i] = (unsigned short) value; |
||||||
|
} |
||||||
|
|
||||||
|
glfwSetGammaRamp(&ramp); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Return the currently set gamma ramp
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp) |
||||||
|
{ |
||||||
|
if (!_glfwInitialized) |
||||||
|
{ |
||||||
|
_glfwSetError(GLFW_NOT_INITIALIZED); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
*ramp = _glfwLibrary.currentRamp; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Make the specified gamma ramp current
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp) |
||||||
|
{ |
||||||
|
if (!_glfwInitialized) |
||||||
|
{ |
||||||
|
_glfwSetError(GLFW_NOT_INITIALIZED); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
_glfwPlatformSetGammaRamp(ramp); |
||||||
|
_glfwLibrary.currentRamp = *ramp; |
||||||
|
} |
||||||
|
|
@ -0,0 +1,67 @@ |
|||||||
|
//========================================================================
|
||||||
|
// GLFW - An OpenGL framework
|
||||||
|
// Platform: Win32/WGL
|
||||||
|
// API version: 2.7
|
||||||
|
// WWW: http://www.glfw.org/
|
||||||
|
//------------------------------------------------------------------------
|
||||||
|
// Copyright (c) 2002-2006 Marcus Geelnard
|
||||||
|
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
|
||||||
|
//
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
//
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
//
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgment in the product documentation would
|
||||||
|
// be appreciated but is not required.
|
||||||
|
//
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not
|
||||||
|
// be misrepresented as being the original software.
|
||||||
|
//
|
||||||
|
// 3. This notice may not be removed or altered from any source
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
#include "internal.h" |
||||||
|
|
||||||
|
#include <limits.h> |
||||||
|
|
||||||
|
|
||||||
|
//************************************************************************
|
||||||
|
//**** GLFW internal functions ****
|
||||||
|
//************************************************************************
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Save the gamma ramp to our internal copy
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
void _glfwPlatformSaveGammaRamp(int ramp) |
||||||
|
{ |
||||||
|
if (!_glfwLibrary.gammaSize) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
_glfw_GetDeviceGammaRamp(_glfwLibrary.Win32.desktopDC, |
||||||
|
_glfwLibrary.gammaRamp[ramp]); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Restore the gamma ramp to our internal copy of the gamma ramp
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
void _glfwPlatformRestoreGammaRamp(int ramp) |
||||||
|
{ |
||||||
|
if (!_glfwLibrary.gammaSize) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
_glfw_SetDeviceGammaRamp(_glfwLibrary.Win32.desktopDC, |
||||||
|
_glfwLibrary.gammaRamp[ramp]); |
||||||
|
} |
@ -0,0 +1,124 @@ |
|||||||
|
//========================================================================
|
||||||
|
// GLFW - An OpenGL framework
|
||||||
|
// Platform: X11/GLX
|
||||||
|
// API version: 3.0
|
||||||
|
// WWW: http://www.glfw.org/
|
||||||
|
//------------------------------------------------------------------------
|
||||||
|
// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
|
||||||
|
//
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
//
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
//
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgment in the product documentation would
|
||||||
|
// be appreciated but is not required.
|
||||||
|
//
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not
|
||||||
|
// be misrepresented as being the original software.
|
||||||
|
//
|
||||||
|
// 3. This notice may not be removed or altered from any source
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
#include "internal.h" |
||||||
|
|
||||||
|
#include <limits.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
|
||||||
|
//************************************************************************
|
||||||
|
//**** GLFW internal functions ****
|
||||||
|
//************************************************************************
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Save the original gamma ramp so that we can restore it later
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
void _glfwPlatformSaveGammaRamp(void) |
||||||
|
{ |
||||||
|
if (_glfwLibrary.X11.XRandR.available && |
||||||
|
!_glfwLibrary.X11.XRandR.gammaBroken) |
||||||
|
{ |
||||||
|
#if defined (_GLFW_HAS_XRANDR) |
||||||
|
size_t size = GLFW_GAMMA_RAMP_SIZE * sizeof(unsigned short); |
||||||
|
|
||||||
|
XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, |
||||||
|
_glfwLibrary.X11.root); |
||||||
|
|
||||||
|
XRRCrtcGamma* gamma = XRRGetCrtcGamma(_glfwLibrary.X11.display, |
||||||
|
rr->crtcs[0]); |
||||||
|
|
||||||
|
memcpy(_glfwLibrary.originalRamp.red, gamma->red, size); |
||||||
|
memcpy(_glfwLibrary.originalRamp.green, gamma->green, size); |
||||||
|
memcpy(_glfwLibrary.originalRamp.blue, gamma->blue, size); |
||||||
|
|
||||||
|
XRRFreeGamma(gamma); |
||||||
|
XRRFreeScreenResources(rr); |
||||||
|
} |
||||||
|
#endif |
||||||
|
else if (_glfwLibrary.X11.XF86VidMode.available) |
||||||
|
{ |
||||||
|
#if defined (_GLFW_HAS_XF86VIDMODE) |
||||||
|
XF86VidModeGetGammaRamp(_glfwLibrary.X11.display, |
||||||
|
_glfwLibrary.X11.screen, |
||||||
|
GLFW_GAMMA_RAMP_SIZE, |
||||||
|
_glfwLibrary.originalRamp.red, |
||||||
|
_glfwLibrary.originalRamp.green, |
||||||
|
_glfwLibrary.originalRamp.blue); |
||||||
|
#endif |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Make the specified gamma ramp current
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp) |
||||||
|
{ |
||||||
|
if (_glfwLibrary.X11.XRandR.available && |
||||||
|
!_glfwLibrary.X11.XRandR.gammaBroken) |
||||||
|
{ |
||||||
|
#if defined (_GLFW_HAS_XRANDR) |
||||||
|
int i; |
||||||
|
size_t size = GLFW_GAMMA_RAMP_SIZE * sizeof(unsigned short); |
||||||
|
|
||||||
|
XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, |
||||||
|
_glfwLibrary.X11.root); |
||||||
|
|
||||||
|
// Update gamma per monitor
|
||||||
|
for (i = 0; i < rr->ncrtc; i++) |
||||||
|
{ |
||||||
|
XRRCrtcGamma* gamma = XRRAllocGamma(GLFW_GAMMA_RAMP_SIZE); |
||||||
|
|
||||||
|
memcpy(gamma->red, ramp->red, size); |
||||||
|
memcpy(gamma->green, ramp->green, size); |
||||||
|
memcpy(gamma->blue, ramp->blue, size); |
||||||
|
|
||||||
|
XRRSetCrtcGamma(_glfwLibrary.X11.display, rr->crtcs[i], gamma); |
||||||
|
XRRFreeGamma(gamma); |
||||||
|
} |
||||||
|
|
||||||
|
XRRFreeScreenResources(rr); |
||||||
|
} |
||||||
|
#endif |
||||||
|
else if (_glfwLibrary.X11.XF86VidMode.available) |
||||||
|
{ |
||||||
|
#if defined (_GLFW_HAS_XF86VIDMODE) |
||||||
|
XF86VidModeSetGammaRamp(_glfwLibrary.X11.display, |
||||||
|
_glfwLibrary.X11.screen, |
||||||
|
GLFW_GAMMA_RAMP_SIZE, |
||||||
|
(unsigned short*) ramp->red, |
||||||
|
(unsigned short*) ramp->green, |
||||||
|
(unsigned short*) ramp->blue); |
||||||
|
#endif |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,178 @@ |
|||||||
|
//========================================================================
|
||||||
|
// Gamma correction test program
|
||||||
|
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
|
||||||
|
//
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
//
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
//
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgment in the product documentation would
|
||||||
|
// be appreciated but is not required.
|
||||||
|
//
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not
|
||||||
|
// be misrepresented as being the original software.
|
||||||
|
//
|
||||||
|
// 3. This notice may not be removed or altered from any source
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
//
|
||||||
|
// This program is used to test the gamma correction functionality for
|
||||||
|
// both fullscreen and windowed mode windows
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
#include <GL/glfw3.h> |
||||||
|
|
||||||
|
#include <stdio.h> |
||||||
|
#include <stdlib.h> |
||||||
|
|
||||||
|
#include "getopt.h" |
||||||
|
|
||||||
|
static GLfloat ggamma = 1.0f; |
||||||
|
static GLfloat ggain = 1.0f; |
||||||
|
static GLfloat gblacklevel = 0.0f; |
||||||
|
|
||||||
|
static void usage(void) |
||||||
|
{ |
||||||
|
printf("Usage: gammatest [-h] [-f]\n"); |
||||||
|
} |
||||||
|
|
||||||
|
static void key_callback(GLFWwindow window, int key, int action) |
||||||
|
{ |
||||||
|
if (action != GLFW_PRESS) |
||||||
|
return; |
||||||
|
|
||||||
|
switch (key) |
||||||
|
{ |
||||||
|
case GLFW_KEY_ESC: |
||||||
|
glfwCloseWindow(window); |
||||||
|
break; |
||||||
|
case 'Q': |
||||||
|
ggamma += 0.1f; |
||||||
|
printf("Gamma: %f\n", ggamma); |
||||||
|
glfwSetGammaFormula( ggamma, gblacklevel, ggain ); |
||||||
|
break; |
||||||
|
case 'W': |
||||||
|
ggamma -= 0.1f; |
||||||
|
printf("Gamma: %f\n", ggamma); |
||||||
|
glfwSetGammaFormula( ggamma, gblacklevel, ggain ); |
||||||
|
break; |
||||||
|
case 'A': |
||||||
|
ggain += 0.1f; |
||||||
|
printf("Gain: %f\n", ggain); |
||||||
|
glfwSetGammaFormula( ggamma, gblacklevel, ggain ); |
||||||
|
break; |
||||||
|
case 'S': |
||||||
|
ggain -= 0.1f; |
||||||
|
printf("Gain: %f\n", ggain); |
||||||
|
glfwSetGammaFormula( ggamma, gblacklevel, ggain ); |
||||||
|
break; |
||||||
|
case 'Z': |
||||||
|
gblacklevel += 0.1f; |
||||||
|
printf("Black Level: %f\n", gblacklevel); |
||||||
|
glfwSetGammaFormula( ggamma, gblacklevel, ggain ); |
||||||
|
break; |
||||||
|
case 'X': |
||||||
|
gblacklevel -= 0.1f; |
||||||
|
printf("Black Level: %f\n", gblacklevel); |
||||||
|
glfwSetGammaFormula( ggamma, gblacklevel, ggain ); |
||||||
|
break; |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void size_callback(GLFWwindow window, int width, int height) |
||||||
|
{ |
||||||
|
glViewport(0, 0, width, height); |
||||||
|
} |
||||||
|
|
||||||
|
int main(int argc, char** argv) |
||||||
|
{ |
||||||
|
int width, height, ch; |
||||||
|
int mode = GLFW_WINDOWED; |
||||||
|
GLFWwindow window; |
||||||
|
|
||||||
|
while ((ch = getopt(argc, argv, "fh")) != -1) |
||||||
|
{ |
||||||
|
switch (ch) |
||||||
|
{ |
||||||
|
case 'h': |
||||||
|
usage(); |
||||||
|
exit(EXIT_SUCCESS); |
||||||
|
|
||||||
|
case 'f': |
||||||
|
mode = GLFW_FULLSCREEN; |
||||||
|
break; |
||||||
|
|
||||||
|
default: |
||||||
|
usage(); |
||||||
|
exit(EXIT_FAILURE); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (!glfwInit()) |
||||||
|
{ |
||||||
|
fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError())); |
||||||
|
exit(EXIT_FAILURE); |
||||||
|
} |
||||||
|
|
||||||
|
if (mode == GLFW_FULLSCREEN) |
||||||
|
{ |
||||||
|
GLFWvidmode mode; |
||||||
|
glfwGetDesktopMode(&mode); |
||||||
|
width = mode.width; |
||||||
|
height = mode.height; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
width = 0; |
||||||
|
height = 0; |
||||||
|
} |
||||||
|
|
||||||
|
window = glfwOpenWindow(width, height, mode, "Gamma Test", NULL); |
||||||
|
if (!window) |
||||||
|
{ |
||||||
|
glfwTerminate(); |
||||||
|
|
||||||
|
fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); |
||||||
|
exit(EXIT_FAILURE); |
||||||
|
} |
||||||
|
|
||||||
|
printf("Gamma: %f\nGain: %f\nBlack Level: %f\n", |
||||||
|
ggamma, ggain, gblacklevel); |
||||||
|
|
||||||
|
glfwSwapInterval(1); |
||||||
|
glfwSetKeyCallback(window, key_callback); |
||||||
|
glfwSetWindowSizeCallback(window, size_callback); |
||||||
|
|
||||||
|
glEnable(GL_SCISSOR_TEST); |
||||||
|
|
||||||
|
while (glfwIsWindow(window)) |
||||||
|
{ |
||||||
|
int width, height; |
||||||
|
|
||||||
|
glfwGetWindowSize(window, &width, &height); |
||||||
|
|
||||||
|
glScissor(0, 0, width, height); |
||||||
|
glClearColor(0.5f, 0.5f, 0.5f, 0); |
||||||
|
glClear(GL_COLOR_BUFFER_BIT); |
||||||
|
|
||||||
|
glScissor(0, 0, 640, 480); |
||||||
|
glClearColor(0.8f, 0.2f, 0.4f, 0); |
||||||
|
glClear(GL_COLOR_BUFFER_BIT); |
||||||
|
|
||||||
|
glfwSwapBuffers(); |
||||||
|
glfwPollEvents(); |
||||||
|
} |
||||||
|
|
||||||
|
glfwTerminate(); |
||||||
|
exit(EXIT_SUCCESS); |
||||||
|
} |
||||||
|
|
Loading…
Reference in New Issue