Skip to content

Instantly share code, notes, and snippets.

@Ming-Tang
Created September 16, 2010 05:05
Show Gist options
  • Save Ming-Tang/581986 to your computer and use it in GitHub Desktop.
Save Ming-Tang/581986 to your computer and use it in GitHub Desktop.
#if INTERACTIVE
#r "OpenTK.dll"
#endif
open System
open System.Drawing
open System.Collections.Generic
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL
open OpenTK.Input
[<AbstractClass>]
type Game(width, height, title) =
inherit GameWindow(width, height, GraphicsMode.Default, title)
do base.VSync <- VSyncMode.On
override x.OnLoad e =
base.OnLoad(e)
GL.Enable(EnableCap.DepthTest)
x.setup()
override x.OnResize e =
base.OnResize(e)
x.onResize()
override x.OnUpdateFrame e =
base.OnUpdateFrame(e)
x.update()
override x.OnRenderFrame e =
base.OnRenderFrame e
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
let mutable modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&modelview)
x.render()
base.SwapBuffers()
abstract member setup : unit -> unit
abstract member update : unit -> unit
abstract member render : unit -> unit
abstract member onResize : unit -> unit
type Game1() =
inherit Game(640, 480, "Hello World")
let colorCube() =
for z in [ 0.0f; 1.0f ] do
GL.Begin(BeginMode.Quads)
for x, y in [ 0.0f, 0.0f
0.0f, 1.0f
1.0f, 1.0f
1.0f, 0.0f ] do
GL.Color3(x, y, z)
GL.Vertex3(x, y, z)
GL.End()
for y in [ 0.0f; 1.0f ] do
GL.Begin(BeginMode.Quads)
for x, z in [ 0.0f, 0.0f
0.0f, 1.0f
1.0f, 1.0f
1.0f, 0.0f ] do
GL.Color3(x, y, z)
GL.Vertex3(x, y, z)
GL.End()
for x in [ 0.0f; 1.0f ] do
GL.Begin(BeginMode.Quads)
for y, z in [ 0.0f, 0.0f
0.0f, 1.0f
1.0f, 1.0f
1.0f, 0.0f ] do
GL.Color3(x, y, z)
GL.Vertex3(x, y, z)
GL.End()
override x.setup() =
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f)
override x.update() =
()
override x.render() =
let mx, my = x.Mouse.X, x.Mouse.Y
GL.LineWidth(2.0f)
GL.Translate(0.0f, 0.0f, 5.0f)
GL.Rotate(float(mx) / 640.0 * 360.0, new Vector3d(0.0, 0.5, 0.0))
GL.Rotate(float(my) / 480.0 * 360.0, new Vector3d(0.5, 0.0, 0.0))
for x, y, z in [ 1.0f, 0.0f, 0.0f
0.0f, 1.0f, 0.0f
0.0f, 0.0f, 1.0f ] do
GL.Begin(BeginMode.Lines)
GL.Color3(x, y, z)
GL.Vertex3(x, y, z)
GL.Vertex3(x * 2.0f, y * 2.0f, z * 2.0f)
GL.End()
colorCube()
override x.onResize() =
GL.Viewport(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width, base.ClientRectangle.Height)
let mutable projection = Matrix4.CreatePerspectiveFieldOfView(float32 (Math.PI / 4.), float32 base.Width / float32 base.Height, 1.f, 64.f)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadMatrix(&projection)
do
(new Game1()).Run(30.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment