This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
TestGammaRamp.cs
188 lines (152 loc) · 5.59 KB
/
TestGammaRamp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
namespace Glfw3.Tests
{
using CommandLine;
using CommandLine.Text;
using OpenGL;
using System;
/// <summary>
/// This program is used to test the gamma correction functionality for both full screen and
/// windowed mode windows using <see cref="Glfw.SetGammaRamp(Glfw.Monitor, Glfw.GammaRamp)"/>.
/// </summary>
class TestGammaRamp : TestBase
{
const float kStepSize = 0.1f;
static float m_GammaValue = 1f;
class Options
{
[Option('f', HelpText = "Create full screen window")]
public bool Fullscreen { get; set; }
[HelpOption(HelpText = "Display this help screen.")]
public string GetUsage()
{
return HelpText.AutoBuild(this,
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
static void SetGamma(Glfw.Window window, float gamma)
{
var monitor = Glfw.GetWindowMonitor(window);
if (!monitor)
monitor = Glfw.GetPrimaryMonitor();
if (gamma < 0f)
return;
// It is recommended to use gamma ramps of size 256, as that is the size supported by
// all graphics cards on all platforms.
const int kSize = 256;
var ramp = new Glfw.GammaRamp
{
Red = new ushort[256],
Green = new ushort[256],
Blue = new ushort[256]
};
for (int i = 0; i < kSize; i++)
{
float value;
// Calculate intensity
value = (float)i / (float)(kSize - 1);
// Apply gamma curve
value = (float)Math.Pow(value, 1f / gamma) * 65535f + 0.5f;
// Clamp to value range
if (value < 0f)
value = 0f;
else if (value > 65535f)
value = 65535f;
ramp.Red[i] = (ushort)value;
ramp.Green[i] = (ushort)value;
ramp.Blue[i] = (ushort)value;
}
m_GammaValue = gamma;
Log("Set Gamma: {0}", m_GammaValue);
Glfw.SetGammaRamp(monitor, ramp);
var r = Glfw.GetGammaRamp(monitor);
for (int i = 0; i < r.Size; i++)
Log("Ramp {0}: {1},{2},{3}", i, r.Red[i], r.Green[i], r.Blue[i]);
}
static void KeyCallback(Glfw.Window window, Glfw.KeyCode key, int scancode, Glfw.InputState state, Glfw.KeyMods mods)
{
if (state != Glfw.InputState.Press)
return;
switch (key)
{
case Glfw.KeyCode.Escape:
{
Glfw.SetWindowShouldClose(window, true);
break;
}
case Glfw.KeyCode.NumpadAdd:
case Glfw.KeyCode.Up:
case Glfw.KeyCode.Q:
{
SetGamma(window, m_GammaValue + kStepSize);
break;
}
case Glfw.KeyCode.NumpadSubtract:
case Glfw.KeyCode.Down:
case Glfw.KeyCode.W:
{
if (m_GammaValue - kStepSize > 0f)
SetGamma(window, m_GammaValue - kStepSize);
break;
}
}
}
static void FramebufferSizeCallback(Glfw.Window window, int width, int height)
{
Gl.Viewport(0, 0, width, height);
}
public static void Main(string[] args)
{
Init();
int width, height;
Glfw.Window window;
Glfw.Monitor monitor = Glfw.Monitor.None;
var options = new Options();
if (Parser.Default.ParseArguments(args, options))
{
if (options.Fullscreen)
monitor = Glfw.GetPrimaryMonitor();
}
if (!Glfw.Init())
Environment.Exit(1);
if (monitor)
{
var mode = Glfw.GetVideoMode(monitor);
Glfw.WindowHint(Glfw.Hint.RefreshRate, mode.RefreshRate);
Glfw.WindowHint(Glfw.Hint.RedBits, mode.RedBits);
Glfw.WindowHint(Glfw.Hint.GreenBits, mode.GreenBits);
Glfw.WindowHint(Glfw.Hint.BlueBits, mode.BlueBits);
width = mode.Width;
height = mode.Height;
}
else
{
width = 200;
height = 200;
}
window = Glfw.CreateWindow(width, height, "Gamma Test", monitor, Glfw.Window.None);
if (!window)
{
Glfw.Terminate();
Environment.Exit(1);
}
SetGamma(window, 1f);
Glfw.MakeContextCurrent(window);
Glfw.SwapInterval(1);
Glfw.SetKeyCallback(window, KeyCallback);
Glfw.SetFramebufferSizeCallback(window, FramebufferSizeCallback);
Gl.MatrixMode(MatrixMode.Projection);
Gl.Ortho(-1f, 1f, -1f, 1f, -1f, 1f);
Gl.MatrixMode(MatrixMode.Modelview);
Gl.ClearColor(0.5f, 0.5f, 0.5f, 0);
while (!Glfw.WindowShouldClose(window))
{
Gl.Clear(ClearBufferMask.ColorBufferBit);
Gl.Color3(0.8f, 0.2f, 0.4f);
Gl.Rect(-0.5f, -0.5f, 0.5f, 0.5f);
Glfw.SwapBuffers(window);
Glfw.WaitEvents();
}
Glfw.Terminate();
}
}
}