Created
July 27, 2019 15:11
-
-
Save MaximovInk/3ad6dd2586b43b4420f979d943cd65b7 to your computer and use it in GitHub Desktop.
2D Camera for OpenTK with zoom, scale, translation and rotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Camera | |
{ | |
public Vector3 Position; | |
public Vector3 Rotation; | |
public Vector3 Scale = Vector3.One/3; | |
public float Size = 1; | |
public static Camera Main = new Camera(); | |
public void Update(float deltaTime) | |
{ | |
if (Input.GetKey(OpenTK.Input.Key.W)) | |
{ | |
Position -= new Vector3(0, 1, 0)*deltaTime; | |
} | |
if (Input.GetKey(OpenTK.Input.Key.S)) | |
{ | |
Position += new Vector3(0, 1, 0)*deltaTime; | |
} | |
if (Input.GetKey(OpenTK.Input.Key.A)) | |
{ | |
Position -= new Vector3(1, 0, 0)*deltaTime; | |
} | |
if (Input.GetKey(OpenTK.Input.Key.D)) | |
{ | |
Position += new Vector3(1, 0, 0)*deltaTime; | |
} | |
if (Input.GetMouseButton(OpenTK.Input.MouseButton.Left)) | |
{ | |
Rotation.Z += Input.MouseYDelta*deltaTime; | |
} | |
Size += -Input.MouseScrollDelta * deltaTime; | |
Size = Size.Clamp(0, 1000); | |
} | |
public void Render(int width, int height) | |
{ | |
GL.MatrixMode(MatrixMode.Projection); | |
GL.LoadIdentity(); | |
Matrix4 proj = | |
Matrix4.CreateTranslation(new Vector3(-Position.X*width, -Position.Y*height,-Position.Z))* | |
Matrix4.CreateScale(Scale.X, -Scale.Y, Scale.Z) * Matrix4.CreateRotationZ(Rotation.Z)* | |
Matrix4.CreateOrthographic(width*Size, height*Size, 0, 1000) | |
; | |
GL.LoadMatrix(ref proj); | |
GL.MatrixMode(MatrixMode.Modelview); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment