Search in sources :

Example 56 with Vector4f

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

the class GLContext method drawPoint.

private static void drawPoint(Vertex p, Vector4f[] color, float[] depth, GLContext gl) {
    int o = (int) (Math.floor(p.coord.x) + Math.floor(p.coord.y) * gl.w);
    if (gl.depthEnabled) {
        float d = p.coord.z;
        if (d > depth[o])
            return;
        else
            depth[o] = d;
    }
    // Vertex color
    Vector4f fragColor = new Vector4f();
    fragColor.x = p.color.x;
    fragColor.y = p.color.y;
    fragColor.z = p.color.z;
    fragColor.w = p.color.w;
    // Texture sample
    Texture tex = gl.textures.get(gl.curTexture);
    if (gl.textureEnabled && tex != null) {
        float u = p.texCoord.x;
        float v = p.texCoord.y;
        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].x = fragColor.x;
    color[o].y = fragColor.y;
    color[o].z = fragColor.z;
    color[o].w = fragColor.w;
}
Also used : Vector4f(org.joml.Vector4f)

Example 57 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 58 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 59 with Vector4f

use of org.joml.Vector4f in project Terasology by MovingBlocks.

the class VectorTypeSerializerTest method testSerializationConstant.

@Test
void testSerializationConstant() {
    TestObject2 a = new TestObject2();
    a.v1 = new Vector3f(1.0f, 2.0f, 3.0f);
    a.v2 = new Vector4f(1.0f, 2.0f, 3.0f, 5.0f);
    a.v3 = new Vector2f(1.0f, 2.0f);
    byte[] data = gsonSerializer.serialize(a, new TypeInfo<TestObject2>() {
    }).get();
    TestObject2 o = gsonSerializer.deserialize(new TypeInfo<TestObject2>() {
    }, data).get();
    assertEquals(new Vector3f(1.0f, 2.0f, 3.0f), o.v1, .00001f);
    assertEquals(new Vector4f(1.0f, 2.0f, 3.0f, 5.0f), o.v2, .00001f);
    assertEquals(new Vector2f(1.0f, 2.0f), o.v3, .00001f);
}
Also used : Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test) ModuleEnvironmentTest(org.terasology.engine.ModuleEnvironmentTest)

Example 60 with Vector4f

use of org.joml.Vector4f in project Terasology by MovingBlocks.

the class VectorEventSerializer method testEventSerializationConstant.

@Test
public void testEventSerializationConstant() throws IOException {
    Vector3fTestEvent a = new Vector3fTestEvent();
    a.v1 = new Vector3f(1.0f, 2.0f, 3.0f);
    a.v2 = new Vector4f(1.0f, 2.0f, 3.0f, 5.0f);
    a.v3 = new Vector2f(1.0f, 2.0f);
    a.v1c = new Vector3f(1.0f, 1.0f, 1.0f);
    a.v2c = new Vector4f(1.0f, 1.0f, 2.0f, 2.0f);
    a.v3c = new Vector2f(1.0f, 1.0f);
    EntityData.Event ev = serializer.serialize(a);
    Event dev = serializer.deserialize(ev);
    assumeTrue(dev instanceof Vector3fTestEvent);
    assertEquals(((Vector3fTestEvent) dev).v1, new Vector3f(1.0f, 2.0f, 3.0f), .00001f);
    assertEquals(((Vector3fTestEvent) dev).v2, new Vector4f(1.0f, 2.0f, 3.0f, 5.0f), .00001f);
    assertEquals(((Vector3fTestEvent) dev).v3, new Vector2f(1.0f, 2.0f), .00001f);
    assertEquals(((Vector3fTestEvent) dev).v1c, new Vector3f(1.0f, 1.0f, 1.0f), .00001f);
    assertEquals(((Vector3fTestEvent) dev).v2c, new Vector4f(1.0f, 1.0f, 2.0f, 2.0f), .00001f);
    assertEquals(((Vector3fTestEvent) dev).v3c, new Vector2f(1.0f, 1.0f), .00001f);
}
Also used : Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f) EntityData(org.terasology.protobuf.EntityData) Event(org.terasology.engine.entitySystem.event.Event) Test(org.junit.jupiter.api.Test)

Aggregations

Vector4f (org.joml.Vector4f)68 Vector3f (org.joml.Vector3f)13 Font (io.xol.chunkstories.api.rendering.text.FontRenderer.Font)12 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 Vector2f (org.joml.Vector2f)5 Mouse (io.xol.chunkstories.api.input.Mouse)4 Texture2DGL (io.xol.chunkstories.renderer.opengl.texture.Texture2DGL)4 ByteBuffer (java.nio.ByteBuffer)4 Matrix4f (org.joml.Matrix4f)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