Search in sources :

Example 1 with DynamicByteBuffer

use of org.lwjgl.demo.opengl.util.DynamicByteBuffer in project lwjgl3-demos by LWJGL.

the class Tutorial7 method createSceneSSBOs.

/**
 * Convert the Assimp-imported scene into the Shader Storage Buffer Objects
 * needed for stackless kd-tree traversable in the compute shader.
 */
private void createSceneSSBOs() {
    KDTreeForTutorial7 kdtree = new KDTreeForTutorial7();
    List<KDTreeForTutorial7.Boundable> triangles = new ArrayList<KDTreeForTutorial7.Boundable>();
    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);
    for (Model.Mesh mesh : model.meshes) {
        int trianglesCount = mesh.elementCount / 3;
        for (int i = 0; i < trianglesCount; i++) {
            Triangle t = new Triangle();
            int i0 = mesh.indicesIB.get(i * 3 + 0);
            int i1 = mesh.indicesIB.get(i * 3 + 1);
            int i2 = mesh.indicesIB.get(i * 3 + 2);
            float v0x = mesh.verticesFB.get(i0 * 3 + 0);
            float v0y = mesh.verticesFB.get(i0 * 3 + 1);
            float v0z = mesh.verticesFB.get(i0 * 3 + 2);
            float v1x = mesh.verticesFB.get(i1 * 3 + 0);
            float v1y = mesh.verticesFB.get(i1 * 3 + 1);
            float v1z = mesh.verticesFB.get(i1 * 3 + 2);
            float v2x = mesh.verticesFB.get(i2 * 3 + 0);
            float v2y = mesh.verticesFB.get(i2 * 3 + 1);
            float v2z = mesh.verticesFB.get(i2 * 3 + 2);
            float n0x = mesh.normalsFB.get(i0 * 3 + 0);
            float n0y = mesh.normalsFB.get(i0 * 3 + 1);
            float n0z = mesh.normalsFB.get(i0 * 3 + 2);
            float n1x = mesh.normalsFB.get(i1 * 3 + 0);
            float n1y = mesh.normalsFB.get(i1 * 3 + 1);
            float n1z = mesh.normalsFB.get(i1 * 3 + 2);
            float n2x = mesh.normalsFB.get(i2 * 3 + 0);
            float n2y = mesh.normalsFB.get(i2 * 3 + 1);
            float n2z = mesh.normalsFB.get(i2 * 3 + 2);
            t.v0 = new Vector3f(v0x, v0y, v0z);
            t.v1 = new Vector3f(v1x, v1y, v1z);
            t.v2 = new Vector3f(v2x, v2y, v2z);
            t.n0 = new Vector3f(n0x, n0y, n0z);
            t.n1 = new Vector3f(n1x, n1y, n1z);
            t.n2 = new Vector3f(n2x, n2y, n2z);
            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, new KDTreeForTutorial7.Box(min, max));
    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 : Vector3f(org.joml.Vector3f) ArrayList(java.util.ArrayList) DynamicByteBuffer(org.lwjgl.demo.opengl.util.DynamicByteBuffer)

Example 2 with DynamicByteBuffer

use of org.lwjgl.demo.opengl.util.DynamicByteBuffer 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)

Example 3 with DynamicByteBuffer

use of org.lwjgl.demo.opengl.util.DynamicByteBuffer in project lwjgl3-demos by LWJGL.

the class HybridDemoSsboTriangles method createSceneSSBO.

/**
 * Create two SSBOs:
 * <ul>
 * <li>one to hold all our triangles of the mesh
 * <li>another to hold the objects of the mesh with their AABBs and triangle
 * indexes
 * </ul>
 */
private void createSceneSSBO() {
    this.trianglesSsbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, trianglesSsbo);
    ByteBuffer ssboData = BufferUtils.createByteBuffer(4 * (4) * mesh.numVertices);
    FloatBuffer fv = ssboData.asFloatBuffer();
    for (int i = 0; i < mesh.numVertices; i++) {
        float x = mesh.positions.get(3 * i + 0);
        float y = mesh.positions.get(3 * i + 1);
        float z = mesh.positions.get(3 * i + 2);
        fv.put(x).put(y).put(z).put(0.0f);
    /* We do not take normals into account, currently! */
    // float nx = mesh.normals.get(3 * i + 0);
    // float ny = mesh.normals.get(3 * i + 1);
    // float nz = mesh.normals.get(3 * i + 2);
    // fv.put(nx).put(ny).put(nz).put(0.0f);
    }
    glBufferData(GL_ARRAY_BUFFER, ssboData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    this.objectsSsbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, objectsSsbo);
    DynamicByteBuffer objectsBuffer = new DynamicByteBuffer();
    List<GPUObject> objects = new ArrayList<GPUObject>();
    for (MeshObject o : mesh.objects) {
        GPUObject obj = new GPUObject();
        obj.min = o.min;
        obj.max = o.max;
        obj.first = o.first;
        obj.count = o.count;
        objects.add(obj);
    }
    Std430Writer.write(objects, GPUObject.class, objectsBuffer);
    objectsBuffer.flip();
    glBufferData(GL_ARRAY_BUFFER, objectsBuffer.bb, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Also used : MeshObject(org.lwjgl.demo.opengl.util.WavefrontMeshLoader.MeshObject) DynamicByteBuffer(org.lwjgl.demo.opengl.util.DynamicByteBuffer) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) DynamicByteBuffer(org.lwjgl.demo.opengl.util.DynamicByteBuffer)

Aggregations

ArrayList (java.util.ArrayList)3 DynamicByteBuffer (org.lwjgl.demo.opengl.util.DynamicByteBuffer)3 Vector3f (org.joml.Vector3f)2 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 KDTree (org.lwjgl.demo.opengl.util.KDTree)1 Box (org.lwjgl.demo.opengl.util.KDTree.Box)1 Triangle (org.lwjgl.demo.opengl.util.KDTree.Triangle)1 MeshObject (org.lwjgl.demo.opengl.util.WavefrontMeshLoader.MeshObject)1