Search in sources :

Example 51 with Vector4f

use of org.joml.Vector4f in project IDEProgram by Itay2805.

the class GLContext method drawTriangle.

public static void drawTriangle(Vertex[] p, Vector4f[] color, float[] depth, GLContext gl) {
    if (gl.cullingEnabled && !p[0].cull)
        return;
    int x1 = (int) Math.floor(p[0].coord.x);
    int x2 = (int) Math.floor(p[1].coord.x);
    int x3 = (int) Math.floor(p[2].coord.x);
    int y1 = (int) Math.floor(p[0].coord.y);
    int y2 = (int) Math.floor(p[1].coord.y);
    int y3 = (int) Math.floor(p[2].coord.y);
    int minX = (int) min(x1, x2, x3);
    int minY = (int) min(y1, y2, y3);
    int maxX = (int) max(x1, x2, x3);
    int maxY = (int) max(y1, y2, y3);
    float factor = 1.0f / ((y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3));
    int o = 0;
    for (int x = minX; x <= maxX; x++) {
        for (int y = minY; y <= maxY; y++) {
            float ic0 = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) * factor;
            if (ic0 < 0 || ic0 > 1)
                continue;
            float ic1 = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) * factor;
            if (ic1 < 0 || ic1 > 1)
                continue;
            float ic2 = 1.0f - ic0 - ic1;
            if (ic2 < 0 || ic2 > 1)
                continue;
            o = (x + y * gl.w);
            if (o >= color.length || o < 0) {
                continue;
            }
            float z = 1 / (ic0 * 1 / p[0].coord.z + ic1 * 1 / p[1].coord.z + ic2 * 1 / p[2].coord.z);
            if (gl.depthEnabled) {
                if (z > depth[o])
                    continue;
                else
                    depth[o] = z;
            }
            // Vertex color
            Vector4f fragColor = new Vector4f();
            fragColor.x = (ic0 * p[0].color.x / p[0].coord.z + ic1 * p[1].color.x / p[1].coord.z + ic2 * p[2].color.x / p[2].coord.z) * z;
            fragColor.y = (ic0 * p[0].color.y / p[0].coord.z + ic1 * p[1].color.y / p[1].coord.z + ic2 * p[2].color.y / p[2].coord.z) * z;
            fragColor.z = (ic0 * p[0].color.z / p[0].coord.z + ic1 * p[1].color.z / p[1].coord.z + ic2 * p[2].color.z / p[2].coord.z) * z;
            fragColor.w = (ic0 * p[0].color.w / p[0].coord.z + ic1 * p[1].color.w / p[1].coord.z + ic2 * p[2].color.w / p[2].coord.z) * z;
            // Texture sample
            if (gl.textureEnabled && gl.curTexture < gl.textures.size()) {
                Texture tex = gl.textures.get(gl.curTexture);
                float u = (ic0 * p[0].texCoord.x / p[0].coord.z + ic1 * p[1].texCoord.x / p[1].coord.z + ic2 * p[2].texCoord.x / p[2].coord.z) * z;
                float v = (ic0 * p[0].texCoord.y / p[0].coord.z + ic1 * p[1].texCoord.y / p[1].coord.z + ic2 * p[2].texCoord.y / p[2].coord.z) * z;
                // This behaviour should later depend on GL_TEXTURE_WRAP_S
                u = (float) (Math.floor(u * tex.w) % tex.w);
                v = (float) (Math.floor(v * tex.h) % tex.h);
                int to = (int) (u + v * tex.w);
                fragColor.x *= tex.pixels[to].x;
                fragColor.y *= tex.pixels[to].y;
                fragColor.z *= tex.pixels[to].z;
                fragColor.w *= tex.pixels[to].w;
            }
            color[o].set(fragColor);
        }
    }
}
Also used : Vector4f(org.joml.Vector4f)

Example 52 with Vector4f

use of org.joml.Vector4f in project chunkstories-core by Hugobros3.

the class HitBoxImpl method lineIntersection.

/**
 * Tricky maths; transforms the inbound ray so the hitbox would be at 0.0.0 and axis-aligned
 */
public Vector3dc lineIntersection(Vector3dc lineStart, Vector3dc lineDirection) {
    Matrix4f fromAABBToWorld = new Matrix4f(entity.getAnimatedSkeleton().getBoneHierarchyTransformationMatrix(skeletonPart, System.currentTimeMillis() % 1000000));
    Matrix4f worldPositionTransformation = new Matrix4f();
    Location entityLoc = entity.getLocation();
    Vector3f pos = new Vector3f((float) entityLoc.x, (float) entityLoc.y, (float) entityLoc.z);
    worldPositionTransformation.translate(pos);
    // Creates from AABB space to worldspace
    worldPositionTransformation.mul(fromAABBToWorld, fromAABBToWorld);
    // Invert it.
    Matrix4f fromWorldToAABB = new Matrix4f();
    fromAABBToWorld.invert(fromWorldToAABB);
    // Transform line start into AABB space
    Vector4f lineStart4 = new Vector4f((float) lineStart.x(), (float) lineStart.y(), (float) lineStart.z(), 1.0f);
    Vector4f lineDirection4 = new Vector4f((float) lineDirection.x(), (float) lineDirection.y(), (float) lineDirection.z(), 0.0f);
    fromWorldToAABB.transform(lineStart4);
    fromWorldToAABB.transform(lineDirection4);
    Vector3d lineStartTransformed = new Vector3d(lineStart4.x(), lineStart4.y(), lineStart4.z());
    Vector3d lineDirectionTransformed = new Vector3d(lineDirection4.x(), lineDirection4.y(), lineDirection4.z());
    // Actual computation
    Vector3dc hitPoint = box.lineIntersection(lineStartTransformed, lineDirectionTransformed);
    if (hitPoint == null)
        return null;
    // Transform hitPoint back into world
    Vector4f hitPoint4 = new Vector4f((float) hitPoint.x(), (float) hitPoint.y(), (float) hitPoint.z(), 1.0f);
    fromAABBToWorld.transform(hitPoint4);
    return new Vector3d((double) (float) hitPoint4.x(), (double) (float) hitPoint4.y(), (double) (float) hitPoint4.z());
}
Also used : Vector3dc(org.joml.Vector3dc) Matrix4f(org.joml.Matrix4f) Vector4f(org.joml.Vector4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) Location(io.xol.chunkstories.api.Location)

Example 53 with Vector4f

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

the class SpaceGame method drawShots.

private void drawShots() {
    shotsVertices.clear();
    int num = 0;
    for (int i = 0; i < projectilePositions.length; i++) {
        Vector3d projectilePosition = projectilePositions[i];
        Vector4f projectileVelocity = projectileVelocities[i];
        if (projectileVelocity.w > 0.0f) {
            float x = (float) (projectilePosition.x - cam.position.x);
            float y = (float) (projectilePosition.y - cam.position.y);
            float z = (float) (projectilePosition.z - cam.position.z);
            if (frustumIntersection.testPoint(x, y, z)) {
                float w = projectileVelocity.w;
                viewMatrix.transformPosition(tmp2.set(x, y, z));
                shotsVertices.put(tmp2.x - shotSize).put(tmp2.y - shotSize).put(tmp2.z).put(w).put(-1).put(-1);
                shotsVertices.put(tmp2.x + shotSize).put(tmp2.y - shotSize).put(tmp2.z).put(w).put(1).put(-1);
                shotsVertices.put(tmp2.x + shotSize).put(tmp2.y + shotSize).put(tmp2.z).put(w).put(1).put(1);
                shotsVertices.put(tmp2.x + shotSize).put(tmp2.y + shotSize).put(tmp2.z).put(w).put(1).put(1);
                shotsVertices.put(tmp2.x - shotSize).put(tmp2.y + shotSize).put(tmp2.z).put(w).put(-1).put(1);
                shotsVertices.put(tmp2.x - shotSize).put(tmp2.y - shotSize).put(tmp2.z).put(w).put(-1).put(-1);
                num++;
            }
        }
    }
    shotsVertices.flip();
    if (num > 0) {
        glUseProgram(shotProgram);
        glDepthMask(false);
        glEnable(GL_BLEND);
        glVertexPointer(4, GL_FLOAT, 6 * 4, shotsVertices);
        shotsVertices.position(4);
        glTexCoordPointer(2, GL_FLOAT, 6 * 4, shotsVertices);
        shotsVertices.position(0);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glDrawArrays(GL_TRIANGLES, 0, num * 6);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisable(GL_BLEND);
        glDepthMask(true);
    }
}
Also used : Vector4f(org.joml.Vector4f) Vector3d(org.joml.Vector3d) STBEasyFont.stb_easy_font_print(org.lwjgl.stb.STBEasyFont.stb_easy_font_print)

Example 54 with Vector4f

use of org.joml.Vector4f 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 Vector4f

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

the class SpaceGame method shoot.

private void shoot() {
    boolean firstShot = false;
    for (int i = 0; i < projectilePositions.length; i++) {
        Vector3d projectilePosition = projectilePositions[i];
        Vector4f projectileVelocity = projectileVelocities[i];
        invViewProjMatrix.transformProject(tmp2.set(mouseX, -mouseY, 1.0f)).normalize();
        if (projectileVelocity.w <= 0.0f) {
            projectileVelocity.x = cam.linearVel.x + tmp2.x * shotVelocity;
            projectileVelocity.y = cam.linearVel.y + tmp2.y * shotVelocity;
            projectileVelocity.z = cam.linearVel.z + tmp2.z * shotVelocity;
            projectileVelocity.w = 0.01f;
            if (!firstShot) {
                projectilePosition.set(cam.right(tmp3)).mul(shotSeparation).add(cam.position);
                firstShot = true;
            } else {
                projectilePosition.set(cam.right(tmp3)).mul(-shotSeparation).add(cam.position);
                break;
            }
        }
    }
}
Also used : Vector4f(org.joml.Vector4f) Vector3d(org.joml.Vector3d) STBEasyFont.stb_easy_font_print(org.lwjgl.stb.STBEasyFont.stb_easy_font_print)

Aggregations

Vector4f (org.joml.Vector4f)55 Font (io.xol.chunkstories.api.rendering.text.FontRenderer.Font)12 Vector3f (org.joml.Vector3f)7 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)6 Vector3d (org.joml.Vector3d)6 Shader (io.xol.chunkstories.api.rendering.shader.Shader)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5 Mouse (io.xol.chunkstories.api.input.Mouse)4 Texture2DGL (io.xol.chunkstories.renderer.opengl.texture.Texture2DGL)4 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)3 STBEasyFont.stb_easy_font_print (org.lwjgl.stb.STBEasyFont.stb_easy_font_print)3 Location (io.xol.chunkstories.api.Location)2 Entity (io.xol.chunkstories.api.entity.Entity)2 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)2 GuiRenderer (io.xol.chunkstories.api.gui.GuiRenderer)2 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)2 Voxel (io.xol.chunkstories.api.voxel.Voxel)2 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)2 ByteBuffer (java.nio.ByteBuffer)2 Matrix4f (org.joml.Matrix4f)2