|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Windows.Forms; |
| 4 | +using OpenTK; |
| 5 | +using OpenTK.Graphics.OpenGL; |
| 6 | + |
| 7 | +namespace _056avatar |
| 8 | +{ |
| 9 | + public partial class Form1 |
| 10 | + { |
| 11 | + // Realtime based animation: |
| 12 | + private bool animate = true; |
| 13 | + private double timeInSeconds = 0.0; |
| 14 | + private double timeOffset = 0.0; |
| 15 | + |
| 16 | + #region Camera attributes |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Current camera position. |
| 20 | + /// </summary> |
| 21 | + private Vector3 eye = new Vector3( 1.0f, 2.0f, 10.0f ); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Current point to look at. |
| 25 | + /// </summary> |
| 26 | + private Vector3 pointAt = Vector3.Zero; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Current "up" vector. |
| 30 | + /// </summary> |
| 31 | + private Vector3 up = Vector3.UnitY; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Vertical field-of-view angle in radians. |
| 35 | + /// </summary> |
| 36 | + private float fov = 1.0f; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Camera's far point. |
| 40 | + /// </summary> |
| 41 | + private float far = 200.0f; |
| 42 | + |
| 43 | + #endregion |
| 44 | + |
| 45 | + #region Dynamics |
| 46 | + |
| 47 | + private double lastFrameTime = DateTime.Now.Ticks * 1.0e-7; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Current camera speed vector [1/sec]. |
| 51 | + /// </summary> |
| 52 | + private Vector3 dEye = new Vector3( 0.4f, 0.0f, -0.8f ); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Second derivative of the camera position (used in mouse-wheel handler). |
| 56 | + /// </summary> |
| 57 | + private Vector3 ddEye = new Vector3( 0.2f, 0.0f, -0.4f ); |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Current point-at speed vector [1/sec]. |
| 61 | + /// </summary> |
| 62 | + private Vector3 dPointAt = Vector3.Zero; |
| 63 | + |
| 64 | + #endregion |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Called in case the GLcontrol geometry changes. |
| 68 | + /// </summary> |
| 69 | + private void SetupViewport () |
| 70 | + { |
| 71 | + int wid = glControl1.Width; |
| 72 | + int hei = glControl1.Height; |
| 73 | + |
| 74 | + // 1. set ViewPort transform: |
| 75 | + GL.Viewport( 0, 0, wid, hei ); |
| 76 | + |
| 77 | + // 2. set projection matrix |
| 78 | + GL.MatrixMode( MatrixMode.Projection ); |
| 79 | + Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView( fov, wid / (float)hei, 0.1f, far ); |
| 80 | + GL.LoadMatrix( ref proj ); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Setup of a camera called for every frame prior to any rendering. |
| 85 | + /// </summary> |
| 86 | + private void SetCamera () |
| 87 | + { |
| 88 | + // !!!{{ TODO: add camera setup here |
| 89 | + |
| 90 | + // Animation based on simple linear dynamics: |
| 91 | + timeInSeconds = DateTime.Now.Ticks * 1.0e-7; |
| 92 | + if ( animate ) |
| 93 | + { |
| 94 | + float dTime = (float)(timeInSeconds - lastFrameTime); |
| 95 | + eye += dEye * dTime; |
| 96 | + pointAt += dPointAt * dTime; |
| 97 | + } |
| 98 | + lastFrameTime = timeInSeconds; |
| 99 | + |
| 100 | + // Current modelview matrix based on eye and pointAt position: |
| 101 | + GL.MatrixMode( MatrixMode.Modelview ); |
| 102 | + Matrix4 lookAt = Matrix4.LookAt( eye, pointAt, up ); |
| 103 | + GL.LoadMatrix( ref lookAt ); |
| 104 | + |
| 105 | + // !!!}} |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Rendering of one frame. |
| 110 | + /// </summary> |
| 111 | + private void Render () |
| 112 | + { |
| 113 | + if ( !loaded ) return; |
| 114 | + |
| 115 | + frameCounter++; |
| 116 | + GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit ); |
| 117 | + GL.ShadeModel( checkSmooth.Checked ? ShadingModel.Smooth : ShadingModel.Flat ); |
| 118 | + GL.PolygonMode( MaterialFace.FrontAndBack, checkWireframe.Checked ? PolygonMode.Line : PolygonMode.Fill ); |
| 119 | + |
| 120 | + SetCamera(); |
| 121 | + |
| 122 | + // Scene rendering: |
| 123 | + RenderScene(); |
| 124 | + |
| 125 | + glControl1.SwapBuffers(); |
| 126 | + } |
| 127 | + |
| 128 | + #region 2D variant |
| 129 | + |
| 130 | + private void SetupViewport2D () |
| 131 | + { |
| 132 | + int wid = glControl1.Width; |
| 133 | + int hei = glControl1.Height; |
| 134 | + |
| 135 | + GL.MatrixMode( MatrixMode.Projection ); |
| 136 | + GL.LoadIdentity(); |
| 137 | + GL.Ortho( 0, wid, 0, hei, -1, 1 ); |
| 138 | + GL.Viewport( 0, 0, wid, hei ); |
| 139 | + } |
| 140 | + |
| 141 | + private void Render2D () |
| 142 | + { |
| 143 | + if ( !loaded ) return; |
| 144 | + |
| 145 | + frameCounter++; |
| 146 | + GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit ); |
| 147 | + |
| 148 | + GL.MatrixMode( MatrixMode.Modelview ); |
| 149 | + GL.LoadIdentity(); |
| 150 | + GL.Translate( 200.0f, 200.0f, 0.0f ); |
| 151 | + |
| 152 | + // Animation: |
| 153 | + if ( animate ) |
| 154 | + timeInSeconds = DateTime.Now.Ticks * 1.0e-7; |
| 155 | + GL.Rotate( (float)Math.IEEERemainder( timeInSeconds * 45.0, 360.0 ), Vector3.UnitZ ); |
| 156 | + |
| 157 | + triangleCounter++; |
| 158 | + GL.Color3( Color.Yellow ); |
| 159 | + GL.Begin( BeginMode.Triangles ); |
| 160 | + GL.Vertex3( -20.0f, -20.0f, 0.0f ); |
| 161 | + GL.Vertex3( 30.0f, -25.0f, 0.0f ); |
| 162 | + GL.Vertex3( 0.0f, 40.0f, 0.0f ); |
| 163 | + GL.End(); |
| 164 | + |
| 165 | + glControl1.SwapBuffers(); |
| 166 | + } |
| 167 | + |
| 168 | + #endregion |
| 169 | + |
| 170 | + private void glControl1_MouseDown ( object sender, MouseEventArgs e ) |
| 171 | + { |
| 172 | + // !!!{{ TODO: add the event handler here |
| 173 | + // !!!}} |
| 174 | + } |
| 175 | + |
| 176 | + private void glControl1_MouseUp ( object sender, MouseEventArgs e ) |
| 177 | + { |
| 178 | + // !!!{{ TODO: add the event handler here |
| 179 | + // !!!}} |
| 180 | + } |
| 181 | + |
| 182 | + private void glControl1_MouseMove ( object sender, MouseEventArgs e ) |
| 183 | + { |
| 184 | + // !!!{{ TODO: add the event handler here |
| 185 | + // !!!}} |
| 186 | + } |
| 187 | + |
| 188 | + private void glControl1_MouseWheel ( object sender, MouseEventArgs e ) |
| 189 | + { |
| 190 | + // !!!{{ TODO: add the event handler here |
| 191 | + |
| 192 | + if ( e.Delta != 0 ) |
| 193 | + { |
| 194 | + float notches = e.Delta / 120.0f; |
| 195 | + dEye += ddEye * notches; |
| 196 | + } |
| 197 | + |
| 198 | + // !!!}} |
| 199 | + } |
| 200 | + |
| 201 | + private void glControl1_KeyDown ( object sender, KeyEventArgs e ) |
| 202 | + { |
| 203 | + // !!!{{ TODO: add the event handler here |
| 204 | + // !!!}} |
| 205 | + } |
| 206 | + |
| 207 | + private void glControl1_KeyUp ( object sender, KeyEventArgs e ) |
| 208 | + { |
| 209 | + // !!!{{ TODO: add the event handler here |
| 210 | + |
| 211 | + if ( e.KeyCode == Keys.R ) // R => reset camera position |
| 212 | + { |
| 213 | + eye = new Vector3( 1.0f, 2.0f, 10.0f ); |
| 214 | + dEye = Vector3.Zero; |
| 215 | + } |
| 216 | + |
| 217 | + if ( e.KeyCode == Keys.Space ) // space => toggle animation mode |
| 218 | + { |
| 219 | + if ( !animate ) // restart the animation |
| 220 | + timeOffset += DateTime.Now.Ticks * 1.0e-7 - timeInSeconds; |
| 221 | + animate = !animate; |
| 222 | + } |
| 223 | + |
| 224 | + // !!!}} |
| 225 | + } |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments