/
PointDrawer.kt
108 lines (90 loc) · 3.37 KB
/
PointDrawer.kt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.openrndr.internal
import org.openrndr.draw.*
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.platform.Platform
import org.openrndr.platform.PlatformType
import kotlin.jvm.JvmName
class PointDrawer {
val vertices: VertexBuffer = VertexBuffer.createDynamic(VertexFormat().apply {
position(3)
normal(3)
}, 1)
internal var batch = PointBatch.create(10_000, session = Session.root)
private var count = 0
private val singleBatches = (0 until DrawerConfiguration.vertexBufferMultiBufferCount).map { PointBatch.create(1) }
private val shaderManager: ShadeStyleManager = ShadeStyleManager.fromGenerators("point",
vsGenerator = Driver.instance.shaderGenerators::pointVertexShader,
fsGenerator = Driver.instance.shaderGenerators::pointFragmentShader)
internal fun ensureBatchSize(size: Int) {
if (batch.size < size) {
batch.destroy()
batch = PointBatch.create(size, session = Session.root)
}
}
init {
val w = vertices.shadow.writer()
w.rewind()
val x = 0.0
val y = 0.0
val pa = Vector3(x, y, 0.0)
val n = Vector3(0.0, 0.0, -1.0)
w.apply {
write(pa)
write(n)
}
vertices.shadow.upload()
}
@JvmName("drawPoints2D")
fun drawPoints(drawContext: DrawContext, drawStyle: DrawStyle, positions: List<Vector2>) {
ensureBatchSize(positions.size)
batch.geometry.put {
for (i in positions.indices) {
write(Vector3(positions[i].x, positions[i].y, 0.0))
}
}
batch.drawStyle.put {
for (i in positions.indices) {
write(drawStyle)
}
}
drawPoints(drawContext, drawStyle, batch, positions.size)
}
@JvmName("drawPoints3D")
fun drawPoints(drawContext: DrawContext, drawStyle: DrawStyle, positions: List<Vector3>) {
ensureBatchSize(positions.size)
batch.geometry.put {
for (i in positions.indices) {
write(positions[i])
}
}
batch.drawStyle.put {
for (i in positions.indices) {
write(drawStyle)
}
}
drawPoints(drawContext, drawStyle, batch, positions.size)
}
fun drawPoint(drawContext: DrawContext,
drawStyle: DrawStyle, x: Double, y: Double, z: Double) {
ensureBatchSize(1)
val batch = singleBatches[count.mod(singleBatches.size)]
batch.geometry.put {
write(Vector3(x, y, z))
}
batch.drawStyle.put {
write(drawStyle)
}
drawPoints(drawContext, drawStyle, batch, 1)
}
fun drawPoints(drawContext: DrawContext, drawStyle: DrawStyle, batch: PointBatch, count: Int) {
val shader = shaderManager.shader(drawStyle.shadeStyle, listOf(vertices.vertexFormat), listOf(batch.geometry.vertexFormat, batch.drawStyle.vertexFormat))
shader.begin()
drawContext.applyToShader(shader)
drawStyle.applyToShader(shader)
Driver.instance.setState(drawStyle)
Driver.instance.drawInstances(shader, listOf(vertices), listOf(batch.drawStyle, batch.geometry) + (drawStyle.shadeStyle?.attributes
?: emptyList()), DrawPrimitive.POINTS, 0, 1, 0, count)
shader.end()
}
}