Wiki

root/cobra/trunk/HowTo/710-OpenTK.cobra

Revision 2437, 3.2 KB (checked in by Chuck.Esterbrook, 19 months ago)

Add a How-To for using OpenTK.

  • Property svn:eol-style set to native
Line 
1# .skip.
2# .compile-only.
3"""
4How To User OpenTK
5
6OpenTK provides type-safe, well organized wrappers around OpenGL, OpenCL and OpenAL.
7
8http://www.opentk.com/
9
10
11If you're on Mono, and you want to build OpenTK from source, try:
12
13svn co https://opentk.svn.sourceforge.net/svnroot/opentk/trunk opentk
14cd opentk/
15xbuild
16
17Then look in Binaries/ subdirectory for the OpenTK binary libraries.
18
19Tested on Mac OS X 10.6 with Mono 2.6.7 and OpenTK subversion revisions 2755 and 2780.
20
21"""
22
23@ref 'System.Drawing'
24@ref 'OpenTK'
25@number float64
26
27use OpenTK
28use OpenTK.Graphics
29use OpenTK.Graphics.OpenGL
30use OpenTK.Audio
31use OpenTK.Audio.OpenAL
32use OpenTK.Input
33
34
35class ThisGameWindow inherits GameWindow
36
37    var _rotation = 0.0
38    var _rotationSpeed = 20.0
39
40    cue init
41        """ Creates a 800x600 window with the specified title. """
42        base.init(800, 600, GraphicsMode.default, 'OpenTK Quick Start Sample')
43        # .windowState = WindowState.Fullscreen   # to run in fullscreen
44        .vSync = if(.windowState == WindowState.Fullscreen, VSyncMode.On, VSyncMode.Off)
45
46    def onLoad(e as EventArgs?) is protected, override
47        """ Load resources here. """
48        base.onLoad(e)
49        GL.clearColor(0.1f32, 0.2f32, 0.5f32, 0.0f32)
50        GL.enable(EnableCap.DepthTest)
51        .printDisplayDevices
52
53    def printDisplayDevices
54        for device in DisplayDevice.availableDisplays
55            print
56            print 'DisplayDevice'
57            trace device
58            trace device.isPrimary
59            trace device.bounds
60            trace device.refreshRate
61            trace device.bitsPerPixel
62            # for res in device.availableResolutions, trace res
63        print
64
65    def onResize(e as EventArgs?) is protected
66        """
67        Called when your window is resized. Set your viewport here. It is also
68        a good place to set up your projection matrix (which probably changes
69        along when the aspect ratio of your window).
70        """
71        base.onResize(e)
72        cr = .clientRectangle
73        GL.viewport(cr.x, cr.y, cr.width, cr.height)
74        projection = Matrix4d.createPerspectiveFieldOfView(
75            Math.pi / 4, .width / (.height to number), 1.0, 64.0)
76        GL.matrixMode(MatrixMode.Projection)
77        GL.loadMatrix(inout projection)
78
79    def onUpdateFrame(e as FrameEventArgs?) is protected
80        """ Called when it is time to setup the next frame. Add you game logic here. """
81        base.onUpdateFrame(e)
82        if .keyboard[Key.Escape], .exit
83
84    def onRenderFrame(e as FrameEventArgs?) is protected
85        """ Called when it is time to render the next frame. Add your rendering code here. """
86        base.onRenderFrame(e)
87
88        GL.clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit)
89
90        modelview = Matrix4.lookAt(Vector3.zero, Vector3.unitZ, Vector3.unitY)
91        GL.matrixMode(MatrixMode.Modelview)
92        GL.loadMatrix(inout modelview)
93
94        dt = e.time
95        _rotation += _rotationSpeed * dt
96        # trace _rotation, dt
97        GL.rotate(_rotation, 0, 0, 1)
98       
99        GL.begin(BeginMode.Triangles)
100
101        GL.color3(1.0, 1.0, 0.0)
102        GL.vertex3(-1.0, -1.0, 4.0)
103       
104        GL.color3(1.0, 0.0, 0.0)
105        GL.vertex3(1.0, -1.0, 4.0)
106       
107        GL.color3(0.2, 0.9, 1.0)
108        GL.vertex3(0.0, 1.0, 4.0)
109
110        GL.end
111
112        .swapBuffers
113
114    def main is shared has STAThread
115        # The 'using' idiom guarantees proper resource cleanup.
116        # We request 30 UpdateFrame events per second, and unlimited
117        # RenderFrame events (as fast as the computer can handle).
118        using game = ThisGameWindow()
119            game.run(30.0)
Note: See TracBrowser for help on using the browser.