-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.cs
214 lines (189 loc) · 5.81 KB
/
template.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Drawing;
using System.Threading;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using RayTracer.Helpers;
namespace RayTracer
{
public class OpenTKApp : GameWindow
{
private static int _screenId;
private static Game _game;
private static CancellationTokenSource _exited = new CancellationTokenSource();
protected override void OnLoad(EventArgs e)
{
// called upon app init
GL.ClearColor(Color.Black);
GL.Enable(EnableCap.Texture2D);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
ClientSize = new Size(512, 512);
//ClientSize = new Size(1024, 1024);
_game = new Game();
_game.Screen = new Surface(Width, Height);
Sprite.Target = _game.Screen;
_screenId = _game.Screen.GenTexture();
_game.Init(this, _exited.Token);
}
protected override void OnUnload(EventArgs e)
{
// called upon app close
GL.DeleteTextures(1, ref _screenId);
}
protected override void OnResize(EventArgs e)
{
// called upon window resize
GL.Viewport(0, 0, Width, Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
_game.TraceRay(e.X, e.Y, new RNG());
}
protected override void OnKeyUp(KeyboardKeyEventArgs e)
{
if (e.Key == Key.Pause)
{
lockKeys = !lockKeys;
Console.WriteLine($"Keys locked: {lockKeys}");
}
if (lockKeys)
{
return;
}
if (e.Key == Key.Slash)
{
_game.PreviousScene();
}
if (e.Key == Key.KeypadMultiply)
{
_game.NextScene();
}
if (e.Key == Key.KeypadPlus)
{
_game.Antialiasing(1);
}
if (e.Key == Key.KeypadMinus)
{
_game.Antialiasing(-1);
}
if (e.Key == Key.Keypad7)
{
_game.GammaCorrection(true);
}
if (e.Key == Key.Keypad8)
{
_game.GammaCorrection(false);
}
base.OnKeyUp(e);
}
private bool lockKeys = false;
protected override void OnUpdateFrame(FrameEventArgs e)
{
// called once per frame; app logic
var keyboard = OpenTK.Input.Keyboard.GetState();
if (keyboard[Key.Escape])
{
_exited.Cancel();
Exit();
}
if (lockKeys)
{
return;
}
if (keyboard[Key.KeypadEnter])
{
_game.RestartSample();
}
var moveVector = Vector3.Zero;
if (keyboard[Key.A])
{
moveVector += -Vector3.UnitX;
}else if (keyboard[Key.D])
{
moveVector += Vector3.UnitX;
}
if (keyboard[Key.W])
{
moveVector += Vector3.UnitZ;
}else if (keyboard[Key.S])
{
moveVector += -Vector3.UnitZ;
}
if (keyboard[Key.Space])
{
moveVector += Vector3.UnitY;
}else if (keyboard[Key.ControlLeft])
{
moveVector += -Vector3.UnitY;
}
if (moveVector != Vector3.Zero)
{
_game.MoveCamera(moveVector.Normalized());
}
var rotVector = Vector2.Zero;
if (keyboard[Key.Left])
{
rotVector += -Vector2.UnitX;
}else if (keyboard[Key.Right])
{
rotVector += Vector2.UnitX;
}
if (keyboard[Key.Up])
{
rotVector += Vector2.UnitY;
}else if (keyboard[Key.Down])
{
rotVector += -Vector2.UnitY;
}
if (rotVector != Vector2.Zero)
{
_game.RotateCamera(rotVector);
}
}
protected override void OnRenderFrame(FrameEventArgs e)
{
// called once per frame; render
_game.Tick();
GL.BindTexture(TextureTarget.Texture2D, _screenId);
GL.TexImage2D(TextureTarget.Texture2D,
0,
PixelInternalFormat.Rgba,
_game.Screen.Width,
_game.Screen.Height,
0,
PixelFormat.Bgra,
PixelType.UnsignedByte,
_game.Screen.Pixels
);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.BindTexture(TextureTarget.Texture2D, _screenId);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0.0f, 1.0f);
GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, 1.0f);
GL.End();
_game.Render();
SwapBuffers();
}
[STAThread]
public static void Main()
{
// entry point
using (var app = new OpenTKApp())
{
app.Run(30.0, 0.0);
}
}
}
}