Skip to content

Commit 07573cd

Browse files
committed
Depth sorting and backface culling!
1 parent f10e698 commit 07573cd

5 files changed

Lines changed: 83 additions & 24 deletions

File tree

src/rendering/depth_sorting.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Mesh } from "./mesh";
2+
3+
4+
export function depth_sort(mesh: Mesh, stride: number = 4): number {
5+
const projected = mesh.projected_buffer;
6+
const raster = mesh.raster_buffer;
7+
const indices = mesh.indices;
8+
const num_triangles = indices.length / 3;
9+
let visible_count = 0;
10+
11+
for (let i = 0; i < num_triangles; i++) {
12+
const i0 = indices[i * 3] * stride;
13+
const i1 = indices[i * 3 + 1] * stride;
14+
const i2 = indices[i * 3 + 2] * stride;
15+
16+
const z_avg = (projected[i0 + 2] + projected[i1 + 2] + projected[i2 + 2]) / 3;
17+
18+
raster[i] = z_avg;
19+
mesh.draw_order[visible_count] = i;
20+
visible_count++;
21+
}
22+
23+
const active_order = mesh.draw_order.subarray(0, visible_count);
24+
active_order.sort((a, b) => raster[b] - raster[a]);
25+
26+
return visible_count;
27+
}

src/rendering/mesh.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ export class Mesh{
66
indices: IndexingType
77
projected_buffer: ArrayType
88
raster_buffer: ArrayType
9+
draw_order:IndexingType
910
raster_end:number
11+
visible_triangles_count:number
1012

1113
constructor(vertices:ArrayType, indices:IndexingType){
1214
this.vertices = vertices;
1315
this.indices = indices;
1416
this.projected_buffer = new ArrayType(vertices.length * 4 / 3);
1517
this.raster_buffer = new ArrayType(indices.length * 4);
18+
this.draw_order = new IndexingType(indices.length);
1619
this.raster_end = indices.length * 4;
20+
this.visible_triangles_count = 0;
1721
}
1822

1923
}
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1-
import { ArrayType } from "../math/types";
1+
import { depth_sort } from "./depth_sorting";
22
import type { Mesh } from "./mesh";
33

44
/**
55
*
66
* @param mesh Mutated
77
* @param stride
88
*/
9-
export function assemble_primitives(mesh:Mesh, stride:number = 4):void{
10-
const vertices = mesh.projected_buffer;
9+
export function assemble_primitives(mesh:Mesh):void{
10+
const visible_count = depth_sort(mesh);
11+
const projected = mesh.projected_buffer;
1112
const indices = mesh.indices;
12-
const required_space = indices.length * stride;
13-
if (mesh.raster_buffer.length < required_space){
14-
console.warn("Resizing the raster buffer's out - this generally shouldn't happen");
15-
mesh.raster_buffer = new ArrayType(required_space);
16-
}
13+
const order = mesh.draw_order;
14+
1715
const out = mesh.raster_buffer
1816
let out_index = 0;
19-
for(let i = 0; i < indices.length; ++i){
20-
const index = indices[i];
21-
const start = index * stride;
17+
for(let i = 0; i < visible_count; ++i){
18+
const index = order[i];
19+
20+
const v1 = indices[index * 3] * 4;
21+
const v2 = indices[index * 3 + 1] * 4;
22+
const v3 = indices[index * 3 + 2] * 4;
2223

23-
for(let k = 0; k < stride; ++k){
24-
out[out_index++] = vertices[start + k];
25-
}
24+
out[out_index++] = projected[v1];
25+
out[out_index++] = projected[v1 + 1];
26+
out[out_index++] = projected[v1 + 2];
27+
out[out_index++] = projected[v1 + 3];
28+
29+
out[out_index++] = projected[v2];
30+
out[out_index++] = projected[v2 + 1];
31+
out[out_index++] = projected[v2 + 2];
32+
out[out_index++] = projected[v2 + 3];
33+
34+
out[out_index++] = projected[v3];
35+
out[out_index++] = projected[v3 + 1];
36+
out[out_index++] = projected[v3 + 2];
37+
out[out_index++] = projected[v3 + 3];
2638
}
39+
mesh.visible_triangles_count = visible_count;
2740
}

src/rendering/rasterizer.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { vec3, vec4 } from "../math/types";
22
import type { Mesh } from "./mesh";
33
import { process_perspective_mutate} from "./vertex";
44

5-
65
/**
76
*
87
* @param mesh (Mutated)
@@ -26,7 +25,7 @@ export function rasterize(mesh:Mesh, invert_y:boolean = true, stride:number = 4)
2625
let n2 = vec3(0,0,0);
2726
let n3 = vec3(0,0,0);
2827

29-
for(let i = 0; i < num_triangles; ++i){
28+
for(let i = 0; i < mesh.visible_triangles_count; ++i){
3029
const base = 3 * i * stride;
3130
c1[0] = vertices[base + 0]; c1[1] = vertices[base + 1]; c1[2] = vertices[base + 2]; c1[3] = vertices[base + 3];
3231
c2[0] = vertices[base + stride + 0]; c2[1] = vertices[base + stride + 1]; c2[2] = vertices[base + stride + 2]; c2[3] = vertices[base + stride + 3];
@@ -36,18 +35,34 @@ export function rasterize(mesh:Mesh, invert_y:boolean = true, stride:number = 4)
3635
const v2 = process_perspective_mutate(c2, n2);
3736
const v3 = process_perspective_mutate(c3, n3);
3837

38+
const x1 = n1[0];
39+
const y1 = n1[1] * sign;
40+
41+
const x2 = n2[0];
42+
const y2 = n2[1] * sign;
43+
44+
const x3 = n3[0];
45+
const y3 = n3[1] * sign;
46+
47+
const area = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
48+
49+
if (area <= 0) {
50+
continue;
51+
}
52+
3953
if(!v1 || !v2 || !v3){
4054
continue;
4155
}
4256

43-
out[out_index++] = n1[0];
44-
out[out_index++] = sign * n1[1];
57+
out[out_index++] = x1;
58+
out[out_index++] = y1;
4559

46-
out[out_index++] = n2[0];
47-
out[out_index++] = sign * n2[1];
60+
out[out_index++] = x2;
61+
out[out_index++] = y2;
4862

49-
out[out_index++] = n3[0];
50-
out[out_index++] = sign * n3[1];
63+
out[out_index++] = x3;
64+
out[out_index++] = y3;
5165
}
5266
mesh.raster_end = out_index;
53-
}
67+
}
68+

src/rendering/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { StringBuffer } from "../utils/string_buffer";
1010
export function render(mesh: Mesh, model:mat4, view:mat4, projection:mat4, invert_y:boolean = true, stride:number = 4):void {
1111
const mvp = mul_mat4(mul_mat4(projection,view),model);
1212
transform_vertices(mesh,mvp);
13-
assemble_primitives(mesh,stride);
13+
assemble_primitives(mesh);
1414
rasterize(mesh, invert_y, stride);
1515
}
1616

0 commit comments

Comments
 (0)