123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OpenTK;
- using OpenTK.Graphics.OpenGL;
- using System.Drawing;
- using System.Diagnostics;
- using MathNet.Numerics.LinearAlgebra.Single;
- using bbiwarg.Graphics.GraphicElements2D;
- using bbiwarg.InputProviders;
- using bbiwarg.Images;
- using bbiwarg.Helpers;
- using bbiwarg.VideoHandles;
- namespace bbiwarg.Graphics
- {
- class Output2D: GameWindow
- {
- private IVideoHandle videoHandle;
- private uint textureId;
- public Output2D(IVideoHandle videoHandle): base(3 * 320, 3 * 240)
- {
- this.videoHandle = videoHandle;
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Title = "OutputTest";
- GL.ClearColor(Color.Black);
- // transparency
- GL.Enable(EnableCap.Blend);
- GL.BlendEquation(BlendEquationMode.Max);
- //Depth Test
- GL.Enable(EnableCap.DepthTest);
- // Textures
- GL.Enable(EnableCap.Texture2D);
- GL.GenTextures(1, out textureId);
- GL.BindTexture(TextureTarget.Texture2D, textureId);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
- }
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- base.OnRenderFrame(e);
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadMatrix(ref modelview);
- videoHandle.nextFrame();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- short[] textureData = new short[3 * videoHandle.getWidth() * videoHandle.getHeight()];
- int index = 0;
- short minDepth = videoHandle.getHandImage().getMinDepth();
- short maxDepth = videoHandle.getHandImage().getMaxDepth();
-
- for (int y = 0; y < videoHandle.getHeight(); ++y)
- {
- for (int x = 0; x < videoHandle.getWidth(); ++x)
- {
- short depth = videoHandle.getHandImage().getDepth(x, y);
- short diff = (short) Math.Max(maxDepth - minDepth, 1);
- short scaledDepth = (short) (Int16.MaxValue - (((int) depth - minDepth) * (int) Int16.MaxValue / diff));
- textureData[index] = scaledDepth;
- textureData[index + 1] = scaledDepth;
- textureData[index + 2] = scaledDepth;
- index += 3;
- }
- }
- /*GL.BindTexture(TextureTarget.Texture2D, textureId);
- GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, videoHandle.getWidth(), videoHandle.getHeight(), 0,
- PixelFormat.Rgb, PixelType.Short, textureData);
- float size_2 = 0.5f / 2.0f;
- GL.Begin(PrimitiveType.Quads);
- GL.Color3(1.0, 1.0, 1.0);
- GL.TexCoord2(0.0, 0.0); GL.Vertex3(-size_2, size_2, -0.5);
- GL.TexCoord2(1.0, 0.0); GL.Vertex3( size_2, size_2, -0.5);
- GL.TexCoord2(1.0, 1.0); GL.Vertex3( size_2, -size_2, -0.5);
- GL.TexCoord2(0.0, 1.0); GL.Vertex3(-size_2, -size_2, -0.5);
- GL.End();*/
- int[] histogram = videoHandle.getHistogram();
- int maxValue = 0;
- for (int i = 0; i < histogram.Length; ++i)
- {
- if (histogram[i] > maxValue)
- maxValue = histogram[i];
- }
- GL.Begin(PrimitiveType.LineStrip);
- GL.Color3(0, 0, 1.0);
- GL.LineWidth(5.0f);
- for (int i = 0; i < histogram.Length; ++i)
- {
- GL.Vertex3(-0.25 + i * (0.5 / histogram.Length), -0.25 + histogram[i] * (0.5 / (maxValue - 5)), -0.5);
- }
- GL.End();
- sw.Stop();
- Console.WriteLine(sw.ElapsedMilliseconds);
- SwapBuffers();
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
- Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref projection);
- }
- }
- }
|