Pages

View Change and 2D & 3D Camera Templates

Going to be lazy here, A method to change views and a camera class.
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
private void ChangeView(bool ortho = false)
{
    Matrix4 matrix;

    if (ortho)
    {
        matrix = Matrix4.CreateOrthographic(Width, Height, 1.0f, 100.0f);
    }
    else
    {
        matrix = Matrix4.CreatePerspectiveFieldOfView(1.0f, (float)(Width / (double)Height), 1.0f, 10000.0f);
    }

    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadMatrix(ref matrix);
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Camera2D
{
    public enum DrawMode
    {
        Normal,
        Wireframe
    }

    public float X { get; set; }
    public float Y { get; set; }

    public float Angle { get; set; }

    private DrawMode _mode;
    public DrawMode Mode
    {
        get { return _mode; }
        set
        { 
            _mode = value;
            UpdateDrawMode();
        }
    }

    public Camera2D(float x = 0.0f, float y = 0.0f)
    {
        X = x;
        Y = y;
        _mode = DrawMode.Normal;
    }

    private void UpdateDrawMode()
    {
        switch (_mode)
        {
            case DrawMode.Normal:
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                break;
            case DrawMode.Wireframe:
                GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
                break;
        }
    }

    public void Update()
    {
        GL.Rotate(Angle, 0, 0, 1);
        GL.Translate(-X, -Y, 0);
    }
}

Also a 3D version RotationZ is ignored on movement, it is just a simple camera.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Camera3D
{
    public enum Direction
    {
        Forward,
        Backward,
        Left,
        Right,
        Up,
        Down
    }

    public float X { get; set; }
    public float Y { get; set; }
    public float Z { get; set; }

    public float RotationX { get; set; }
    public float RotationY { get; set; }
    public float RotationZ { get; set; }

    public Camera3D(float x = 0.0f, float y = 0.0f, float z = 0.0f)
    {
        X = x;
        Y = y;
        Z = z;
    }

    public void Update()
    {
        GL.Rotate(RotationX, 1, 0, 0);
        GL.Rotate(RotationY, 0, 1, 0);
        GL.Rotate(RotationZ, 0, 0, 1);
        GL.Translate(-X, -Y, -Z);
    }

    public void Move(Direction direction, float speed = 1.0f, bool flying = false)
    {
        float dX = RotationX * (float)(Math.PI / 180);
        float dY = RotationY * (float)(Math.PI / 180);

        switch (direction)
        {
            case Direction.Forward:
                X +=  (float)Math.Sin(dY) * speed;
                Z += -(float)Math.Cos(dY) * speed;
                if (flying) 
                    Y += -(float)Math.Sin(dX) * speed;
                break;
            case Direction.Backward:
                X += -(float)Math.Sin(dY) * speed;
                Z +=  (float)Math.Cos(dY) * speed;
                if (flying) 
                    Y += (float)Math.Sin(dX) * speed;
                break;
            case Direction.Left:
                X += -(float)Math.Cos(dY) * speed;
                Z += -(float)Math.Sin(dY) * speed;
                break;
            case Direction.Right:
                X += (float)Math.Cos(dY) * speed;
                Z += (float)Math.Sin(dY) * speed;
                break;
            case Direction.Up:
                Y += speed;
                break;
            case Direction.Down:
                Y -= speed;
                break;
        }
    }
}

No comments :

Post a Comment