blob: 0718088684f0e06f9594330abcad58015d41b618 [file] [log] [blame]
Camilla Berglund2630d492010-10-13 04:04:43 +02001//========================================================================
Camilla Berglund6e553c72011-03-06 01:46:39 +01002// GLFW - An OpenGL library
Camilla Berglund2630d492010-10-13 04:04:43 +02003// Platform: Any
4// API version: 3.0
5// WWW: http://www.glfw.org/
6//------------------------------------------------------------------------
7// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
8//
9// This software is provided 'as-is', without any express or implied
10// warranty. In no event will the authors be held liable for any damages
11// arising from the use of this software.
12//
13// Permission is granted to anyone to use this software for any purpose,
14// including commercial applications, and to alter it and redistribute it
15// freely, subject to the following restrictions:
16//
17// 1. The origin of this software must not be misrepresented; you must not
18// claim that you wrote the original software. If you use this software
19// in a product, an acknowledgment in the product documentation would
20// be appreciated but is not required.
21//
22// 2. Altered source versions must be plainly marked as such, and must not
23// be misrepresented as being the original software.
24//
25// 3. This notice may not be removed or altered from any source
26// distribution.
27//
28//========================================================================
29
30#include "internal.h"
31
32#include <math.h>
33#include <string.h>
34
35
36//////////////////////////////////////////////////////////////////////////
37////// GLFW public API //////
38//////////////////////////////////////////////////////////////////////////
39
40//========================================================================
Camilla Berglundca0dbdb2011-09-06 15:43:31 +020041// Calculate a gamma ramp from the specified value and set it
Camilla Berglund2630d492010-10-13 04:04:43 +020042//========================================================================
43
Camilla Berglundca0dbdb2011-09-06 15:43:31 +020044GLFWAPI void glfwSetGamma(float gamma)
Camilla Berglund2630d492010-10-13 04:04:43 +020045{
Camilla Berglundad973a52011-09-06 15:54:04 +020046 int i, size = GLFW_GAMMA_RAMP_SIZE;
Camilla Berglund2630d492010-10-13 04:04:43 +020047 GLFWgammaramp ramp;
48
49 if (!_glfwInitialized)
50 {
Camilla Berglundf1e7d7c2010-11-23 17:45:23 +010051 _glfwSetError(GLFW_NOT_INITIALIZED, NULL);
Camilla Berglund2630d492010-10-13 04:04:43 +020052 return;
53 }
54
Camilla Berglund62f81282011-09-06 15:47:17 +020055 if (gamma <= 0.f)
56 {
57 _glfwSetError(GLFW_INVALID_VALUE,
Camilla Berglund9c0e19a2012-12-13 19:18:04 +010058 "Gamma value must be greater than zero");
Camilla Berglund62f81282011-09-06 15:47:17 +020059 return;
60 }
61
Camilla Berglund2630d492010-10-13 04:04:43 +020062 for (i = 0; i < size; i++)
63 {
Camilla Berglund18392832012-09-12 21:34:23 +020064 float value;
Camilla Berglund554bf5d2011-09-20 15:44:54 +020065
Camilla Berglund18392832012-09-12 21:34:23 +020066 // Calculate intensity
67 value = (float) i / (float) (size - 1);
68 // Apply gamma curve
Camilla Berglund4afc67c2011-07-27 17:09:17 +020069 value = (float) pow(value, 1.f / gamma) * 65535.f + 0.5f;
Camilla Berglundc5f7eff2012-09-27 02:35:19 +020070
Camilla Berglund18392832012-09-12 21:34:23 +020071 // Clamp to value range
Camilla Berglundc5f7eff2012-09-27 02:35:19 +020072 if (value < 0.f)
73 value = 0.f;
74 else if (value > 65535.f)
75 value = 65535.f;
Camilla Berglund554bf5d2011-09-20 15:44:54 +020076
Camilla Berglund554bf5d2011-09-20 15:44:54 +020077 ramp.red[i] = (unsigned short) value;
78 ramp.green[i] = (unsigned short) value;
79 ramp.blue[i] = (unsigned short) value;
Camilla Berglund2630d492010-10-13 04:04:43 +020080 }
81
82 glfwSetGammaRamp(&ramp);
83}
84
85
86//========================================================================
Camilla Berglundc592cd52010-10-14 14:13:39 +020087// Return the cached currently set gamma ramp
Camilla Berglund2630d492010-10-13 04:04:43 +020088//========================================================================
89
90GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp)
91{
92 if (!_glfwInitialized)
93 {
Camilla Berglundf1e7d7c2010-11-23 17:45:23 +010094 _glfwSetError(GLFW_NOT_INITIALIZED, NULL);
Camilla Berglund2630d492010-10-13 04:04:43 +020095 return;
96 }
97
98 *ramp = _glfwLibrary.currentRamp;
99}
100
101
102//========================================================================
103// Make the specified gamma ramp current
104//========================================================================
105
106GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp)
107{
108 if (!_glfwInitialized)
109 {
Camilla Berglundf1e7d7c2010-11-23 17:45:23 +0100110 _glfwSetError(GLFW_NOT_INITIALIZED, NULL);
Camilla Berglund2630d492010-10-13 04:04:43 +0200111 return;
112 }
113
114 _glfwPlatformSetGammaRamp(ramp);
115 _glfwLibrary.currentRamp = *ramp;
Camilla Berglund79bef682012-05-24 11:46:51 +0200116 _glfwLibrary.rampChanged = GL_TRUE;
Camilla Berglund2630d492010-10-13 04:04:43 +0200117}
118