Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Diagnostics;
  5. using System.Windows;
  6. using Microsoft.Kinect;
  7.  
  8. //SharpDX
  9. using SharpDX;
  10. using SharpDX.D3DCompiler;
  11. using SharpDX.Direct3D;
  12. using SharpDX.Direct3D11;
  13. using SharpDX.DXGI;
  14. using SharpDX.Windows;
  15. using Buffer = SharpDX.Direct3D11.Buffer;
  16. using Device = SharpDX.Direct3D11.Device;
  17. using MapFlags = SharpDX.Direct3D11.MapFlags;
  18. using Vector4 = SharpDX.Vector4;
  19.  
  20. namespace SkeletonDrawing
  21. {
  22.     partial class Program
  23.     {
  24.  
  25.         public void Run()
  26.         {
  27.             RenderLoop.Run(sDXdata.form, () =>
  28.             {
  29.                 // Record time and delta time between frames.
  30.                 prevTime = time;
  31.                 time = clock.ElapsedMilliseconds / 1000.0f;
  32.                 deltaTime = time - prevTime;
  33.  
  34.                 // Calculate a crazy colour that changes with time.
  35.                 crazyColour += deltaTime * colourDeltaPerSecond;
  36.                 if (crazyColour.Red < 0) { crazyColour.Red = 1; }
  37.                 if (crazyColour.Green < 0) { crazyColour.Green = 1; }
  38.                 if (crazyColour.Blue < 0) { crazyColour.Blue = 1; }
  39.  
  40.                 // Calculate a faded version of the crazy colour and set it as the background colour.
  41.                 fader -= faderRate * deltaTime;
  42.                 if (fader < 0) { fader = 0; }
  43.                 var backgroundColour = crazyColour * fader;
  44.  
  45.                 // Clear depth buffer.
  46.                 sDXdata.context.ClearDepthStencilView(sDXdata.depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
  47.  
  48.                 // Set screen to background colour.
  49.                 sDXdata.context.ClearRenderTargetView(sDXdata.renderView, backgroundColour);
  50.  
  51.                 // Calculate the camera transform.
  52.                 view = getCameraTransform(time);
  53.  
  54.                 // Draw skeletons.
  55.                 if (skeletons.Length != 0)
  56.                 {
  57.                     skelepos = jointVector(skeletons[0].Joints[JointType.HipCenter]);
  58.                     if (skeletons[0].Joints[JointType.FootLeft].Position.Y < floor)
  59.                     {
  60.  
  61.                         floor = skeletons[0].Joints[JointType.FootLeft].Position.Y;
  62.  
  63.                     }
  64.                     makeCubeg(skeletons[0], 0.1f, new Vector3(0.8f, floor + 0.1f, -2f));
  65.                     if (checkStomp(skeletons[0], 0.1f, new Vector3(0.8f, floor + 0.1f, -2f)))
  66.                     {
  67.                         gamestate = 0;
  68.                     }
  69.                     makeSkybox(skeletons[0]);
  70.                     DrawBonesAndJoints(skeletons[0]);
  71.                     check_gamestate(skeletons[0]);
  72.  
  73.  
  74.                     //foreach (Skeleton skeleton in skeletons)
  75.                     //{
  76.                     //    /*foreach (Joint joint in skeleton.Joints)
  77.                     //    {
  78.                     //        //Draw for each cube
  79.                     //        world = getJointTransform(joint);
  80.                     //        worldViewProj = world * view * proj;
  81.                     //        worldViewProj.Transpose();
  82.                     //      sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  83.  
  84.                     //        sDXdata.context.Draw(36, 0);
  85.                     //    }*/
  86.  
  87.                     //    // Try to draw limbs
  88.                     //    DrawBonesAndJoints(skeleton);
  89.  
  90.                     //    // Draw a floating cube infront of the avatar
  91.                     //    check_gamestate(skeleton);
  92.  
  93.  
  94.  
  95.                     //    //this is a shiny comment
  96.                     //}
  97.  
  98.                 }
  99.  
  100.  
  101.                 // Swap buffers.
  102.                 sDXdata.swapChain.Present(0, PresentFlags.None);
  103.             });
  104.  
  105.         }
  106.         private void makeSkybox(Skeleton skeletons)
  107.         {
  108.             Vector3 midpoint = new Vector3(skeletons.Position.X, skeletons.Position.Y, skeletons.Position.Z);
  109.             view = getCameraTransform(time);
  110.  
  111.             //draw floor
  112.             world = Matrix.Translation(new Vector3(0, -1, 0)) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(0, floor, 0));
  113.             worldViewProj = world * view * proj;
  114.             worldViewProj.Transpose();
  115.             sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  116.             sDXdata.textureView = GetTexture("grass.jpg");
  117.             sDXdata.context.PixelShader.SetShaderResource(0, sDXdata.textureView);
  118.             sDXdata.context.Draw(6, 12);
  119.  
  120.             //draw back
  121.             world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.RotationY((float)Math.PI) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(0, 0, 20));
  122.             worldViewProj = world * view * proj;
  123.             worldViewProj.Transpose();
  124.             sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  125.  
  126.             sDXdata.textureView = GetTexture("sky.jpg");
  127.             sDXdata.context.PixelShader.SetShaderResource(0, sDXdata.textureView);
  128.             sDXdata.context.Draw(6, 6);
  129.  
  130.            
  131.             world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(0, 0, -20));
  132.             worldViewProj = world * view * proj;
  133.             worldViewProj.Transpose();
  134.             sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  135.             sDXdata.context.Draw(6, 6);
  136.             world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.RotationY((float)Math.PI/2) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(-20, 0, 0));
  137.             worldViewProj = world * view * proj;
  138.             worldViewProj.Transpose();
  139.             sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  140.             sDXdata.context.Draw(6, 6);
  141.             world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.RotationY(3*(float)Math.PI/2) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(20, 0, 0));
  142.             worldViewProj = world * view * proj;
  143.             worldViewProj.Transpose();
  144.             sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  145.             sDXdata.context.Draw(6, 6);
  146.         }
  147.  
  148.         Vector3 jointVector(Joint joint0)
  149.         {
  150.             return new Vector3(joint0.Position.X, joint0.Position.Y, -1 * joint0.Position.Z);
  151.         }
  152.  
  153.         static Quaternion GetRotation(Vector3 source, Vector3 dest, Vector3 up)
  154.         {
  155.             float dot = Vector3.Dot(source, dest);
  156.  
  157.             if (Math.Abs(dot - (-1.0f)) < 0.000001f)
  158.             {
  159.                 // vector a and b point exactly in the opposite direction,
  160.                 // so it is a 180 degrees turn around the up-axis
  161.                 return new Quaternion(up, (float)Math.PI);
  162.             }
  163.             if (Math.Abs(dot - (1.0f)) < 0.000001f)
  164.             {
  165.                 // vector a and b point exactly in the same direction
  166.                 // so we return the identity quaternion
  167.                 return Quaternion.Identity;
  168.             }
  169.  
  170.             float rotAngle = (float)Math.Acos(dot);
  171.             Vector3 rotAxis = Vector3.Cross(source, dest);
  172.             rotAxis = Vector3.Normalize(rotAxis);
  173.             return Quaternion.RotationAxis(rotAxis, rotAngle);
  174.         }
  175.         /// <summary>
  176.         /// Draws a bone line between two joints
  177.         /// </summary>
  178.         /// <param name="skeleton">skeleton to draw bones from</param>
  179.         /// <param name="jointType0">joint to start drawing from</param>
  180.         /// <param name="jointType1">joint to end drawing at</param>
  181.         void DrawBone(Skeleton skeleton, JointType jointType0, JointType jointType1)
  182.         {
  183.             Joint joint0 = skeleton.Joints[jointType0];
  184.             Joint joint1 = skeleton.Joints[jointType1];
  185.  
  186.             // If we can't find either of these joints, exit
  187.             if (joint0.TrackingState == JointTrackingState.NotTracked ||
  188.                 joint1.TrackingState == JointTrackingState.NotTracked)
  189.             {
  190.                 return;
  191.             }
  192.  
  193.             // Don't draw if both points are inferred
  194.             if (joint0.TrackingState == JointTrackingState.Inferred &&
  195.                 joint1.TrackingState == JointTrackingState.Inferred)
  196.             {
  197.                 return;
  198.             }
  199.  
  200.  
  201.  
  202.             // We assume all drawn bones are inferred unless BOTH joints are tracked
  203.             if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
  204.  
  205.  
  206.  
  207.                 // Set Vectors and Matrices
  208.                 view = getCameraTransform(time);
  209.             Vector3 bonevector = Vector3.Subtract(jointVector(joint0), jointVector(joint1));
  210.             Vector3 cubevector = new Vector3(2, 0, 0);
  211.             float scaleFactor = Math.Abs(bonevector.Length() / cubevector.Length());
  212.  
  213.             Vector3 translation = Vector3.Divide(Vector3.Add(jointVector(joint0), jointVector(joint1)), 2);
  214.             Quaternion rotQuat = GetRotation(cubevector, bonevector, new Vector3(0, 1, 0));
  215.  
  216.  
  217.  
  218.             // Create final transform matrix and draw shape
  219.             world = Matrix.Transformation(new Vector3(0, 0, 0), new Quaternion(0), new Vector3(scaleFactor, 0.05f, 0.05f), new Vector3(0, 0, 0), rotQuat, translation);
  220.  
  221.             worldViewProj = world * view * proj;
  222.             worldViewProj.Transpose();
  223.             sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
  224.             sDXdata.textureView = GetTexture("orange.jpg");
  225.             sDXdata.context.PixelShader.SetShaderResource(0, sDXdata.textureView);
  226.             sDXdata.context.Draw(36, 0);
  227.             return;
  228.         }
  229.  
  230.         /// <summary>
  231.         /// Draws a skeleton's bones and joints
  232.         /// </summary>
  233.         /// <param name="skeleton">skeleton to draw</param>
  234.         private void DrawBonesAndJoints(Skeleton skeleton)
  235.         {
  236.             // Render Torso
  237.             this.DrawBone(skeleton, JointType.Head, JointType.ShoulderCenter);
  238.             this.DrawBone(skeleton, JointType.ShoulderCenter, JointType.ShoulderLeft);
  239.             this.DrawBone(skeleton, JointType.ShoulderCenter, JointType.ShoulderRight);
  240.             this.DrawBone(skeleton, JointType.ShoulderCenter, JointType.Spine);
  241.             this.DrawBone(skeleton, JointType.Spine, JointType.HipCenter);
  242.             this.DrawBone(skeleton, JointType.HipCenter, JointType.HipLeft);
  243.             this.DrawBone(skeleton, JointType.HipCenter, JointType.HipRight);
  244.  
  245.             // Left Arm
  246.             this.DrawBone(skeleton, JointType.ShoulderLeft, JointType.ElbowLeft);
  247.             this.DrawBone(skeleton, JointType.ElbowLeft, JointType.WristLeft);
  248.             this.DrawBone(skeleton, JointType.WristLeft, JointType.HandLeft);
  249.  
  250.             // Right Arm
  251.             this.DrawBone(skeleton, JointType.ShoulderRight, JointType.ElbowRight);
  252.             this.DrawBone(skeleton, JointType.ElbowRight, JointType.WristRight);
  253.             this.DrawBone(skeleton, JointType.WristRight, JointType.HandRight);
  254.  
  255.             // Left Leg
  256.             this.DrawBone(skeleton, JointType.HipLeft, JointType.KneeLeft);
  257.             this.DrawBone(skeleton, JointType.KneeLeft, JointType.AnkleLeft);
  258.             this.DrawBone(skeleton, JointType.AnkleLeft, JointType.FootLeft);
  259.  
  260.             // Right Leg
  261.             this.DrawBone(skeleton, JointType.HipRight, JointType.KneeRight);
  262.             this.DrawBone(skeleton, JointType.KneeRight, JointType.AnkleRight);
  263.             this.DrawBone(skeleton, JointType.AnkleRight, JointType.FootRight);
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement