Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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)
GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
protected override void OnResize(EventArgs 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.LoadMatrix(ref projection);
protected override void OnUpdateFrame(FrameEventArgs e)
if (Keyboard[Key.Escape])
protected override void OnRenderFrame(FrameEventArgs e)
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.LoadMatrix(ref modelview);
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);
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())

This comment has been minimized.

Show comment Hide comment

yaoqhuang Jun 26, 2017

Can this code be run on ubuntu?

Can this code be run on ubuntu?

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