Пример #1
0
        /// <summary>
        /// Correctly adjust the position in accordance with the fruit element's rotation and rotationOrigin  (compare ShapeTesselator.TesselateShapeElements())
        /// </summary>
        private double[] Rotate(double[] pos, ShapeElement element, double[] matrix, double[] triple)
        {
            Mat4d.Identity(matrix);
            Mat4d.Translate(matrix, matrix, (element.RotationOrigin[0]) / 16, (element.RotationOrigin[1]) / 16, (element.RotationOrigin[2]) / 16);

            if (element.RotationX != 0)
            {
                triple[0] = 1;
                triple[1] = 0;
                triple[2] = 0;
                Mat4d.Rotate(matrix, matrix, element.RotationX * GameMath.DEG2RAD, triple);
            }
            if (element.RotationY != 0)
            {
                triple[0] = 0;
                triple[1] = 1;
                triple[2] = 0;
                Mat4d.Rotate(matrix, matrix, element.RotationY * GameMath.DEG2RAD, triple);
            }
            if (element.RotationZ != 0)
            {
                triple[0] = 0;
                triple[1] = 0;
                triple[2] = 1;
                Mat4d.Rotate(matrix, matrix, element.RotationZ * GameMath.DEG2RAD, triple);
            }

            Mat4d.Translate(matrix, matrix, (element.From[0] - element.RotationOrigin[0]) / 16, (element.From[1] - element.RotationOrigin[1]) / 16, (element.From[2] - element.RotationOrigin[2]) / 16);

            return Mat4d.MulWithVec4(matrix, pos);
        }
Пример #2
0
        public static void MatFacePlayer(double[] m)
        {
            // http://stackoverflow.com/a/5487981
            // | d 0 0 T.x |
            // | 0 d 0 T.y |
            // | 0 0 d T.z |
            // | 0 0 0   1 |
            double d = GameMath.Sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);

            m[0] = d;
            m[1] = 0;
            m[2] = 0;
            m[3] = 0;

            m[4] = 0;
            m[5] = d;
            m[6] = 0;
            m[7] = 0;

            m[8]  = 0;
            m[9]  = 0;
            m[10] = d;
            m[11] = 0;

            m[12] = m[12];
            m[13] = m[13];
            m[14] = m[14];
            m[15] = 1;

            Mat4d.RotateX(m, m, GameMath.PI);
        }
Пример #3
0
        public void Rotate(double rad, double x, double y, double z)
        {
            triple[0] = x;
            triple[1] = y;
            triple[2] = z;

            Mat4d.Rotate(Top, Top, rad, triple);
        }
Пример #4
0
 public StackMatrix4(int max = 1024)
 {
     values = new double[max][];
     for (int i = 0; i < max; i++)
     {
         values[i] = Mat4d.Create();
     }
 }
Пример #5
0
        public void Scale(double x, double y, double z)
        {
            triple[0] = x;
            triple[1] = y;
            triple[2] = z;

            Mat4d.Scale(Top, Top, triple);
        }
Пример #6
0
        public void Translate(double x, double y, double z)
        {
            triple[0] = x;
            triple[1] = y;
            triple[2] = z;

            Mat4d.Translate(Top, Top, triple);
        }
Пример #7
0
        public void Push()
        {
            Mat4d.Copy(values[count], Top);
            count++;

            if (count >= values.Length)
            {
                throw new Exception("Stack matrix overflow");
            }
        }
Пример #8
0
        public void CalcFrustumEquations(BlockPos playerPos, double[] projectionMatrix, double[] cameraMatrix)
        {
            double[] matFrustum = Mat4d.Create();

            Mat4d.Multiply(matFrustum, projectionMatrix, cameraMatrix);

            for (int i = 0; i < 16; i++)
            {
                tmpMat[i] = matFrustum[i];
            }

            CalcFrustumEquations(playerPos, tmpMat);
        }
Пример #9
0
        public static Vec3d Project(Vec3d pos, double[] projection, double[] view, int viewportWidth, int viewportHeight)
        {
            double[] outmat = new double[16];
            Mat4d.Mul(outmat, projection, view);

            double[] outpos = Mat4d.MulWithVec4(outmat, new double[] { pos.X, pos.Y, pos.Z, 1 });

            return(new Vec3d(
                       (outpos[0] / outpos[3] + 1) * (viewportWidth / 2),
                       (outpos[1] / outpos[3] + 1) * (viewportHeight / 2),
                       outpos[2]
                       ));
        }
Пример #10
0
        /// <summary>
        /// Generates a look-at matrix with the given eye position, focal point, and up axis
        /// </summary>
        /// <param name="output">{mat4} out mat4 frustum matrix will be written into</param>
        /// <param name="eye">{vec3} eye Position of the viewer</param>
        /// <param name="center">{vec3} center Point the viewer is looking at</param>
        /// <param name="up">{vec3} up vec3 pointing up</param>
        /// <returns>{mat4} out</returns>
        public static double[] LookAt(double[] output, double[] eye, double[] center, double[] up)
        {
            double x0; double x1; double x2; double y0; double y1; double y2; double z0; double z1; double z2; double len;
            double eyex    = eye[0];
            double eyey    = eye[1];
            double eyez    = eye[2];
            double upx     = up[0];
            double upy     = up[1];
            double upz     = up[2];
            double centerx = center[0];
            double centery = center[1];
            double centerz = center[2];

            if (GlMatrixMathd.Abs(eyex - centerx) < GlMatrixMathd.GLMAT_EPSILON() &&
                GlMatrixMathd.Abs(eyey - centery) < GlMatrixMathd.GLMAT_EPSILON() &&
                GlMatrixMathd.Abs(eyez - centerz) < GlMatrixMathd.GLMAT_EPSILON())
            {
                return(Mat4d.Identity(output));
            }

            z0 = eyex - centerx;
            z1 = eyey - centery;
            z2 = eyez - centerz;

            len = 1 / GameMath.Sqrt(z0 * z0 + z1 * z1 + z2 * z2);
            z0 *= len;
            z1 *= len;
            z2 *= len;

            x0  = upy * z2 - upz * z1;
            x1  = upz * z0 - upx * z2;
            x2  = upx * z1 - upy * z0;
            len = GameMath.Sqrt(x0 * x0 + x1 * x1 + x2 * x2);
            if (len == 0)
            {
                x0 = 0;
                x1 = 0;
                x2 = 0;
            }
            else
            {
                len = 1 / len;
                x0 *= len;
                x1 *= len;
                x2 *= len;
            }

            y0 = z1 * x2 - z2 * x1;
            y1 = z2 * x0 - z0 * x2;
            y2 = z0 * x1 - z1 * x0;

            len = GameMath.Sqrt(y0 * y0 + y1 * y1 + y2 * y2);
            if (len == 0)
            {
                y0 = 0;
                y1 = 0;
                y2 = 0;
            }
            else
            {
                len = 1 / len;
                y0 *= len;
                y1 *= len;
                y2 *= len;
            }

            output[0]  = x0;
            output[1]  = y0;
            output[2]  = z0;
            output[3]  = 0;
            output[4]  = x1;
            output[5]  = y1;
            output[6]  = z1;
            output[7]  = 0;
            output[8]  = x2;
            output[9]  = y2;
            output[10] = z2;
            output[11] = 0;
            output[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
            output[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
            output[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
            output[15] = 1;

            return(output);
        }
Пример #11
0
        public void OnRenderFrame(float deltaTime, EnumRenderStage stage)
        {
            capi.Render.GlMatrixModeModelView();

            if (!renderClouds || capi.Render.FrameWidth == 0)
            {
                return;
            }



            prog.Use();
            prog.Uniform("sunPosition", capi.World.Calendar.SunPositionNormalized);

            int[] hsv = ColorUtil.RgbToHsvInts((int)(capi.World.Calendar.SunColor.R * 255), (int)(capi.World.Calendar.SunColor.G * 255), (int)(capi.World.Calendar.SunColor.B * 255));
            hsv[1] = (int)(hsv[1] * 0.9);
            hsv[2] = (int)(hsv[2] * 0.9);
            int[] rgba   = ColorUtil.Hsv2RgbInts(hsv[0], hsv[1], hsv[2]);
            Vec3f sunCol = new Vec3f(rgba[0] / 255f, rgba[1] / 255f, rgba[2] / 255f);

            prog.Uniform("sunColor", sunCol);
            prog.Uniform("dayLight", Math.Max(0, capi.World.Calendar.DayLightStrength + capi.World.Calendar.MoonLightStrength - 0.15f));
            prog.Uniform("windOffset", new Vec3f(
                             (float)(GameMath.Mod((float)capi.World.Player.Entity.Pos.X, CloudTileSize)),
                             0,
                             (float)(GameMath.Mod((float)capi.World.Player.Entity.Pos.Z, CloudTileSize))
                             ));
            prog.Uniform("globalCloudBrightness", blendedGlobalCloudBrightness);
            prog.Uniform("rgbaFogIn", capi.Ambient.BlendedFogColor);
            prog.Uniform("fogMinIn", capi.Ambient.BlendedFogMin);
            prog.Uniform("fogDensityIn", capi.Ambient.BlendedFogDensity);
            prog.Uniform("playerPos", capi.World.Player.Entity.Pos.XYZFloat);

            prog.Uniform("cloudTileSize", CloudTileSize);
            prog.Uniform("cloudsLength", (float)CloudTileSize * CloudTileLength);

            prog.UniformMatrix("projectionMatrix", capi.Render.CurrentProjectionMatrix);

            //int cnt = capi.Render.PointLightsCount;
            //prog.Uniform("pointLightQuantity", cnt);
            //shv.PointLightsArray(cnt, ScreenManager.Platform.PointLights3);
            //shv.PointLightColorsArray(cnt, ScreenManager.Platform.PointLightColors3);

            prog.Uniform("flatFogDensity", capi.Ambient.BlendedFlatFogDensity);
            prog.Uniform("flatFogStart", capi.Ambient.BlendedFlatFogYPosForShader);



            capi.Render.GlPushMatrix();
            {
                double[] mvMatd = capi.Render.MvMatrix.Top;
                mvMatd[12] = 0;
                mvMatd[13] = 0;
                mvMatd[14] = 0;

                double partialTileOffsetX = capi.World.Player.Entity.Pos.X % CloudTileSize;
                double partialTileOffsetZ = capi.World.Player.Entity.Pos.Z % CloudTileSize;

                Mat4d.Translate(mvMatd, mvMatd, new double[] {
                    windOffsetX - partialTileOffsetX,
                    (int)(0.8627 * capi.World.BlockAccessor.MapSizeY) + 0.5 - capi.World.Player.Entity.Pos.Y,
                    windOffsetZ - partialTileOffsetZ
                });

                // Basic model view matrix
                float[] mvMatf = new float[16];
                for (int i = 0; i < 16; i++)
                {
                    mvMatf[i] = (float)mvMatd[i];
                }

                prog.UniformMatrix("modelViewMatrix", mvMatf);
                capi.Render.RenderMeshInstanced(cloudTilesMeshRef, QuantityCloudTiles);
            }
            capi.Render.GlPopMatrix();


            // Very slightly rotate all the clouds so it looks better when they pass through blocks

            // Putting this line before translate = Clouds no longer centered around the player
            // Putting it after the translate = Clouds hop by a few pixels on every reload
            // Need to find a correct solution for this
            // Mat4.Rotate(mv, mv, 0.04f, new float[] { 0, 1, 0 });


            prog.Stop();
        }
Пример #12
0
 public void Translate(double[] rotationOrigin)
 {
     Mat4d.Translate(Top, Top, rotationOrigin);
 }
Пример #13
0
 public void Scale(double x, double y, double z)
 {
     Mat4d.Scale(Top, x, y, z);
 }
Пример #14
0
 public void Translate(double x, double y, double z)
 {
     Mat4d.Translate(Top, Top, x, y, z);
 }