Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module entity.camera;
- import
- ws.math.math,
- ws.math.angle,
- ws.math.quaternion,
- ws.math.vector,
- ws.math.matrix,
- entity.entity,
- engine;
- class Camera: Entity {
- this(Engine e){
- super(e);
- matrix = new Matrix!(4,4);
- fov = 45;
- near = 1;
- far = 10000;
- update();
- }
- void setAspect(float aspect){
- this.aspect = aspect;
- update;
- }
- void setFov(float fov){
- this.fov = fov;
- update;
- }
- void setBounds(float near, float far){
- this.near = near;
- this.far = far;
- update;
- }
- void update(){
- float xmin, xmax, ymin, ymax;
- ymax = near * tan(fov*PI/360.0);
- ymin = -ymax;
- xmin = ymin * aspect;
- xmax = -xmin;
- matrix[0] = (2*near)/(xmax - xmin);
- matrix[5] = (2*near)/(ymax - ymin);
- matrix[8] = (xmax + xmin) / (xmax - xmin);
- matrix[9] = (ymax + ymin) / (ymax - ymin);
- matrix[10] = -((far + near)/(far - near));
- matrix[11] = -1;
- matrix[14] = -((2*far*near)/(far-near));
- matrix[15] = 0;
- }
- Matrix!(4,4) getProjection(){
- auto m = matrix.dup();
- Angle a = angle.euler();
- a.r *= -1;
- a.y *= -1;
- m.rotate(Quaternion.euler(a));
- m.translate(Vector!3(position.x, -position.y, position.z));
- return m;
- }
- protected {
- float aspect, fov, near, far;
- Matrix!(4,4) matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement