Beispiel #1
0
        public Game(int width, int height)
        {
            this.Window = new GameWindow(width, height, new OpenTK.Graphics.GraphicsMode(new OpenTK.Graphics.ColorFormat(), 2, 4, 4));
            this.Loader = new Loader();
            this.model  = this.Loader.LoadRawModel(this.vertices, this.textureCoords, null, this.indices);

            this.ComputeShader = new Shaders.ComputeShader(@"..\..\..\Shaders\ComputeShader.glsl");
            this.StaticShader  = new StaticShader();

            //string[] cubeMap = new string[]
            //{
            //    "../../../res/right.jpg",
            //    "../../../res/left.jpg",
            //    "../../../res/top.jpg",
            //    "../../../res/bottom.jpg",
            //    "../../../res/back.jpg",
            //    "../../../res/front.jpg",
            //};

            string[] cubeMap = new string[]
            {
                "../../../res/right.png",
                "../../../res/left.png",
                "../../../res/top.png",
                "../../../res/bottom.png",
                "../../../res/back.png",
                "../../../res/front.png",
            };

            this.SkyboxTex = this.Loader.LoadCubeTexture(cubeMap);

            this.Init();

            //this.cameraViewMatrix *= Matrix4.CreateTranslation(-0.5f, 0.0f, 0.0f);
            this.cameraViewMatrix = Matrix4.Identity;
            //this.cameraViewMatrix *= Matrix4.CreateRotationX((float)Math.PI / 10);
            //this.cameraViewMatrix *= Matrix4.CreateTranslation(0.0f, 100.0f, 100.0f);
            //this.cameraViewMatrix *= Matrix4.CreateTranslation(3.0f, 3.0f, 5.0f);
            //this.cameraViewMatrix *= Matrix4.CreateTranslation(0.0f, 100.0f, 10.0f);

            this.cameraViewMatrix *= Matrix4.CreateRotationX(MathHelper.DegreesToRadians(10));

            //this.cameraViewMatrix *= Matrix4.CreateRotationX(-MathHelper.DegreesToRadians(50));
            this.cameraViewMatrix *= Matrix4.CreateTranslation(0.0f, -10.0f, 60.0f);

            this.cameraViewMatrix *= Matrix4.CreateRotationY(MathHelper.DegreesToRadians(RotY));
        }
Beispiel #2
0
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            float cameraSpeed = 0.01f;

            base.OnKeyPress(e);
            if (e.KeyChar == 'a')
            {
                mView = mView * Matrix4.CreateTranslation(cameraSpeed, 0, 0);
                int uView = MoveCamera();
                GL.UniformMatrix4(uView, true, ref mView);
            }

            if (e.KeyChar == 'd')
            {
                mView = mView * Matrix4.CreateTranslation(-cameraSpeed, 0, 0);
                int uView = MoveCamera();
                GL.UniformMatrix4(uView, true, ref mView);
            }

            if (e.KeyChar == 'w')
            {
                mView = mView * Matrix4.CreateTranslation(0, 0, cameraSpeed);
                int uView = MoveCamera();
                GL.UniformMatrix4(uView, true, ref mView);
            }

            if (e.KeyChar == 's')
            {
                mView = mView * Matrix4.CreateTranslation(0, 0, -cameraSpeed);
                int uView = MoveCamera();
                GL.UniformMatrix4(uView, true, ref mView);
            }

            if (e.KeyChar == 'q')
            {
                mView = mView * Matrix4.CreateRotationY(cameraSpeed);
                int uView = MoveCamera();
                GL.UniformMatrix4(uView, true, ref mView);
            }

            if (e.KeyChar == 'e')
            {
                mView = mView * Matrix4.CreateRotationY(-cameraSpeed);
                int uView = MoveCamera();
                GL.UniformMatrix4(uView, true, ref mView);
            }
        }
Beispiel #3
0
        public void Render2D(Viewport2D viewport, MapObject o)
        {
            if (viewport.Zoom < 0.5m)
            {
                return;
            }

            var entityData = o.GetEntityData();

            if (entityData == null)
            {
                return;
            }

            var angles = entityData.GetPropertyCoordinate("angles");

            if (angles == null)
            {
                return;
            }

            angles = new Coordinate(DMath.DegreesToRadians(angles.Z), DMath.DegreesToRadians(angles.X), DMath.DegreesToRadians(angles.Y));
            var m = new UnitMatrixMult(Matrix4.CreateRotationX((float)angles.X) * Matrix4.CreateRotationY((float)angles.Y) * Matrix4.CreateRotationZ((float)angles.Z));

            var min = Math.Min(o.BoundingBox.Width, Math.Min(o.BoundingBox.Height, o.BoundingBox.Length));
            var p1  = viewport.Flatten(o.BoundingBox.Center);
            //Extend direction
            //var p2 = p1 + viewport.Flatten(m.Transform(Coordinate.UnitX)) * min * 0.4m;
            var p2 = p1 + viewport.Flatten(m.Transform(Coordinate.UnitX)) * min * 7.5m;

            var multiplier = 4 / viewport.Zoom;
            var dir        = (p2 - p1).Normalise();
            var cp         = new Coordinate(-dir.Y, dir.X, 0).Normalise();

            GL.Color4(Color.FromArgb(255, o.Colour));

            GL.Begin(PrimitiveType.Lines);
            Coord(p1);
            Coord(p2);
            GL.End();

            GL.Begin(PrimitiveType.Triangles);
            Coord(p2 - (dir * 2 - cp) * multiplier);
            Coord(p2 - (dir * 2 + cp) * multiplier);
            Coord(p2);
            GL.End();
        }
Beispiel #4
0
        void DefaultControls()
        {
            var io = ImGui.GetIO();

            if (ImGui.IsMouseDragging(0, 1f))
            {
                var delta = (Vector2)ImGui.GetMouseDragDelta(0, 1f);
                ImGui.ResetMouseDragDelta(0);
                var rotmat = Matrix4.CreateRotationX(CameraRotation.Y) *
                             Matrix4.CreateRotationY(CameraRotation.X);
                if (ImGui.IsMouseDown(1))
                {
                    //LMB + RMB - Move up and down
                    ImGui.ResetMouseDragDelta(1);
                    var y = rotmat.Transform(Vector3.UnitY);
                    CameraOffset += y * (delta.Y * ModelScale / 52f);
                }
                else
                {
                    var z = rotmat.Transform(Vector3.UnitZ);
                    var x = rotmat.Transform(Vector3.UnitX);

                    CameraOffset += x * (delta.X * ModelScale / 52f);
                    CameraOffset -= z * (delta.Y * ModelScale / 44f);
                }
            }
            else if (ImGui.IsMouseDragging(1, 1f))
            {
                var delta = (Vector2)ImGui.GetMouseDragDelta(1, 1f);
                ImGui.ResetMouseDragDelta(1);
                if (io.KeyCtrl)
                {
                    //CTRL + RMB - Rotate Model
                    Rotation += (delta / 100) * new Vector2(1, -1);
                }
                else
                {
                    //RMB - Rotate viewport camera
                    CameraRotation -= (delta / 100);
                    DefaultKeyboardControls();
                }
            }
            else if (io.MouseDown[1])
            {
                DefaultKeyboardControls();
            }
        }
Beispiel #5
0
        public void Update(int pipeline, int width, int height, int widthTex, int heightTex)
        {
            var view = Matrix4.CreateTranslation(-pos[0], -pos[1], -pos[2])
                       * Matrix4.CreateRotationY(-rot[1] * rad2deg)
                       * Matrix4.CreateRotationX(-rot[0] * rad2deg);
            var aspect = (float)width / height;
            var proj   = Matrix4.CreatePerspectiveFieldOfView(fov * rad2deg, aspect, near, far);

            // GET OR CREATE CAMERA UNIFORMS FOR program
            UniformBlock <Names> unif;

            if (uniform.TryGetValue(pipeline, out unif) == false)
            {
                uniform.Add(pipeline, unif = new UniformBlock <Names>(pipeline, name));
            }

            // SET UNIFORM VALUES
            unif.Set(Names.view, view.AsInt32());

            unif.Set(Names.proj, proj.AsInt32());

            if (unif.Has(Names.viewProj))
            {
                unif.Set(Names.viewProj, (view * proj).AsInt32());
            }

            if (unif.Has(Names.camera))
            {
                unif.Set(Names.camera, new[] { fov *rad2deg, aspect, near, far }.AsInt32());
            }

            if (unif.Has(Names.color))
            {
                unif.Set(Names.color, new[] { color[0], color[1], color[2], intensity }.AsInt32());
            }

            if (unif.Has(Names.light))
            {
                var x = near * (float)Math.Tan(fov * rad2deg);
                var y = x / aspect;
                unif.Set(Names.light, new[] { innerCone *rad2deg, radius, x, y }.AsInt32());
            }

            // UPDATE UNIFORM BUFFER
            unif.Update();
            unif.Bind();
        }
        private void GetCurrentRotation(out Matrix4 rotation)
        {
            float xRotationInDegrees = _xRotationInDegrees;
            float yRotationInDegrees = _yRotationInDegrees;
            float zRotationInDegrees = _zRotationInDegrees;

            if (_iConfiguration.ViewProjection == ViewProjection.Orthographic_Front)
            {
                xRotationInDegrees = 90;
                yRotationInDegrees = 180;
                zRotationInDegrees = 180;
            }
            else if (_iConfiguration.ViewProjection == ViewProjection.Orthographic_Side)
            {
                xRotationInDegrees = 90;
                yRotationInDegrees = 180;
                zRotationInDegrees = 270;
            }
            else if (_iConfiguration.ViewProjection.IsBirdsEye())
            {
                xRotationInDegrees = -180;
                yRotationInDegrees = 0;
                switch (_iConfiguration.ViewProjection)
                {
                case ViewProjection.BirdsEye_0:
                    zRotationInDegrees = 0;
                    break;

                case ViewProjection.BirdsEye_90:
                    zRotationInDegrees = 90;
                    break;

                case ViewProjection.BirdsEye_180:
                    zRotationInDegrees = 180;
                    break;

                case ViewProjection.BirdsEye_270:
                    zRotationInDegrees = 270;
                    break;
                }
            }

            rotation =
                Matrix4.CreateRotationZ(zRotationInDegrees / constDegreesPerRadian) *
                Matrix4.CreateRotationY(yRotationInDegrees / constDegreesPerRadian) *
                Matrix4.CreateRotationX(xRotationInDegrees / constDegreesPerRadian);
        }
Beispiel #7
0
        public void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs e)
        {
            switch (e.Key)
            {
            case Key.Up:
                view = view * Matrix4.CreateTranslation(0.0f, 0.0f, 5);
                break;

            case Key.Left:

                view = view * Matrix4.CreateRotationY(-0.025f);
                break;

            case Key.Down:
                view = view * Matrix4.CreateTranslation(0.0f, 0.0f, -5f);
                break;

            case Key.Right:
                view = view * Matrix4.CreateRotationY(0.025f);
                break;

            case Key.L:
                view = view * Matrix4.CreateTranslation(-5, 0.0f, 0.0f);
                break;

            case Key.J:
                view = view * Matrix4.CreateTranslation(5, 0.0f, 0.0f);
                break;

            case Key.I:
                view = view * Matrix4.CreateTranslation(0.0f, 5, 0.0f);
                break;

            case Key.K:
                view = view * Matrix4.CreateTranslation(0.0f, -5f, 0.0f);
                break;

            case Key.G:
                view = view * Matrix4.CreateRotationX(0.05f);
                break;

            case Key.V:
                view = view * Matrix4.CreateRotationX(-0.05f);
                break;
            }
        }
Beispiel #8
0
        public Matrix4 GetTransform()
        {
            Matrix4 meshScale = Matrix4.Identity;

            if (MeshData != null)
            {
                meshScale = Matrix4.CreateScale(MeshData.ScaleX, MeshData.ScaleY, MeshData.ScaleZ);
            }

            return
                (Matrix4.CreateScale(ScaleX, ScaleY, ScaleZ) *
                 (Matrix4.CreateRotationX(RotateX) *
                  Matrix4.CreateRotationY(RotateY) *
                  Matrix4.CreateRotationZ(RotateZ)) *
                 Matrix4.CreateTranslation(TranslateX, TranslateY, TranslateZ) *
                 meshScale);
        }
Beispiel #9
0
        public void setterRotacion(string XYZ, float angulo)// poner x o y o z y el angulo al cual rotar
        {
            switch (XYZ)
            {
            case "x":
                model = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(angulo));     //rotacion
                break;

            case "y":
                model = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(angulo));     //rotacion
                break;

            case "z":
                model = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(angulo));     //rotacion
                break;
            }
        }
        public void Render(Gun gun, EntityRenderer entRenderer, ItemViewbob viewbob)
        {
            if (muzzleFlashTime > 0)
            {
                Matrix4 flashMatrix;

                if (ownerPlayer.IsRenderingThirdperson)
                {
                    flashMatrix =
                        Matrix4.CreateTranslation(gun.MuzzleFlashOffset)
                        * Matrix4.CreateScale(gun.ThirdpersonScale)
                        * Matrix4.CreateTranslation(0, 1.5f, -0.25f)
                        * Matrix4.CreateRotationZ(MathHelper.ToRadians(viewbob.CurrentTilt))
                        * Matrix4.CreateTranslation(gun.ModelOffset + viewbob.CurrentViewBob
                                                    + new Vector3(-1.35f, 0, -viewbob.CurrentKickback + -2))
                        * Matrix4.CreateRotationX(MathHelper.ToRadians(camera.Pitch))
                        * Matrix4.CreateRotationY(MathHelper.ToRadians(-camera.Yaw) - MathHelper.Pi)
                        * Matrix4.CreateTranslation(ownerPlayer.Transform.Position
                                                    + new Vector3(0, ownerPlayer.Size.Y / 2f - 1.5f, 0));
                }
                else
                {
                    flashMatrix =
                        Matrix4.CreateTranslation(gun.MuzzleFlashOffset)
                        * Matrix4.CreateRotationX(MathHelper.ToRadians(viewbob.CurrentSway.X))
                        * Matrix4.CreateRotationY(MathHelper.ToRadians(viewbob.CurrentSway.Y))
                        * Matrix4.CreateRotationZ(MathHelper.ToRadians(viewbob.CurrentTilt
                                                                       + viewbob.CurrentSway.Y * 0.5f))
                        * Matrix4.CreateTranslation(gun.ModelOffset + viewbob.CurrentViewBob
                                                    + new Vector3(0, 0, -viewbob.CurrentKickback))
                        * Matrix4.CreateRotationX(MathHelper.ToRadians(camera.Pitch))
                        * Matrix4.CreateRotationY(MathHelper.ToRadians(-camera.Yaw) - MathHelper.Pi)
                        * Matrix4.CreateTranslation(camera.OffsetPosition);
                }

                light.Position   = flashMatrix.ExtractTranslation();
                light.LightPower = (muzzleFlashTime / MUZZLE_FLASH_COOLDOWN) * MUZZLE_FLASH_LIGHT_POWER;

                flashCube.RenderFront = !ownerPlayer.IsRenderingThirdperson;
                entRenderer.Batch(flashCube, flashMatrix);
            }
            else
            {
                light.Visible = false;
            }
        }
        /// <summary>
        /// Sets up the orientations we use by default for bones
        /// </summary>
        private static void initializeOrientations()
        {
            /** First convert the degrees to radians */
            VECTOR_ORIENT_LEFT.X = MathHelper.DegreesToRadians(VECTOR_ORIENT_LEFT.X);
            VECTOR_ORIENT_LEFT.Y = MathHelper.DegreesToRadians(VECTOR_ORIENT_LEFT.Y);
            VECTOR_ORIENT_LEFT.Z = MathHelper.DegreesToRadians(VECTOR_ORIENT_LEFT.Z);

            VECTOR_ORIENT_RIGHT.X = MathHelper.DegreesToRadians(VECTOR_ORIENT_RIGHT.X);
            VECTOR_ORIENT_RIGHT.Y = MathHelper.DegreesToRadians(VECTOR_ORIENT_RIGHT.Y);
            VECTOR_ORIENT_RIGHT.Z = MathHelper.DegreesToRadians(VECTOR_ORIENT_RIGHT.Z);

            VECTOR_ORIENT_UP.X = MathHelper.DegreesToRadians(VECTOR_ORIENT_UP.X);
            VECTOR_ORIENT_UP.Y = MathHelper.DegreesToRadians(VECTOR_ORIENT_UP.Y);
            VECTOR_ORIENT_UP.Z = MathHelper.DegreesToRadians(VECTOR_ORIENT_UP.Z);

            VECTOR_ORIENT_DOWN.X = MathHelper.DegreesToRadians(VECTOR_ORIENT_DOWN.X);
            VECTOR_ORIENT_DOWN.Y = MathHelper.DegreesToRadians(VECTOR_ORIENT_DOWN.Y);
            VECTOR_ORIENT_DOWN.Z = MathHelper.DegreesToRadians(VECTOR_ORIENT_DOWN.Z);

            /** Build the orientation matrixes */

            ORIENT_LEFT = Matrix4.Identity;
            ORIENT_LEFT = ORIENT_LEFT *
                          Matrix4.CreateRotationX(VECTOR_ORIENT_LEFT.X) *
                          Matrix4.CreateRotationY(VECTOR_ORIENT_LEFT.Y) *
                          Matrix4.CreateRotationZ(VECTOR_ORIENT_LEFT.Z);

            ORIENT_RIGHT = Matrix4.Identity;
            ORIENT_RIGHT = ORIENT_RIGHT *
                           Matrix4.CreateRotationX(VECTOR_ORIENT_RIGHT.X) *
                           Matrix4.CreateRotationY(VECTOR_ORIENT_RIGHT.Y) *
                           Matrix4.CreateRotationZ(VECTOR_ORIENT_RIGHT.Z);

            ORIENT_UP = Matrix4.Identity;
            ORIENT_UP = ORIENT_UP *
                        Matrix4.CreateRotationX(VECTOR_ORIENT_UP.X) *
                        Matrix4.CreateRotationY(VECTOR_ORIENT_UP.Y) *
                        Matrix4.CreateRotationZ(VECTOR_ORIENT_UP.Z);

            ORIENT_DOWN = Matrix4.Identity;
            ORIENT_DOWN = ORIENT_DOWN *
                          Matrix4.CreateRotationX(VECTOR_ORIENT_DOWN.X) *
                          Matrix4.CreateRotationY(VECTOR_ORIENT_DOWN.Y) *
                          Matrix4.CreateRotationZ(VECTOR_ORIENT_DOWN.Z);
            ORIENT_FRONT = Matrix4.Identity;
        }
        /// <summary>
        /// Increments the position of the camera, without changing the look at
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        public void incrementCameraPosition(float x, float y, float z)
        {
            Vector3 diff = mTargetPosition - mCameraPosition;

            diff.Normalize();

            /** diff is camera to target, rotate by 90 to get some value? */
            Matrix4 ry = Matrix4.CreateRotationX(MathHelper.PiOver2);
            Matrix4 rx = Matrix4.CreateRotationY(MathHelper.PiOver2);
            Vector3 xt = Vector3.Transform(diff, rx);
            Vector3 yt = Vector3.Transform(diff, ry);

            mCameraPosition += diff * z;
            mCameraPosition += x * xt;
            mCameraPosition += y * yt;
            recalculateCameraTransform();
        }
Beispiel #13
0
        public Matrix4 CalcMatrix()
        {
            FovY = MathHelper.Clamp(FovY, 0.1f, 180);
            var mtxProjection = Matrix4.Transpose(Matrix4.CreatePerspectiveFieldOfView(
                                                      MathHelper.DegreesToRadians(FovY),
                                                      Aspect, NearClip, FarClip));

            Matrix4 rotationZ = Matrix4.CreateRotationZ(Tilt);
            Matrix4 rotationY = Matrix4.CreateRotationY(Heading);
            Matrix4 rotation  = rotationY * rotationZ;
            Vector3 target    = new Vector3(rotation.M11, rotation.M21, rotation.M31);
            var     mtxLookAt = Matrix4.Transpose(Matrix4.LookAt(Position, Position + target, Vector3.UnitY));

            var camera = mtxProjection * mtxLookAt;

            return(camera);
        }
Beispiel #14
0
        public void Rotate(float dx, float dy)
        {
            yaw   += dx;
            pitch += dy;
            if (pitch < 1.8f)
            {
                pitch = 1.8f;
            }
            if (pitch > 4.5f)
            {
                pitch = 4.5f;
            }

            Matrix4 rotationMatrix = Matrix4.Mult(Matrix4.CreateRotationX(pitch), Matrix4.CreateRotationY(yaw));

            LookAt = Vector3.Add(position, -Vector3.TransformVector(Vector3.UnitZ, rotationMatrix));
        }
Beispiel #15
0
        private static void OnRenderFrame()
        {
            // calculate how much time has elapsed since the last frame
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // use the deltaTime to adjust the angle of the cube and pyramid
            angle += deltaTime;

            // set up the OpenGL viewport and clear both the color and depth bits
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // use our shader program
            Gl.UseProgram(program);
            /////////////////
            // bind the vertex positions, colors and elements of the cube for
            //program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(-1.5f, 0, 0)));
            //Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
            //Gl.BindBufferToShaderAttribute(cubeColor, program, "vertexColor");
            //Gl.BindBuffer(cubeQuads);

            //// draw the cube
            //Gl.DrawElements(BeginMode.Quads, cubeQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            //////////////////

            /////////////////////////////////////////////////////////////////////////////////////////////////
            // bind the vertex positions, colors and elements of the cube
            //program["model_matrix"].SetValue(Matrix4.CreateRotationY(myAngle) * Matrix4.CreateTranslation(new Vector3(1.5f, 0, 0)));
            //program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateRotationX(1) * Matrix4.CreateTranslation(new Vector3(1.5f, 0, 0)));
            program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(1.5f, 0, 0)));

            Gl.BindBufferToShaderAttribute(poly, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(polyColor, program, "vertexColor");
            Gl.BindBuffer(polygon3d);

            // draw the cube
            Gl.DrawElements(BeginMode.Quads, cubeQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);



            /////////////////////////////////////////////////////////////////////////////////////////////////
            Glut.glutSwapBuffers();
        }
Beispiel #16
0
        private void MoveCamera(List <KeyAction> actions)
        {
            Matrix4 rotationY = Matrix4.CreateRotationY(_renderer.Camera.Heading);

            if (actions.Contains(KeyAction.MoveForwards))
            {
                _renderer.Camera.Position += new Vector3(rotationY.M11, rotationY.M21, rotationY.M31);
            }

            if (actions.Contains(KeyAction.MoveBackwards))
            {
                _renderer.Camera.Position -= new Vector3(rotationY.M11, rotationY.M21, rotationY.M31);
            }

            if (actions.Contains(KeyAction.MoveLeft))
            {
                Matrix4 rotationLeft = Matrix4.CreateRotationY(_renderer.Camera.Heading - (float)Math.PI / 2);
                _renderer.Camera.Position += new Vector3(rotationLeft.M11, rotationLeft.M21, rotationLeft.M31);
            }

            if (actions.Contains(KeyAction.MoveRight))
            {
                Matrix4 rotationRight = Matrix4.CreateRotationY(_renderer.Camera.Heading + (float)Math.PI / 2);
                _renderer.Camera.Position += new Vector3(rotationRight.M11, rotationRight.M21, rotationRight.M31);
            }

            if (actions.Contains(KeyAction.MoveUp))
            {
                _renderer.Camera.Position += Vector3.UnitY;
            }

            if (actions.Contains(KeyAction.MoveDown))
            {
                _renderer.Camera.Position -= Vector3.UnitY;
            }

            if (actions.Contains(KeyAction.SwitchMethod))
            {
                _renderer.SwitchRenderMethod();
            }

            if (actions.Contains(KeyAction.SwitchAlgorythm))
            {
                _renderer.SwitchRenderAlgorythm();
            }
        }
Beispiel #17
0
        public Matrix4 GetTransformationMatrix(int transformationIndex)
        {
            Matrix4 matrix = Matrix4.Identity;

            matrix *= Matrix4.CreateScale(Transformations[transformationIndex].Scale);
            matrix *= Matrix4.CreateRotationX(Transformations[transformationIndex].Rotation.X);
            matrix *= Matrix4.CreateRotationY(Transformations[transformationIndex].Rotation.Y);
            matrix *= Matrix4.CreateRotationZ(Transformations[transformationIndex].Rotation.Z);
            matrix *= Matrix4.CreateTranslation(Transformations[transformationIndex].Translation * 10.0f);

            if (ParentNode != null)
            {
                matrix *= ParentNode.GetTransformationMatrix(transformationIndex);
            }

            return(matrix);
        }
        public static Matrix4 CreateViewMatrix(Camera camera)
        {
            Matrix4 viewMatrix = new Matrix4();

            viewMatrix = Matrix4.Identity;

            var pitchM = Matrix4.CreateRotationX(camera.pitch);
            var yawM   = Matrix4.CreateRotationY(camera.yaw);

            var negCameraPos = new Vector3(-camera.position.X, -camera.position.Y, -camera.position.Z);

            var translateM = Matrix4.CreateTranslation(negCameraPos);

            viewMatrix = pitchM * yawM * translateM;

            return(viewMatrix);
        }
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            if (!_isUpdating)
            {
                return;
            }

            _currentAngle  = _currentAngle + 80f * (float)e.Time;
            _currentAngle %= 360;

            _modelMatrix =
                Matrix4.CreateScale(2f, 2f, 2f) *
                Matrix4.CreateRotationY(MathHelper.DegreesToRadians(_currentAngle));
            _mvpMatrix = _modelMatrix * _viewMatrix * _projMatrix;
        }
Beispiel #20
0
        public void Rotate(float degX, float degY, float degZ)
        {
            Console.WriteLine("Before Rotate: ");
            Console.WriteLine(arm._transform.ExtractRotation());
            arm._transform *= Matrix4.CreateRotationX(MathHelper.DegreesToRadians(degX));
            arm._transform *= Matrix4.CreateRotationY(MathHelper.DegreesToRadians(degY));
            arm._transform *= Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(degZ));


            forearm._transform *= Matrix4.CreateRotationX(MathHelper.DegreesToRadians(degX));
            forearm._transform *= Matrix4.CreateRotationY(MathHelper.DegreesToRadians(degY));
            forearm._transform *= Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(degZ));

            palm._transform *= Matrix4.CreateRotationX(MathHelper.DegreesToRadians(degX));
            palm._transform *= Matrix4.CreateRotationY(MathHelper.DegreesToRadians(degY));
            palm._transform *= Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(degZ));
        }
Beispiel #21
0
        public void TestMatrix()
        {
            var     mat1 = Matrix4.CreateRotationX(30);
            var     mat2 = Matrix4.CreateRotationY(40);
            var     mat3 = Matrix4.CreateRotationZ(50);
            var     mat4 = Matrix4.CreateScale(100, 100, 100);
            var     mat5 = Matrix4.CreateTranslation(1000, 1000, 1000);
            Matrix4 res  = new Matrix4();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                res = mat1 * mat2 * mat3 * mat4 * mat5;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
        }
        /// <summary>
        /// Sets position of the camera
        /// </summary>
        private void SetCameraPosition()
        {
            Vector3 cameraPosition = new Vector3(0.0f, 0.0f, actualZoom);

            Matrix4 rotationX = Matrix4.CreateRotationX(-yAngle);
            Matrix4 rotationY = Matrix4.CreateRotationY(xAngle);

            cameraPosition = Vector3.TransformPosition(cameraPosition, rotationX);
            cameraPosition = Vector3.TransformPosition(cameraPosition, rotationY);

            GL.MatrixMode(MatrixMode.Modelview);

            Matrix4 lookAt = Matrix4.LookAt(cameraPosition, Vector3.Zero, Vector3.UnitY);

            lookAt *= Matrix4.CreateTranslation(new Vector3(-cameraShift.X, -cameraShift.Y, 0));
            GL.LoadMatrix(ref lookAt);
        }
        public virtual void Render(ICamera camera)
        {
            _model.Bind();
            var t2 = Matrix4.CreateTranslation(_possition.X, _possition.Y, _possition.Z);
            var s  = Matrix4.CreateScale(_scale);
            // var r = Matrix4.CreateFromQuaternion(quat);
            var p1 = Matrix4.CreateTranslation(-_center);
            var r  = Matrix4.CreateRotationY(ang);
            var p2 = Matrix4.CreateTranslation(_center);

            r            = p1 * r * p2;
            _modelMatrix = s * t2 * r;
            _viewMatrix  = camera.LookAtMatrix;
            GL.UniformMatrix4(20, false, ref _modelMatrix);
            GL.UniformMatrix4(21, false, ref _viewMatrix);
            _model.Render();
        }
Beispiel #24
0
        public Matrix4 GetMatrix(int id)
        {
            // ha a gyökérnél vagyunk, akkor egységmátrix
            if (id == -1)
            {
                return(Matrix4.Identity);
            }

            // változások a szülõhöz képest
            Vector3 tr  = current_skeleton.bones[id].translate; // eltolás
            Vector3 rot = current_skeleton.bones[id].rotate;    // forgatás
            // lokális és a szülõ mátrixok egybe gyúrása
            Matrix4 local         = Matrix4.CreateRotationX(rot.X) * Matrix4.CreateRotationY(rot.Y) * Matrix4.CreateRotationZ(rot.Z) * Matrix4.CreateTranslation(tr);
            Matrix4 parent_global = GetMatrix(nodes[id].parent_id);

            return(local * parent_global);
        }
Beispiel #25
0
        public void RotatePosition(Int32 deltaX, Int32 deltaY)
        {
            // rotate pitch
            Vector3 lookDir     = Vector3.Normalize(Look - Position);
            Vector3 binormalDir = Vector3.Normalize(Vector3.Cross(lookDir, Up));
            Matrix4 rotatePitch = Matrix4.CreateFromAxisAngle(binormalDir, MathHelper.DegreesToRadians(-deltaY * ROTATE_MEASURE));

            // rotate yaw
            Matrix4 rotateYaw = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(-deltaX * ROTATE_MEASURE));

            Matrix4 rotationMatrix = Matrix4.Identity;

            rotationMatrix *= rotateYaw;
            rotationMatrix *= rotatePitch;

            Position = new Vector3(VectorMath.VectorMathOperations.multMatrix(rotationMatrix, new Vector4(this.Position, 1.0f)));
        }
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            HandleMove();
            view = Matrix4.LookAt(camPos, camPos + camFront, camUp);

            Vector3[] pos =
            {
                new Vector3(0, 0,  0),
                new Vector3(0, 0, -2),
                new Vector3(2, 2, 0)
            };
            GL.ClearColor(Color4.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);



            Matrix4 translate;
            int     translLoc = GL.GetUniformLocation(progHandle, "translate");


            foreach (Vector3 vec in pos)
            {
                //build model matrix


                Matrix4 scl = Matrix4.CreateScale(1);

                Matrix4 rX = Matrix4.CreateRotationX(0);
                Matrix4 rY = Matrix4.CreateRotationY(0);
                Matrix4 rZ = Matrix4.CreateRotationZ(0);

                Matrix4 trans = Matrix4.CreateTranslation(vec);

                Matrix4 model = scl * (rX * rY * rZ) * trans;



                translate = model * view * projection;
                GL.UniformMatrix4(translLoc, false, ref translate);
                GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
            }
            //  GL.DrawElements(BeginMode.Triangles, 6, DrawElementsType.UnsignedInt, 0);

            this.SwapBuffers();
        }
Beispiel #27
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            time += 400.0 * e.Time;

            GL.Clear(mask: ClearBufferMask.ColorBufferBit);

            //// ---------- Uniform

            //float time = (float)this.sw.Elapsed.TotalMilliseconds;
            //GL.Uniform1(GL.GetUniformLocation(shader.Program, "time"), time);

            //Vector2 mouse = this.mouse;
            //GL.Uniform2(GL.GetUniformLocation(shader.Program, "mouse"), mouse);

            //Vector2 resolution = new Vector2(Width, Height);
            //GL.Uniform2(GL.GetUniformLocation(shader.Program, "resolution"), resolution);

            ////vertices[0] += 0.001f;

            // ----------- VAO

            GL.BindVertexArray(VertexArrayObject); // VAO

            // ----------- VAO
            texture.Use(TextureUnit.Texture0);
            texture2.Use(TextureUnit.Texture1);
            shader.Use();
            // -----------

            Matrix4 model = Matrix4.Identity * Matrix4.CreateRotationY
                                ((float)MathHelper.DegreesToRadians(time));

            shader.SetMatrix4("model", model);
            shader.SetMatrix4("view", view);
            shader.SetMatrix4("projection", projection);

            // -----------

            GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
            // -----------

            this.Context.SwapBuffers();

            base.OnRenderFrame(e);
        }
Beispiel #28
0
        //画面更新で実行される。
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            var keyboard = Keyboard.GetState();

            //Escapeキーで終了
            if (keyboard[Key.Escape])
            {
                this.Exit();
            }

            #region Camera__Keyboard

            //F1キーで回転をリセット
            if (keyboard[Key.Number1])
            {
                rotate = Matrix4.Identity;
            }

            //F2キーでY軸90度回転
            if (keyboard[Key.Number2])
            {
                rotate = Matrix4.CreateRotationY(MathHelper.PiOver2);
            }

            //F3キーでY軸180度回転
            if (keyboard[Key.Number3])
            {
                rotate = Matrix4.CreateRotationY(MathHelper.Pi);
            }

            //F4キーでY軸270度回転
            if (keyboard[Key.Number4])
            {
                rotate = Matrix4.CreateRotationY(MathHelper.ThreePiOver2);
            }

            //F5キーで拡大をリセット
            if (keyboard[Key.Number5])
            {
                zoom = 1.0f;
            }

            #endregion
        }
Beispiel #29
0
        // tick for background surface
        public void Tick()
        {
            screen.Clear(0);

            //camera movement
            if (Keyboard.GetState().IsKeyDown(Key.S))
            {
                camPos.Z -= moveSpeed * timer.ElapsedMilliseconds * (float)Math.Cos(camRot.Y) * (float)Math.Cos(camRot.X);
                camPos.X -= moveSpeed * timer.ElapsedMilliseconds * (float)Math.Sin(camRot.Y) * -(float)Math.Cos(camRot.X);
                camPos.Y -= moveSpeed * timer.ElapsedMilliseconds * (float)Math.Sin(camRot.X);

            }
            if (Keyboard.GetState().IsKeyDown(Key.W))
            {
                camPos.Z += moveSpeed * timer.ElapsedMilliseconds * (float)Math.Cos(camRot.Y) * (float)Math.Cos(camRot.X);
                camPos.X += moveSpeed * timer.ElapsedMilliseconds * (float)Math.Sin(camRot.Y) * -(float)Math.Cos(camRot.X);
                camPos.Y += moveSpeed * timer.ElapsedMilliseconds * (float)Math.Sin(camRot.X);
            }
            if (Keyboard.GetState().IsKeyDown(Key.A))
            {
                camPos.X += moveSpeed * timer.ElapsedMilliseconds * (float)Math.Cos(camRot.Y) * (float)Math.Cos(camRot.X);
                camPos.Z += moveSpeed * timer.ElapsedMilliseconds * (float)Math.Sin(camRot.Y) * (float)Math.Cos(camRot.X);
            }               
            if (Keyboard.GetState().IsKeyDown(Key.D))
            {
                camPos.X -= moveSpeed * timer.ElapsedMilliseconds * (float)Math.Cos(camRot.Y) * (float)Math.Cos(camRot.X);
                camPos.Z -= moveSpeed * timer.ElapsedMilliseconds * (float)Math.Sin(camRot.Y) * (float)Math.Cos(camRot.X);
            }               
            camtransMatrix = Matrix4.CreateTranslation(0 + camPos.X, -4 + camPos.Y, -15 + camPos.Z);

            //camera rotation
            if (Keyboard.GetState().IsKeyDown(Key.Left))
                camRot.Y -= rotSpeed;
            if (Keyboard.GetState().IsKeyDown(Key.Right))
                camRot.Y += rotSpeed;
            if (Keyboard.GetState().IsKeyDown(Key.Up))
                camRot.X -= rotSpeed;
            if (Keyboard.GetState().IsKeyDown(Key.Down))
                camRot.X += rotSpeed;

            camrotMatrixY = Matrix4.CreateRotationY(camRot.Y);
            camrotMatrixX = Matrix4.CreateRotationX(camRot.X);
            fullcamRot = camrotMatrixX * camrotMatrixY;
            viewDirection = fullcamRot.Row2.Xyz;
        }
        public Vector3 WorldToScreenPos(Vector3 obj_pos, GLView view)
        {
            if (view.m_view_type == ViewType.TOP)
            {
                obj_pos = Vector3.Transform(obj_pos, Matrix4.CreateRotationX(-Utility.RAD_90));
            }
            else if (view.m_view_type == ViewType.RIGHT)
            {
                obj_pos = Vector3.Transform(obj_pos, Matrix4.CreateRotationY(Utility.RAD_90));
            }
            else if (view.m_view_type == ViewType.PERSP)
            {
                //obj_pos.Z *= -1f;
            }
            obj_pos.Z *= -1f;

            return(Utility.WorldToScreen(obj_pos, view.m_cam_mat, view.m_persp_mat, view.m_control_sz.X, view.m_control_sz.Y, (view.m_view_type == ViewType.PERSP)));
        }