Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Diagnostics;
- using System.Windows;
- using Microsoft.Kinect;
- //SharpDX
- using SharpDX;
- using SharpDX.D3DCompiler;
- using SharpDX.Direct3D;
- using SharpDX.Direct3D11;
- using SharpDX.DXGI;
- using SharpDX.Windows;
- using Buffer = SharpDX.Direct3D11.Buffer;
- using Device = SharpDX.Direct3D11.Device;
- using MapFlags = SharpDX.Direct3D11.MapFlags;
- using Vector4 = SharpDX.Vector4;
- namespace SkeletonDrawing
- {
- partial class Program
- {
- public void Run()
- {
- RenderLoop.Run(sDXdata.form, () =>
- {
- // Record time and delta time between frames.
- prevTime = time;
- time = clock.ElapsedMilliseconds / 1000.0f;
- deltaTime = time - prevTime;
- // Calculate a crazy colour that changes with time.
- crazyColour += deltaTime * colourDeltaPerSecond;
- if (crazyColour.Red < 0) { crazyColour.Red = 1; }
- if (crazyColour.Green < 0) { crazyColour.Green = 1; }
- if (crazyColour.Blue < 0) { crazyColour.Blue = 1; }
- // Calculate a faded version of the crazy colour and set it as the background colour.
- fader -= faderRate * deltaTime;
- if (fader < 0) { fader = 0; }
- var backgroundColour = crazyColour * fader;
- // Clear depth buffer.
- sDXdata.context.ClearDepthStencilView(sDXdata.depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
- // Set screen to background colour.
- sDXdata.context.ClearRenderTargetView(sDXdata.renderView, backgroundColour);
- // Calculate the camera transform.
- view = getCameraTransform(time);
- // Draw skeletons.
- if (skeletons.Length != 0)
- {
- skelepos = jointVector(skeletons[0].Joints[JointType.HipCenter]);
- if (skeletons[0].Joints[JointType.FootLeft].Position.Y < floor)
- {
- floor = skeletons[0].Joints[JointType.FootLeft].Position.Y;
- }
- makeCubeg(skeletons[0], 0.1f, new Vector3(0.8f, floor + 0.1f, -2f));
- if (checkStomp(skeletons[0], 0.1f, new Vector3(0.8f, floor + 0.1f, -2f)))
- {
- gamestate = 0;
- }
- makeSkybox(skeletons[0]);
- DrawBonesAndJoints(skeletons[0]);
- check_gamestate(skeletons[0]);
- //foreach (Skeleton skeleton in skeletons)
- //{
- // /*foreach (Joint joint in skeleton.Joints)
- // {
- // //Draw for each cube
- // world = getJointTransform(joint);
- // worldViewProj = world * view * proj;
- // worldViewProj.Transpose();
- // sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- // sDXdata.context.Draw(36, 0);
- // }*/
- // // Try to draw limbs
- // DrawBonesAndJoints(skeleton);
- // // Draw a floating cube infront of the avatar
- // check_gamestate(skeleton);
- // //this is a shiny comment
- //}
- }
- // Swap buffers.
- sDXdata.swapChain.Present(0, PresentFlags.None);
- });
- }
- private void makeSkybox(Skeleton skeletons)
- {
- Vector3 midpoint = new Vector3(skeletons.Position.X, skeletons.Position.Y, skeletons.Position.Z);
- view = getCameraTransform(time);
- //draw floor
- world = Matrix.Translation(new Vector3(0, -1, 0)) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(0, floor, 0));
- worldViewProj = world * view * proj;
- worldViewProj.Transpose();
- sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- sDXdata.textureView = GetTexture("grass.jpg");
- sDXdata.context.PixelShader.SetShaderResource(0, sDXdata.textureView);
- sDXdata.context.Draw(6, 12);
- //draw back
- world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.RotationY((float)Math.PI) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(0, 0, 20));
- worldViewProj = world * view * proj;
- worldViewProj.Transpose();
- sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- sDXdata.textureView = GetTexture("sky.jpg");
- sDXdata.context.PixelShader.SetShaderResource(0, sDXdata.textureView);
- sDXdata.context.Draw(6, 6);
- world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(0, 0, -20));
- worldViewProj = world * view * proj;
- worldViewProj.Transpose();
- sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- sDXdata.context.Draw(6, 6);
- world = Matrix.Translation(new Vector3(0, 0, -1)) * Matrix.RotationY((float)Math.PI/2) * Matrix.Scaling(30f) * Matrix.Translation(new Vector3(-20, 0, 0));
- worldViewProj = world * view * proj;
- worldViewProj.Transpose();
- sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- sDXdata.context.Draw(6, 6);
- 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));
- worldViewProj = world * view * proj;
- worldViewProj.Transpose();
- sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- sDXdata.context.Draw(6, 6);
- }
- Vector3 jointVector(Joint joint0)
- {
- return new Vector3(joint0.Position.X, joint0.Position.Y, -1 * joint0.Position.Z);
- }
- static Quaternion GetRotation(Vector3 source, Vector3 dest, Vector3 up)
- {
- float dot = Vector3.Dot(source, dest);
- if (Math.Abs(dot - (-1.0f)) < 0.000001f)
- {
- // vector a and b point exactly in the opposite direction,
- // so it is a 180 degrees turn around the up-axis
- return new Quaternion(up, (float)Math.PI);
- }
- if (Math.Abs(dot - (1.0f)) < 0.000001f)
- {
- // vector a and b point exactly in the same direction
- // so we return the identity quaternion
- return Quaternion.Identity;
- }
- float rotAngle = (float)Math.Acos(dot);
- Vector3 rotAxis = Vector3.Cross(source, dest);
- rotAxis = Vector3.Normalize(rotAxis);
- return Quaternion.RotationAxis(rotAxis, rotAngle);
- }
- /// <summary>
- /// Draws a bone line between two joints
- /// </summary>
- /// <param name="skeleton">skeleton to draw bones from</param>
- /// <param name="jointType0">joint to start drawing from</param>
- /// <param name="jointType1">joint to end drawing at</param>
- void DrawBone(Skeleton skeleton, JointType jointType0, JointType jointType1)
- {
- Joint joint0 = skeleton.Joints[jointType0];
- Joint joint1 = skeleton.Joints[jointType1];
- // If we can't find either of these joints, exit
- if (joint0.TrackingState == JointTrackingState.NotTracked ||
- joint1.TrackingState == JointTrackingState.NotTracked)
- {
- return;
- }
- // Don't draw if both points are inferred
- if (joint0.TrackingState == JointTrackingState.Inferred &&
- joint1.TrackingState == JointTrackingState.Inferred)
- {
- return;
- }
- // We assume all drawn bones are inferred unless BOTH joints are tracked
- if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
- // Set Vectors and Matrices
- view = getCameraTransform(time);
- Vector3 bonevector = Vector3.Subtract(jointVector(joint0), jointVector(joint1));
- Vector3 cubevector = new Vector3(2, 0, 0);
- float scaleFactor = Math.Abs(bonevector.Length() / cubevector.Length());
- Vector3 translation = Vector3.Divide(Vector3.Add(jointVector(joint0), jointVector(joint1)), 2);
- Quaternion rotQuat = GetRotation(cubevector, bonevector, new Vector3(0, 1, 0));
- // Create final transform matrix and draw shape
- 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);
- worldViewProj = world * view * proj;
- worldViewProj.Transpose();
- sDXdata.context.UpdateSubresource(ref worldViewProj, sDXdata.constantBuffer);
- sDXdata.textureView = GetTexture("orange.jpg");
- sDXdata.context.PixelShader.SetShaderResource(0, sDXdata.textureView);
- sDXdata.context.Draw(36, 0);
- return;
- }
- /// <summary>
- /// Draws a skeleton's bones and joints
- /// </summary>
- /// <param name="skeleton">skeleton to draw</param>
- private void DrawBonesAndJoints(Skeleton skeleton)
- {
- // Render Torso
- this.DrawBone(skeleton, JointType.Head, JointType.ShoulderCenter);
- this.DrawBone(skeleton, JointType.ShoulderCenter, JointType.ShoulderLeft);
- this.DrawBone(skeleton, JointType.ShoulderCenter, JointType.ShoulderRight);
- this.DrawBone(skeleton, JointType.ShoulderCenter, JointType.Spine);
- this.DrawBone(skeleton, JointType.Spine, JointType.HipCenter);
- this.DrawBone(skeleton, JointType.HipCenter, JointType.HipLeft);
- this.DrawBone(skeleton, JointType.HipCenter, JointType.HipRight);
- // Left Arm
- this.DrawBone(skeleton, JointType.ShoulderLeft, JointType.ElbowLeft);
- this.DrawBone(skeleton, JointType.ElbowLeft, JointType.WristLeft);
- this.DrawBone(skeleton, JointType.WristLeft, JointType.HandLeft);
- // Right Arm
- this.DrawBone(skeleton, JointType.ShoulderRight, JointType.ElbowRight);
- this.DrawBone(skeleton, JointType.ElbowRight, JointType.WristRight);
- this.DrawBone(skeleton, JointType.WristRight, JointType.HandRight);
- // Left Leg
- this.DrawBone(skeleton, JointType.HipLeft, JointType.KneeLeft);
- this.DrawBone(skeleton, JointType.KneeLeft, JointType.AnkleLeft);
- this.DrawBone(skeleton, JointType.AnkleLeft, JointType.FootLeft);
- // Right Leg
- this.DrawBone(skeleton, JointType.HipRight, JointType.KneeRight);
- this.DrawBone(skeleton, JointType.KneeRight, JointType.AnkleRight);
- this.DrawBone(skeleton, JointType.AnkleRight, JointType.FootRight);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement