Skip to content

Instantly share code, notes, and snippets.

@khokm
Forked from ashwin/OpenTKExample.cs
Created December 27, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khokm/be539c2d3cbccbcb6001b3021c0daf3a to your computer and use it in GitHub Desktop.
Save khokm/be539c2d3cbccbcb6001b3021c0daf3a to your computer and use it in GitHub Desktop.
An example C# program that uses OpenTK. Displays a colored triangle.
// C# example program to demonstrate OpenTK
//
// Steps:
// 1. Create an empty C# console application project in Visual Studio
// 2. Place OpenTK.dll in the directory of the C# source file
// 3. Add System.Drawing and OpenTK as References to the project
// 4. Paste this source code into the C# source file
// 5. Run. You should see a colored triangle. Press ESC to quit.
//
// Copyright (c) 2013 Ashwin Nanjappa
// Released under the MIT License
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace StarterKit
{
class Game : GameWindow
{
public Game()
: base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
{
VSync = VSyncMode.On;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
GL.Enable(EnableCap.DepthTest);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (Keyboard[Key.Escape])
Exit();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
GL.Begin(BeginMode.Triangles);
GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 4.0f);
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 4.0f);
GL.Color3(0.2f, 0.9f, 1.0f); GL.Vertex3(0.0f, 1.0f, 4.0f);
GL.End();
SwapBuffers();
}
[STAThread]
static void Main()
{
// 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 (Game game = new Game())
{
game.Run(30.0);
}
}
}
}
@nyendwa
Copy link

nyendwa commented Jul 13, 2022

Kindly Re-do this work in Visual Studio 2022, I tried running it on VS22 and it is crashing.

@ignatvs
Copy link

ignatvs commented Sep 4, 2022

// 2. Place OpenTK.dll in the directory of the C# source file
Instead add it as a reference (in Proyect References) do the same for System.Drawing

I have it running in vs2022.

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment