use of org.lwjgl.demo.opengl.util.KDTree.Triangle 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.Triangle in project lwjgl3-demos by LWJGL.
the class DemoSsboTrianglesStacklessKdTree method createSceneSSBO.
/**
* Build the kd-tree of the scene and create two SSBOs:
* <ul>
* <li>one for the nodes of the kd-tree
* <li>and another one to hold all the triangles stored in the leaf nodes of the kd-tree
* </ul>
*/
void createSceneSSBO() {
/* Build Kd-tree */
KDTree kdtree = new KDTree();
List<Triangle> triangles = new ArrayList<Triangle>();
int trianglesCount = mesh.positions.remaining() / 3 / 3;
sceneBounds = new Box();
Vector3f min = new Vector3f(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
Vector3f max = new Vector3f(-Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE);
sceneBounds.min = min;
sceneBounds.max = max;
for (int i = 0; i < trianglesCount; i++) {
Triangle t = new Triangle();
t.v0 = new Vector3f(mesh.positions.get(i * 3 * 3 + 0), mesh.positions.get(i * 3 * 3 + 1), mesh.positions.get(i * 3 * 3 + 2));
t.v1 = new Vector3f(mesh.positions.get(i * 3 * 3 + 3), mesh.positions.get(i * 3 * 3 + 4), mesh.positions.get(i * 3 * 3 + 5));
t.v2 = new Vector3f(mesh.positions.get(i * 3 * 3 + 6), mesh.positions.get(i * 3 * 3 + 7), mesh.positions.get(i * 3 * 3 + 8));
triangles.add(t);
min.min(t.v0).min(t.v1).min(t.v2);
max.max(t.v0).max(t.v1).max(t.v2);
}
kdtree.buildTree(triangles, sceneBounds);
DynamicByteBuffer nodesBuffer = new DynamicByteBuffer();
DynamicByteBuffer trianglesBuffer = new DynamicByteBuffer();
kdTreeToBuffers(kdtree, nodesBuffer, trianglesBuffer);
nodesBuffer.flip();
trianglesBuffer.flip();
this.nodesSsbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, nodesSsbo);
glBufferData(GL_ARRAY_BUFFER, nodesBuffer.bb, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
this.trianglesSsbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, trianglesSsbo);
glBufferData(GL_ARRAY_BUFFER, trianglesBuffer.bb, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Aggregations