GLWidgetBase.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // $Id$
  2. using System;
  3. using Gtk;
  4. using Gdk;
  5. using OpenGl;
  6. using DataStructures;
  7. using Drawing;
  8. public abstract class GLWidgetBase : GLArea
  9. {
  10. public static GLWidgetBase ShareArea = null;
  11. private float _Zoom = 1.0f;
  12. protected float Zoom
  13. {
  14. get
  15. {
  16. return _Zoom;
  17. }
  18. set
  19. {
  20. _Zoom = value;
  21. QueueDraw();
  22. }
  23. }
  24. private Vector _Translation;
  25. protected Vector Translation
  26. {
  27. get
  28. {
  29. return _Translation;
  30. }
  31. set
  32. {
  33. _Translation = value;
  34. QueueDraw();
  35. }
  36. }
  37. private static int[] attrlist = {
  38. GLContextAttributes.Rgba,
  39. GLContextAttributes.RedSize, 1,
  40. GLContextAttributes.GreenSize, 1,
  41. GLContextAttributes.BlueSize, 1,
  42. // not really needed but some opengl drivers are buggy and need this
  43. GLContextAttributes.DepthSize, 1,
  44. GLContextAttributes.DoubleBuffer,
  45. GLContextAttributes.None
  46. };
  47. public GLWidgetBase()
  48. : base(attrlist, ShareArea)
  49. {
  50. GlUtil.ContextValid = false;
  51. ExposeEvent += OnExposed;
  52. ConfigureEvent += OnConfigure;
  53. if(ShareArea == null) {
  54. ShareArea = this;
  55. }
  56. }
  57. private void OnExposed(object o, ExposeEventArgs args)
  58. {
  59. if(!MakeCurrent()) {
  60. LogManager.Log(LogLevel.Warning, "Make Current - OnExposed failed");
  61. return;
  62. }
  63. GlUtil.ContextValid = true;
  64. gl.MatrixMode(gl.MODELVIEW);
  65. gl.LoadIdentity();
  66. gl.Scalef(Zoom, Zoom, 1f);
  67. gl.Translatef(Translation.X, Translation.Y, 0f);
  68. DrawGl();
  69. GlUtil.Assert("After Drawing");
  70. SwapBuffers();
  71. GlUtil.ContextValid = false;
  72. }
  73. private void OnConfigure(object o, ConfigureEventArgs args)
  74. {
  75. if(!MakeCurrent()) {
  76. LogManager.Log(LogLevel.Warning, "MakeCurrent() - OnConfigure failed");
  77. return;
  78. }
  79. GlUtil.ContextValid = true;
  80. // setup opengl state and transform
  81. gl.Disable(gl.DEPTH_TEST);
  82. gl.Disable(gl.CULL_FACE);
  83. gl.Enable(gl.TEXTURE_2D);
  84. gl.Enable(gl.BLEND);
  85. gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  86. gl.ClearColor(0f, 0f, 0f, 0f);
  87. gl.Viewport(0, 0, Allocation.Width, Allocation.Height);
  88. gl.MatrixMode(gl.PROJECTION);
  89. gl.LoadIdentity();
  90. gl.Ortho(0, Allocation.Width, Allocation.Height, 0,
  91. -1.0f, 1.0f);
  92. gl.MatrixMode(gl.MODELVIEW);
  93. gl.LoadIdentity();
  94. GlUtil.Assert("After setting opengl transforms");
  95. GlUtil.ContextValid = false;
  96. }
  97. protected abstract void DrawGl();
  98. }