use of org.lwjgl.demo.opengl.util.KDTree.Node in project lwjgl3-demos by LWJGL.
the class DemoSsboTrianglesStacklessKdTree method kdTreeToBuffers.
static void kdTreeToBuffers(KDTree tree, DynamicByteBuffer nodesBuffer, DynamicByteBuffer trianglesBuffer) {
Map<Node, Integer> indexes = new LinkedHashMap<Node, Integer>();
// Allocate indexes for each of the nodes
allocate(tree.mRootNode, indexes);
int triangleIndex = 0;
List<GPUNode> gpuNodes = new ArrayList<GPUNode>();
// Iterate over each node in insertion order and write to the buffers
for (Map.Entry<Node, Integer> e : indexes.entrySet()) {
Node n = e.getKey();
GPUNode gn = new GPUNode();
gn.min = n.boundingBox.min;
gn.max = n.boundingBox.max;
gn.dim = n.splitAxis.dim;
gn.plane = n.splitPlane;
gn.ropes = new int[6];
/* Write ropes */
for (int i = 0; i < 6; i++) {
Node r = n.ropes[i];
if (r != null) {
gn.ropes[i] = indexes.get(r).intValue();
} else {
// no neighbor
gn.ropes[i] = -1;
}
}
if (n.isLeafNode()) {
// no left child
gn.left = -1;
// no right child
gn.right = -1;
gn.firstTri = triangleIndex;
gn.numTris = n.triangles.size();
triangleIndex += n.triangles.size();
/* Write triangles to buffer */
for (int i = 0; i < n.triangles.size(); i++) {
Triangle t = n.triangles.get(i);
trianglesBuffer.putFloat(t.v0.x).putFloat(t.v0.y).putFloat(t.v0.z).putFloat(1.0f);
trianglesBuffer.putFloat(t.v1.x).putFloat(t.v1.y).putFloat(t.v1.z).putFloat(1.0f);
trianglesBuffer.putFloat(t.v2.x).putFloat(t.v2.y).putFloat(t.v2.z).putFloat(1.0f);
}
} else {
gn.left = indexes.get(n.left).intValue();
gn.right = indexes.get(n.right).intValue();
// no triangles
gn.firstTri = 0;
// no triangles
gn.numTris = 0;
}
gpuNodes.add(gn);
}
// Write GPUNode list to ByteBuffer in std430 layout
Std430Writer.write(gpuNodes, GPUNode.class, nodesBuffer);
}
use of org.lwjgl.demo.opengl.util.KDTree.Node in project lwjgl3-demos by LWJGL.
the class DemoSsboTrianglesStacklessKdTree method allocate.
static void allocate(Node node, Map<Node, Integer> indexes) {
Queue<Node> nodes = new LinkedList<Node>();
nodes.add(node);
while (!nodes.isEmpty()) {
Node n = nodes.poll();
if (n == null)
continue;
int index = indexes.size();
indexes.put(n, Integer.valueOf(index));
nodes.add(n.left);
nodes.add(n.right);
}
}
Aggregations