Permalink
Browse files

Going to stick with two windows for now. Added some UI stuff to switc…

…h between point cloud and color map. Still need to figure out OpenGL stuff for point cloud.

Signed-off-by: Aditya Gaddam <adityagaddam@gmail.com> (LostInCake)
  • Loading branch information...
1 parent 3e34a2a commit 66e7b95a2ed61618a19ef96e846d2f9bae9d3365 @lostinspacebar lostinspacebar committed May 9, 2011
@@ -22,10 +22,30 @@ public DepthPreviewMode PreviewMode
set;
}
+ /// <summary>
+ /// Context menu for the depth window. This lets us change things like the preview mode
+ /// </summary>
+ private ContextMenuStrip contextMenu;
+
// Gamma constants for color map generation
private UInt16[] gamma = new UInt16[2048];
/// <summary>
+ /// Point cloud data array
+ /// </summary>
+ private Vector3[] pointCloudData;
+
+ /// <summary>
+ /// Vertex buffer for the point cloud
+ /// </summary>
+ int pointCloudVertexBuffer;
+
+ /// <summary>
+ /// Projection for point cloud
+ /// </summary>
+ Matrix4 pointCloudProjection;
+
+ /// <summary>
/// Constructor
/// </summary>
public DepthPreviewWindow() : base()
@@ -38,6 +58,22 @@ public DepthPreviewWindow() : base()
gamma[i] = (UInt16)(v * 6.0 * 256.0);
}
+ // Initialize context menu
+ this.contextMenu = new ContextMenuStrip();
+ ToolStripItem colorMapModeItem = new ToolStripButton("Color Map");
+ colorMapModeItem.Click += delegate {
+ this.PreviewMode = DepthPreviewWindow.DepthPreviewMode.ColorMap;
+ this.Resize(this.Mode);
+ };
+ ToolStripItem pointCloudModeItem = new ToolStripButton("Point Cloud");
+ pointCloudModeItem.Click += delegate {
+ this.PreviewMode = DepthPreviewWindow.DepthPreviewMode.PointCloud;
+ this.Resize(this.Mode);
+ };
+ this.contextMenu.Items.Add(colorMapModeItem);
+ this.contextMenu.Items.Add(pointCloudModeItem);
+ this.ContextMenuStrip = this.contextMenu;
+
// Color map rendering by default
this.PreviewMode = DepthPreviewWindow.DepthPreviewMode.ColorMap;
}
@@ -69,6 +105,34 @@ public override void HandleBackBufferUpdate()
}
/// <summary>
+ /// Translates new back buffer data for point cloud
+ /// </summary>
+ private void HandleBackBufferUpdatePointCloud()
+ {
+ DepthFrameMode mode = this.Mode as DepthFrameMode;
+ if(mode.Format == DepthFormat.Depth10Bit || mode.Format == DepthFormat.Depth11Bit)
+ {
+ this.HandleBackBufferUpdatePointCloudUnpacked();
+ }
+ }
+
+ /// <summary>
+ /// Translates back buffer data for unpacked 11bit or 10it data
+ /// </summary>
+ private void HandleBackBufferUpdatePointCloudUnpacked()
+ {
+ unsafe
+ {
+ Int16 *ptrBack = (Int16 *)this.previewDataBuffers.GetHandle(2);
+ int dim = this.Mode.Width * this.Mode.Height;
+ for(int i = 0; i < dim; i++)
+ {
+ this.pointCloudData[i].Z = ptrBack[i];
+ }
+ }
+ }
+
+ /// <summary>
/// Translates new back buffer data into the middle buffer as a color map
/// </summary>
private void HandleBackBufferUpdateColorMap()
@@ -151,15 +215,97 @@ public override void RenderPreview()
case DepthPreviewMode.ColorMap:
this.RenderColorMap();
break;
+ case DepthPreviewMode.PointCloud:
+ this.RenderPointCloud();
+ break;
}
}
/// <summary>
+ /// Renders the preview data as a point cloud
+ /// </summary>
+ private void RenderPointCloud()
+ {
+ GL.Clear(ClearBufferMask.ColorBufferBit |
+ ClearBufferMask.DepthBufferBit|
+ ClearBufferMask.StencilBufferBit);
+
+ GL.ClearColor(Color.Black);
+ Console.Write("-");
+ GL.Color3(Color.White);
+ GL.PointSize(2.0f);
+
+ Matrix4 lookat = Matrix4.LookAt(0, 128, 256, 0, 0, 0, 0, 1, 0);
+ Vector3 scale = new Vector3(4, 4, 4);
+ GL.MatrixMode(MatrixMode.Modelview);
+ GL.LoadMatrix(ref lookat);
+ GL.Scale(scale);
+
+ GL.EnableClientState(ArrayCap.VertexArray);
+ GL.BindBuffer(BufferTarget.ArrayBuffer, this.pointCloudVertexBuffer);
+ GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, new IntPtr(0));
+ GL.DrawArrays(BeginMode.Points, 0, this.pointCloudData.Length);
+ }
+
+ /// <summary>
/// Renders the preview data as a color map
/// </summary>
private void RenderColorMap()
{
+ // Use preview texture for rendering
+ GL.BindTexture(TextureTarget.Texture2D, this.previewTexture);
+
+ // Setup texture
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Three, this.Mode.Width, this.Mode.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelType.UnsignedByte, this.previewDataBuffers[0]);
+
+ // Draw texture
+ GL.Begin(BeginMode.TriangleFan);
+ GL.Color4(255.0f, 255.0f, 255.0f, 255.0f);
+ GL.TexCoord2(0, 0); GL.Vertex3(0, 0, 0);
+ GL.TexCoord2(1, 0); GL.Vertex3(this.Mode.Width, 0, 0);
+ GL.TexCoord2(1, 1); GL.Vertex3(this.Mode.Width, this.Mode.Height, 0);
+ GL.TexCoord2(0, 1); GL.Vertex3(0, this.Mode.Height, 0);
+ GL.End();
+ }
+
+ /// <summary>
+ /// Resize stuff for new mode
+ /// </summary>
+ /// <param name="mode">
+ /// A <see cref="FrameMode"/>
+ /// </param>
+ public override void Resize(FrameMode mode)
+ {
+ base.Resize(mode);
+
+ if(this.PreviewMode == DepthPreviewWindow.DepthPreviewMode.PointCloud)
+ {
+ // Resize point cloud array
+ this.pointCloudData = new Vector3[mode.Width * mode.Height];
+ int count = 0;
+ for(int y = 0; y < this.Mode.Height; y++)
+ {
+ for(int x = 0; x < this.Mode.Width; x++)
+ {
+ this.pointCloudData[count].X = x;
+ this.pointCloudData[count].Y = y;
+ count++;
+ }
+ }
+
+ // Generate vertex buffer
+ GL.GenBuffers(1, out this.pointCloudVertexBuffer);
+ GL.BindBuffer(BufferTarget.ArrayBuffer, this.pointCloudVertexBuffer);
+ GL.BufferData(BufferTarget.ArrayBuffer,
+ new IntPtr(this.pointCloudData.Length * BlittableValueType.StrideOf(this.pointCloudData)),
+ this.pointCloudData, BufferUsageHint.StaticDraw);
+
+ // Setup projection matrix
+ float aspect_ratio = this.Mode.Width / (float)this.Mode.Height;
+ this.pointCloudProjection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 512);
+ GL.MatrixMode(MatrixMode.Projection);
+ GL.LoadMatrix(ref this.pointCloudProjection);
+ }
}
/// <summary>
@@ -43,6 +43,9 @@ public override void RenderPreview()
// Swap middle and front buffers (we will be rendering off of front buffer)
this.previewDataBuffers.Swap(0, 1);
+ // Use preview texture for rendering
+ GL.BindTexture(TextureTarget.Texture2D, this.previewTexture);
+
// Setup texture
VideoFormat format = ((VideoFrameMode)this.Mode).Format;
switch(format)
@@ -54,6 +57,15 @@ public override void RenderPreview()
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.One, this.Mode.Width, this.Mode.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Luminance, PixelType.UnsignedByte, this.previewDataBuffers[0]);
break;
}
+
+ // Draw texture
+ GL.Begin(BeginMode.TriangleFan);
+ GL.Color4(255.0f, 255.0f, 255.0f, 255.0f);
+ GL.TexCoord2(0, 0); GL.Vertex3(0, 0, 0);
+ GL.TexCoord2(1, 0); GL.Vertex3(this.Mode.Width, 0, 0);
+ GL.TexCoord2(1, 1); GL.Vertex3(this.Mode.Width, this.Mode.Height, 0);
+ GL.TexCoord2(0, 1); GL.Vertex3(0, this.Mode.Height, 0);
+ GL.End();
}
}
@@ -92,7 +92,7 @@ public PreviewWindow()
/// <param name="mode">
/// A <see cref="FrameMode"/>
/// </param>
- public new void Resize(FrameMode mode)
+ public new virtual void Resize(FrameMode mode)
{
// Make this window's control current
this.renderPanel.MakeCurrent();
@@ -152,23 +152,11 @@ private void HandleRenderPanelPaint (object sender, PaintEventArgs e)
// Do we have new data?
if(this.newDataPending)
{
- // Use preview texture for rendering
- GL.BindTexture(TextureTarget.Texture2D, this.previewTexture);
-
// Yes! Render
this.IsBusy = true;
this.RenderPreview();
this.IsBusy = false;
- // Draw texture
- GL.Begin(BeginMode.TriangleFan);
- GL.Color4(255.0f, 255.0f, 255.0f, 255.0f);
- GL.TexCoord2(0, 0); GL.Vertex3(0, 0, 0);
- GL.TexCoord2(1, 0); GL.Vertex3(this.Mode.Width, 0, 0);
- GL.TexCoord2(1, 1); GL.Vertex3(this.Mode.Width, this.Mode.Height, 0);
- GL.TexCoord2(0, 1); GL.Vertex3(0, this.Mode.Height, 0);
- GL.End();
-
// No more new data
this.newDataPending = false;
}
@@ -57,6 +57,9 @@
<Compile Include="..\..\..\lib\VideoPreviewWindow.cs">
<Link>VideoPreviewWindow.cs</Link>
</Compile>
+ <Compile Include="..\PreviewControl.cs">
+ <Link>PreviewControl.cs</Link>
+ </Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Drawing" />
@@ -71,7 +74,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\OpenTK\OpenTK.dll</HintPath>
</Reference>
- <Reference Include="freenect, Version=1.0.4145.1756, Culture=neutral, PublicKeyToken=null">
+ <Reference Include="freenect, Version=1.0.4145.23674, Culture=neutral, PublicKeyToken=null">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\bin\freenect.dll</HintPath>
</Reference>

0 comments on commit 66e7b95

Please sign in to comment.