Java Code Examples for org.joml.Vector3f#mul()

The following examples show how to use org.joml.Vector3f#mul() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShadowCascade.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void update(Window window, Matrix4f viewMatrix, DirectionalLight light) {
    // Build projection view matrix for this cascade
    float aspectRatio = (float) window.getWidth() / (float) window.getHeight();
    projViewMatrix.setPerspective(Window.FOV, aspectRatio, zNear, zFar);
    projViewMatrix.mul(viewMatrix);

    // Calculate frustum corners in world space
    float maxZ = Float.MIN_VALUE;
    float minZ = Float.MAX_VALUE;
    for (int i = 0; i < FRUSTUM_CORNERS; i++) {
        Vector3f corner = frustumCorners[i];
        corner.set(0, 0, 0);
        projViewMatrix.frustumCorner(i, corner);
        centroid.add(corner);
        centroid.div(8.0f);
        minZ = Math.min(minZ, corner.z);
        maxZ = Math.max(maxZ, corner.z);
    }

    // Go back from the centroid up to max.z - min.z in the direction of light
    Vector3f lightDirection = light.getDirection();
    Vector3f lightPosInc = new Vector3f().set(lightDirection);
    float distance = maxZ - minZ;
    lightPosInc.mul(distance);
    Vector3f lightPosition = new Vector3f();
    lightPosition.set(centroid);
    lightPosition.add(lightPosInc);

    updateLightViewMatrix(lightDirection, lightPosition);

    updateLightProjectionMatrix();
}
 
Example 2
Source File: ShadowCascade.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void update(Window window, Matrix4f viewMatrix, DirectionalLight light) {
    // Build projection view matrix for this cascade
    float aspectRatio = (float) window.getWidth() / (float) window.getHeight();
    projViewMatrix.setPerspective(Window.FOV, aspectRatio, zNear, zFar);
    projViewMatrix.mul(viewMatrix);

    // Calculate frustum corners in world space
    float maxZ = Float.MIN_VALUE;
    float minZ = Float.MAX_VALUE;
    for (int i = 0; i < FRUSTUM_CORNERS; i++) {
        Vector3f corner = frustumCorners[i];
        corner.set(0, 0, 0);
        projViewMatrix.frustumCorner(i, corner);
        centroid.add(corner);
        centroid.div(8.0f);
        minZ = Math.min(minZ, corner.z);
        maxZ = Math.max(maxZ, corner.z);
    }

    // Go back from the centroid up to max.z - min.z in the direction of light
    Vector3f lightDirection = light.getDirection();
    Vector3f lightPosInc = new Vector3f().set(lightDirection);
    float distance = maxZ - minZ;
    lightPosInc.mul(distance);
    Vector3f lightPosition = new Vector3f();
    lightPosition.set(centroid);
    lightPosition.add(lightPosInc);

    updateLightViewMatrix(lightDirection, lightPosition);

    updateLightProjectionMatrix();
}
 
Example 3
Source File: ShadowCascade.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void update(Window window, Matrix4f viewMatrix, DirectionalLight light) {
    // Build projection view matrix for this cascade
    float aspectRatio = (float) window.getWidth() / (float) window.getHeight();
    projViewMatrix.setPerspective(Window.FOV, aspectRatio, zNear, zFar);
    projViewMatrix.mul(viewMatrix);

    // Calculate frustum corners in world space
    float maxZ = Float.MIN_VALUE;
    float minZ = Float.MAX_VALUE;
    for (int i = 0; i < FRUSTUM_CORNERS; i++) {
        Vector3f corner = frustumCorners[i];
        corner.set(0, 0, 0);
        projViewMatrix.frustumCorner(i, corner);
        centroid.add(corner);
        centroid.div(8.0f);
        minZ = Math.min(minZ, corner.z);
        maxZ = Math.max(maxZ, corner.z);
    }

    // Go back from the centroid up to max.z - min.z in the direction of light
    Vector3f lightDirection = light.getDirection();
    Vector3f lightPosInc = new Vector3f().set(lightDirection);
    float distance = maxZ - minZ;
    lightPosInc.mul(distance);
    Vector3f lightPosition = new Vector3f();
    lightPosition.set(centroid);
    lightPosition.add(lightPosInc);

    updateLightViewMatrix(lightDirection, lightPosition);

    updateLightProjectionMatrix();
}
 
Example 4
Source File: ShadowCascade.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void update(Window window, Matrix4f viewMatrix, DirectionalLight light) {
    // Build projection view matrix for this cascade
    float aspectRatio = (float) window.getWidth() / (float) window.getHeight();
    projViewMatrix.setPerspective(Window.FOV, aspectRatio, zNear, zFar);
    projViewMatrix.mul(viewMatrix);

    // Calculate frustum corners in world space
    float maxZ = Float.MIN_VALUE;
    float minZ = Float.MAX_VALUE;
    for (int i = 0; i < FRUSTUM_CORNERS; i++) {
        Vector3f corner = frustumCorners[i];
        corner.set(0, 0, 0);
        projViewMatrix.frustumCorner(i, corner);
        centroid.add(corner);
        centroid.div(8.0f);
        minZ = Math.min(minZ, corner.z);
        maxZ = Math.max(maxZ, corner.z);
    }

    // Go back from the centroid up to max.z - min.z in the direction of light
    Vector3f lightDirection = light.getDirection();
    Vector3f lightPosInc = new Vector3f().set(lightDirection);
    float distance = maxZ - minZ;
    lightPosInc.mul(distance);
    Vector3f lightPosition = new Vector3f();
    lightPosition.set(centroid);
    lightPosition.add(lightPosInc);

    updateLightViewMatrix(lightDirection, lightPosition);

    updateLightProjectionMatrix();
}
 
Example 5
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 6
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 7
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 8
Source File: DummyGame.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Window window) throws Exception {
    renderer.init(window);

    scene = new Scene();

    float reflectance = 1f;

    // Setup  GameItems
    Mesh quadMesh = OBJLoader.loadMesh("/models/plane.obj");
    Material quadMaterial = new Material(new Vector4f(0.0f, 0.0f, 1.0f, 1.0f), reflectance);
    quadMesh.setMaterial(quadMaterial);
    GameItem quadGameItem = new GameItem(quadMesh);
    quadGameItem.setPosition(0, 0, 0);
    quadGameItem.setScale(2.5f);

    scene.setGameItems(new GameItem[] { quadGameItem} );

    Vector3f particleSpeed = new Vector3f(0, 1, 0);
    particleSpeed.mul(2.5f);
    long ttl = 4000;
    int maxParticles = 200;
    long creationPeriodMillis = 300;
    float range = 0.2f;
    float scale = 1.0f;
    Mesh partMesh = OBJLoader.loadMesh("/models/particle.obj");
    Texture texture = new Texture("textures/particle.png");
    Material partMaterial = new Material(texture, reflectance);
    partMesh.setMaterial(partMaterial);
    Particle particle = new Particle(partMesh, particleSpeed, ttl);
    particle.setScale(scale);
    particleEmitter = new FlowParticleEmitter(particle, maxParticles, creationPeriodMillis);
    particleEmitter.setActive(true);
    particleEmitter.setPositionRndRange(range);
    particleEmitter.setSpeedRndRange(range);
    this.scene.setParticleEmitters(new FlowParticleEmitter[] {particleEmitter});
    
    // Setup Lights
    setupLights();

    camera.getPosition().x = 0.25f;
    camera.getPosition().y = 6.5f;
    camera.getPosition().z = 6.5f;
    camera.getRotation().x = 25;
    camera.getRotation().y = -1;

    hud = new Hud("DEMO");
}
 
Example 9
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 10
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 11
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 12
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 13
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh, Vector4f defaultColour) throws Exception {
    List<VertexInfo> vertexInfoList = new ArrayList<>();
    List<Float> textCoords = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex vertex : vertices) {
        Vector3f vertexPos = new Vector3f();
        Vector2f vertexTextCoords = vertex.getTextCoords();
        textCoords.add(vertexTextCoords.x);
        textCoords.add(vertexTextCoords.y);

        int startWeight = vertex.getStartWeight();
        int numWeights = vertex.getWeightCount();

        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertexPos.add(acumPos);
        }

        vertexInfoList.add(new VertexInfo(vertexPos));
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        VertexInfo v0 = vertexInfoList.get(tri.getVertex0());
        VertexInfo v1 = vertexInfoList.get(tri.getVertex1());
        VertexInfo v2 = vertexInfoList.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }
    
    // Once the contributions have been added, normalize the result
    for(VertexInfo v : vertexInfoList) {
        v.normal.normalize();
    }

    float[] positionsArr = VertexInfo.toPositionsArr(vertexInfoList);
    float[] textCoordsArr = Utils.listToArray(textCoords);
    float[] normalsArr = VertexInfo.toNormalArr(vertexInfoList);
    int[] indicesArr = indices.stream().mapToInt(i -> i).toArray();
    Mesh mesh = new Mesh(positionsArr, textCoordsArr, normalsArr, indicesArr);

    return mesh;
}
 
Example 14
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 15
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 16
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 17
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 18
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}
 
Example 19
Source File: DummyGame.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Window window) throws Exception {
    renderer.init(window);

    scene = new Scene();

    float reflectance = 1f;

    // Setup  GameItems 
    int maxParticles = 200;
    Mesh quadMesh = OBJLoader.loadMesh("/models/plane.obj");
    Material quadMaterial = new Material(new Vector4f(0.0f, 0.0f, 1.0f, 1.0f), reflectance);
    quadMesh.setMaterial(quadMaterial);
    GameItem quadGameItem = new GameItem(quadMesh);
    quadGameItem.setPosition(0, 0, 0);
    quadGameItem.setScale(2.5f);

    scene.setGameItems(new GameItem[]{quadGameItem});

    Vector3f particleSpeed = new Vector3f(0, 1, 0);
    particleSpeed.mul(2.5f);
    long ttl = 4000;
    long creationPeriodMillis = 300;
    float range = 0.2f;
    float scale = 1.0f;
    Mesh partMesh = OBJLoader.loadMesh("/models/particle.obj", maxParticles);
    Texture texture = new Texture("textures/particle_anim.png", 4, 4);
    Material partMaterial = new Material(texture, reflectance);
    partMesh.setMaterial(partMaterial);
    Particle particle = new Particle(partMesh, particleSpeed, ttl, 100);
    particle.setScale(scale);
    particleEmitter = new FlowParticleEmitter(particle, maxParticles, creationPeriodMillis);
    particleEmitter.setActive(true);
    particleEmitter.setPositionRndRange(range);
    particleEmitter.setSpeedRndRange(range);
    particleEmitter.setAnimRange(10);
    this.scene.setParticleEmitters(new FlowParticleEmitter[]{particleEmitter});

    // Shadows
    scene.setRenderShadows(false);

    // Setup Lights
    setupLights();

    camera.getPosition().x = 0.25f;
    camera.getPosition().y = 6.5f;
    camera.getPosition().z = 6.5f;
    camera.getRotation().x = 25;
    camera.getRotation().y = -1;

    hud = new Hud("DEMO");
}
 
Example 20
Source File: MD5Loader.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
private static Mesh generateMesh(MD5Model md5Model, MD5Mesh md5Mesh) {
    List<AnimVertex> vertices = new ArrayList<>();
    List<Integer> indices = new ArrayList<>();

    List<MD5Mesh.MD5Vertex> md5Vertices = md5Mesh.getVertices();
    List<MD5Mesh.MD5Weight> weights = md5Mesh.getWeights();
    List<MD5JointInfo.MD5JointData> joints = md5Model.getJointInfo().getJoints();

    for (MD5Mesh.MD5Vertex md5Vertex : md5Vertices) {
        AnimVertex vertex = new AnimVertex();
        vertices.add(vertex);

        vertex.position = new Vector3f();
        vertex.textCoords = md5Vertex.getTextCoords();

        int startWeight = md5Vertex.getStartWeight();
        int numWeights = md5Vertex.getWeightCount();

        vertex.jointIndices = new int[numWeights];
        Arrays.fill(vertex.jointIndices, -1);
        vertex.weights = new float[numWeights];
        Arrays.fill(vertex.weights, -1);
        for (int i = startWeight; i < startWeight + numWeights; i++) {
            MD5Mesh.MD5Weight weight = weights.get(i);
            MD5JointInfo.MD5JointData joint = joints.get(weight.getJointIndex());
            Vector3f rotatedPos = new Vector3f(weight.getPosition()).rotate(joint.getOrientation());
            Vector3f acumPos = new Vector3f(joint.getPosition()).add(rotatedPos);
            acumPos.mul(weight.getBias());
            vertex.position.add(acumPos);
            vertex.jointIndices[i - startWeight] = weight.getJointIndex();
            vertex.weights[i - startWeight] = weight.getBias();
        }
    }

    for (MD5Mesh.MD5Triangle tri : md5Mesh.getTriangles()) {
        indices.add(tri.getVertex0());
        indices.add(tri.getVertex1());
        indices.add(tri.getVertex2());

        // Normals
        AnimVertex v0 = vertices.get(tri.getVertex0());
        AnimVertex v1 = vertices.get(tri.getVertex1());
        AnimVertex v2 = vertices.get(tri.getVertex2());
        Vector3f pos0 = v0.position;
        Vector3f pos1 = v1.position;
        Vector3f pos2 = v2.position;

        Vector3f normal = (new Vector3f(pos2).sub(pos0)).cross(new Vector3f(pos1).sub(pos0));

        v0.normal.add(normal);
        v1.normal.add(normal);
        v2.normal.add(normal);
    }

    // Once the contributions have been added, normalize the result
    for(AnimVertex v : vertices) {
        v.normal.normalize();
    }

    Mesh mesh = createMesh(vertices, indices);
    return mesh;
}