Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created June 7, 2013 07:16
Show Gist options
  • Save ashwin/5727560 to your computer and use it in GitHub Desktop.
Save ashwin/5727560 to your computer and use it in GitHub Desktop.
An example IronPython program that uses OpenTK. Draws a colored triangle.
# IronPython example program to demonstrate OpenTK
#
# Steps:
# 1. Create an empty IronPython project in Visual Studio
# 2. Place OpenTK.dll in the directory of the Python source file
# 3. Paste this source code into the Python source file
# 4. Run. You should see a colored triangle. Press ESC to quit.
#
# Copyright (c) 2013 Ashwin Nanjappa
# Released under the MIT License
import clr
import math
clr.AddReferenceToFile("OpenTK.dll")
import OpenTK as TK
import OpenTK.Graphics.OpenGL as OGL
import OpenTK.Input as TKInput
class Game(TK.GameWindow):
def __init__(self):
self.VSync = TK.VSyncMode.On
self.base = super(Game, self)
def OnLoad(self, e):
self.base.OnLoad(e)
OGL.GL.ClearColor(1, 1, 1, 0.0)
def OnResize(self, e):
self.base.OnResize(e)
OGL.GL.Viewport(self.ClientRectangle.X, self.ClientRectangle.Y, self.ClientRectangle.Width, self.ClientRectangle.Height)
projection = TK.Matrix4.CreatePerspectiveFieldOfView(math.pi / 4, self.Width / self.Height, 1.0, 64.0)
OGL.GL.MatrixMode(OGL.MatrixMode.Projection)
OGL.GL.LoadMatrix(projection)
def OnUpdateFrame(self, e):
self.base.OnUpdateFrame(e)
if self.Keyboard[TKInput.Key.Escape]:
self.Exit()
def OnRenderFrame(self, e):
self.base.OnRenderFrame(e)
OGL.GL.Clear(OGL.ClearBufferMask.ColorBufferBit)
modelview = TK.Matrix4.LookAt(TK.Vector3.Zero, TK.Vector3.UnitZ, TK.Vector3.UnitY)
OGL.GL.MatrixMode(OGL.MatrixMode.Modelview)
OGL.GL.LoadMatrix(modelview)
OGL.GL.Begin(OGL.BeginMode.Triangles)
OGL.GL.Color3(1.0, 1.0, 0.0)
OGL.GL.Vertex3(-1.0, -1.0, 4.0)
OGL.GL.Color3(1.0, 0.0, 0.0)
OGL.GL.Vertex3(1.0, -1.0, 4.0)
OGL.GL.Color3(0.2, 0.9, 1.0)
OGL.GL.Vertex3(0.0, 1.0, 4.0)
OGL.GL.End()
self.SwapBuffers()
def main():
game = Game()
game.Run(30)
if "__main__" == __name__:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment