-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoincareWindow.cs
353 lines (280 loc) · 10.3 KB
/
PoincareWindow.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
//using System.Linq;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;
using Poincare.Geometry;
using Poincare.PoincareDisc;
namespace Poincare.Application {
public class PoincareWindow : GameWindow {
static GraphicsMode graphicsMode;
const int windowDefaultSize = 800;
Disc disc;
Bitmap bitmap;
Random random = new Random();
double time = System.DateTime.Now.Ticks * 1E-7;
double oldTime = System.DateTime.Now.Ticks * 1E-7;
double startTime = System.DateTime.Now.Ticks * 1E-7;
double resetTime = 0;
double resetDuration = 60;
JoystickControl joystickControl = null;
MouseControl mouseControl = null;
int p = 5, q = 5 ;
int imageIndex = 0;
public Complex Offset { get; set; }
public double AngleOffset { get; set; }
public bool IsMoving { get; set; }
public bool IsRandomizing { get; set; }
public static List<string> ImageFiles{ get; set; }
public double ImageSpeed { get; set; }
public double ImageOffset { get; set; }
public bool IsInverting { get; set; }
/// <summary>Creates a window with the specified title.</summary>
public PoincareWindow()
: base(windowDefaultSize, windowDefaultSize, graphicsMode, "Poincare'") {
VSync = VSyncMode.Off;
Offset = Complex.Zero;
AngleOffset = 0;
IsMoving = false;
IsRandomizing = false;
ImageSpeed = 0.04;
ImageOffset = 0;
IsInverting = false;
}
/// <summary>Load resources here.</summary>
/// <param name="e">Not used.</param>
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
new KeyboardControl(this);
mouseControl = new MouseControl(this);
if (Joysticks.Count == 1)
joystickControl = new JoystickControl(Joysticks[0], this);
Reset(P, Q, imageIndex);
}
public void MakeLimitRotation() {
AngleOffset = Offset.Modulus * 2;
}
public void Randomize() {
P = random.Next(5) + 3;
Q = random.Next(10 - P);
imageIndex = random.Next(ImageFiles.Count);
Reset();
}
public void ToggleFullscreen() {
if (WindowState == WindowState.Fullscreen) {
WindowState = WindowState.Normal;
Width = windowDefaultSize;
Height = windowDefaultSize;
OnResize(null);
return;
}
WindowState = WindowState.Fullscreen;
}
protected override void OnUnload(EventArgs e) {
disc.Dispose();
}
/// <summary>
/// Called when your window is resized. Set your viewport here. It is also
/// a good place to set up your projection matrix (which probably changes
/// along when the aspect ratio of your window).
/// </summary>
/// <param name="e">Not used.</param>
protected override void OnResize(EventArgs e) {
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
//jetblbc Height = Width;
float aspect = (float)Height / Width;
Matrix4 projection = Matrix4.CreateOrthographic(2, 2 * aspect, -1f, 1f);
projection *= Matrix4.CreateRotationY((float)Math.PI);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
/// <summary>
/// Called when it is time to setup the next frame. Add you game logic here.
/// </summary>
/// <param name="e">Contains timing information for framerate independent logic.</param>
protected override void OnUpdateFrame(FrameEventArgs e) {
base.OnUpdateFrame(e);
}
public void Reset() {
Reset(P, Q, imageIndex);
}
private void Reset(int p, int q, int pictureIndex) {
if (disc != null)
disc.Dispose();
bitmap = new Bitmap(ImageFiles[pictureIndex]);
disc = new Disc(new FundamentalRegion(p, q), bitmap, IsInverting);
GL.Enable(EnableCap.LineSmooth);
GL.Enable(EnableCap.PolygonSmooth);
// GL.Enable(EnableCap.DepthTest);
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
startTime = time;
}
/// <summary>
/// Called when it is time to render the next frame. Add your rendering code here.
/// </summary>
/// <param name="e">Contains timing information.</param>
protected override void OnRenderFrame(FrameEventArgs e) {
//GC.Collect();
// GC.WaitForPendingFinalizers();
base.OnRenderFrame(e);
oldTime = time;
time = System.DateTime.Now.Ticks * 1E-7;
// Console.WriteLine(string.Format("Frame:{0:F5} Avg:{1:F5}", time - oldTime, (time - startTime) / ++drawCount));
if (IsRandomizing && time - resetTime > resetDuration) {
Randomize();
resetTime = time;
}
if (joystickControl != null)
joystickControl.Sample(disc.DrawTime);
mouseControl.Sample();
Mobius movement =
Mobius.CreateRotation(AngleOffset) *
Mobius.CreateDiscTranslation(Complex.Zero, Offset);
if (IsMoving)
movement = Mobius.CreateDiscTranslation(Complex.Zero, Complex.CreatePolar(0.01 * Math.Sin(2 * Math.PI * time / 50), 2 * Math.PI * time / 30)) * movement;
ImageOffset += ImageSpeed;
GL.ClearColor(Color4.Black);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
disc.DrawGL(movement, ImageOffset);
// PolarDemo(time);
// LoopDemo(mousePos.Re);
// SaveGL(time.ToString("000000000.000000"));
SwapBuffers();
}
// http://www.opengl.org/discussion_boards/showthread.php/165932-Capture-OpenGL-screen-C/page2
public Bitmap SaveGL() {
Bitmap bmp = new Bitmap(Width, Height);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(this.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GL.ReadPixels(0, 0, Width, Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
GL.Finish();
bmp.UnlockBits(data);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
return bmp;
}
private void SaveGL(string timestamp) {
Bitmap bmp = SaveGL();
bmp.Save("/home/blake/Projects/Poincare/Poincare/output/" + timestamp + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// bmp.Save("/dev/null", System.Drawing.Imaging.ImageFormat.Jpeg);
}
void LoopDemo(double t) {
int pointCount = 100;
Complex[] gamma = new Complex[100];
Complex[] gammaF = new Complex[100];
List<Complex > zeros = new List<Complex>();
zeros.Add(new Complex(0.1, 0.1));
zeros.Add(new Complex(-0.3, 0.3));
zeros.Add(new Complex(-0.5, -0.5));
for (int i = 0; i < pointCount; i++) {
gamma[i] = Complex.CreatePolar(t, (double)i / pointCount * Math.PI * 2);
gammaF[i] = Complex.One;
foreach (Complex zero in zeros)
gammaF[i] *= gamma[i] - zero;
}
Circle.Create(Complex.Zero, 0.02).DrawGL(Color4.Gray);
foreach (Complex zero in zeros)
Circle.Create(zero, 0.02).DrawGL(Color4.White);
GL.Begin(BeginMode.LineLoop);
GL.Color4(Color.Blue);
for (int i = 0; i < pointCount; i++)
GL.Vertex3(gamma[i].Vector3d);
GL.End();
GL.Begin(BeginMode.LineLoop);
GL.Color4(Color.Red);
for (int i = 0; i < pointCount; i++)
GL.Vertex3(gammaF[i].Vector3d);
GL.End();
Complex aa = Complex.One * 3;
Complex bb = -(2 * zeros[0] + 2 * zeros[1] + 2 * zeros[2]);
Complex cc = zeros[0] * zeros[1] + zeros[1] * zeros[2] + zeros[2] * zeros[0];
Complex r1 = (-bb + (bb * bb - 4 * aa * cc).Sqrt) / 2 / aa;
Complex r2 = (-bb - (bb * bb - 4 * aa * cc).Sqrt) / 2 / aa;
Circle.Create(r1, 0.01).DrawGL(Color4.Green);
Circle.Create(r2, 0.01).DrawGL(Color4.Green);
}
void PolarDemo(double time) {
// CircLine inversionCircle = Circle.Create(new Complex(-1, -0.5), 1);
CircLine inversionCircle = Circle.Create(new Complex(0, 0), 1);
Mobius inversion = inversionCircle.AsInversion;
inversionCircle.DrawGL(Color4.Gray);
int circleCount = 6;
Complex center = Complex.CreatePolar(0.3, time);
for (int i = 0; i < circleCount; i++) {
CircLine radialLine = Line.Create(center, Math.PI * i / circleCount);
radialLine.DrawGL(Color4.Green);
CircLine radialLineImage = inversion * radialLine.Conjugate;
radialLineImage.DrawGL(Color4.GreenYellow);
CircLine radialLineBack = inversion * radialLineImage.Conjugate;
radialLineBack.DrawGL(Color4.Aqua);
//CircLine circumferencialLine = Circle.Create(center, (double) (i + 1) / circleCount);
//circumferencialLine.DrawGL(Color4.DarkRed);
//CircLine circumferencialLineImage = inversion * circumferencialLine.Conjugate;
//circumferencialLineImage.DrawGL(Color4.Red);
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
ImageFiles = new List<string>();
string defaultImageFileDir = System.IO.Path.Combine(System.Environment.CurrentDirectory, "Resources");
ImageFiles.AddRange(GetImages(defaultImageFileDir));
// graphicsMode = new GraphicsMode(GraphicsMode.Default.ColorFormat, GraphicsMode.Default.Depth, GraphicsMode.Default.Stencil, graphicsModeSamples);
graphicsMode = new GraphicsMode();
foreach (string imageFileDir in args) {
if (imageFileDir != string.Empty)
ImageFiles.AddRange(GetImages(imageFileDir));
}
// The 'using' idiom guarantees proper resource cleanup.
// We request 30 UpdateFrame events per second, and unlimited
// RenderFrame events (as fast as the computer can handle).
using (PoincareWindow game = new PoincareWindow()) {
game.Run(30.0);
}
}
private static List<string> GetImages(string directory) {
var files = new List<string>();
files.AddRange(System.IO.Directory.GetFiles(directory, "*.jpg"));
files.AddRange(System.IO.Directory.GetFiles(directory, "*.png"));
files.AddRange(System.IO.Directory.GetFiles(directory, "*.bmp"));
return files;
}
public int P {
get { return p; }
set {
p = value;
while ((p - 2) * (q - 2) <= 4)
p++;
}
}
public int Q {
get { return q; }
set {
q = value;
while ((p - 2) * (q - 2) <= 4)
q++;
// q = Math.Max(q, 4);
}
}
public int ImageIndex {
get { return imageIndex; }
set {
imageIndex = value;
while (imageIndex < 0)
imageIndex += ImageFiles.Count;
imageIndex %= ImageFiles.Count;
}
}
}
}