82 lines
No EOL
2.4 KiB
TypeScript
82 lines
No EOL
2.4 KiB
TypeScript
import { mat4, vec3, quat, mat3 } from "gl-matrix";
|
|
|
|
export class Camera {
|
|
pos: vec3;
|
|
|
|
yaw: number;
|
|
pitch: number;
|
|
roll: number;
|
|
|
|
#proj: mat4;
|
|
|
|
fovY: number;
|
|
aspect: number;
|
|
far: number;
|
|
near: number;
|
|
constructor(pos: vec3) {
|
|
this.pos = pos;
|
|
this.#proj = mat4.create();
|
|
this.yaw = 0;
|
|
this.pitch = 0;
|
|
this.roll = 0;
|
|
console.log("forward" , this.forward);
|
|
console.log("up" , this.up);
|
|
}
|
|
rotateRight(rad: number) {
|
|
this.yaw += rad;
|
|
if(this.yaw >= Math.PI * 2 ){
|
|
this.yaw -= Math.PI * 2;
|
|
}
|
|
}
|
|
rotateUp(rad: number) {
|
|
this.pitch += rad;
|
|
this.pitch = Math.min(this.pitch, Math.PI * 89/ 180);
|
|
this.pitch = Math.max(this.pitch, - Math.PI * 89 / 180);
|
|
}
|
|
rotateClockwise(rad: number) {
|
|
this.roll += rad;
|
|
}
|
|
get viewMatrix(): mat4 {
|
|
const eye = vec3.create();
|
|
const lookAt = mat4.create();
|
|
vec3.add(eye, this.pos, this.forward);
|
|
return mat4.lookAt(lookAt, this.pos, eye, this.up);
|
|
}
|
|
/**
|
|
* update projection matrix
|
|
*/
|
|
UpdateProjectionMat() {
|
|
mat4.perspective(this.#proj, this.fovY, this.aspect, this.near, this.far);
|
|
}
|
|
get projectionMatrix(): mat4 {
|
|
return this.#proj;
|
|
}
|
|
get forward(): vec3 {
|
|
const sinYaw = -Math.sin(this.yaw);
|
|
const cosYaw = Math.cos(this.yaw);
|
|
const ret = vec3.fromValues(sinYaw * Math.cos(this.pitch), -Math.sin(this.pitch), cosYaw * Math.cos(this.pitch));
|
|
vec3.normalize(ret,ret);
|
|
return ret;
|
|
}
|
|
get up(): vec3 {
|
|
const right = vec3.cross(vec3.create(),this.forward,[0,1,0]);
|
|
vec3.normalize(right,right);
|
|
const ret = vec3.cross(vec3.create(),right,this.forward);
|
|
vec3.normalize(ret,ret);
|
|
/*const a = Math.sin(this.yaw);
|
|
const b = Math.cos(this.yaw);
|
|
const u = vec3.fromValues(b * Math.sin(this.pitch), Math.cos(this.pitch), a * Math.sin(this.pitch));
|
|
const v = vec3.create();
|
|
vec3.cross(v, u, this.forward);
|
|
const ret = vec3.create();
|
|
const tmp1 = vec3.create();
|
|
const tmp2 = vec3.create();
|
|
vec3.add(ret, vec3.scale(tmp1, u, Math.cos(this.roll)), vec3.scale(tmp2, v, -Math.sin(this.roll)));*/
|
|
return ret;
|
|
}
|
|
get right(): vec3 {
|
|
const ret = vec3.create();
|
|
vec3.cross(ret, this.forward, this.up);
|
|
return ret;
|
|
}
|
|
}; |