|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Drawing; |
| 4 | +using Examples.Shaders; |
| 5 | +using ObjectTK.Buffers; |
| 6 | +using ObjectTK.Shaders; |
| 7 | +using ObjectTK.Textures; |
| 8 | +using ObjectTK.Tools.Shapes; |
| 9 | +using OpenTK; |
| 10 | +using OpenTK.Graphics.OpenGL; |
| 11 | +using OpenTK.Input; |
| 12 | + |
| 13 | +namespace Examples.BasicExamples |
| 14 | +{ |
| 15 | + [ExampleProject("Rotating textured cube rendering")] |
| 16 | + public class RotatingTexturedCubeExample |
| 17 | + : ExampleWindow |
| 18 | + { |
| 19 | + private Texture2D _texture; |
| 20 | + |
| 21 | + private SimpleTextureProgram _textureProgram; |
| 22 | + |
| 23 | + private TexturedCube _cube; |
| 24 | + private VertexArray _cubeVao; |
| 25 | + |
| 26 | + private Matrix4 _baseView; |
| 27 | + private Matrix4 _objectView; |
| 28 | + |
| 29 | + private Vector3[] _rotateVectors = new[] { Vector3.Zero, Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ, Vector3.One }; |
| 30 | + private const int _defaultRotateIndex = 4; |
| 31 | + |
| 32 | + private int _rotateIndex = _defaultRotateIndex; |
| 33 | + private readonly Stopwatch _stopwatch = new Stopwatch(); |
| 34 | + |
| 35 | + public RotatingTexturedCubeExample() |
| 36 | + { |
| 37 | + Load += OnLoad; |
| 38 | + RenderFrame += OnRenderFrame; |
| 39 | + KeyDown += OnKeyDown; |
| 40 | + } |
| 41 | + |
| 42 | + private void OnKeyDown(object sender, KeyboardKeyEventArgs e) |
| 43 | + { |
| 44 | + switch (e.Key) |
| 45 | + { |
| 46 | + case Key.R: |
| 47 | + _objectView = _baseView = Matrix4.Identity; |
| 48 | + _rotateIndex = _defaultRotateIndex; |
| 49 | + _stopwatch.Restart(); |
| 50 | + break; |
| 51 | + |
| 52 | + case Key.Space: |
| 53 | + _baseView = _objectView; |
| 54 | + _rotateIndex = (_rotateIndex + 1) % _rotateVectors.Length; |
| 55 | + _stopwatch.Restart(); |
| 56 | + break; |
| 57 | + |
| 58 | + case Key.Number0: |
| 59 | + case Key.Number1: |
| 60 | + case Key.Number2: |
| 61 | + case Key.Number3: |
| 62 | + case Key.Number4: |
| 63 | + _baseView = _objectView; |
| 64 | + _rotateIndex = (e.Key - Key.Number0) % _rotateVectors.Length; |
| 65 | + _stopwatch.Restart(); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void OnLoad(object sender, EventArgs e) |
| 71 | + { |
| 72 | + // load texture from file |
| 73 | + using (var bitmap = new Bitmap("Data/Textures/crate.png")) |
| 74 | + { |
| 75 | + BitmapTexture.CreateCompatible(bitmap, out _texture); |
| 76 | + _texture.LoadBitmap(bitmap); |
| 77 | + } |
| 78 | + |
| 79 | + // initialize shaders |
| 80 | + _textureProgram = ProgramFactory.Create<SimpleTextureProgram>(); |
| 81 | + |
| 82 | + // initialize cube object and base view matrix |
| 83 | + _objectView = _baseView = Matrix4.Identity; |
| 84 | + |
| 85 | + // initialize demonstration geometry |
| 86 | + _cube = new TexturedCube(); |
| 87 | + _cube.UpdateBuffers(); |
| 88 | + |
| 89 | + // set up vertex attributes for the quad |
| 90 | + _cubeVao = new VertexArray(); |
| 91 | + _cubeVao.Bind(); |
| 92 | + _cubeVao.BindAttribute(_textureProgram.InPosition, _cube.VertexBuffer); |
| 93 | + _cubeVao.BindAttribute(_textureProgram.InTexCoord, _cube.TexCoordBuffer); |
| 94 | + |
| 95 | + // Enable culling, our cube vertices are defined inside out, so we flip them |
| 96 | + GL.Enable(EnableCap.CullFace); |
| 97 | + GL.CullFace(CullFaceMode.Back); |
| 98 | + |
| 99 | + // initialize camera position |
| 100 | + Camera.DefaultState.Position = new Vector3(0, 0, 4); |
| 101 | + Camera.ResetToDefault(); |
| 102 | + |
| 103 | + // set nice clear color |
| 104 | + GL.ClearColor(Color.MidnightBlue); |
| 105 | + |
| 106 | + _stopwatch.Restart(); |
| 107 | + } |
| 108 | + |
| 109 | + private void OnRenderFrame(object sender, OpenTK.FrameEventArgs e) |
| 110 | + { |
| 111 | + // set up viewport |
| 112 | + GL.Viewport(0, 0, Width, Height); |
| 113 | + GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
| 114 | + SetupPerspective(); |
| 115 | + |
| 116 | + // determinate object view rotation vectors and apply them |
| 117 | + _objectView = _baseView; |
| 118 | + var rotation = _rotateVectors[_rotateIndex]; |
| 119 | + if (rotation != Vector3.Zero) |
| 120 | + _objectView *= Matrix4.CreateFromAxisAngle(_rotateVectors[_rotateIndex], (float)(_stopwatch.Elapsed.TotalSeconds * 1.0)); |
| 121 | + |
| 122 | + // set transformation matrix |
| 123 | + _textureProgram.Use(); |
| 124 | + _textureProgram.ModelViewProjectionMatrix.Set(_objectView * ModelView * Projection); |
| 125 | + |
| 126 | + // render cube with texture |
| 127 | + _cubeVao.Bind(); |
| 128 | + _cubeVao.DrawArrays(_cube.DefaultMode, 0, _cube.VertexBuffer.ElementCount); |
| 129 | + |
| 130 | + // swap buffers |
| 131 | + SwapBuffers(); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments