-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
87 lines (83 loc) · 2.78 KB
/
Main.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
using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace RayTracer
{
public class OpenTKApp : GameWindow
{
static int screenID; // unique integer identifier of the OpenGL texture
static MyApplication app; // instance of the application
static bool terminated = false; // application terminates gracefully when this is true
protected override void OnLoad( EventArgs e )
{
// called during application initialization
GL.ClearColor( 0, 0, 0, 0 );
GL.Enable( EnableCap.Texture2D );
GL.Disable( EnableCap.DepthTest );
GL.Hint( HintTarget.PerspectiveCorrectionHint, HintMode.Nicest );
ClientSize = new Size( 1024, 512 );
// initialise screen
app = new MyApplication();
app.screen = new Surface( Width, Height );
Sprite.target = app.screen;
screenID = app.screen.GenTexture();
app.Init();
}
protected override void OnUnload( EventArgs e )
{
// called upon app close
GL.DeleteTextures( 1, ref screenID );
Environment.Exit( 0 ); // bypass wait for key on CTRL-F5
}
protected override void OnResize( EventArgs e )
{
// called upon window resize. Note: does not change the size of the pixel buffer.
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 );
app.screen = new Surface(Width, Height);
app.OnResize(Width, Height);
}
protected override void OnUpdateFrame( FrameEventArgs e )
{
// called once per frame; app logic
var keyboard = OpenTK.Input.Keyboard.GetState();
if( keyboard[OpenTK.Input.Key.Escape] ) terminated = true;
if (keyboard.IsAnyKeyDown)
app.KeyboardPress(keyboard);
}
protected override void OnRenderFrame( FrameEventArgs e )
{
// called once per frame; render
app.Tick();
if( terminated )
{
Exit();
return;
}
// convert MyApplication.screen to OpenGL texture
GL.BindTexture( TextureTarget.Texture2D, screenID );
GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
app.screen.width, app.screen.height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
PixelType.UnsignedByte, app.screen.pixels
);
// draw screen filling quad
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();
// tell OpenTK we're done rendering
SwapBuffers();
}
public static void Main( string[] args )
{
// entry point
using( OpenTKApp app = new OpenTKApp() ) { app.Run( 30.0, 0.0 ); }
}
}
}