Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created June 7, 2013 04:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ashwin/5727049 to your computer and use it in GitHub Desktop.
Save ashwin/5727049 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);
}
}
}
}
@yaoqhuang
Copy link

Can this code be run on ubuntu?

@WhoseTheNerd
Copy link

This code is not latest. To fix it, the 'BeginMode' is deprecated, use 'PrimitiveType' instead.

@LeaveTheCapital
Copy link

When I run this with Rider on Windows the window is on the start menu with the correct title and the preview can be seen by hovering over the title, but the window willl not actually pop up. Any ideas?

@MaximovInk
Copy link

MaximovInk commented Dec 3, 2018

1.)GL.Begin(BeginMode.Triangles); -> GL.Begin(PrimitiveType.Triangles);
2.)
if (Keyboard[Key.Escape])
Exit();
->
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Key.Escape))
Exit();

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