public override void Initialize() { GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Lighting); GL.Enable(EnableCap.Light0); cameraPlane = new Plane(); Resize(MainGameWindow.Window.Width, MainGameWindow.Window.Height); grid = new Grid(true); model = new OBJLoader("Assets/test_object.obj"); GL.Light(LightName.Light0, LightParameter.Position, new float[] { 0.0f, 0.5f, 0.5f, 0.0f }); GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { 0f, 1f, 0f, 1f }); GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { 0f, 1f, 0f, 1f }); GL.Light(LightName.Light0, LightParameter.Specular, new float[] { 1f, 1f, 1f, 1f }); for (int i = 0; i < 6; i++) { frustum[i] = new Plane(); } MouseState mouse = OpenTK.Input.Mouse.GetState(); LastMousePosition = new Vector2(mouse.X, mouse.Y); viewMatrix = Move3DCamera(0f); }
public static Plane FromNumbers(Vector4 numbers) { Plane p = new Plane(); p.n = new Vector3(); p.n.X = numbers.X; p.n.Y = numbers.Y; p.n.Z = numbers.Z; p.d = numbers.W; return p; }
public static float HalfSpace(Plane p,Vector3 v) { return (p.n.X*v.X) + (p.n.Y*v.Y) + (p.n.Z*v.Z) + p.d; /* Vector4 N = new Vector4(p.n.X, p.n.Y, p.n.Z, 0f); Vector4 PointOnPlane = new Vector4(p.n.X * p.d, p.n.Y * p.d, p.n.Z * p.d, 1f); Vector4 V = PointOnPlane - new Vector4(v.X, v.Y, v.X, 1f); return (int)Vector4.Dot(N,V); */ }
Matrix4 Move3DCamera(float timeStep, float moveSpeed = 10.0f) { //helper variables for mouse position const float mouseSensitivity = .01f; MouseState mouse = OpenTK.Input.Mouse.GetState(); KeyboardState keyboard = OpenTK.Input.Keyboard.GetState(); //figure out delta mouse movement Vector2 mousePosition = new Vector2(mouse.X, mouse.Y); var mouseMove = mousePosition - LastMousePosition; LastMousePosition = mousePosition; //if left is pressed, update yaw and pitch if (mouse.LeftButton == ButtonState.Pressed) { Yaw -= mouseSensitivity * mouseMove.X; Pitch -= mouseSensitivity * mouseMove.Y; if (Pitch < -90f) { Pitch = -90f; } else if (Pitch > 90f) { Pitch = 90f; } } //now that we have yaw and pitch, create an orientation Matrix4 pitch = Matrix4.XRotation(Pitch); Matrix4 yaw = Matrix4.YRotation(Yaw); Matrix4 orientation = /*rol * */pitch * yaw; //get the orientations right and forwards vectors Vector3 right = Matrix4.MultiplyVector(orientation, new Vector3(1f, 0f, 0f)); Vector3 forward = Matrix4.MultiplyVector(orientation, new Vector3(0f, 0f, 1f)); //update movement based on WASD if (keyboard[OpenTK.Input.Key.W]) { CameraPosition += forward * -1f * moveSpeed * timeStep; } if (keyboard[OpenTK.Input.Key.S]) { CameraPosition += forward * moveSpeed * timeStep; } if (keyboard[OpenTK.Input.Key.A]) { CameraPosition += right * -1f * moveSpeed * timeStep; } if (keyboard[OpenTK.Input.Key.D]) { CameraPosition += right * moveSpeed * timeStep; } //now we have position vector, make position matrix Matrix4 position = Matrix4.Translate(CameraPosition); //using position and orientation, get camera into world Matrix4 cameraWorldPosition = position * orientation; //the view matrix is inverse of world Matrix4 cameraViewMatrix = Matrix4.Inverse(cameraWorldPosition); right = new Vector3(cameraWorldPosition[0, 0], cameraWorldPosition[1, 0], cameraWorldPosition[2, 0]); Vector3 left = new Vector3(-right.X, -right.Y, -right.Z); Vector3 up = new Vector3(cameraWorldPosition[0, 1], cameraWorldPosition[1, 1], cameraWorldPosition[2, 1]); right = Matrix4.MultiplyPoint(cameraWorldPosition, right); left = Matrix4.MultiplyPoint(cameraWorldPosition, left); up = Matrix4.MultiplyPoint(cameraWorldPosition, up); cameraPlane = Plane.ComputePlane(left, right, up); Matrix4 perspective = Matrix4.Perspective(60.0f, aspect, 0.01f, 1000.0f); Matrix4 mv = perspective * cameraViewMatrix; Vector4 row1 = new Vector4(mv[0, 0], mv[0, 1], mv[0, 2], mv[0, 3]); Vector4 row2 = new Vector4(mv[1, 0], mv[1, 1], mv[1, 2], mv[1, 3]); Vector4 row3 = new Vector4(mv[2, 0], mv[2, 1], mv[2, 2], mv[2, 3]); Vector4 row4 = new Vector4(mv[3, 0], mv[3, 1], mv[3, 2], mv[3, 3]); frustum[0] = Plane.FromNumbers(row4 + row1); frustum[1] = Plane.FromNumbers(row4 - row1); frustum[2] = Plane.FromNumbers(row4 + row2); frustum[3] = Plane.FromNumbers(row4 - row2); frustum[4] = Plane.FromNumbers(row4 + row3); frustum[5] = Plane.FromNumbers(row4 - row3); return cameraViewMatrix; }
public bool PointInFrustum(Plane[] frustum,Vector3 point) { foreach(Plane plane in frustum) { if(Plane.HalfSpace(plane, point) < 0) { return false; } } return true; }
public float d = 0; //distance from origin, d=dot(n,p) public static Plane ComputePlane(Vector3 a, Vector3 b, Vector3 c) { Plane p = new Plane(); p.n = Vector3.Normalize(Vector3.Cross(b - a, c - a)); p.d = Vector3.Dot(p.n, a); return p; }