Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 1 | //======================================================================== |
Camilla Berglund | 6e553c7 | 2011-03-06 01:46:39 +0100 | [diff] [blame] | 2 | // GLFW - An OpenGL library |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 3 | // 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 Berglund | ca0dbdb | 2011-09-06 15:43:31 +0200 | [diff] [blame] | 41 | // Calculate a gamma ramp from the specified value and set it |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 42 | //======================================================================== |
| 43 | |
Camilla Berglund | ca0dbdb | 2011-09-06 15:43:31 +0200 | [diff] [blame] | 44 | GLFWAPI void glfwSetGamma(float gamma) |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 45 | { |
Camilla Berglund | ad973a5 | 2011-09-06 15:54:04 +0200 | [diff] [blame] | 46 | int i, size = GLFW_GAMMA_RAMP_SIZE; |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 47 | GLFWgammaramp ramp; |
| 48 | |
| 49 | if (!_glfwInitialized) |
| 50 | { |
Camilla Berglund | f1e7d7c | 2010-11-23 17:45:23 +0100 | [diff] [blame] | 51 | _glfwSetError(GLFW_NOT_INITIALIZED, NULL); |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
Camilla Berglund | 62f8128 | 2011-09-06 15:47:17 +0200 | [diff] [blame] | 55 | if (gamma <= 0.f) |
| 56 | { |
| 57 | _glfwSetError(GLFW_INVALID_VALUE, |
Camilla Berglund | 9c0e19a | 2012-12-13 19:18:04 +0100 | [diff] [blame] | 58 | "Gamma value must be greater than zero"); |
Camilla Berglund | 62f8128 | 2011-09-06 15:47:17 +0200 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 62 | for (i = 0; i < size; i++) |
| 63 | { |
Camilla Berglund | 1839283 | 2012-09-12 21:34:23 +0200 | [diff] [blame] | 64 | float value; |
Camilla Berglund | 554bf5d | 2011-09-20 15:44:54 +0200 | [diff] [blame] | 65 | |
Camilla Berglund | 1839283 | 2012-09-12 21:34:23 +0200 | [diff] [blame] | 66 | // Calculate intensity |
| 67 | value = (float) i / (float) (size - 1); |
| 68 | // Apply gamma curve |
Camilla Berglund | 4afc67c | 2011-07-27 17:09:17 +0200 | [diff] [blame] | 69 | value = (float) pow(value, 1.f / gamma) * 65535.f + 0.5f; |
Camilla Berglund | c5f7eff | 2012-09-27 02:35:19 +0200 | [diff] [blame] | 70 | |
Camilla Berglund | 1839283 | 2012-09-12 21:34:23 +0200 | [diff] [blame] | 71 | // Clamp to value range |
Camilla Berglund | c5f7eff | 2012-09-27 02:35:19 +0200 | [diff] [blame] | 72 | if (value < 0.f) |
| 73 | value = 0.f; |
| 74 | else if (value > 65535.f) |
| 75 | value = 65535.f; |
Camilla Berglund | 554bf5d | 2011-09-20 15:44:54 +0200 | [diff] [blame] | 76 | |
Camilla Berglund | 554bf5d | 2011-09-20 15:44:54 +0200 | [diff] [blame] | 77 | ramp.red[i] = (unsigned short) value; |
| 78 | ramp.green[i] = (unsigned short) value; |
| 79 | ramp.blue[i] = (unsigned short) value; |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | glfwSetGammaRamp(&ramp); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | //======================================================================== |
Camilla Berglund | c592cd5 | 2010-10-14 14:13:39 +0200 | [diff] [blame] | 87 | // Return the cached currently set gamma ramp |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 88 | //======================================================================== |
| 89 | |
| 90 | GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp) |
| 91 | { |
| 92 | if (!_glfwInitialized) |
| 93 | { |
Camilla Berglund | f1e7d7c | 2010-11-23 17:45:23 +0100 | [diff] [blame] | 94 | _glfwSetError(GLFW_NOT_INITIALIZED, NULL); |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | *ramp = _glfwLibrary.currentRamp; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | //======================================================================== |
| 103 | // Make the specified gamma ramp current |
| 104 | //======================================================================== |
| 105 | |
| 106 | GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp) |
| 107 | { |
| 108 | if (!_glfwInitialized) |
| 109 | { |
Camilla Berglund | f1e7d7c | 2010-11-23 17:45:23 +0100 | [diff] [blame] | 110 | _glfwSetError(GLFW_NOT_INITIALIZED, NULL); |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
| 114 | _glfwPlatformSetGammaRamp(ramp); |
| 115 | _glfwLibrary.currentRamp = *ramp; |
Camilla Berglund | 79bef68 | 2012-05-24 11:46:51 +0200 | [diff] [blame] | 116 | _glfwLibrary.rampChanged = GL_TRUE; |
Camilla Berglund | 2630d49 | 2010-10-13 04:04:43 +0200 | [diff] [blame] | 117 | } |
| 118 | |