public void InitializeRender(EventType eventType, viz.Context context)
        {
            switch (eventType)
            {
                case EventType.Monitor:
                    this.InitializeRender(context, this.monitorData);
                    break;

                case EventType.Inspection:
                    this.InitializeRender(context, this.inspectionData);
                    break;
            }
        }
        public void InitializeRender(EventType eventType, viz.Context context)
        {
            if (context != null)
            {
                lock (this.lockObj)
                {
                    switch (eventType)
                    {
                        case EventType.Monitor:
                            this.monitorAccessory = new viz.Accessory(context, 
                                AccessoryPlugin.cKinectSensorFovX, AccessoryPlugin.cKinectSensorFovY,
                                AccessoryPlugin.cKinectSensorMinZmm, AccessoryPlugin.cKinectSensorMaxZmm);
                            break;

                        case EventType.Inspection:
                            this.inspectionAccessory = new viz.Accessory(context,
                                AccessoryPlugin.cKinectSensorFovX, AccessoryPlugin.cKinectSensorFovY,
                                AccessoryPlugin.cKinectSensorMinZmm, AccessoryPlugin.cKinectSensorMaxZmm);
                            break;
                    }
                }
            }
        }
 protected virtual void OnBeginRender(viz.Context context) { }
        // object owning data should be locked
        private static void CreateTextures(EventTypePluginData data, viz.Context context)
        {
            Debug.Assert(data != null);

            if (data.rawIrTexture != null)
            {
                data.rawIrTexture.Dispose();
                data.rawIrTexture = null;
            }

            if (data.irTexture != null)
            {
                data.irTexture.Dispose();
                data.irTexture = null;
            }

            if (data.depthTexture != null)
            {
                data.depthTexture.Dispose();
                data.depthTexture = null;
            }

            if (data.depthMap != null)
            {
                data.depthMap.Dispose();
                data.depthMap = null;
            }

            if (data.uvTable != null)
            {
                data.uvTable.Dispose();
                data.uvTable = null;
            }

            data.uvTable = new HGlobalBuffer(data.imageWidth * data.imageHeight * 2 * sizeof(float));

            if (context != null)
            {
                data.rawIrTexture = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.R16_UNORM, false);
                data.irTexture = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.R8G8B8A8_UNORM, true);

                data.depthTexture = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.R8G8B8A8_UNORM, true); // need to be created as render target for GPU conversion
                data.depthMap = new viz.DepthMap(context, data.imageWidth, data.imageHeight, RawIrPlugin.cFovX, RawIrPlugin.cFovY, RawIrPlugin.cMinZmm, RawIrPlugin.cMaxZmm);
            }

            data.depthFrame = new ushort[data.imageWidth * data.imageHeight];
            data.irFrame = new ushort[data.imageWidth * data.imageHeight];
        }
        public void Render3D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture)
        {
            lock (this.lockObj)
            {
                nui.Registration registration = null;
                if (this.pluginService != null)
                {
                    registration = pluginService.GetRegistration(eventType);
                }
                switch (eventType)
                {
                    case EventType.Monitor:
                        RawIrPlugin.Render3D(pluginViewSettings, texture, this.monitorData, registration);
                        break;

                    case EventType.Inspection:
                        RawIrPlugin.Render3D(pluginViewSettings, texture, this.inspectionData, registration);
                        break;
                }
            }
        }
        public void InitializeRender(EventType eventType, viz.Context context)
        {
            switch (eventType)
            {
                case EventType.Monitor:
                    Debug.Assert(this.monitorData != null);
                    break;

                case EventType.Inspection:
                    Debug.Assert(this.inspectionData != null);

                    if (this.inspectionData.sharedRawFrame != null)
                    {
                        EventTypePluginData data = this.inspectionData; 

                        RawIrPlugin.CreateTextures(this.inspectionData, context);
                        DepthIrEngine depthIrEngine = this.pluginService.DepthIrEngine;

                        if ((depthIrEngine != null) && (data.depthFrame != null) && (data.irFrame != null))
                        {
                            unsafe
                            {
                                fixed (ushort* pDepthFrame = &data.depthFrame[0], pIrFrame = &data.irFrame[0])
                                {
                                    depthIrEngine.HandleEvent(data.sharedRawFrame.Buffer, data.sharedRawFrame.Size, pDepthFrame, pIrFrame, data.imageWidth, data.imageHeight);

                                    uint imageDataSize = data.imageWidth * data.imageHeight * sizeof(ushort);
                                    if (data.rawIrTexture != null)
                                    {
                                        data.rawIrTexture.UpdateData((byte*)pIrFrame, imageDataSize);
                                    }

                                    if (data.depthMap != null)
                                    {
                                        data.depthMap.UpdateData(pDepthFrame, imageDataSize);
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        }
        private void InitializeRender(viz.Context context, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            lock (this.lockObj)
            {
                Debug.Assert(data.rampTexture3d == null);
                Debug.Assert(data.rampTexture2d == null);

                if (context != null)
                {
                    BodyIndexPlugin.CreateTextures(data, context);

                    // 2D ramp texture is different from 3D ramp texture for visualization with skeleton
                    uint[] rampData = new uint[BodyIndexPlugin.cRampTextureLength];
                    rampData[0] = 0xFFFF0000;
                    rampData[1] = 0xFF00FF00;
                    rampData[2] = 0xFF40FFFF;
                    rampData[3] = 0xFFFFFF40;
                    rampData[4] = 0xFFFF40FF;
                    rampData[5] = 0xFF8080FF;
                    for (int i = 6; i < rampData.Length; ++i)
                    {
                        rampData[i] = 0xFFFFFFFF;
                    }

                    data.rampTexture3d = new viz.Texture(context, (uint)rampData.Length, 1, viz.TextureFormat.B8G8R8A8_UNORM, false);

                    unsafe
                    {
                        fixed (uint* pRampData = rampData)
                        {
                            data.rampTexture3d.UpdateData((byte*)pRampData, (uint)rampData.Length * sizeof(uint));
                        }
                    }

                    rampData[0] = 0xC0FF0000;
                    rampData[1] = 0xC000FF00;
                    rampData[2] = 0xC040FFFF;
                    rampData[3] = 0xC0FFFF40;
                    rampData[4] = 0xC0FF40FF;
                    rampData[5] = 0xC08080FF;
                    for (int i = 6; i < rampData.Length; i++)
                    {
                        rampData[i] = 0xFF000000;
                    }

                    data.rampTexture2d = new viz.Texture(context, (uint)rampData.Length, 1, viz.TextureFormat.B8G8R8A8_UNORM, false);

                    unsafe
                    {
                        fixed (uint* pRampData = rampData)
                        {
                            data.rampTexture2d.UpdateData((byte*)pRampData, (uint)rampData.Length * sizeof(uint));
                        }
                    }
                }
            }
        }
        // object owning data should be locked
        private static void CreateTextures(EventTypePluginData data, viz.Context context)
        {
            Debug.Assert(data != null);

            if (data.colorTexture != null)
            {
                data.colorTexture.Dispose();
                data.colorTexture = null;
            }

            data.rgbaFrame = new byte[data.imageWidth * data.imageHeight * sizeof(uint)];

            if (context != null)
            {
                data.colorTexture = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.R8G8B8A8_UNORM, false);
            }
        }
        public void Render3D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture)
        {
            if (eventType == EventType.Monitor) 
            {
                lock (this.lockObj)
                {
                    if (pluginViewSettings is AudioPlugin3DViewSettings)
                    {
                        if (this.beamConfidence > 0.0f)
                        {
                            viz.Effect effectBeam = new viz.Effect()
                                {
                                    EnableLighting = true,
                                    Ambient = new viz.Vector(0.0f, 1.0f, 0.0f, 0.5f),
                                };

                            float[] matrixFloats = new float[16];
                            unsafe
                            {
                                fixed (float* pMatrix = &matrixFloats[0])
                                {
                                    MatrixHelper.CalculateBeamMatrix(this.beamAngle, pMatrix);
                                }
                            }

                            viz.Matrix mat = new viz.Matrix();
                            mat.R0 = new viz.Vector(matrixFloats[0], matrixFloats[1], matrixFloats[2], matrixFloats[3]);
                            mat.R1 = new viz.Vector(matrixFloats[4], matrixFloats[5], matrixFloats[6], matrixFloats[7]);
                            mat.R2 = new viz.Vector(matrixFloats[8], matrixFloats[9], matrixFloats[10], matrixFloats[11]);
                            mat.R3 = new viz.Vector(matrixFloats[12], matrixFloats[13], matrixFloats[14], matrixFloats[15]);

                            this.beamMesh.Render(viz.MeshRenderMode.IndexedTriangleList, mat, effectBeam, null);
                        }
                    }
                }
            }
        }
        protected override void OnRenderTargetChanged(viz.D3DImageTexture renderTarget)
        {
            DebugHelper.AssertUIThread();

            if (renderTarget != null)
            {
                viz.MouseNavigator oldMouseNavigator = this.mouseNavigator;

                mouseNavigator = new viz.MouseNavigator(renderTarget); // must get something to setup view matrix

                if (oldMouseNavigator != null)
                {
                    mouseNavigator.Set(oldMouseNavigator);
                }

                viz.ArcBallCamera oldArcBallCamera = this.arcBallCamera;

                this.arcBallCamera = new viz.ArcBallCamera(renderTarget);

                if (oldArcBallCamera == null)
                {
                    this.arcBallCamera.SetFrontView();
                    this.arcBallCamera.Rotate(0.3f, -0.2f);
                    this.arcBallCamera.Zoom(0.5f);
                }
                else
                {
                    this.arcBallCamera.Set(oldArcBallCamera);
                }
            }
        }
        // object owning accessory should be locked
        private static void Render3D(IPluginViewSettings pluginViewSettings, viz.Accessory accessory)
        {
            AccessoryPlugin3DViewSettings accessoryViewSettings = pluginViewSettings as AccessoryPlugin3DViewSettings;
            if ((accessoryViewSettings != null) && (accessory != null))
            {
                if (accessoryViewSettings.RenderOrientationCube)
                {
                    accessory.SetMode(viz.AccessoryMode.RotationCube);
                    accessory.Render();
                }

                if (accessoryViewSettings.RenderFrustum)
                {
                    accessory.SetMode(viz.AccessoryMode.ViewFrustum);
                    accessory.Render();
                }

                if (accessoryViewSettings.RenderFloorPlane)
                {
                    accessory.SetMode(viz.AccessoryMode.FloorPlane);
                    accessory.Render();
                }
            }
        }
        // instance for accessory should be locked
        private static void HandleEvent(KStudioEvent eventObj, viz.Accessory accessory)
        {
            if (eventObj != null)
            {
                if ((eventObj.EventStreamDataTypeId == KStudioEventStreamDataTypeIds.Body) ||
                    (eventObj.EventStreamDataTypeId == HackKStudioEventStreamDataTypeIds.BodyMonitor))
                {
                    if (accessory != null)
                    {
                        uint bufferSize;
                        IntPtr bufferPtr;

                        eventObj.AccessUnderlyingEventDataBuffer(out bufferSize, out bufferPtr);

                        if (bufferSize >= AccessoryPlugin.cMinimumBodyFrameSize)
                        {
                            viz.Vector floorPlane;
                            viz.Vector upVector;

                            unsafe
                            {
                                float* pFloats = (float*)((bufferPtr + AccessoryPlugin.cFloorClipPlaneOffset).ToPointer());

                                if (pFloats[3] == 0.0f)
                                {
                                    // if W is 0, assume no real floor plane
                                    floorPlane = new viz.Vector(0.0f, 0.0f, 0.0f, 0.0f); 
                                }
                                else
                                {
                                    floorPlane = new viz.Vector(pFloats[0], pFloats[1], pFloats[2], pFloats[3]);
                                }

                                pFloats = (float*)((bufferPtr + AccessoryPlugin.cUpVectorOffset).ToPointer());
                                upVector =  new viz.Vector(0.0f, 0.0f, 0.0f, 0.0f); // never use up vector for visualizing the floor

                                accessory.UpdateFloorPlaneAndUpVector(floorPlane, upVector);
                            }
                        }
                    }
                }
            }
        }
        public void Render3D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture)
        {
            lock (this.lockObj)
            {
                switch (eventType)
                {
                    case EventType.Monitor:
                        AccessoryPlugin.Render3D(pluginViewSettings, this.monitorAccessory);
                        break;

                    case EventType.Inspection:
                        AccessoryPlugin.Render3D(pluginViewSettings, this.inspectionAccessory);
                        break;
                }
            }
        }
 protected virtual void OnEndRender(viz.Context context, viz.Texture texture, float width, float height) { }
 protected virtual void OnGetLayout(viz.Context context, viz.Texture texture, ref float layoutWidth, ref float layoutHeight) { }
        public void InitializeRender(EventType eventType, viz.Context context)
        {
            if (eventType == EventType.Monitor)
            {
                lock (this.lockObj)
                {
                    if (this.audioPlayer == null)
                    {
                        try
                        {
                            this.audioPlayer = new AudioPlayer();
                        }
                        catch (Exception)
                        {
                            if (this.loggingService != null)
                            {
                                this.loggingService.LogLine(Strings.Audio_Error_CannotInitializePlayer);
                            }
                        }
                    }

                    if (context != null)
                    {
                        this.font = new viz.Font(context);
                        this.overlay = new viz.Overlay(context);
                        const float cHorizontalFov = 70.6f;
                        float beamZ = 4.5f; // max depth in meters
                        float beamY = (float)(beamZ * Math.Tan(cHorizontalFov / 2.0));

                        viz.Vertex[] vertices = new viz.Vertex[]
                            {
                                new viz.Vertex(0,     0,      0, 0, 0, 0, 0, 0, 0xFFFFFFFF),
                                new viz.Vertex(0,  beamY, beamZ, 0, 0, 0, 0, 0, 0xFFFFFFFF),
                                new viz.Vertex(0, -beamY, beamZ, 0, 0, 0, 0, 0, 0xFFFFFFFF),
                            };

                        uint[] indices = new uint[]
                            {
                                0, 1, 2, 0, 2, 1,
                            };

                        this.beamMesh = new viz::Mesh(context, (uint)vertices.Length, (uint)indices.Length);
                        this.beamMesh.UpdateVertex(vertices);
                        this.beamMesh.UpdateIndex(indices);

                        this.outChart = new viz.TemporalChart(context);
                        this.micCharts = new viz.TemporalChart[nui.Constants.AUDIO_NUM_MIC];

                        for (int i = 0; i < this.micCharts.Length; ++i)
                        {
                            this.micCharts[i] = new viz.TemporalChart(context);
                        }

                        this.speakerCharts = new viz.TemporalChart[nui.Constants.AUDIO_NUM_SPK];
                        for (int i = 0; i < this.speakerCharts.Length; ++i)
                        {
                            this.speakerCharts[i] = new viz.TemporalChart(context);
                        }

#if TODO_AUDIO_OUT // audio out
                        if (FAILED(InitializeWASAPI()))
                        {
                            // TODO_LOG
                        }
#endif // TODO_AUDIO_OUT
                    }
                }
            }
        }
        public void Render2D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture, float left, float top, float width, float height)
        {
            if (eventType == EventType.Monitor) 
            {
                lock (this.lockObj)
                {
                    if (this.frameTime == TimeSpan.MinValue)
                    {
                        return;
                    }

                    AudioPlugin2DViewSettings audioPluginViewSettings = pluginViewSettings as AudioPlugin2DViewSettings;
                    if (audioPluginViewSettings != null)
                    {
                        float x = 10; // margin on left
                        float chartX = x + 100; // offset to chart rendering
                        float y = 10; // margin on top
                        float deltaY = 24; // height of each row

                        // chart window size
                        float chartHeight = 20;
                        float chartWidth = 600;

                        // max value in chart, bigger ones got capped
                        float chartValueCap = 0.02f;

                        // special color for current frame in time line
                        viz.Vector currentFrameColor = new viz::Vector(1, 0, 0, 1);

                        viz.Vector? color = null;

                        if ((audioPluginViewSettings.RenderBeam) && (this.font != null))
                        {
                            string str;
                            if (this.beamConfidence > 0.0f)
                            {
                                str = string.Format(CultureInfo.CurrentCulture, Strings.Audio_Beam_Label_Format, this.beamAngle); // TODO: option for other display? like degrees?
                            }
                            else
                            {
                                str = Strings.Audio_Beam_Invalid_Label;
                            }
                            this.font.DrawText(str, x, y, color);
                        }
                        y += deltaY;

                        if (this.sharedAudioFrame != null)
                        {
                            if (this.sharedAudioFrame.Size >= cAudioFrameSizeMinimum)
                            {
                                IntPtr bufferPtr = this.sharedAudioFrame.Buffer;

                                unsafe
                                {
                                    nui.AUDIO_FRAME* pFrame = (nui.AUDIO_FRAME*)bufferPtr.ToPointer();
                                    nui.AUDIO_SUBFRAME* pSubFrame = &(pFrame->FirstSubFrame);

                                    ulong currentFirstFrameTimeStamp = (ulong)this.frameTime.Ticks;
                                    ulong timeStampOffset = currentFirstFrameTimeStamp - pSubFrame->TimeCounter;
                                    ulong currentLastFrameTimeStamp = pSubFrame[pFrame->SubFrameCount - 1].TimeCounter + timeStampOffset;

#if TODO_LOCAL_PLAYBACK
                                    if ((this.timelineBegin != 0) || (this.timelineEnd != 0))
                                    {
                                        // timeline rendering

                                        if (this.timelineDirty)
                                        {
                                            this.timelineData->Sort(new System.Comparison<TimelineEntry>(TimelineEntry.Compare));
                                            timelineDirty = false;
                                        }

                                        if (audioPluginViewSettings.RenderOutput)
                                        {
                                            if (this.font != null)
                                            {
                                                this.font.DrawText(this.outString, x, y, color);

                                                for (int i = 0; i < this.timelineData.Count; i++)
                                                {
                                                    ulong timeStamp = this.timelineData[i]->TimeStamp;
                                                    if (timeStamp < this.timelineBegin) continue;
                                                    if (timeStamp > this.timelineEnd) break;

                                                    float normalizedValue = (float)(Math.Min(chartValueCap, Math.Abs(this.timelineData[i].Output)) / chartValueCap);
                                                    float topLeftX = chartX + (timeStamp - this.timelineBegin) * chartWidth / (this.timelineEnd - this.timelineBegin);
                                                    float topLeftY = y + (1 - normalizedValue) * chartHeight / 2;
                                                    float chartBarHeight = Math.Max(normalizedValue * chartHeight, 1.0f);
                                                    bool isCurrentFrame = (timeStamp >= currentFirstFrameTimeStamp && timeStamp <= currentLastFrameTimeStamp);
                                                    float barZ = isCurrentFrame ? 0 : 0.01f; // put current frame at a smaller z so it's always visible
                                                    float barWidth = isCurrentFrame ? 2.0f : 1.0f; // make current frame tick bold
                                                    viz::Vector? barColor = isCurrentFrame ? currentFrameColor : null;

                                                    if (this.overlay != null)
                                                    {
                                                        this.overlay->DrawColor(topLeftX, topLeftY, barWidth, chartBarHeight, barZ, barColor);
                                                    }
                                                }
                                            }
                                            y += deltaY;
                                        }

                                        for (int iMIC = 0; iMIC < NUIP_AUDIO_NUM_MIC; iMIC++)
                                        {
                                            if ((int)renderOptionType == (int)RenderOptionType::MIC0 + iMIC)
                                            {
                                                _font->DrawText(renderOptionName, x, y, color);

                                                for (int iSample = 0; iSample < _timelineData->Count; iSample++)
                                                {
                                                    UInt64 timeStamp = _timelineData[iSample]->TimeStamp;
                                                    if (timeStamp < _timelineBegin) continue;
                                                    if (timeStamp > _timelineEnd) break;

                                                    float normalizedValue = min(chartValueCap, fabs(_timelineData[iSample]->MIC[iMIC])) / chartValueCap;
                                                    float topLeftX = chartX + (timeStamp - _timelineBegin) * chartWidth / (_timelineEnd - _timelineBegin);
                                                    float topLeftY = y + (1 - normalizedValue) * chartHeight / 2;
                                                    float chartBarHeight = max(normalizedValue * chartHeight, 1.0f);
                                                    bool isCurrentFrame = (timeStamp >= currentFirstFrameTimeStamp && timeStamp <= currentLastFrameTimeStamp);
                                                    float barZ = isCurrentFrame ? 0 : 0.01f;
                                                    float barWidth = isCurrentFrame ? 2.0f : 1.0f;
                                                    Nullable<Xbox::Kinect::Viz::Vector> barColor;
                                                    if (isCurrentFrame) barColor = currentFrameColor;

                                                    _overlay->DrawColor(topLeftX, topLeftY, barWidth, chartBarHeight, barZ, barColor);
                                                }
                                            }
                                            y += deltaY;
                                        }

                                        for (int iSPK = 0; iSPK < NUIP_AUDIO_NUM_SPK; iSPK++)
                                        {
                                            if ((int)renderOptionType == (int)RenderOptionType::SPK0 + iSPK)
                                            {
                                                _font->DrawText(renderOptionName, x, y, color);

                                                for (int iSample = 0; iSample < _timelineData->Count; iSample++)
                                                {
                                                    UInt64 timeStamp = _timelineData[iSample]->TimeStamp;
                                                    if (timeStamp < _timelineBegin) continue;
                                                    if (timeStamp > _timelineEnd) break;

                                                    float normalizedValue = min(chartValueCap, fabs(_timelineData[iSample]->SPK[iSPK])) / chartValueCap;
                                                    float topLeftX = chartX + (timeStamp - _timelineBegin) * chartWidth / (_timelineEnd - _timelineBegin);
                                                    float topLeftY = y + (1 - normalizedValue) * chartHeight / 2;
                                                    float chartBarHeight = max(normalizedValue * chartHeight, 1.0f);
                                                    bool isCurrentFrame = (timeStamp >= currentFirstFrameTimeStamp && timeStamp <= currentLastFrameTimeStamp);
                                                    float barZ = isCurrentFrame ? 0 : 0.01f;
                                                    float barWidth = isCurrentFrame ? 2.0f : 1.0f;
                                                    Nullable<Xbox::Kinect::Viz::Vector> barColor;
                                                    if (isCurrentFrame) barColor = currentFrameColor;

                                                    _overlay->DrawColor(topLeftX, topLeftY, barWidth, chartBarHeight, barZ, barColor);
                                                }
                                            }
                                            y += deltaY;
                                        }
                                    }
                                    else
#endif // TODO_LOCAL_PLAYBACK
                                    {
                                        // live rendering
                                        float barWidth = 1;
                                        UInt64 timeSpan = 2 * 10 * 1000 * 1000; // in 100ns unit

                                        if (audioPluginViewSettings.RenderOutput)
                                        {
                                            if (this.font != null)
                                            {
                                                this.font.DrawText(this.outString, x, y, color);
                                            }

                                            if (this.outChart != null)
                                            {
                                                this.outChart.RenderBar(chartX, y, chartWidth, chartHeight, 0, barWidth, color, currentLastFrameTimeStamp, timeSpan, 0, chartValueCap);
                                            }

                                            y += deltaY;
                                        }

                                        if (this.micCharts != null)
                                        {
                                            for (int i = 0; i < this.micCharts.Length; ++i)
                                            {
                                                if (audioPluginViewSettings.GetTrackOption((AudioTrack)(AudioTrack.Mic0 + i)))
                                                {
                                                    if (this.font != null)
                                                    {
                                                        this.font.DrawText(this.micStrings[i], x, y, color);
                                                    }

                                                    this.micCharts[i].RenderBar(chartX, y, chartWidth, chartHeight, 0, barWidth, color, currentLastFrameTimeStamp, timeSpan, 0, chartValueCap);

                                                    y += deltaY;
                                                }
                                            }
                                        }

                                        if (this.speakerCharts != null)
                                        {
                                            for (int i = 0; i < this.speakerCharts.Length; ++i)
                                            {
                                                if (audioPluginViewSettings.GetTrackOption((AudioTrack)(AudioTrack.SpeakerL + i)))
                                                {
                                                    if (this.font != null)
                                                    {
                                                        this.font.DrawText(this.speakerStrings[i], x, y, color);
                                                    }

                                                    this.speakerCharts[i].RenderBar(chartX, y, chartWidth, chartHeight, 0, barWidth, color, currentLastFrameTimeStamp, timeSpan, 0, chartValueCap);

                                                    y += deltaY;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        protected override void OnNuiVizInitialize(viz.Context context)
        {
            DebugHelper.AssertUIThread(); 

            base.OnNuiVizInitialize(context);

            if (context != null)
            {
                this.overlay = new viz.Overlay(context);
            }
        }
        private void InitializeRender(viz.Context context, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            lock (this.lockObj)
            {
                if (data.sharedColorFrame != null)
                {

                    data.needsUpdate = true;
                }
            }
        }
        protected override void OnGetLayout(viz.Context context, viz.Texture texture, ref float layoutWidth, ref float layoutHeight)
        {
            DebugHelper.AssertUIThread(); 

            bool fixZoom = (this.imageWidth == 0) || (this.imageHeight == 0);

            if (texture == null)
            {
                this.imageWidth = Image2DVisualizationControl.defaultWidth;
                this.imageHeight = Image2DVisualizationControl.defaultHeight;
            }
            else
            {
                this.imageWidth = texture.GetWidth();
                this.imageHeight = texture.GetHeight();
            }

            double scale = 1.0;

            if (this.IsZoomToFit)
            {
                if (this.scrollViewer != null)
                {
                    this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                    this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;

                    scale = CalcScaleOnZoomToFit(this.scrollViewer.ActualWidth, this.scrollViewer.ActualHeight);
                }

                int zoom = (int)(100.0 * scale);

                if (this.Zoom != zoom)
                {
                    if (this.scaleTransform != null)
                    {
                        this.scaleTransform.ScaleX = scale;
                        this.scaleTransform.ScaleY = scale;
                    }

                    fixZoom = true;

                    this.ignoreZoom++;
                    this.SetValue(Image2DVisualizationControl.ZoomProperty, zoom);
                    this.ignoreZoom--;
                }
            }
            else
            {
                scale = this.Zoom / 100.0;

                if (this.scrollViewer != null)
                {
                    this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                }
            }

            if (fixZoom)
            {
                this.OnZoomChanged();
            }

            layoutWidth = (float)(this.imageWidth * scale);
            layoutHeight = (float)(this.imageHeight * scale);
        }
 public void Render3D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture)
 {
 }
        protected override void OnEndRender(viz.Context context, viz.Texture texture, float width, float height)
        {
            DebugHelper.AssertUIThread();

            base.OnEndRender(context, texture, width, height);

            if ((this.overlay != null) && (texture != null))
            {
                overlay.DrawTexture(texture, 0, 0, (int)width, height, 1.0f);
            }

            this.Do2DPropertyView();
        }
        // object owning data should be locked
        private static void CreateTextures(EventTypePluginData data, viz.Context context)
        {
            Debug.Assert(data != null);

            if (data.rawBodyIndexTexture != null)
            {
                data.rawBodyIndexTexture.Dispose();
                data.rawBodyIndexTexture = null;
            }

            if (data.bodyIndexTexture3d != null)
            {
                data.bodyIndexTexture3d.Dispose();
                data.bodyIndexTexture3d = null;
            }

            if (data.bodyIndexTexture2d != null)
            {
                data.bodyIndexTexture2d.Dispose();
                data.bodyIndexTexture2d = null;
            }

            if ((context != null) && (data.imageWidth > 0) && (data.imageHeight > 0))
            {
                data.bodyIndexTexture3d = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.B8G8R8A8_UNORM, true);
                data.bodyIndexTexture2d = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.B8G8R8A8_UNORM, true);
                data.rawBodyIndexTexture = new viz.Texture(context, data.imageWidth, data.imageHeight, viz.TextureFormat.R8_UNORM, false);
            }
        }
        public void Render2D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture, float left, float top, float width, float height)
        {
            lock (this.lockObj)
            {
                nui.Registration registration = null;

                if (this.pluginService != null)
                {
                    registration = this.pluginService.GetRegistration(eventType);
                }

                switch (eventType)
                {
                    case EventType.Monitor:
                        BodyPlugin.Render2D(pluginViewSettings, texture, left, top, width, height, this.monitorData, registration);
                        break;

                    case EventType.Inspection:
                        BodyPlugin.Render2D(pluginViewSettings, texture, left, top, width, height, this.inspectionData, registration);
                        break;
                }
            }
        }
 public void Render2D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture, float left, float top, float width, float height)
 {
 }
        private void InitializeRender(viz.Context context, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            lock (this.lockObj)
            {
                Debug.Assert(data.body == null);
                Debug.Assert(data.font == null);

                if (context != null)
                {
                    data.body = new viz.Body(context);
                    data.font = new viz.Font(context);

                    if (data.bodiesValid && (data.sharedBodyFrame != null))
                    {
                        if (Marshal.SizeOf(typeof(nui.BODY_FRAME)) == data.sharedBodyFrame.Size)
                        {
                            data.body.UpdateData(data.sharedBodyFrame.Buffer);
                        }
                    }
                }
            }
        }
        private static void UpdateData(RawIrPlugin3DViewSettings pluginViewSettings, viz.Texture texture, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            if ((pluginViewSettings != null) && (data.depthMap != null) && (data.depthTexture != null))
            {
                if (pluginViewSettings.IsSupplyingSurface)
                {
                    data.depthMap.SetMode(viz.DepthVertexMode.Surface, viz.DepthRampMode.None);
                }
                else
                {
                    switch (pluginViewSettings.ViewType)
                    {
                        case RawIrPlugin3DViewSettings.RawIr3DViewType.DepthColor:
                            data.depthMap.SetMode(viz.DepthVertexMode.Point, viz.DepthRampMode.Color);
                            break;

                        case RawIrPlugin3DViewSettings.RawIr3DViewType.DepthGrey:
                            data.depthMap.SetMode(viz.DepthVertexMode.Point, viz.DepthRampMode.Grey);
                            break;

                        case RawIrPlugin3DViewSettings.RawIr3DViewType.SurfaceNormal:
                            data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithNormal, viz.DepthRampMode.None);
                            break;

                        case RawIrPlugin3DViewSettings.RawIr3DViewType.Ir:
                            data.depthMap.SetMode(viz.DepthVertexMode.Surface, viz.DepthRampMode.None);
                            data.irTexture.RampConversion(data.rawIrTexture, pluginViewSettings.RampTexture);
                            break;

                        default:
                            Debug.Assert(false);
                            break;
                    }
                }
            }
        }
        // object owning data should be locked
        private static void Render2D(IPluginViewSettings pluginViewSettings, viz.Texture texture, float left, float top, float width, float height, EventTypePluginData data, nui.Registration registration)
        {
            Debug.Assert(data != null);

            BodyPlugin2DViewSettings bodyPluginViewSettings = pluginViewSettings as BodyPlugin2DViewSettings;

            if ((bodyPluginViewSettings != null) && (registration != null) && (data.body != null) && data.bodiesValid)
            {
                if (bodyPluginViewSettings.RenderBodies || bodyPluginViewSettings.RenderHands)
                {
                    viz.Body2DMode bodyMode = viz.Body2DMode.DepthIR;
                    if ((texture != null) && (texture.GetWidth() == nui.Constants.STREAM_COLOR_WIDTH) && (texture.GetHeight() == nui.Constants.STREAM_COLOR_HEIGHT))
                    {
                        bodyMode = viz.Body2DMode.Color;
                    }

                    data.body.Begin2D(left, top, width, height, bodyMode, registration.GetCalibrationData());

                    for (uint i = 0; i < BodyPlugin.bodyOptions.Length; ++i)
                    {
                        if (bodyPluginViewSettings.RenderBodies)
                        {
                            viz.Vector color = BodyPlugin.bodyOptions[i].ColorVector;
                            color.A = 0.7f;
                            data.body.RenderBones2D(i, color);
                        }

                        if (bodyPluginViewSettings.RenderHands)
                        {
                            data.body.RenderHandStates2D(i);
                        }
                    }

                    data.body.End2D();
                }
            }
        }
        private static void Render3D(IPluginViewSettings pluginViewSettings, viz.Texture texture, EventTypePluginData data, nui.Registration registration)
        {
            Debug.Assert(data != null);

            RawIrPlugin3DViewSettings rawPlugin3DViewSettings = pluginViewSettings as RawIrPlugin3DViewSettings;
            if (rawPlugin3DViewSettings != null)
            {
                RawIrPlugin.UpdateData(rawPlugin3DViewSettings, texture, data);

                bool doColor = false;
                bool doBodyIndex = false;

                if (texture != null)
                {
                    uint textureWidth = texture.GetWidth();
                    uint textureHeight = texture.GetHeight();

                    doColor = (textureWidth == nui.Constants.STREAM_COLOR_WIDTH) &&
                                (textureHeight == nui.Constants.STREAM_COLOR_HEIGHT);

                    doBodyIndex = (texture.GetTextureFormat() == viz.TextureFormat.B8G8R8A8_UNORM) &&
                                    (textureWidth == nui.Constants.STREAM_IR_WIDTH) &&
                                    (textureHeight == nui.Constants.STREAM_IR_HEIGHT);
                }

                if ((!rawPlugin3DViewSettings.IsSupplyingSurface && (rawPlugin3DViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.Ir)) ||
                    (rawPlugin3DViewSettings.IsSupplyingSurface && doBodyIndex) ||
                    (!rawPlugin3DViewSettings.IsSupplyingSurface && rawPlugin3DViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.SurfaceNormal))
                {
                    if (data.depthMap != null)
                    {
                        // special case for body index
                        viz.Effect effect = new viz.Effect()
                        {
                            Direction = new viz.Vector(0.5f, 0.3f, 1.5f, 0),
                            Ambient = new viz.Vector(0.0f, 0.0f, 0.0f, 1.0f),
                            Diffuse = new viz.Vector(0.5f, 0.5f, 0.5f, 1.0f),
                            Specular = new viz.Vector(1.0f, 1.0f, 1.0f, 1.0f),
                            Power = 25.0f,
                            EnableLighting = true,
                            EnableTexture = false,
                        };

                        if ((rawPlugin3DViewSettings.IsSupplyingSurface && doBodyIndex))
                        {
                            if (data.depthMap != null)
                            {
                                data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithNormal, viz.DepthRampMode.None);
                                effect.EnableTexture = true;
                            }
                        }

                        if (!rawPlugin3DViewSettings.IsSupplyingSurface && (rawPlugin3DViewSettings.ViewType != RawIrPlugin3DViewSettings.RawIr3DViewType.Ir))
                        {
                            texture = null;
                        }

                        data.depthMap.Render(effect, texture);
                    }
                }
                else
                {
                    if (rawPlugin3DViewSettings.IsSupplyingSurface && doColor)
                    {
                        // special case for color
                        if ((registration != null) && (data.depthMap != null) && (data.uvTable != null) && (data.sharedRawFrame != null))
                        {
                            data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithUV, viz.DepthRampMode.None);

                            IntPtr ptr = data.sharedRawFrame.Buffer;

                            if (ptr != IntPtr.Zero)
                            {
                                registration.Process(ptr, data.uvTable.Buffer);
                            }

                            data.depthMap.UpdateUVTable(data.uvTable.Buffer, data.uvTable.Size);
                        }
                    }

                    if (data.depthMap != null)
                    {
                        viz.Effect effect = new viz.Effect()
                            {
                                EnableTexture = texture != null,
                            };

                        data.depthMap.Render(effect, texture);
                    }
                }
            }
        }
 protected virtual void OnRenderTargetChanged(viz.D3DImageTexture renderTarget) { }