Skip to content

Commit cf7d636

Browse files
committed
Vertex transformation done!
1 parent 33a65b3 commit cf7d636

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/math/quaternions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { add_vec3, cross, normalize, scalar_mult_vec3 } from "./matrix_operators";
2-
import { mat4, vec3, vec4 } from "./types";
2+
import { mat4, vec3, vec4, EPSILON } from "./types";
33

44

5-
const EPSILON:number = 1e-7;
65

76
export function identity_quat():vec4{
87
return vec4(0,0,0,1);

src/math/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,5 @@ export function mat4(
109109
m[12] = m30; m[13] = m31; m[14] = m32; m[15] = m33;
110110
return m as mat4;
111111
}
112+
113+
export const EPSILON = 1e-6;

src/vertex/primitive_assembler.ts

Whitespace-only changes.

src/vertex/vertex.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { mul_mat4, mul_mat4_vec4, scalar_mult_vec3 } from "../math/matrix_operators";
2+
import { mat4, vec4, vec3, EPSILON, vec2 } from "../math/types";
3+
4+
export function transform_vertex(projection:mat4, view:mat4, model:mat4, vertex:vec3):vec4{
5+
const left:mat4 = mul_mat4(projection,view);
6+
const right:vec4 = mul_mat4_vec4(model,vec4(vertex[0],vertex[1],vertex[2],1.0));
7+
return mul_mat4_vec4(left,right);
8+
}
9+
10+
export function process_perspective(clip_pos:vec4):vec3|null {
11+
const w = clip_pos[3];
12+
if (w <= EPSILON){
13+
return null;
14+
}
15+
return scalar_mult_vec3(vec3(clip_pos[0],clip_pos[1],clip_pos[2]),1/w);
16+
}
17+
18+
export function to_viewport_space(ndc:vec3, width:number, height:number):vec2 {
19+
const x = (ndc[0] + 1) * 0.5 * width;
20+
const y = (1 - ndc[1]) * 0.5 * height;
21+
return vec2(x,y);
22+
}
23+

0 commit comments

Comments
 (0)