use of org.lwjgl.demo.opengl.util.KDTree.Box 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);
}