Search in sources :

Example 51 with Vector3f

use of org.joml.Vector3f in project lwjgl3-demos by LWJGL.

the class Tutorial6 method bhvToBuffers.

/**
 * Build the memory of the Shader Storage Buffer Objects for the nodes and
 * triangles list.
 * <p>
 * We will use {@link Std430Writer} to write the memory in std430 layout
 * expected by the shader. For this, we simply fill {@link GPUNode} instances.
 *
 * @param root
 *            the BVH root node
 * @param nodesBuffer
 *            a dynamic/growable ByteBuffer holding the BVH nodes
 * @param trianglesBuffer
 *            a dynamic/growable ByteBuffer holding the triangles
 */
private static void bhvToBuffers(BVH root, DynamicByteBuffer nodesBuffer, DynamicByteBuffer trianglesBuffer) {
    Map<BVH, Integer> indexes = new LinkedHashMap<BVH, Integer>();
    // Allocate indexes for each of the nodes
    allocate(root, 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<BVH, Integer> e : indexes.entrySet()) {
        BVH n = e.getKey();
        GPUNode gn = new GPUNode();
        gn.min = new Vector3f(n.minX, n.minY, n.minZ);
        gn.max = new Vector3f(n.maxX, n.maxY, n.maxZ);
        if (n.hitNext != null)
            gn.hitNext = indexes.get(n.hitNext).intValue();
        else
            gn.hitNext = -1;
        if (n.missNext != null)
            gn.missNext = indexes.get(n.missNext).intValue();
        else
            gn.missNext = -1;
        if (n.triangles != null) {
            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 = (Triangle) n.triangles.get(i);
                trianglesBuffer.putFloat(t.v0x).putFloat(t.v0y).putFloat(t.v0z).putFloat(1.0f);
                trianglesBuffer.putFloat(t.v1x).putFloat(t.v1y).putFloat(t.v1z).putFloat(1.0f);
                trianglesBuffer.putFloat(t.v2x).putFloat(t.v2y).putFloat(t.v2z).putFloat(1.0f);
                trianglesBuffer.putFloat(t.n0x).putFloat(t.n0y).putFloat(t.n0z).putFloat(0.0f);
                trianglesBuffer.putFloat(t.n1x).putFloat(t.n1y).putFloat(t.n1z).putFloat(0.0f);
                trianglesBuffer.putFloat(t.n2x).putFloat(t.n2y).putFloat(t.n2z).putFloat(0.0f);
            }
        } else {
            // 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 : Vector3f(org.joml.Vector3f)

Example 52 with Vector3f

use of org.joml.Vector3f 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 53 with Vector3f

use of org.joml.Vector3f in project lwjgl3-demos by LWJGL.

the class SpaceGame method drawHudShotDirection.

private void drawHudShotDirection() {
    glUseProgram(0);
    Ship enemyShip = ships[shootingShip];
    if (enemyShip == null)
        return;
    Vector3d targetOrigin = tmp;
    targetOrigin.set(enemyShip.x, enemyShip.y, enemyShip.z);
    Vector3f interceptorDir = intercept(cam.position, shotVelocity, targetOrigin, tmp3.set(cam.linearVel).negate(), tmp2);
    viewMatrix.transformDirection(interceptorDir);
    if (interceptorDir.z > 0.0)
        return;
    projMatrix.transformProject(interceptorDir);
    float crosshairSize = 0.01f;
    float xs = crosshairSize * height / width;
    float ys = crosshairSize;
    crosshairVertices.clear();
    crosshairVertices.put(interceptorDir.x - xs).put(interceptorDir.y - ys);
    crosshairVertices.put(interceptorDir.x + xs).put(interceptorDir.y - ys);
    crosshairVertices.put(interceptorDir.x + xs).put(interceptorDir.y + ys);
    crosshairVertices.put(interceptorDir.x - xs).put(interceptorDir.y + ys);
    crosshairVertices.flip();
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glVertexPointer(2, GL_FLOAT, 0, crosshairVertices);
    glDrawArrays(GL_QUADS, 0, 4);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
Also used : Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f)

Example 54 with Vector3f

use of org.joml.Vector3f in project lwjgl3-demos by LWJGL.

the class SpaceGame method shootFromShip.

private void shootFromShip(long thisTime, int index) {
    Ship ship = ships[index];
    if (ship == null)
        return;
    if (thisTime - ship.lastShotTime < 1E6 * shotOpponentMilliseconds) {
        return;
    }
    ship.lastShotTime = thisTime;
    Vector3d shotPos = tmp.set(ship.x, ship.y, ship.z).sub(cam.position).negate().normalize().mul(1.01f * shipRadius).add(ship.x, ship.y, ship.z);
    Vector3f icept = intercept(shotPos, shotVelocity, cam.position, cam.linearVel, tmp2);
    if (icept == null)
        return;
    // jitter the direction a bit
    GeometryUtils.perpendicular(icept, tmp3, tmp4);
    icept.fma(((float) Math.random() * 2.0f - 1.0f) * 0.01f, tmp3);
    icept.fma(((float) Math.random() * 2.0f - 1.0f) * 0.01f, tmp4);
    icept.normalize();
    for (int i = 0; i < projectilePositions.length; i++) {
        Vector3d projectilePosition = projectilePositions[i];
        Vector4f projectileVelocity = projectileVelocities[i];
        if (projectileVelocity.w <= 0.0f) {
            projectilePosition.set(shotPos);
            projectileVelocity.x = tmp2.x * shotVelocity;
            projectileVelocity.y = tmp2.y * shotVelocity;
            projectileVelocity.z = tmp2.z * shotVelocity;
            projectileVelocity.w = 0.01f;
            break;
        }
    }
}
Also used : Vector4f(org.joml.Vector4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) STBEasyFont.stb_easy_font_print(org.lwjgl.stb.STBEasyFont.stb_easy_font_print)

Example 55 with Vector3f

use of org.joml.Vector3f in project lwjgl3-demos by LWJGL.

the class Demo20 method createBoxesTexture.

/**
 * Create the RGB/XYZ texture holding our boxes.
 */
private void createBoxesTexture() {
    this.boxesTexture = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, boxesTexture);
    ByteBuffer bb = BufferUtils.createByteBuffer(4 * 4 * Demo20.boxes.length);
    FloatBuffer fb = bb.asFloatBuffer();
    for (int i = 0; i < Demo20.boxes.length; i += 2) {
        Vector3f min = Demo20.boxes[i];
        Vector3f max = Demo20.boxes[i + 1];
        fb.put(min.x).put(min.y).put(min.z).put(0.0f);
        fb.put(max.x).put(max.y).put(max.z).put(0.0f);
    }
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, Demo20.boxes.length, 1, 0, GL_RGBA, GL_FLOAT, bb);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
}
Also used : Vector3f(org.joml.Vector3f) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Vector3f (org.joml.Vector3f)60 ByteBuffer (java.nio.ByteBuffer)14 FloatBuffer (java.nio.FloatBuffer)12 Matrix4f (org.joml.Matrix4f)10 Vector3d (org.joml.Vector3d)7 Vector4f (org.joml.Vector4f)7 ArrayList (java.util.ArrayList)6 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)5 Location (io.xol.chunkstories.api.Location)4 Light (io.xol.chunkstories.api.rendering.lightning.Light)4 RenderingInterface (io.xol.chunkstories.api.rendering.RenderingInterface)3 Shader (io.xol.chunkstories.api.rendering.shader.Shader)3 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)2 Voxel (io.xol.chunkstories.api.voxel.Voxel)2 CellData (io.xol.chunkstories.api.world.cell.CellData)2 DummyCell (io.xol.chunkstories.api.world.cell.DummyCell)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2