OSDN Git Service

Calculate position of all prims accurately
authorLatif Khalifa <latifer@streamgrid.net>
Mon, 11 Jul 2011 11:52:28 +0000 (11:52 +0000)
committerLatif Khalifa <latifer@streamgrid.net>
Mon, 11 Jul 2011 11:52:28 +0000 (11:52 +0000)
git-svn-id: https://radegast.googlecode.com/svn/trunk@972 f7a694da-4d33-11de-9ad6-1127a62b9fcd

Radegast/GUI/Rendering/Rendering.cs
Radegast/GUI/Rendering/RenderingHelpers.cs

index 004983a..4113c2f 100644 (file)
@@ -921,44 +921,78 @@ namespace Radegast.Rendering
 \r
         Vector3 PrimPos(Primitive prim)\r
         {\r
+            Vector3 pos;\r
+            Quaternion rot;\r
+            PrimPosAndRot(prim, out pos, out rot);\r
+            return pos;\r
+        }\r
+\r
+        object GetParent(uint localID)\r
+        {\r
+            RenderPrimitive parent;\r
+            RenderAvatar avi;\r
+            if (Prims.TryGetValue(localID, out parent))\r
+            {\r
+                return parent;\r
+            }\r
+            else if (Avatars.TryGetValue(localID, out avi))\r
+            {\r
+                return avi;\r
+            }\r
+            return null;\r
+        }\r
+\r
+        void PrimPosAndRot(Primitive prim, out Vector3 pos, out Quaternion rot)\r
+        {\r
             if (prim.ParentID == 0)\r
             {\r
-                return prim.Position;\r
+                pos = prim.Position;\r
+                rot = prim.Rotation;\r
+                return;\r
             }\r
             else\r
             {\r
-                RenderPrimitive parent;\r
-                RenderAvatar parentav;\r
-                if (Prims.TryGetValue(prim.ParentID, out parent))\r
+                pos = new Vector3(99999f, 99999f, 99999f);\r
+                rot = Quaternion.Identity;\r
+\r
+                object p = GetParent(prim.ParentID);\r
+                if (p == null) return;\r
+\r
+                Primitive parentPrim = null;\r
+                if (p is RenderPrimitive)\r
                 {\r
-                    if (parent.Prim.ParentID == 0)\r
-                    {\r
-                        return parent.Prim.Position + prim.Position * Matrix4.CreateFromQuaternion(parent.Prim.Rotation);\r
-                    }\r
-                    else\r
-                    {\r
-                        // This is an child prim of an attachment\r
-                        if (Avatars.TryGetValue(parent.Prim.ParentID, out parentav))\r
-                        {\r
-                            var avPos = PrimPos(parentav.avatar);\r
-                            return avPos;\r
-                        }\r
-                        else\r
-                        {\r
-                            return new Vector3(99999f, 99999f, 99999f);\r
-                        }\r
-                    }\r
+                    parentPrim = ((RenderPrimitive)p).Prim;\r
                 }\r
-                else if (Avatars.TryGetValue(prim.ParentID, out parentav))\r
+                else if (p is RenderAvatar)\r
                 {\r
-                    var avPos = PrimPos(parentav.avatar);\r
+                    parentPrim = ((RenderAvatar)p).avatar;\r
+                }\r
+\r
+                Vector3 parentPos;\r
+                Quaternion parentRot;\r
+                PrimPosAndRot(parentPrim, out parentPos, out parentRot);\r
 \r
-                    return avPos + prim.Position * Matrix4.CreateFromQuaternion(parentav.avatar.Rotation);\r
+                if (p is RenderPrimitive)\r
+                {\r
+                    pos = parentPos + prim.Position * parentRot;\r
+                    rot = parentRot * prim.Rotation;\r
                 }\r
-                else\r
+                else if (p is RenderAvatar)\r
                 {\r
-                    return new Vector3(99999f, 99999f, 99999f);\r
+                    RenderAvatar parentav = (RenderAvatar)p;\r
+\r
+                    int attachment_index = (int)prim.PrimData.AttachmentPoint;\r
+                    // Check for invalid LL attachment point\r
+                    if (attachment_index > GLAvatar.attachment_points.Count()) return;\r
+\r
+                    attachment_point apoint = GLAvatar.attachment_points[attachment_index];\r
+                    Vector3 point = parentav.glavatar.skel.getOffset(apoint.joint) + apoint.position;\r
+                    Quaternion qrot = parentav.glavatar.skel.getRotation(apoint.joint) * apoint.rotation;\r
+\r
+                    pos = parentPos + point * parentRot + prim.Position * (parentRot * qrot);\r
+                    rot = qrot * parentRot * prim.Rotation;\r
                 }\r
+                return;\r
             }\r
         }\r
 \r
@@ -1660,8 +1694,9 @@ namespace Radegast.Rendering
         }\r
 \r
 \r
-        void RenderBoundingBox(BoundingVolume bbox)\r
+        void RenderBoundingBox(RenderPrimitive prim)\r
         {\r
+            BoundingVolume bbox = prim.BoundingVolume;\r
             GL.PushAttrib(AttribMask.AllAttribBits);\r
             GL.Disable(EnableCap.Fog);\r
             GL.Disable(EnableCap.Texture2D);\r
@@ -1673,6 +1708,12 @@ namespace Radegast.Rendering
             GL.Disable(EnableCap.Blend);\r
             GL.Disable(EnableCap.AlphaTest);\r
 \r
+            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);\r
+            GL.PushMatrix();\r
+            GL.MultMatrix(Math3D.CreateTranslationMatrix(prim.SimPosition));\r
+            GL.MultMatrix(Math3D.CreateRotationMatrix(prim.SimRotation));\r
+            GL.Scale(prim.Prim.Scale.X, prim.Prim.Scale.Y, prim.Prim.Scale.Z);\r
+            GL.Color3(1f, 0f, 0f);\r
             GL.Begin(BeginMode.Quads);\r
             var bmin = bbox.Min;\r
             var bmax = bbox.Max;\r
@@ -1714,6 +1755,8 @@ namespace Radegast.Rendering
             GL.Vertex3(bmax.X, bmin.Y, bmin.Z);\r
 \r
             GL.End();\r
+            GL.PopMatrix();\r
+            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);\r
             GL.PopAttrib();\r
         }\r
 \r
@@ -1988,7 +2031,7 @@ namespace Radegast.Rendering
                 SortedPrims = new List<RenderPrimitive>(Prims.Count);\r
                 foreach (RenderPrimitive prim in Prims.Values)\r
                 {\r
-                    prim.SimPosition = PrimPos(prim.Prim);\r
+                    PrimPosAndRot(prim.Prim, out prim.SimPosition, out prim.SimRotation);\r
                     prim.DistanceSquared = Vector3.DistanceSquared(Camera.RenderPosition, prim.SimPosition);\r
                     SortedPrims.Add(prim);\r
                 }\r
@@ -2011,6 +2054,7 @@ namespace Radegast.Rendering
             {\r
                 for (int i = 0; i < nrPrims; i++)\r
                 {\r
+                    //RenderBoundingBox(SortedPrims[i]);\r
                     RenderPrim(SortedPrims[i], pass, i);\r
                 }\r
             }\r
index be9cfa4..c88c938 100644 (file)
@@ -288,6 +288,7 @@ namespace Radegast.Rendering
         public Primitive Prim;\r
         public List<Face> Faces;\r
         public Vector3 SimPosition;\r
+        public Quaternion SimRotation;\r
         public float DistanceSquared;\r
         public BoundingVolume BoundingVolume;\r
 \r