-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtessplane.js
More file actions
67 lines (52 loc) · 1.79 KB
/
tessplane.js
File metadata and controls
67 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { GL } from './gl.js'
import { StaticGeometry } from './staticgeometry.js'
export class TessPlane {
#createBuffers(res) {
if (res < 1) { res = 1 }
const n_strips = Math.pow(2, res - 1)
const edge_length = Math.sqrt(2.0) / Math.pow(3.0, 1.0 / 4.0)
const line_edge = edge_length / n_strips
const height = Math.pow(3.0, 1.0 / 4.0) / Math.sqrt(2.0)
const vertexBuffer = []
for (let line = 0; line <= n_strips; ++line) {
const y = 0.5 * height - (line / n_strips) * height
const n_vertices = line + 1
for (let i = 0; i < n_vertices; ++i) {
const x = n_vertices === 1 ? 0.0 : i / (n_vertices - 1) * line_edge * (n_vertices - 1) - 0.5 * line_edge * (n_vertices - 1)
vertexBuffer.push(x, y)
}
}
let ie0 = 0
let ie1 = 1
const indexBuffer = []
for (let line = 0; line < n_strips; ++line) {
const n_triangles = line * 2 + 1
for (let triangle = 0; triangle < n_triangles; ++triangle) {
if (triangle & 1) {
indexBuffer.push(ie1, ie0, ie0 - 1)
}
else {
indexBuffer.push(ie0, ie1, ie1 + 1)
++ie0
++ie1
}
}
++ie1
}
return [vertexBuffer, indexBuffer]
}
draw() {
GL.gl.bindVertexArray(this.vao)
GL.gl.drawElements(GL.gl.TRIANGLES, this.count, GL.gl.UNSIGNED_SHORT, this.elementOffset)
}
constructor(res) {
const t = this.#createBuffers(res)
this.count = t[1].length
this.vao = GL.gl.createVertexArray()
GL.gl.bindVertexArray(this.vao)
const vertexOffet = StaticGeometry.addVertices(new Float32Array(t[0]))
this.elementOffset = StaticGeometry.addElements(new Uint16Array(t[1]))
GL.gl.vertexAttribPointer(0, 2, GL.gl.FLOAT, false, 8, vertexOffet)
GL.gl.enableVertexAttribArray(0)
}
}