Beispiel #1
1
		protected override void SetupViewport()
		{
			GL.ClearColor(System.Drawing.SystemColors.Control);
			GL.Viewport(0, 0, this.Width, this.Height);
			GL.Ortho(0.0, 0.0, 1.0, 1.0, 0.0, 0.0);
			GL.MatrixMode(OpenTK.Graphics.OpenGL.MatrixMode.Projection);
			OpenTK.Matrix4 projection = new OpenTK.Matrix4()
			{
				Row0 = new OpenTK.Vector4(2.0f / this.Width, 0.0f, 0, 0),
				Row1 = new OpenTK.Vector4(0.0f, -2.0f / this.Height, 0, 0),
				Row2 = new OpenTK.Vector4(0, 0, 1, 0),
				Row3 = new OpenTK.Vector4(-1.0f, 1.0f, 0, 1),
			};
			GL.LoadMatrix(ref projection);
			GL.MatrixMode(OpenTK.Graphics.OpenGL.MatrixMode.Modelview);
			OpenTK.Matrix4 identity = new OpenTK.Matrix4()
			{
				Row0 = new OpenTK.Vector4(1, 0, 0, 0),
				Row1 = new OpenTK.Vector4(0, 1, 0, 0),
				Row2 = new OpenTK.Vector4(0, 0, 1, 0),
				Row3 = new OpenTK.Vector4(0, 0, 0, 1),
			};
			GL.LoadMatrix(ref identity);
		}
Beispiel #2
0
 public unsafe static void GetCalculatedTransformB(this SliderConstraint obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.CalculatedTransformB;
     }
 }
        public void Draw(OpenTK.Matrix4 wvp)
        {
            if (hasPrimitiveBegin)
            {
                throw new InvalidOperationException("Begin cannot be called until End has been successfully called.");
            }

            var count = batches.Count;

            if (count > 0)
            {
                // Apply shaders
                GL.UseProgram(shaderProgramHandle);

                // Set shader paramaters
                GL.UniformMatrix4(transformLocation, false, ref wvp);

                // Bind buffers
                GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOid[1]);

                // if dirty update buffers
                if (isDirty)
                {
                    GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertexData.Length * Vertex.SizeInBytes), vertexData, BufferUsageHint.StaticDraw);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indexData.Length * sizeof(ushort)), indexData, BufferUsageHint.StaticDraw);
                    isDirty = false;
                }

                // Apply vertex layout
                GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
                GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 12);
                GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 28);

                // Enable vertex layout
                GL.EnableVertexAttribArray(0);
                GL.EnableVertexAttribArray(1);
                GL.EnableVertexAttribArray(2);

                // Enable depth
                GL.Enable(EnableCap.DepthTest);

                // Draw batches
                for (int i = 0; i < count; ++i)
                {
                    DrawBatch(batches[i]);
                }

                // Reset features
                GL.LineWidth(1.0f);
                GL.Disable(EnableCap.DepthTest);

                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                GL.DisableVertexAttribArray(0);
                GL.DisableVertexAttribArray(1);
                GL.DisableVertexAttribArray(2);
            }
        }
        void SetProjection(float bmpaspect, float minx, float maxx, float miny, float maxy)
        {
            float width  = maxx - minx;
            float height = maxy - miny;

            maxx += width * Scale;
            minx -= width * Scale;
            maxy += height * Scale;
            miny -= height * Scale;
            float sceneaspect = width / height;

            if (sceneaspect < bmpaspect)
            {
                // tall image
                width = ((maxy - miny) * bmpaspect - (maxx - minx)) / 2;
                minx -= width;
                maxx += width;

                /*float scale2 = bmpaspect / sceneaspect;
                 * minx *= scale2;
                 * maxx *= scale2;*/
            }
            else
            {
                // wide image
                height = ((maxx - minx) / bmpaspect - (maxy - miny)) / 2;
                miny  -= height;
                maxy  += height;

                /*float scale2 = sceneaspect / bmpaspect;
                 * miny *= scale2;
                 * maxy *= scale2;*/
            }
            projection = OpenTK.Matrix4.CreateOrthographicOffCenter(minx, maxx, miny, maxy, 1, 2000);
        }
        public void UpdateViewMatrix()
        {
            ViewMat = OpenTK.Matrix4.CreateRotationZ(OpenTK.MathHelper.DegreesToRadians(CamRotation));
            OpenTK.Matrix4 tm = OpenTK.Matrix4.CreateTranslation(new OpenTK.Vector3((View.ClientSize.Width / 2) - CamX, (View.ClientSize.Height / 2) - CamY, 0));

            ViewMat = ViewMat * tm;
        }
Beispiel #6
0
        public static void solveForAffineTransformOpenCV(List <OpenTK.Vector3> xs, List <OpenTK.Vector3> bs, ref OpenTK.Matrix4 M)
        {
            if (xs.Count < 4)
            {
                return;
            }

            List <Emgu.CV.Structure.MCvPoint3D32f> OpenCvXs = new List <Emgu.CV.Structure.MCvPoint3D32f>();
            List <Emgu.CV.Structure.MCvPoint3D32f> OpenCvBs = new List <Emgu.CV.Structure.MCvPoint3D32f>();

            //* STAR can replace with OpenTK to array
            foreach (OpenTK.Vector3 x in xs)
            {
                OpenCvXs.Add(new Emgu.CV.Structure.MCvPoint3D32f(x.X, x.Y, x.Z));
            }

            foreach (OpenTK.Vector3 b in bs)
            {
                OpenCvBs.Add(new Emgu.CV.Structure.MCvPoint3D32f(b.X, b.Y, b.Z));
            }

            byte[] inliers;
            Emgu.CV.Matrix <double> OpenCvM;
            Emgu.CV.CvInvoke.EstimateAffine3D(OpenCvXs.ToArray(), OpenCvBs.ToArray(), out OpenCvM, out inliers, 7, 0.95);

            M = new OpenTK.Matrix4(
                (float)OpenCvM[0, 0], (float)OpenCvM[0, 1], (float)OpenCvM[0, 2], (float)OpenCvM[0, 3],
                (float)OpenCvM[1, 0], (float)OpenCvM[1, 1], (float)OpenCvM[1, 2], (float)OpenCvM[1, 3],
                (float)OpenCvM[2, 0], (float)OpenCvM[2, 1], (float)OpenCvM[2, 2], (float)OpenCvM[2, 3],
                0, 0, 0, 1
                );
        }
Beispiel #7
0
 public unsafe static void GetFrameOffsetA(this SliderConstraint obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.FrameOffsetA;
     }
 }
Beispiel #8
0
 public unsafe static void GetWorldTransform(this CollisionObjectWrapper obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.WorldTransform;
     }
 }
Beispiel #9
0
        public static global::SlimDX.Matrix ToSlimDXMatrix(this OpenTK.Matrix4 m, bool transpose)
        {
            global::SlimDX.Matrix ret = new global::SlimDX.Matrix
            {
                M11 = m.M11,
                M12 = m.M12,
                M13 = m.M13,
                M14 = m.M14,
                M21 = m.M21,
                M22 = m.M22,
                M23 = m.M23,
                M24 = m.M24,
                M31 = m.M31,
                M32 = m.M32,
                M33 = m.M33,
                M34 = m.M34,
                M41 = m.M41,
                M42 = m.M42,
                M43 = m.M43,
                M44 = m.M44
            };

            //could be optimized later into the above copies
            if (transpose)
            {
                ret = global::SlimDX.Matrix.Transpose(ret);
            }

            return(ret);
        }
Beispiel #10
0
        private void ProcessNode(Entity3D root, Assimp.Node s, List <Mesh3D> ml)
        {
            Entity3D r1 = new Entity3D();

            root.Sub.Add(r1);
            r1.Top  = root;
            r1.Name = s.Name;
            if (s.Name.ToLower().Contains("root"))
            {
                r1.Name     = r1.Name + "*";
                r1.BreakTop = true;
            }

            //r1.LocalTurn = new OpenTK.Matrix4(s.Transform.A1, s.Transform.A2, s.Transform.A3, s.Transform.A4, s.Transform.B1, s.Transform.B2, s.Transform.B3, s.Transform.B4, s.Transform.C1, s.Transform.C2, s.Transform.C3, s.Transform.C4, s.Transform.D1, s.Transform.D2, s.Transform.D3, s.Transform.D4);
            r1.LocalTurn = new OpenTK.Matrix4(s.Transform.A1, s.Transform.B1, s.Transform.C1, s.Transform.D1, s.Transform.A2, s.Transform.B2, s.Transform.C2, s.Transform.D2, s.Transform.A3, s.Transform.B3, s.Transform.C3, s.Transform.D3, s.Transform.A4, s.Transform.B4, s.Transform.C4, s.Transform.D4);
            OpenTK.Matrix4 lt = r1.LocalTurn;

            r1.LocalTurn = lt.ClearTranslation();
            r1.LocalTurn = r1.LocalTurn.ClearScale();
            r1.LocalPos  = lt.ExtractTranslation();

            r1.LocalScale = lt.ExtractScale();
            // r1.LocalPos = new OpenTK.Vector3(r1.LocalPos.X + 100, 0, 0);
            for (int i = 0; i < s.MeshCount; i++)
            {
                r1.AddMesh(ml[s.MeshIndices[i]]);
            }
            if (s.HasChildren)
            {
                foreach (Node pn in s.Children)
                {
                    ProcessNode(r1, pn, ml);
                }
            }
        }
Beispiel #11
0
 public static void WriteMatrix(OpenTK.Matrix4 m)
 {
     WriteVec(m.Row0);
     WriteVec(m.Row1);
     WriteVec(m.Row2);
     WriteVec(m.Row3);
 }
 public unsafe static void AddChildShape(this CompoundShape obj, ref OpenTK.Matrix4 localTransform, CollisionShape shape)
 {
     fixed(OpenTK.Matrix4 *localTransformPtr = &localTransform)
     {
         obj.AddChildShape(ref *(BulletSharp.Math.Matrix *)localTransformPtr, shape);
     }
 }
 public unsafe static void GetTransform(this CompoundShapeChild obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.Transform;
     }
 }
Beispiel #14
0
 public unsafe static void SetCenterOfMassTransform(this RigidBody obj, ref OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         obj.CenterOfMassTransform = *(BulletSharp.Math.Matrix *)valuePtr;
     }
 }
Beispiel #15
0
 public unsafe static void ProceedToTransform(this RigidBody obj, ref OpenTK.Matrix4 newTrans)
 {
     fixed(OpenTK.Matrix4 *newTransPtr = &newTrans)
     {
         obj.ProceedToTransform(ref *(BulletSharp.Math.Matrix *)newTransPtr);
     }
 }
Beispiel #16
0
 public unsafe static void GetInvInertiaTensorWorld(this RigidBody obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.InvInertiaTensorWorld;
     }
 }
Beispiel #17
0
 /// <summary>
 /// Left handed homogeneous(W=1) matrix * vector multiplication.
 /// Much more efficient than how it's done inside OpenTK
 /// </summary>
 /// <param name="M"></param>
 /// <param name="V"></param>
 /// <returns></returns>
 public static OpenTK.Vector3 QuickTransform(this OpenTK.Matrix4 M, ref OpenTK.Vector3 V)
 {
     return(new OpenTK.Vector3(
                M.Row0.X * V.X + M.Row0.Y * V.Y + M.Row0.Z * V.Z + M.Row0.W,
                M.Row1.X * V.X + M.Row1.Y * V.Y + M.Row1.Z * V.Z + M.Row1.W,
                M.Row2.X * V.X + M.Row2.Y * V.Y + M.Row2.Z * V.Z + M.Row2.W));
 }
Beispiel #18
0
        public static void TestAffineSolve()
        {
            OpenTK.Matrix4 M = new OpenTK.Matrix4();
            //float norm;

            // Test the identity with no translation
            List <OpenTK.Vector3> xs = new List <OpenTK.Vector3>();
            List <OpenTK.Vector3> bs = new List <OpenTK.Vector3>();

            xs.Add(new OpenTK.Vector3(1, 0, 0)); xs.Add(new OpenTK.Vector3(0, 1, 0)); xs.Add(new OpenTK.Vector3(0, 0, 1)); xs.Add(new OpenTK.Vector3(2, 2, 2));
            bs.Add(new OpenTK.Vector3(1, 0, 0)); bs.Add(new OpenTK.Vector3(0, 1, 0)); bs.Add(new OpenTK.Vector3(0, 0, 1)); bs.Add(new OpenTK.Vector3(2, 2, 2));
            solveForAffineTransform(xs, bs, ref M, false);
            // Normalized
            solveForAffineTransform(xs, bs, ref M, true);

            // Identity with a translation
            xs.Clear(); bs.Clear();
            xs.Add(new OpenTK.Vector3(1, 0, 0)); xs.Add(new OpenTK.Vector3(0, 1, 0)); xs.Add(new OpenTK.Vector3(0, 0, 1)); xs.Add(new OpenTK.Vector3(2, 2, 2));
            bs.Add(new OpenTK.Vector3(0, -1, -1)); bs.Add(new OpenTK.Vector3(-1, 0, -1)); bs.Add(new OpenTK.Vector3(-1, -1, 0)); bs.Add(new OpenTK.Vector3(1, 1, 1));
            // Normalized
            solveForAffineTransform(xs, bs, ref M, false);
            solveForAffineTransform(xs, bs, ref M, true);

            // Rotation
            xs.Clear(); bs.Clear();
            xs.Add(new OpenTK.Vector3(1, 0, 0)); xs.Add(new OpenTK.Vector3(0, 1, 0)); xs.Add(new OpenTK.Vector3(0, 0, 1)); xs.Add(new OpenTK.Vector3(2, 2, 2));
            bs.Add(new OpenTK.Vector3(0, 1, 0)); bs.Add(new OpenTK.Vector3(-1, 0, 0)); bs.Add(new OpenTK.Vector3(0, 0, 1)); bs.Add(new OpenTK.Vector3(-2, 2, 2));
            solveForAffineTransform(xs, bs, ref M, false);
            solveForAffineTransform(xs, bs, ref M, true);
        }
Beispiel #19
0
        float AngleBetween(OpenTK.Matrix4 m1, OpenTK.Matrix4 m2)
        {
            var v1 = OpenTK.Vector3.Transform(new OpenTK.Vector3(1, 0, 0), m1);
            var v2 = OpenTK.Vector3.Transform(new OpenTK.Vector3(1, 0, 0), m2);

            return(OpenTK.Vector3.CalculateAngle(v1, v2));
        }
 public unsafe static void GetAFrame(this HingeConstraint obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.AFrame;
     }
 }
Beispiel #21
0
 public unsafe static void GetWorldTransform(this WheelInfo obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.WorldTransform;
     }
 }
 public unsafe static void SetStartWorldTrans(this DefaultMotionState obj, ref OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         obj.StartWorldTrans = *(BulletSharp.Math.Matrix *)valuePtr;
     }
 }
 public unsafe static void GetFrameOffsetB(this Generic6DofConstraint obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.FrameOffsetB;
     }
 }
 public unsafe static void GetGraphicsWorldTrans(this DefaultMotionState obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.GraphicsWorldTrans;
     }
 }
 public unsafe static void GetCenterOfMassOffset(this DefaultMotionState obj, out OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         *(BulletSharp.Math.Matrix *)valuePtr = obj.CenterOfMassOffset;
     }
 }
Beispiel #26
0
 public unsafe static void SetWorldTransform(this WheelInfo obj, ref OpenTK.Matrix4 value)
 {
     fixed(OpenTK.Matrix4 *valuePtr = &value)
     {
         obj.WorldTransform = *(BulletSharp.Math.Matrix *)valuePtr;
     }
 }
Beispiel #27
0
        private static void SetSharedUniforms(Camera c, Shader shader)
        {
            shader.SetMatrix4x4("mvpMatrix", c.MvpMatrix);

            OpenTK.Matrix4 sphereMatrix = c.ModelViewMatrix;
            sphereMatrix.Invert();
            sphereMatrix.Transpose();
            shader.SetMatrix4x4("sphereMatrix", sphereMatrix);

            shader.SetInt("renderType", (int)Runtime.renderType);

            shader.SetTexture("UVTestPattern", RenderTools.uvTestPattern, 10);

            shader.SetBoolToInt("renderR", Runtime.renderR);
            shader.SetBoolToInt("renderG", Runtime.renderG);
            shader.SetBoolToInt("renderB", Runtime.renderB);
            shader.SetBoolToInt("renderAlpha", Runtime.renderAlpha);
            bool alphaOverride = Runtime.renderAlpha && !Runtime.renderR && !Runtime.renderG && !Runtime.renderB;

            shader.SetBoolToInt("alphaOverride", alphaOverride);

            shader.SetBoolToInt("renderNormalMap", Runtime.renderNormalMap);

            shader.SetBoolToInt("renderDiffuse", Runtime.renderDiffuse);
            shader.SetBoolToInt("renderSpecular", Runtime.renderSpecular);
        }
Beispiel #28
0
        public static global::SlimDX.Matrix ToSlimDXMatrix(this OpenTK.Matrix4 m, bool transpose)
        {
            global::SlimDX.Matrix ret = new global::SlimDX.Matrix();
            ret.M11 = m.M11;
            ret.M12 = m.M12;
            ret.M13 = m.M13;
            ret.M14 = m.M14;
            ret.M21 = m.M21;
            ret.M22 = m.M22;
            ret.M23 = m.M23;
            ret.M24 = m.M24;
            ret.M31 = m.M31;
            ret.M32 = m.M32;
            ret.M33 = m.M33;
            ret.M34 = m.M34;
            ret.M41 = m.M41;
            ret.M42 = m.M42;
            ret.M43 = m.M43;
            ret.M44 = m.M44;

            //could be optimized later into the above copies
            if (transpose)
            {
                ret = global::SlimDX.Matrix.Transpose(ret);
            }

            return(ret);
        }
Beispiel #29
0
        //TODO: Maybe rewrite and document?
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="input"></param>
        public void SetAttribute <T>(String name, T input)
        {
            Int32 program = GL.GetUniformLocation(this.address, name);

            switch (typeof(T).Name) //hmm
            {
            case nameof(Int16): { Int16 output = (Int16)(Object)input; GL.Uniform1(program, output); break; }

            case nameof(Int32): { Int32 output = (Int32)(Object)input; GL.Uniform1(program, output); break; }

            case nameof(Int64): { Int64 output = (Int64)(Object)input; GL.Uniform1(program, output); break; }

            case nameof(Single): { Single output = (Single)(Object)input; GL.Uniform1(program, output); break; }

            case nameof(Double): { Double output = (Double)(Object)input; GL.Uniform1(program, output); break; }

            case nameof(OpenTK.Vector2): { OpenTK.Vector2 output = (OpenTK.Vector2)(Object) input; GL.Uniform2(program, ref output); break; }

            case nameof(OpenTK.Vector3): { OpenTK.Vector3 output = (OpenTK.Vector3)(Object) input; GL.Uniform3(program, ref output); break; }

            case nameof(OpenTK.Vector4): { OpenTK.Vector4 output = (OpenTK.Vector4)(Object) input; GL.Uniform4(program, ref output); break; }

            case nameof(OpenTK.Matrix2): { OpenTK.Matrix2 output = (OpenTK.Matrix2)(Object) input; GL.UniformMatrix2(program, false, ref output); break; }

            case nameof(OpenTK.Matrix3): { OpenTK.Matrix3 output = (OpenTK.Matrix3)(Object) input; GL.UniformMatrix3(program, false, ref output); break; }

            case nameof(OpenTK.Matrix4): { OpenTK.Matrix4 output = (OpenTK.Matrix4)(Object) input; GL.UniformMatrix4(program, false, ref output); break; }

            default: throw new Exception("Bad type providen");
            }
        }
Beispiel #30
0
        public static void CalculateFrustum(OpenTK.Matrix4 ProjMatrix, OpenTK.Matrix4 ModelMatrix)
        {
            ClipMatrix[0] = (ModelMatrix.M11 * ProjMatrix.M11) + (ModelMatrix.M12 * ProjMatrix.M21) + (ModelMatrix.M13 * ProjMatrix.M31) + (ModelMatrix.M14 * ProjMatrix.M41);
            ClipMatrix[1] = (ModelMatrix.M11 * ProjMatrix.M12) + (ModelMatrix.M12 * ProjMatrix.M22) + (ModelMatrix.M13 * ProjMatrix.M32) + (ModelMatrix.M14 * ProjMatrix.M42);
            ClipMatrix[2] = (ModelMatrix.M11 * ProjMatrix.M13) + (ModelMatrix.M12 * ProjMatrix.M23) + (ModelMatrix.M13 * ProjMatrix.M33) + (ModelMatrix.M14 * ProjMatrix.M43);
            ClipMatrix[3] = (ModelMatrix.M11 * ProjMatrix.M14) + (ModelMatrix.M12 * ProjMatrix.M24) + (ModelMatrix.M13 * ProjMatrix.M34) + (ModelMatrix.M14 * ProjMatrix.M44);

            ClipMatrix[4] = (ModelMatrix.M21 * ProjMatrix.M11) + (ModelMatrix.M22 * ProjMatrix.M21) + (ModelMatrix.M23 * ProjMatrix.M31) + (ModelMatrix.M24 * ProjMatrix.M41);
            ClipMatrix[5] = (ModelMatrix.M21 * ProjMatrix.M12) + (ModelMatrix.M22 * ProjMatrix.M22) + (ModelMatrix.M23 * ProjMatrix.M32) + (ModelMatrix.M24 * ProjMatrix.M42);
            ClipMatrix[6] = (ModelMatrix.M21 * ProjMatrix.M13) + (ModelMatrix.M22 * ProjMatrix.M23) + (ModelMatrix.M23 * ProjMatrix.M33) + (ModelMatrix.M24 * ProjMatrix.M43);
            ClipMatrix[7] = (ModelMatrix.M21 * ProjMatrix.M14) + (ModelMatrix.M22 * ProjMatrix.M24) + (ModelMatrix.M23 * ProjMatrix.M34) + (ModelMatrix.M24 * ProjMatrix.M44);

            ClipMatrix[8]  = (ModelMatrix.M31 * ProjMatrix.M11) + (ModelMatrix.M32 * ProjMatrix.M21) + (ModelMatrix.M33 * ProjMatrix.M31) + (ModelMatrix.M34 * ProjMatrix.M41);
            ClipMatrix[9]  = (ModelMatrix.M31 * ProjMatrix.M12) + (ModelMatrix.M32 * ProjMatrix.M22) + (ModelMatrix.M33 * ProjMatrix.M32) + (ModelMatrix.M34 * ProjMatrix.M42);
            ClipMatrix[10] = (ModelMatrix.M31 * ProjMatrix.M13) + (ModelMatrix.M32 * ProjMatrix.M23) + (ModelMatrix.M33 * ProjMatrix.M33) + (ModelMatrix.M34 * ProjMatrix.M43);
            ClipMatrix[11] = (ModelMatrix.M31 * ProjMatrix.M14) + (ModelMatrix.M32 * ProjMatrix.M24) + (ModelMatrix.M33 * ProjMatrix.M34) + (ModelMatrix.M34 * ProjMatrix.M44);

            ClipMatrix[12] = (ModelMatrix.M41 * ProjMatrix.M11) + (ModelMatrix.M42 * ProjMatrix.M21) + (ModelMatrix.M43 * ProjMatrix.M31) + (ModelMatrix.M44 * ProjMatrix.M41);
            ClipMatrix[13] = (ModelMatrix.M41 * ProjMatrix.M12) + (ModelMatrix.M42 * ProjMatrix.M22) + (ModelMatrix.M43 * ProjMatrix.M32) + (ModelMatrix.M44 * ProjMatrix.M42);
            ClipMatrix[14] = (ModelMatrix.M41 * ProjMatrix.M13) + (ModelMatrix.M42 * ProjMatrix.M23) + (ModelMatrix.M43 * ProjMatrix.M33) + (ModelMatrix.M44 * ProjMatrix.M43);
            ClipMatrix[15] = (ModelMatrix.M41 * ProjMatrix.M14) + (ModelMatrix.M42 * ProjMatrix.M24) + (ModelMatrix.M43 * ProjMatrix.M34) + (ModelMatrix.M44 * ProjMatrix.M44);

            // laske frustumin tasot ja normalisoi ne
            frustum[RIGHT, 0] = ClipMatrix[3] - ClipMatrix[0];
            frustum[RIGHT, 1] = ClipMatrix[7] - ClipMatrix[4];
            frustum[RIGHT, 2] = ClipMatrix[11] - ClipMatrix[8];
            frustum[RIGHT, 3] = ClipMatrix[15] - ClipMatrix[12];
            NormalizePlane(frustum, RIGHT);

            frustum[LEFT, 0] = ClipMatrix[3] + ClipMatrix[0];
            frustum[LEFT, 1] = ClipMatrix[7] + ClipMatrix[4];
            frustum[LEFT, 2] = ClipMatrix[11] + ClipMatrix[8];
            frustum[LEFT, 3] = ClipMatrix[15] + ClipMatrix[12];
            NormalizePlane(frustum, LEFT);

            frustum[BOTTOM, 0] = ClipMatrix[3] + ClipMatrix[1];
            frustum[BOTTOM, 1] = ClipMatrix[7] + ClipMatrix[5];
            frustum[BOTTOM, 2] = ClipMatrix[11] + ClipMatrix[9];
            frustum[BOTTOM, 3] = ClipMatrix[15] + ClipMatrix[13];
            NormalizePlane(frustum, BOTTOM);

            frustum[TOP, 0] = ClipMatrix[3] - ClipMatrix[1];
            frustum[TOP, 1] = ClipMatrix[7] - ClipMatrix[5];
            frustum[TOP, 2] = ClipMatrix[11] - ClipMatrix[9];
            frustum[TOP, 3] = ClipMatrix[15] - ClipMatrix[13];
            NormalizePlane(frustum, TOP);

            frustum[BACK, 0] = ClipMatrix[3] - ClipMatrix[2];
            frustum[BACK, 1] = ClipMatrix[7] - ClipMatrix[6];
            frustum[BACK, 2] = ClipMatrix[11] - ClipMatrix[10];
            frustum[BACK, 3] = ClipMatrix[15] - ClipMatrix[14];
            NormalizePlane(frustum, BACK);

            frustum[FRONT, 0] = ClipMatrix[3] + ClipMatrix[2];
            frustum[FRONT, 1] = ClipMatrix[7] + ClipMatrix[6];
            frustum[FRONT, 2] = ClipMatrix[11] + ClipMatrix[10];
            frustum[FRONT, 3] = ClipMatrix[15] + ClipMatrix[14];
            NormalizePlane(frustum, FRONT);
        }
Beispiel #31
0
        public void InitAssImp(Assimp.Scene aiRoot, Scene.Entity3D root)
        {
            if (aiRoot.HasAnimations == false)
            {
                return;
            }

            _skeleton = CreateBoneTree(aiRoot.RootNode, null);
            Console.WriteLine("Proc bones:" + _skeleton.Name + " C:" + _skeleton.Children.Count);
            foreach (Assimp.Mesh mesh in aiRoot.Meshes)
            {
                foreach (Assimp.Bone bone in mesh.Bones)
                {
                    if (!_bonesByName.TryGetValue(bone.Name, out Bone found))
                    {
                        continue;
                    }

                    bool skip = (from t in _bones let bname = bone.Name where t.Name == bname select t).Any();
                    if (skip)
                    {
                        continue;
                    }

                    found.Offset = ToTK(bone.OffsetMatrix);
                    _bones.Add(found);
                    _bonesToIndex[found.Name] = _bones.IndexOf(found);
                }
                Assimp.Mesh mesh1 = mesh;
                foreach (string bone in _bonesByName.Keys.Where(b => mesh1.Bones.All(b1 => b1.Name != b) && b.StartsWith("Bone")))
                {
                    _bonesByName[bone].Offset = _bonesByName[bone].Parent.Offset;
                    _bones.Add(_bonesByName[bone]);
                    _bonesToIndex[bone] = _bones.IndexOf(_bonesByName[bone]);
                }
            }
            ExtractAnimations(aiRoot);

            const float timestep = 1.0f / 30.0f;

            for (int i = 0; i < Animations.Count; i++)
            {
                SetAnimationIndex(i);
                float dt = 0.0f;
                for (float ticks = 0.0f; ticks < Animations[i].Duration; ticks += Animations[i].TicksPerSecond / 30.0f)
                {
                    dt += timestep;
                    Calculate(dt);
                    List <OpenTK.Matrix4> trans = new List <OpenTK.Matrix4>();
                    for (int a = 0; a < _bones.Count; a++)
                    {
                        OpenTK.Matrix4 rotMat = _bones[a].Offset * _bones[a].GlobalTransform;
                        trans.Add(rotMat);
                    }
                    Animations[i].Transforms.Add(trans);
                }
            }
            Console.WriteLine("Finished loading animations with " + _bones.Count + " bones");
        }
        void SetProjection(float bmpaspect, float minx, float maxx, float miny, float maxy)
        {
            float width = maxx - minx;
            float height = maxy - miny;
            maxx += width * Scale;
            minx -= width * Scale;
            maxy += height * Scale;
            miny -= height * Scale;
            float sceneaspect = width / height;
            if (sceneaspect < bmpaspect)
            {
                // tall image
                width = ((maxy - miny) * bmpaspect - (maxx - minx)) / 2;
                minx -= width;
                maxx += width;

                /*float scale2 = bmpaspect / sceneaspect;
                minx *= scale2;
                maxx *= scale2;*/
            }
            else
            {
                // wide image
                height = ((maxx - minx) / bmpaspect - (maxy - miny)) / 2;
                miny -= height;
                maxy += height;
                /*float scale2 = sceneaspect / bmpaspect;
                miny *= scale2;
                maxy *= scale2;*/
            }
            projection = OpenTK.Matrix4.CreateOrthographicOffCenter(minx, maxx, miny, maxy, 1, 2000);
        }
 public void SetGroundPlane(OpenTK.Matrix4 M)
 {
     this.GroundPlane = M;
 }
Beispiel #34
0
 public override void Render()
 {
     Location renderrelpos = GetWeldSpot();
     TheClient.SetEnts();
     // TODO: Merge with base.Render() as much as possible!
     if (TheClient.CVars.n_debugmovement.ValueB)
     {
         TheClient.Rendering.RenderLine(ServerLocation, renderrelpos);
         TheClient.Rendering.RenderLineBox(ServerLocation + new Location(-0.2), ServerLocation + new Location(0.2));
     }
     OpenTK.Matrix4d mat = OpenTK.Matrix4d.Scale(1.5f)
         * OpenTK.Matrix4d.CreateRotationZ((Direction.Yaw * Utilities.PI180))
         * PlayerAngleMat
         * OpenTK.Matrix4d.CreateTranslation(ClientUtilities.ConvertD(renderrelpos));
     TheClient.MainWorldView.SetMatrix(2, mat);
     TheClient.Rendering.SetMinimumLight(0.0f);
     model.CustomAnimationAdjustments = new Dictionary<string, OpenTK.Matrix4>(SavedAdjustmentsOTK);
     // TODO: safe (no-collision) rotation check?
     model.CustomAnimationAdjustments["spine04"] = GetAdjustmentOTK("spine04") * OpenTK.Matrix4.CreateRotationX(-(float)(Direction.Pitch / 2f * Utilities.PI180));
     if (!TheClient.MainWorldView.RenderingShadows && TheClient.CVars.g_firstperson.ValueB)
     {
         model.CustomAnimationAdjustments["neck01"] = GetAdjustmentOTK("neck01") * OpenTK.Matrix4.CreateRotationX(-(float)(160f * Utilities.PI180));
     }
     else
     {
         model.CustomAnimationAdjustments["neck01"] = GetAdjustmentOTK("neck01");
     }
     model.Draw(aHTime, hAnim, aTTime, tAnim, aLTime, lAnim);
     Model mod = TheClient.GetItemForSlot(TheClient.QuickBarPos).Mod;
     bool hasjp = HasJetpack();
     if (!hasjp && tAnim != null && mod != null)
     {
         mat = OpenTK.Matrix4d.CreateTranslation(ClientUtilities.ConvertD(renderrelpos));
         TheClient.MainWorldView.SetMatrix(2, mat);
         Dictionary<string, Matrix> adjs = new Dictionary<string, Matrix>(SavedAdjustments);
         Matrix rotforw = Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(Vector3.UnitX, ((float)(Direction.Pitch / 2f * Utilities.PI180) % 360f)));
         adjs["spine04"] = GetAdjustment("spine04") * rotforw;
         SingleAnimationNode hand = tAnim.GetNode("metacarpal2.r");
         Matrix m4 = Matrix.CreateScale(1.5f, 1.5f, 1.5f)
             * (Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)((-Direction.Yaw + 90) * Utilities.PI180) % 360f))
             * hand.GetBoneTotalMatrix(aTTime, adjs))
             * Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)((-90) * Utilities.PI180) % 360f));
         OpenTK.Matrix4 bonemat = new OpenTK.Matrix4((float)m4.M11, (float)m4.M12, (float)m4.M13, (float)m4.M14,
             (float)m4.M21, (float)m4.M22, (float)m4.M23, (float)m4.M24,
             (float)m4.M31, (float)m4.M32, (float)m4.M33, (float)m4.M34,
             (float)m4.M41, (float)m4.M42, (float)m4.M43, (float)m4.M44);
         GL.UniformMatrix4(40, false, ref bonemat);
         mod.LoadSkin(TheClient.Textures);
         mod.Draw();
         bonemat = OpenTK.Matrix4.Identity;
         GL.UniformMatrix4(40, false, ref bonemat);
     }
     if (hasjp)
     {
         // TODO: Abstractify!
         Model jetp = GetHeldItem().Mod;
         mat = OpenTK.Matrix4d.CreateTranslation(ClientUtilities.ConvertD(renderrelpos));
         TheClient.MainWorldView.SetMatrix(2, mat);
         Dictionary<string, Matrix> adjs = new Dictionary<string, Matrix>();
         Matrix rotforw = Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(Vector3.UnitX, ((float)(Direction.Pitch / 2f * Utilities.PI180) % 360f)));
         adjs["spine04"] = GetAdjustment("spine04") * rotforw;
         SingleAnimationNode spine = tAnim.GetNode("spine04");
         Matrix m4 = Matrix.CreateScale(1.5f, 1.5f, 1.5f)
             * (Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)((-Direction.Yaw + 90) * Utilities.PI180) % 360f))
             * spine.GetBoneTotalMatrix(aTTime, adjs))
              * Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(Vector3.UnitX, (float)((90) * Utilities.PI180) % 360f));
         OpenTK.Matrix4 bonemat = new OpenTK.Matrix4((float)m4.M11, (float)m4.M12, (float)m4.M13, (float)m4.M14, (float)m4.M21, (float)m4.M22, (float)m4.M23, (float)m4.M24,
             (float)m4.M31, (float)m4.M32, (float)m4.M33, (float)m4.M34, (float)m4.M41, (float)m4.M42, (float)m4.M43, (float)m4.M44);
         GL.UniformMatrix4(40, false, ref bonemat);
         jetp.LoadSkin(TheClient.Textures);
         jetp.Draw();
         bonemat = OpenTK.Matrix4.Identity;
         GL.UniformMatrix4(40, false, ref bonemat);
     }
     if (IsTyping)
     {
         TheClient.Textures.GetTexture("ui/game/typing").Bind(); // TODO: store!
         TheClient.Rendering.RenderBillboard(renderrelpos + new Location(0, 0, 4), new Location(2), TheClient.MainWorldView.CameraPos);
     }
 }
            public State(State other)
            {
                this.Thickness = other.Thickness;
                this.Color = other.Color;
                this.Rotation = other.Rotation;
                this.Translation = other.Translation;
                this.Angle = other.Angle;
                this.Distance = other.Distance;
                this.fPoints = other.fPoints;
                this.fMatrix = other.fMatrix;

                other.fPoints = new List<OpenTK.Vector3>();
            }
Beispiel #36
0
 static void Mult(Matrix matrix)
 {
     CurrentState.Matrix = matrix * CurrentState.Matrix;
 }