Search in sources :

Example 1 with Triangle

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);
}
Also used : Node(org.lwjgl.demo.opengl.util.KDTree.Node) ArrayList(java.util.ArrayList) Triangle(org.lwjgl.demo.opengl.util.KDTree.Triangle) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Triangle

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);
}
Also used : KDTree(org.lwjgl.demo.opengl.util.KDTree) Vector3f(org.joml.Vector3f) Triangle(org.lwjgl.demo.opengl.util.KDTree.Triangle) ArrayList(java.util.ArrayList) DynamicByteBuffer(org.lwjgl.demo.opengl.util.DynamicByteBuffer) Box(org.lwjgl.demo.opengl.util.KDTree.Box)

Aggregations

ArrayList (java.util.ArrayList)2 Triangle (org.lwjgl.demo.opengl.util.KDTree.Triangle)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Vector3f (org.joml.Vector3f)1 DynamicByteBuffer (org.lwjgl.demo.opengl.util.DynamicByteBuffer)1 KDTree (org.lwjgl.demo.opengl.util.KDTree)1 Box (org.lwjgl.demo.opengl.util.KDTree.Box)1 Node (org.lwjgl.demo.opengl.util.KDTree.Node)1