Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* Licensed under the MIT/X11 license. |
||
| 3 | * Copyright (c) 2006-2008 the OpenTK Team. |
||
| 4 | * This notice may not be removed from any source distribution. |
||
| 5 | * See license.txt for licensing detailed licensing details. |
||
| 6 | */ |
||
| 7 | #endregion |
||
| 8 | |||
| 9 | #region --- Using Directives --- |
||
| 10 | |||
| 11 | using System; |
||
| 12 | using System.Collections.Generic; |
||
| 13 | using System.Windows.Forms; |
||
| 14 | using System.Threading; |
||
| 15 | using System.Drawing; |
||
| 16 | |||
| 17 | using OpenTK; |
||
| 18 | using OpenTK.Graphics; |
||
| 19 | using OpenTK.Graphics.OpenGL; |
||
| 20 | |||
| 21 | #endregion |
||
| 22 | |||
| 23 | namespace Examples.Tutorial |
||
| 24 | { |
||
| 25 | /// <summary> |
||
| 26 | /// Demonstrates immediate mode rendering. |
||
| 27 | /// </summary> |
||
| 28 | [Example("Immediate mode", ExampleCategory.OpenGL, "1.x", 1, Documentation="ImmediateMode")] |
||
| 29 | public class T03_Immediate_Mode_Cube : GameWindow |
||
| 30 | { |
||
| 31 | #region --- Fields --- |
||
| 32 | |||
| 33 | const float rotation_speed = 180.0f; |
||
| 34 | float angle; |
||
| 35 | |||
| 36 | #endregion |
||
| 37 | |||
| 38 | #region --- Constructor --- |
||
| 39 | |||
| 40 | public T03_Immediate_Mode_Cube() : base(800, 600, new GraphicsMode(16, 16)) |
||
| 41 | { } |
||
| 42 | |||
| 43 | #endregion |
||
| 44 | |||
| 45 | #region OnLoad |
||
| 46 | |||
| 47 | protected override void OnLoad(EventArgs e) |
||
| 48 | { |
||
| 49 | base.OnLoad(e); |
||
| 50 | |||
| 51 | GL.ClearColor(Color.MidnightBlue); |
||
| 52 | GL.Enable(EnableCap.DepthTest); |
||
| 53 | } |
||
| 54 | |||
| 55 | #endregion |
||
| 56 | |||
| 57 | #region OnResize |
||
| 58 | |||
| 59 | /// <summary> |
||
| 60 | /// Called when the user resizes the window. |
||
| 61 | /// </summary> |
||
| 62 | /// <param name="e">Contains the new width/height of the window.</param> |
||
| 63 | /// <remarks> |
||
| 64 | /// You want the OpenGL viewport to match the window. This is the place to do it! |
||
| 65 | /// </remarks> |
||
| 66 | protected override void OnResize(EventArgs e) |
||
| 67 | { |
||
| 68 | base.OnResize(e); |
||
| 69 | |||
| 70 | GL.Viewport(0, 0, Width, Height); |
||
| 71 | |||
| 72 | double aspect_ratio = Width / (double)Height; |
||
| 73 | |||
| 74 | OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64); |
||
| 75 | GL.MatrixMode(MatrixMode.Projection); |
||
| 76 | GL.LoadMatrix(ref perspective); |
||
| 77 | } |
||
| 78 | |||
| 79 | #endregion |
||
| 80 | |||
| 81 | #region OnUpdateFrame |
||
| 82 | |||
| 83 | /// <summary> |
||
| 84 | /// Prepares the next frame for rendering. |
||
| 85 | /// </summary> |
||
| 86 | /// <remarks> |
||
| 87 | /// Place your control logic here. This is the place to respond to user input, |
||
| 88 | /// update object positions etc. |
||
| 89 | /// </remarks> |
||
| 90 | protected override void OnUpdateFrame(FrameEventArgs e) |
||
| 91 | { |
||
| 92 | base.OnUpdateFrame(e); |
||
| 93 | |||
| 94 | if (Keyboard[OpenTK.Input.Key.Escape]) |
||
| 95 | { |
||
| 96 | this.Exit(); |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | #endregion |
||
| 102 | |||
| 103 | #region OnRenderFrame |
||
| 104 | |||
| 105 | /// <summary> |
||
| 106 | /// Place your rendering code here. |
||
| 107 | /// </summary> |
||
| 108 | protected override void OnRenderFrame(FrameEventArgs e) |
||
| 109 | { |
||
| 110 | base.OnRenderFrame(e); |
||
| 111 | |||
| 112 | GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
||
| 113 | |||
| 114 | Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0); |
||
| 115 | GL.MatrixMode(MatrixMode.Modelview); |
||
| 116 | GL.LoadMatrix(ref lookat); |
||
| 117 | |||
| 118 | angle += rotation_speed * (float)e.Time; |
||
| 119 | GL.Rotate(angle, 0.0f, 1.0f, 0.0f); |
||
| 120 | |||
| 121 | DrawCube(); |
||
| 122 | |||
| 123 | this.SwapBuffers(); |
||
| 124 | Thread.Sleep(1); |
||
| 125 | } |
||
| 126 | |||
| 127 | #endregion |
||
| 128 | |||
| 129 | #region private void DrawCube() |
||
| 130 | |||
| 131 | private void DrawCube() |
||
| 132 | { |
||
| 133 | GL.Begin(BeginMode.Quads); |
||
| 134 | |||
| 135 | GL.Color3(Color.Silver); |
||
| 136 | GL.Vertex3(-1.0f, -1.0f, -1.0f); |
||
| 137 | GL.Vertex3(-1.0f, 1.0f, -1.0f); |
||
| 138 | GL.Vertex3(1.0f, 1.0f, -1.0f); |
||
| 139 | GL.Vertex3(1.0f, -1.0f, -1.0f); |
||
| 140 | |||
| 141 | GL.Color3(Color.Honeydew); |
||
| 142 | GL.Vertex3(-1.0f, -1.0f, -1.0f); |
||
| 143 | GL.Vertex3(1.0f, -1.0f, -1.0f); |
||
| 144 | GL.Vertex3(1.0f, -1.0f, 1.0f); |
||
| 145 | GL.Vertex3(-1.0f, -1.0f, 1.0f); |
||
| 146 | |||
| 147 | GL.Color3(Color.Moccasin); |
||
| 148 | |||
| 149 | GL.Vertex3(-1.0f, -1.0f, -1.0f); |
||
| 150 | GL.Vertex3(-1.0f, -1.0f, 1.0f); |
||
| 151 | GL.Vertex3(-1.0f, 1.0f, 1.0f); |
||
| 152 | GL.Vertex3(-1.0f, 1.0f, -1.0f); |
||
| 153 | |||
| 154 | GL.Color3(Color.IndianRed); |
||
| 155 | GL.Vertex3(-1.0f, -1.0f, 1.0f); |
||
| 156 | GL.Vertex3(1.0f, -1.0f, 1.0f); |
||
| 157 | GL.Vertex3(1.0f, 1.0f, 1.0f); |
||
| 158 | GL.Vertex3(-1.0f, 1.0f, 1.0f); |
||
| 159 | |||
| 160 | GL.Color3(Color.PaleVioletRed); |
||
| 161 | GL.Vertex3(-1.0f, 1.0f, -1.0f); |
||
| 162 | GL.Vertex3(-1.0f, 1.0f, 1.0f); |
||
| 163 | GL.Vertex3(1.0f, 1.0f, 1.0f); |
||
| 164 | GL.Vertex3(1.0f, 1.0f, -1.0f); |
||
| 165 | |||
| 166 | GL.Color3(Color.ForestGreen); |
||
| 167 | GL.Vertex3(1.0f, -1.0f, -1.0f); |
||
| 168 | GL.Vertex3(1.0f, 1.0f, -1.0f); |
||
| 169 | GL.Vertex3(1.0f, 1.0f, 1.0f); |
||
| 170 | GL.Vertex3(1.0f, -1.0f, 1.0f); |
||
| 171 | |||
| 172 | GL.End(); |
||
| 173 | } |
||
| 174 | |||
| 175 | #endregion |
||
| 176 | |||
| 177 | #region public static void Main() |
||
| 178 | |||
| 179 | /// <summary> |
||
| 180 | /// Entry point of this example. |
||
| 181 | /// </summary> |
||
| 182 | [STAThread] |
||
| 183 | public static void Main() |
||
| 184 | { |
||
| 185 | using (T03_Immediate_Mode_Cube example = new T03_Immediate_Mode_Cube()) |
||
| 186 | { |
||
| 187 | Utilities.SetWindowTitle(example); |
||
| 188 | example.Run(30.0, 0.0); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | #endregion |
||
| 193 | } |
||
| 194 | } |