OutputTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using OpenTK;
  7. using OpenTK.Graphics.OpenGL;
  8. using MathNet.Numerics.LinearAlgebra.Single;
  9. using bbiwarg.DataSource;
  10. namespace bbiwarg.Test
  11. {
  12. class OutputTest : GameWindow
  13. {
  14. IVideoDataSource source;
  15. static void Main(string[] args)
  16. {
  17. OutputTest demo = new OutputTest();
  18. demo.initSource();
  19. demo.Run(30);
  20. }
  21. public void initSource()
  22. {
  23. source = new IIsuDataSource();
  24. source.init();
  25. source.start();
  26. }
  27. protected override void OnLoad(EventArgs e)
  28. {
  29. base.OnLoad(e);
  30. Title = "OutputTest";
  31. GL.ClearColor(Color.Black);
  32. }
  33. protected override void OnRenderFrame(FrameEventArgs e)
  34. {
  35. base.OnRenderFrame(e);
  36. bool isActive = source.isActive();
  37. if (isActive)
  38. {
  39. source.updateFrame();
  40. }
  41. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  42. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  43. GL.MatrixMode(MatrixMode.Modelview);
  44. GL.LoadMatrix(ref modelview);
  45. DepthImage depthImage = source.getDepthImage();
  46. ConfidenceImage confidenceImage = source.getConfidenceImage();
  47. int width = depthImage.getWidth();
  48. int height = depthImage.getHeight();
  49. for (int x = 0; x < width-1; x++) {
  50. for (int y = 0; y < height-1; y++)
  51. {
  52. short confidence = confidenceImage.getConfidence(x,y);
  53. if(confidence > 100) {
  54. short depth = depthImage.getDepth(x, y);
  55. int relX = x - width / 2;
  56. int relY = height/2 - y;
  57. GL.Begin(BeginMode.Polygon);
  58. GL.Vertex3(relX-0.5f, relY+0.5f, -depth);
  59. GL.Vertex3(relX+0.5f, relY+0.5f, -depth);
  60. GL.Vertex3(relX+0.5f, relY-0.5f, -depth);
  61. GL.Vertex3(relX-0.5f, relY-0.5f, -depth);
  62. GL.End();
  63. }
  64. }
  65. }
  66. /*
  67. GL.Begin(BeginMode.Triangles);
  68. GL.Vertex3(0.0f, 0.0f, -4.0f);
  69. GL.Vertex3(2.0f, 0.0f, -4.0f);
  70. GL.Vertex3(0.0f, 1.0f, -4.0f);
  71. GL.End();
  72. */
  73. SwapBuffers();
  74. if(isActive) {
  75. source.releaseFrame();
  76. }
  77. /*
  78. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  79. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
  80. GL.MatrixMode(MatrixMode.Modelview);
  81. GL.LoadMatrix(ref modelview);
  82. // triangle
  83. GL.Begin(BeginMode.Triangles);
  84. float x = palmPosition.Value._X;
  85. float y = palmPosition.Value._Y;
  86. float z = palmPosition.Value._Z;
  87. GL.Vertex3(-1.0f + x, -1.0f + z, 4.0f - 10 * y);
  88. GL.Vertex3(1.0f, -1.0f, 4.0f);
  89. GL.Vertex3(0.0f, 1.0f, 4.0f);
  90. GL.End();
  91. SwapBuffers();*/
  92. }
  93. protected override void OnResize(EventArgs e)
  94. {
  95. base.OnResize(e);
  96. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  97. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 3000.0f);
  98. GL.MatrixMode(MatrixMode.Projection);
  99. GL.LoadMatrix(ref projection);
  100. }
  101. }
  102. }