/Tutorial05/Tutorial.cs

https://bitbucket.org/kstewart83/opentk-course · C# · 115 lines · 73 code · 27 blank · 15 comment · 0 complexity · d7675bc60204aef73978aafaf556217c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenTK;
  5. using OpenTK.Graphics;
  6. using OpenTK.Graphics.OpenGL;
  7. namespace Tutorial05
  8. {
  9. public class Tutorial : GameWindow
  10. {
  11. const string TITLE = "Tutorial #5";
  12. const int WIDTH = 800;
  13. const int HEIGHT = 600;
  14. //private float rotation = 0.0f; [REMOVED]
  15. private int charTextureId;
  16. private float charSize = 64;
  17. private int charAnimIndex = 0; // An index value into the current state of the animation.
  18. // An array of texture coordinates that match each frame of the animation. I'm only using the first
  19. // 7 frames of the image here (the eighth is a standing image, so I'm not going to include it yet).
  20. private Vector2[] charTextureIndexes = new Vector2[] {
  21. new Vector2(0f, 0f),
  22. new Vector2(0.25f, 0f),
  23. new Vector2(0.5f, 0f),
  24. new Vector2(0.75f, 0f),
  25. new Vector2(0f, 0.25f),
  26. new Vector2(0.25f, 0.25f),
  27. new Vector2(0.5f, 0.25f),
  28. };
  29. public Tutorial() : base(WIDTH, HEIGHT, GraphicsMode.Default, TITLE) { }
  30. protected override void OnLoad(EventArgs e)
  31. {
  32. base.OnLoad(e);
  33. GL.ClearColor(0.1f, 0.1f, 0.1f, 1f);
  34. GL.Enable(EnableCap.Texture2D);
  35. // Add OpenGL state for blending to allow for transparency.
  36. GL.Enable(EnableCap.Blend);
  37. // This may seem a bit confusing, but all it really means is that we want to blend the pixels of the
  38. // source image based on its alpha (1 = fully opaque, 0.5 = half visible, 0 = fully transparent)
  39. // with the pixels behind it based on the inverse of its alpha (1 = fully covered, 0.5 = half covered,
  40. // 0 = fully visible). This basically means that transparent pixels in the image are ignored.
  41. GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  42. // Changed the texture to load an 8-frame tiled animation of our caveman friend.
  43. charTextureId = Utilities.LoadTexture(@"Images\caveman-tiled.png");
  44. }
  45. protected override void OnResize(EventArgs e)
  46. {
  47. base.OnResize(e);
  48. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  49. Matrix4 projection = Matrix4.CreateOrthographic(ClientRectangle.Width, ClientRectangle.Height, -1f, 1f);
  50. GL.MatrixMode(MatrixMode.Projection);
  51. GL.LoadMatrix(ref projection);
  52. GL.MatrixMode(MatrixMode.Modelview);
  53. }
  54. protected override void OnUpdateFrame(FrameEventArgs e)
  55. {
  56. base.OnUpdateFrame(e);
  57. // When the frame is updated, we want to increase the animation index by one, but clamp it to the values
  58. // between 0 and 6 using a modulus operator. This means it will rollback to 0 before it reaches 7.
  59. charAnimIndex = (charAnimIndex + 1) % 7;
  60. //rotation += (float)e.Time * 360; [REMOVED]
  61. }
  62. protected override void OnRenderFrame(FrameEventArgs e)
  63. {
  64. base.OnRenderFrame(e);
  65. GL.LoadIdentity();
  66. //GL.Rotate(rotation, Vector3.UnitZ); [REMOVED]
  67. GL.Clear(ClearBufferMask.ColorBufferBit);
  68. GL.Begin(BeginMode.Quads);
  69. GL.BindTexture(TextureTarget.Texture2D, charTextureId);
  70. // Modified the texture coordinates to use the values from the current animation state, and adding an additional 0.25f
  71. // for the vertices that are on the right or bottom of the frame.
  72. GL.TexCoord2(charTextureIndexes[charAnimIndex] + new Vector2(0f, 0f)); GL.Vertex3(-charSize / 2, charSize / 2, 0);
  73. GL.TexCoord2(charTextureIndexes[charAnimIndex] + new Vector2(0f, 0.25f)); GL.Vertex3(-charSize / 2, -charSize / 2, 0);
  74. GL.TexCoord2(charTextureIndexes[charAnimIndex] + new Vector2(0.25f, 0.25f)); GL.Vertex3(charSize / 2, -charSize / 2, 0);
  75. GL.TexCoord2(charTextureIndexes[charAnimIndex] + new Vector2(0.25f, 0f)); GL.Vertex3(charSize / 2, charSize / 2, 0);
  76. GL.BindTexture(TextureTarget.Texture2D, 0);
  77. GL.End();
  78. SwapBuffers();
  79. }
  80. static void Main(string[] args)
  81. {
  82. using (Tutorial tutorial = new Tutorial())
  83. {
  84. tutorial.Run(10.0f); // Lower the update calls to only happen 10 times a second.
  85. }
  86. }
  87. }
  88. }