Esempio n. 1
0
 internal void AddObjects(IEnumerable <LatipiumObject> objects)
 {
     foreach (LatipiumObject obj in objects)
     {
         LatipiumObject type = obj.InvokeFunction <LatipiumObject>("Type");
         if (type != null)
         {
             if (!InitializedTypes.Contains(type))
             {
                 type.InvokeProcedure <Action <IEnumerable <LatipiumObject> > >("Initialize", UpdatedCallback);
             }
             Com.Latipium.Core.Tuple <float[], int[]> data = type.InvokeFunction <Com.Latipium.Core.Tuple <float[], int[]> >("GetPhysicsData");
             if (data != null && data.Object1 != null && data.Object2 != null)
             {
                 List <JVector> points             = new List <JVector>();
                 List <TriangleVertexIndices> tris = new List <TriangleVertexIndices>();
                 for (int i = 0; i < data.Object1.Length - 2; i += 3)
                 {
                     points.Add(new JVector(data.Object1[i], data.Object1[i + 1], data.Object1[i + 2]));
                 }
                 for (int i = 0; i < data.Object2.Length - 2; i += 3)
                 {
                     tris.Add(new TriangleVertexIndices(data.Object2[i], data.Object2[i + 1], data.Object2[i + 2]));
                 }
                 Octree      octree     = new Octree(points, tris);
                 Shape       shape      = new TriangleMeshShape(octree);
                 RigidBody   body       = new RigidBody(shape);
                 Func <bool> UseGravity = type.GetFunction <bool>("UseGravity");
                 body.AffectedByGravity = UseGravity == null || UseGravity();
                 Bodies.Add(obj, body);
                 World.AddBody(body);
             }
         }
     }
 }
Esempio n. 2
0
        private void RenderObject(LatipiumObject obj, Com.Latipium.Core.Tuple <object, object> data, IObjectRenderer objectR, IColorRenderer colorR)
        {
            Com.Latipium.Core.Tuple <float, float, float> position = obj.InvokeFunction <Com.Latipium.Core.Tuple <float, float, float>, Com.Latipium.Core.Tuple <float, float, float> >("Position", null);
            float[] transform = obj.InvokeFunction <float[], float[]>("Transform", null);
            if (position != null)
            {
                GL.Translate(position.Object1, position.Object2, position.Object3);
            }
            if (transform != null)
            {
                GL.MultMatrix(transform);
            }
            int len = objectR.Start(data.Object1);

            if (colorR == null)
            {
                for (int i = 0; i < len; ++i)
                {
                    objectR.Vertex(i);
                }
            }
            else
            {
                colorR.Start(data.Object2);
                for (int i = 0; i < len; ++i)
                {
                    colorR.Color(i);
                    objectR.Vertex(i);
                }
            }
            objectR.End();
        }
Esempio n. 3
0
 internal void Render()
 {
     if (GetObjects == null)
     {
         TriangleTest();
     }
     else
     {
         GL.PushMatrix();
         SetupCamera();
         foreach (LatipiumObject obj in GetObjects())
         {
             Com.Latipium.Core.Tuple <object, object> data = obj.InvokeFunction <LatipiumObject>("Type")
                                                             .InvokeFunction <LatipiumObject, Com.Latipium.Core.Tuple <object, object> >("GetRenderData", obj);
             if (data != null)
             {
                 IObjectRenderer objectR = FindObjectRenderer(data);
                 if (objectR == null)
                 {
                     continue;
                 }
                 IColorRenderer colorR = FindColorRenderer(data);
                 GL.PushMatrix();
                 RenderObject(obj, data, objectR, colorR);
                 GL.PopMatrix();
             }
         }
         GL.PopMatrix();
     }
 }
Esempio n. 4
0
 public Com.Latipium.Core.Tuple <float, float, float> GetSetPosition(Com.Latipium.Core.Tuple <float, float, float> val = null)
 {
     if (val != null)
     {
         Position = val;
     }
     return(Position);
 }
Esempio n. 5
0
 private IColorRenderer FindColorRenderer(Com.Latipium.Core.Tuple <object, object> data)
 {
     foreach (IColorRenderer color in ColorRenderers)
     {
         if (color.CanRender(data.Object2))
         {
             return(color);
         }
     }
     return(null);
 }
Esempio n. 6
0
 private IObjectRenderer FindObjectRenderer(Com.Latipium.Core.Tuple <object, object> data)
 {
     foreach (IObjectRenderer obj in ObjectRenderers)
     {
         if (obj.CanRender(data.Object1))
         {
             return(obj);
         }
     }
     return(null);
 }
Esempio n. 7
0
 public override void PreStep(float timestep)
 {
     Com.Latipium.Core.Tuple <float, float, float> position = PositionFunc(null);
     Position = new JVector(position.Object1, position.Object2, position.Object3);
     float[] rotation = TransformFunc(null);
     Orientation = new JMatrix(
         rotation[0], rotation[1], rotation[2],                    // rotation[ 3],
         rotation[4], rotation[5], rotation[6],                    // rotation[ 7],
         rotation[8], rotation[9], rotation[10]                    // rotation[11],
         // rotation[12], rotation[13], rotation[14],    rotation[15]
         );
 }
Esempio n. 8
0
 internal ObjectImpl(LatipiumObject type, Guid guid)
 {
     Type      = type;
     Guid      = guid;
     Position  = new Com.Latipium.Core.Tuple <float, float, float>(0, 0, 0);
     Transform = new float[] {
         1, 0, 0, 0,
         0, 1, 0, 0,
         0, 0, 1, 0,
         0, 0, 0, 1
     };
 }
Esempio n. 9
0
        private static Octree CreateOctree(Com.Latipium.Core.Tuple <float[], int[]> data)
        {
            List <JVector> points             = new List <JVector>();
            List <TriangleVertexIndices> tris = new List <TriangleVertexIndices>();

            for (int i = 0; i < data.Object1.Length - 2; i += 3)
            {
                points.Add(new JVector(data.Object1[i], data.Object1[i + 1], data.Object1[i + 2]));
            }
            for (int i = 0; i < data.Object2.Length - 2; i += 3)
            {
                tris.Add(new TriangleVertexIndices(data.Object2[i], data.Object2[i + 1], data.Object2[i + 2]));
            }
            return(new Octree(points, tris));
        }
        internal InterfaceHandler()
        {
            Translation = new Com.Latipium.Core.Tuple <float, float, float>();
            Rotation    = new Com.Latipium.Core.Tuple <float, float>();
            LatipiumModule graphics = ModuleFactory.FindModule("Com.Latipium.Modules.Graphics");

            if (graphics != null)
            {
                graphics.AddEvent("MouseMoved", (Action <int, int, int, int>)HandleMouse);
                Action <Action <int, int>, long> AddKeyboardBinding = graphics.GetProcedure <Action <int, int>, long>("AddKeyboardBinding");
                if (AddKeyboardBinding != null)
                {
                    AddKeyboardBinding(HandleKeyboard, 0);
                }
                graphics.InvokeProcedure("HideCursor");
            }
            int x;
            int y;

            CenterScreen(out x, out y);
            Cursor.Position = new Point(x, y);
        }
Esempio n. 11
0
 public Com.Latipium.Core.Tuple <IEnumerable <LatipiumObject>, IEnumerable <LatipiumObject> > Update(IEnumerable <LatipiumObject> objs)
 {
     if (Handler != null)
     {
         LatipiumObject obj = objs.First(o => o == Object && Handler.Object == o);
         if (obj != null && Position != null && Environment.TickCount > LastFrameTime)
         {
             float delta = (((float)Environment.TickCount) - (float)LastFrameTime) / 1000f;
             LastFrameTime = Environment.TickCount;
             if (Rotate != null)
             {
                 float x = Handler.Rotation.Object1;
                 float y = Handler.Rotation.Object2;
                 if (x != 0)
                 {
                     float xs = x * Sensitivity;
                     XRot -= xs;
                     Rotate(xs, 0, -1, 0);
                     Handler.Rotation.Object1 -= x;
                 }
                 if (y != 0)
                 {
                     Rotate(-y * Sensitivity, (float)Math.Cos(XRot), 0, (float)Math.Sin(-XRot));
                     Handler.Rotation.Object2 -= y;
                 }
             }
             Com.Latipium.Core.Tuple <float, float, float> pos = Position(null);
             delta *= Speed;
             float sin  = (float)Math.Sin(XRot);
             float nsin = (float)Math.Sin(-XRot);
             float cos  = (float)Math.Cos(XRot);
             pos.Object1 += Handler.Translation.Object3 * delta * sin + Handler.Translation.Object1 * delta * cos;
             pos.Object2 += Handler.Translation.Object2 * delta;
             pos.Object3 += Handler.Translation.Object3 * delta * cos + Handler.Translation.Object1 * delta * nsin;
             Position(pos);
         }
     }
     return(null);
 }
Esempio n. 12
0
        private void SetupCamera()
        {
            GL.MatrixMode(MatrixMode.Modelview);
            Matrix4 camera;

            Com.Latipium.Core.Tuple <float, float, float> position = null;
            if (PlayerPosition != null)
            {
                position = PlayerPosition(null);
            }
            if (position == null)
            {
                camera = Matrix4.CreateTranslation(0, -5, -5);
            }
            else
            {
                camera = Matrix4.CreateTranslation(position.Object1, position.Object2, position.Object3);
            }
            float[] transform = null;
            if (PlayerTransform != null)
            {
                transform = PlayerTransform(null);
            }
            if (transform == null || transform.Length != 16)
            {
                camera *= Matrix4.CreateRotationX((float)Math.PI / 4);
            }
            else
            {
                camera *= new Matrix4(
                    transform[0], transform[1], transform[2], transform[3],
                    transform[4], transform[5], transform[6], transform[7],
                    transform[8], transform[9], transform[10], transform[11],
                    transform[12], transform[13], transform[14], transform[15]
                    );
            }
            GL.LoadMatrix(ref camera);
        }
Esempio n. 13
0
        public void Loop()
        {
            int update = Environment.TickCount;

            while (true)
            {
                float time = Environment.TickCount - update;
                update = Environment.TickCount;
                Dictionary <LatipiumObject, List <LatipiumObject> > objects = new Dictionary <LatipiumObject, List <LatipiumObject> >();
                foreach (LatipiumObject realm in GetRealms())
                {
                    objects.Clear();
                    List <LatipiumObject> additions;
                    if (Additions.ContainsKey(realm))
                    {
                        additions = Additions[realm];
                    }
                    else
                    {
                        additions        = new List <LatipiumObject>();
                        Additions[realm] = additions;
                    }
                    List <LatipiumObject> removals;
                    if (Removals.ContainsKey(realm))
                    {
                        removals = Removals[realm];
                    }
                    else
                    {
                        removals        = new List <LatipiumObject>();
                        Removals[realm] = removals;
                    }
                    foreach (LatipiumObject obj in realm.InvokeFunction <IEnumerable <LatipiumObject> >("GetObjects"))
                    {
                        LatipiumObject        type = obj.InvokeFunction <LatipiumObject>("Type");
                        List <LatipiumObject> list;
                        if (objects.ContainsKey(type))
                        {
                            list = objects[type];
                        }
                        else
                        {
                            list          = new List <LatipiumObject>();
                            objects[type] = list;
                        }
                        list.Add(obj);
                    }
                    foreach (LatipiumObject type in objects.Keys)
                    {
                        Com.Latipium.Core.Tuple <IEnumerable <LatipiumObject>, IEnumerable <LatipiumObject> > changes = type.InvokeFunction <IEnumerable <LatipiumObject>, Com.Latipium.Core.Tuple <IEnumerable <LatipiumObject>, IEnumerable <LatipiumObject> > >("PhysicsUpdate", objects[type]);
                        if (changes != null)
                        {
                            lock ( ListLock ) {
                                if (changes.Object1 != null)
                                {
                                    additions.AddRange(changes.Object1);
                                }
                                if (changes.Object2 != null)
                                {
                                    removals.AddRange(changes.Object2);
                                }
                            }
                        }
                    }
                    Systems[realm].Step(time);
                }
            }
        }
Esempio n. 14
0
 internal WorldBody(Com.Latipium.Core.Tuple <float[], int[]> data, LatipiumObject obj) : base(new TriangleMeshShape(CreateOctree(data)))
 {
     obj           = Instance;
     PositionFunc  = Instance.GetFunction <Com.Latipium.Core.Tuple <float, float, float>, Com.Latipium.Core.Tuple <float, float, float> >("Position");
     TransformFunc = Instance.GetFunction <float[], float[]>("Transform");
 }