diff --git a/Program.cs b/Program.cs index 19f157d..a622d55 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; +using System.Text; class Program { static void Main(string[] args) { @@ -27,9 +28,10 @@ public static class Mono { public static Model model = Model.FromFile("cursor.glb", Shader.Default); public static void Run() { + string publicIP, localIP; + GetIPs(); void GetIPs() { - string localIP; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect("8.8.8.8", 65530); @@ -37,11 +39,14 @@ public static class Mono { localIP = endPoint.Address.ToString(); } // Console.WriteLine("Your local IP is: " + localIP); - string publicIP = new WebClient().DownloadString("https://ipv4.icanhazip.com/").TrimEnd(); + publicIP = new WebClient().DownloadString("https://ipv4.icanhazip.com/").TrimEnd(); // Console.WriteLine("Your IP is: " + publicIP); } - - Peer peer = new Peer("ego"); + + // byte[] bytes = Encoding.UTF8.GetBytes(publicIP); + // Console.WriteLine(bytes.Length); + MonoNet net = new MonoNet(publicIP); // temp, until unique usernames + net.Start(false); ColorCube cube = new ColorCube(); OrbitalView.strength = 4; @@ -64,8 +69,12 @@ public static class Mono { mainHand = Input.Controller(Handed.Right); stretchCursor.Step(offHand.aim, mainHand.aim); - peer.cursor = stretchCursor.pos; - model.Draw(Matrix.TS(peer.peerCursor, 0.1f)); + net.cursor = stretchCursor.pos; + net.head = Input.Head; + net.offHand = offHand.aim; + net.mainHand = mainHand.aim; + + // domHand subHand ?? :3 // if (offHand.trigger.) { // lerper.t = 0; @@ -136,24 +145,45 @@ public class Lerper } public class Peer { - public string name; - public Peer(string name) { - this.name = name; + public string id; + public Vec3 cursor; + public Pose head; + public Pose offHand; + public Pose mainHand; + public Peer() { + id = ""; + cursor = Vec3.Zero; + head = new Pose(); + offHand = new Pose(); + mainHand = new Pose(); + } +} + +public class MonoNet { + public string myID; + public MonoNet(string name) { + this.myID = name; } - public Vec3 cursor; // is this stored here??? - public Vec3 peerCursor; + public Vec3 cursor; // are these stored here??? + public Pose head; + public Pose offHand; + public Pose mainHand; + public Peer[] peers; public async void Start(bool log) { int port = 1234; string serverIP = "139.177.201.219"; - serverIP = "192.168.1.70"; + // serverIP = "192.168.1.70"; // try connecting to the server - if (log) Console.WriteLine($"{name} attempting to connect to server..."); + if (log) Console.WriteLine($"{myID} attempting to connect to server..."); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), port); socket.Connect(serverEndPoint); + + peers = new Peer[1]; + peers[0] = new Peer(); // send every 0.1 seconds while (true) { @@ -162,24 +192,91 @@ public class Peer { // send a message to the server dataPos = 0; - BitConverter.GetBytes(cursor.x).CopyTo(data, dataPos); dataPos += 4; - BitConverter.GetBytes(cursor.y).CopyTo(data, dataPos); dataPos += 4; - BitConverter.GetBytes(cursor.z).CopyTo(data, dataPos); dataPos += 4; + Encoding.UTF8.GetBytes(myID).CopyTo(data, dataPos); dataPos += 16; + WriteVec3(ref data, ref dataPos, cursor); + WritePose(ref data, ref dataPos, head); + WritePose(ref data, ref dataPos, offHand); + WritePose(ref data, ref dataPos, mainHand); socket.SendTo(data, serverEndPoint); // receive a message from the server while (socket.Available > 0){ dataPos = 0; socket.ReceiveFrom(data, ref serverEndPoint); - peerCursor.x = BitConverter.ToSingle(data, dataPos); dataPos += 4; - peerCursor.y = BitConverter.ToSingle(data, dataPos); dataPos += 4; - peerCursor.z = BitConverter.ToSingle(data, dataPos); dataPos += 4; + string id = Encoding.UTF8.GetString(data, dataPos, 16); dataPos += 16; + // recieve text + for (int i = 0; i < peers.Length; i++){ + if (peers[i].id == id) { + Peer peer = peers[i]; + peer.cursor = ReadVec3(data, ref dataPos); + peer.head = ReadPose(data, ref dataPos); + peer.offHand = ReadPose(data, ref dataPos); + peer.mainHand = ReadPose(data, ref dataPos); + break; + } + } } // sleep for 0.1 seconds await Task.Delay(100); } } + + Vec3 ReadVec3(byte[] data, ref int dataPos) { + dataPos += 12; + return new Vec3( + BitConverter.ToSingle(data, dataPos), + BitConverter.ToSingle(data, dataPos + 4), + BitConverter.ToSingle(data, dataPos + 8) + ); + } void WriteVec3(ref byte[] data, ref int dataPos, Vec3 vec) { + BitConverter.GetBytes(vec.x).CopyTo(data, dataPos); + BitConverter.GetBytes(vec.y).CopyTo(data, dataPos + 4); + BitConverter.GetBytes(vec.z).CopyTo(data, dataPos + 8); + dataPos += 12; + } + + Quat ReadQuat(byte[] data, ref int dataPos) { + dataPos += 16; + return new Quat( + BitConverter.ToSingle(data, dataPos), + BitConverter.ToSingle(data, dataPos + 4), + BitConverter.ToSingle(data, dataPos + 8), + BitConverter.ToSingle(data, dataPos + 12) + ); + } void WriteQuat(ref byte[] data, ref int dataPos, Quat quat) { + BitConverter.GetBytes(quat.x).CopyTo(data, dataPos); + BitConverter.GetBytes(quat.y).CopyTo(data, dataPos + 4); + BitConverter.GetBytes(quat.z).CopyTo(data, dataPos + 8); + BitConverter.GetBytes(quat.w).CopyTo(data, dataPos + 12); + dataPos += 16; + } + + Pose ReadPose(byte[] data, ref int dataPos) { + dataPos += 24; + return new Pose( + ReadVec3(data, ref dataPos), + ReadQuat(data, ref dataPos) + ); + } void WritePose(ref byte[] data, ref int dataPos, Pose pose) { + WriteVec3(ref data, ref dataPos, pose.position); + WriteQuat(ref data, ref dataPos, pose.orientation); + } + + Mesh meshCube = Default.MeshCube; + Material matCube = Default.Material; + public void Render() { + for (int i = 0; i < peers.Length; i++) { + Peer p = peers[i]; + Cubee(Matrix.TRS(p.cursor, p.offHand.orientation, Vec3.One * 0.05f)); + Cubee(p.head.ToMatrix(Vec3.One * 0.3f)); + Cubee(p.offHand.ToMatrix(Vec3.One * 0.1f)); + Cubee(p.mainHand.ToMatrix(Vec3.One * 0.1f)); + } + } + void Cubee(Matrix m) { + meshCube.Draw(matCube, m); + } } public class Oriel {