private Vector3f[] positionFromFloat16ArrayData(float[] inverseBindMatrixArray) { Vector3f[] translationVectorArray = new Vector3f[inverseBindMatrixArray.length / 16]; for (int i = 0; i < inverseBindMatrixArray.length / 16; ++i) { int offset = i * 16; Matrix4f matrix4f = new Matrix4f(Arrays.copyOfRange(inverseBindMatrixArray, offset, offset + 16)); Vector3f translationVector = matrix4f.getTranslation(); translationVectorArray[i] = translationVector; } return translationVectorArray; }
public void updatePrevViewProjectionMatrix() { prevViewProjectionMatrix.set(viewProjectionMatrix); }
public static Matrix4f createOrthogonalProjectionMatrix(float left, float right, float top, float bottom, float near, float far) { Matrix4f m = new Matrix4f(); float lateral = right - left; float vertical = top - bottom; float forward = far - near; float tx = -(right + left) / (right - left); float ty = -(top + bottom) / (top - bottom); float tz = -(far + near) / (far - near); m.m00 = 2.0f / lateral; m.m10 = 0.0f; m.m20 = 0.0f; m.m30 = tx; m.m01 = 0.0f; m.m11 = 2.0f / vertical; m.m21 = 0.0f; m.m31 = ty; m.m02 = 0.0f; m.m12 = 0.0f; m.m22 = -2.0f / forward; m.m32 = tz; m.m03 = 0.0f; m.m13 = 0.0f; m.m23 = 0.0f; m.m33 = 1.0f; m.transpose(); return m; }
@Override public void updateMatrices(float fov) { prevViewProjectionMatrix.set(viewProjectionMatrix); reflectionMatrix.setRow(0, 1.0f, 0.0f, 0.0f, 0.0f); reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 2f * (-position.y + 32f)); reflectionMatrix.setRow(2, 0.0f, 0.0f, 1.0f, 0.0f); reflectionMatrix.setRow(3, 0.0f, 0.0f, 0.0f, 1.0f); viewMatrixReflected.mul(viewMatrix, reflectionMatrix); reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 0.0f); normViewMatrixReflected.mul(normViewMatrix, reflectionMatrix); viewTranslationLeftEye.setIdentity(); viewTranslationLeftEye.setTranslation(new Vector3f(halfIPD, 0.0f, 0.0f)); viewTranslationRightEye.setIdentity(); viewTranslationRightEye.setTranslation(new Vector3f(-halfIPD, 0.0f, 0.0f)); viewMatrixReflectedLeftEye.mul(viewMatrixReflected, viewTranslationLeftEye); viewMatrixReflectedRightEye.mul(viewMatrixReflected, viewTranslationRightEye); inverseViewProjectionMatrixLeftEye = new Matrix4f(viewProjectionMatrixLeftEye); inverseViewProjectionMatrixRightEye = new Matrix4f(viewProjectionMatrixRightEye); inverseViewProjectionMatrixLeftEye.invert(viewProjectionMatrixLeftEye); inverseViewProjectionMatrixRightEye.invert(viewProjectionMatrixRightEye); inverseProjectionMatrixLeftEye = new Matrix4f(projectionMatrixLeftEye); inverseProjectionMatrixRightEye = new Matrix4f(projectionMatrixRightEye);
public void recurse(CoreChunk view, Random rand, int posX, int posY, int posZ, float angleOffset, CharSequenceIterator axiomIterator, Vector3f position, Matrix4f rotation, Block bark, Block leaf, int depth, AbstractTreeGenerator treeGenerator) { Matrix4f tempRotation = new Matrix4f(); while (axiomIterator.hasNext()) { char c = axiomIterator.nextChar(); rotation.transformVector(dir); recurse(view, rand, posX, posY, posZ, angleOffset, axiomIterator, new Vector3f(position), new Matrix4f(rotation), bark, leaf, depth, treeGenerator); break; case ']': return; case '+': tempRotation = new Matrix4f(new Quat4f(new Vector3f(0f, 0f, 1f), angle + angleOffset), Vector3f.ZERO, 1.0f); rotation.mul(tempRotation); break; case '-': tempRotation = new Matrix4f(new Quat4f(new Vector3f(0f, 0f, -1f), angle + angleOffset), Vector3f.ZERO, 1.0f); rotation.mul(tempRotation); break; case '&': tempRotation = new Matrix4f(new Quat4f(new Vector3f(0f, 1f, 0f), angle + angleOffset), Vector3f.ZERO, 1.0f); rotation.mul(tempRotation); break; case '^': tempRotation = new Matrix4f(new Quat4f(new Vector3f(0f, -1f, 0f), angle + angleOffset), Vector3f.ZERO, 1.0f); rotation.mul(tempRotation); break; case '*':
activeCamera.getViewMatrix().transformPoint(lightPositionInViewSpace); lightGeometryMaterial.setFloat3("lightViewPos", lightPositionInViewSpace.x, lightPositionInViewSpace.y, lightPositionInViewSpace.z, true); Matrix4f modelMatrix = new Matrix4f(); modelMatrix.set(lightComponent.lightAttenuationRange); // scales the modelview matrix, effectively scales the light sphere modelMatrix.setTranslation(lightPositionRelativeToCamera); // effectively moves the light sphere in the right position relative to camera lightGeometryMaterial.setMatrix4("modelMatrix", modelMatrix, true);
private Quat4f[] quad4fArrayFromFloat16ArrayData(float[] inverseBindMatrixArray) { Quat4f[] rotationArray = new Quat4f[inverseBindMatrixArray.length / 16]; for (int i = 0; i < inverseBindMatrixArray.length / 16; ++i) { int offset = i * 16; Matrix4f matrix4f = new Matrix4f(Arrays.copyOfRange(inverseBindMatrixArray, offset, offset + 16)); Quat4f rotation = new Quat4f(); rotation.set(matrix4f); rotationArray[i] = rotation; } return rotationArray; }
up.x + tempRightVector.x, up.y + tempRightVector.y, up.z + tempRightVector.z); reflectionMatrix.setRow(0, 1.0f, 0.0f, 0.0f, 0.0f); reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 2f * (-position.y + getReflectionHeight())); reflectionMatrix.setRow(2, 0.0f, 0.0f, 1.0f, 0.0f); reflectionMatrix.setRow(3, 0.0f, 0.0f, 0.0f, 1.0f); viewMatrixReflected.mul(viewMatrix, reflectionMatrix); reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 0.0f); normViewMatrixReflected.mul(normViewMatrix, reflectionMatrix); inverseProjectionMatrix.invert(projectionMatrix); inverseViewProjectionMatrix.invert(viewProjectionMatrix);
@Override public void preRender() { glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 0, 2048f); glMatrixMode(GL_MODELVIEW); glPushMatrix(); modelView = new Matrix4f(); modelView.setIdentity(); modelView.setTranslation(new Vector3f(0, 0, -1024f)); MatrixUtils.matrixToFloatBuffer(modelView, matrixBuffer); glLoadMatrix(matrixBuffer); matrixBuffer.rewind(); glScalef(uiScale, uiScale, uiScale); requestedCropRegion = Rect2i.createFromMinAndSize(0, 0, Display.getWidth(), Display.getHeight()); currentTextureCropRegion = requestedCropRegion; textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX(), requestedCropRegion.maxX(), requestedCropRegion.minY(), requestedCropRegion.maxY()); }
@Override public void updateMatrices(float fov) { prevViewProjectionMatrix.set(viewProjectionMatrix); // Nothing to do... if (cachedPosition.equals(getPosition()) && cachedViewigDirection.equals(getViewingDirection()) && cachedZFar == zFar && cachedZNear == zNear) { return; } projectionMatrix = MatrixUtils.createOrthogonalProjectionMatrix(left, right, top, bottom, zNear, zFar); viewMatrix = MatrixUtils.createViewMatrix(0f, 0.0f, 0f, viewingDirection.x, viewingDirection.y, viewingDirection.z, up.x, up.y, up.z); normViewMatrix = MatrixUtils.createViewMatrix(0f, 0f, 0f, viewingDirection.x, viewingDirection.y, viewingDirection.z, up.x, up.y, up.z); viewProjectionMatrix = MatrixUtils.calcViewProjectionMatrix(viewMatrix, projectionMatrix); inverseViewProjectionMatrix.invert(viewProjectionMatrix); // Used for dirty checks cachedPosition.set(getPosition()); cachedViewigDirection.set(getViewingDirection()); cachedZFar = zFar; cachedZNear = zNear; updateFrustum(); }
private void positionShadowMapCamera() { // We begin by setting our light coordinates at the player coordinates, ignoring the player's altitude Vector3f mainLightPosition = new Vector3f(activeCamera.getPosition().x, 0.0f, activeCamera.getPosition().z); // world-space coordinates // The shadow projected onto the ground must move in in light-space texel-steps, to avoid causing flickering. // That's why we first convert it to the previous frame's light-space coordinates and then back to world-space. shadowMapCamera.getViewProjectionMatrix().transformPoint(mainLightPosition); // to light-space mainLightPosition.set(TeraMath.fastFloor(mainLightPosition.x / texelSize) * texelSize, 0.0f, TeraMath.fastFloor(mainLightPosition.z / texelSize) * texelSize); shadowMapCamera.getInverseViewProjectionMatrix().transformPoint(mainLightPosition); // back to world-space // This is what causes the shadow map to change infrequently, to prevent flickering. // Notice that this is different from what is done above, which is about spatial steps // and is related to the player's position and texels. Vector3f quantizedMainLightDirection = getQuantizedMainLightDirection(STEP_SIZE); // The shadow map camera is placed away from the player, in the direction of the main light. Vector3f offsetFromPlayer = new Vector3f(quantizedMainLightDirection); offsetFromPlayer.scale(256.0f + 64.0f); // these hardcoded numbers are another mystery. mainLightPosition.add(offsetFromPlayer); shadowMapCamera.getPosition().set(mainLightPosition); // Finally, we adjust the shadow map camera to look toward the player Vector3f fromLightToPlayerDirection = new Vector3f(quantizedMainLightDirection); fromLightToPlayerDirection.scale(-1.0f); shadowMapCamera.getViewingDirection().set(fromLightToPlayerDirection); shadowMapCamera.update(worldRenderer.getSecondsSinceLastFrame()); }
sunPositionWorldSpace4.set(sunDirection.x * 10000.0f, sunDirection.y * 10000.0f, sunDirection.z * 10000.0f, 1.0f); sunPositionScreenSpace.set(sunPositionWorldSpace4); activeCamera.getViewProjectionMatrix().transform(sunPositionScreenSpace);
public static Matrix4f createPerspectiveProjectionMatrix(float fovY, float aspectRatio, float zNear, float zFar) { Matrix4f m = new Matrix4f(); float f = 1.0f / (float) Math.tan((double) fovY * 0.5f); m.m00 = f / aspectRatio; m.m10 = 0; m.m20 = 0; m.m30 = 0; m.m01 = 0; m.m11 = f; m.m21 = 0; m.m31 = 0; m.m02 = 0; m.m12 = 0; m.m22 = (zFar + zNear) / (zNear - zFar); m.m32 = (2 * zFar * zNear) / (zNear - zFar); m.m03 = 0; m.m13 = 0; m.m23 = -1; m.m33 = 0; m.transpose(); return m; }
@Override public void generate(BlockManager blockManager, CoreChunk view, Random rand, int posX, int posY, int posZ) { Vector3f position = new Vector3f(0f, 0f, 0f); Matrix4f rotation = new Matrix4f(new Quat4f(new Vector3f(0f, 0f, 1f), (float) Math.PI / 2f), Vector3f.ZERO, 1.0f); float angleOffset = rand.nextFloat(-MAX_ANGLE_OFFSET, MAX_ANGLE_OFFSET); Block bark = blockManager.getBlock(barkType); Block leaf = blockManager.getBlock(leafType); recursiveGenerator.recurse(view, rand, posX, posY, posZ, angleOffset, new CharSequenceIterator(initialAxiom), position, rotation, bark, leaf, 0, this); }
activeCameraToLightSpace.sub(cameraPosition, lightCamera.getPosition()); mainLightInViewSpace = backdropProvider.getSunDirection(true); activeCamera.getViewMatrix().transformPoint(mainLightInViewSpace);
@Override public void renderOverlay() { if (entityToRegionOutlineMap.isEmpty()) { return; // skip everything if there is nothing to do to avoid possibly costly draw mode changes } glDisable(GL_DEPTH_TEST); glLineWidth(2); Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition(); FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16); FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12); material.setFloat("sunlight", 1.0f, true); material.setFloat("blockLight", 1.0f, true); material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix()); Vector3f worldPos = new Vector3f(); Vector3f worldPositionCameraSpace = new Vector3f(); worldPositionCameraSpace.sub(worldPos, cameraPosition); Matrix4f matrixCameraSpace = new Matrix4f(new Quat4f(0, 0, 0, 1), worldPositionCameraSpace, 1.0f); Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace); MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44); material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true); MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33); material.setMatrix3("normalMatrix", tempMatrixBuffer33, true); for (RegionOutlineComponent regionOutline: entityToRegionOutlineMap.values()) { material.setFloat3("colorOffset", regionOutline.color.rf(), regionOutline.color.gf(), regionOutline.color.bf(), true); drawRegionOutline(regionOutline); } glEnable(GL_DEPTH_TEST); }
private void jomlMatrix4f(org.joml.Matrix4f matrixInput, org.terasology.math.geom.Matrix4f matrixOut) { matrixOut.set(0, 0, matrixInput.m00()); matrixOut.set(0, 1, matrixInput.m10()); matrixOut.set(0, 2, matrixInput.m20()); matrixOut.set(0, 3, matrixInput.m30()); matrixOut.set(1, 0, matrixInput.m01()); matrixOut.set(1, 1, matrixInput.m11()); matrixOut.set(1, 2, matrixInput.m21()); matrixOut.set(1, 3, matrixInput.m31()); matrixOut.set(2, 0, matrixInput.m02()); matrixOut.set(2, 1, matrixInput.m12()); matrixOut.set(2, 2, matrixInput.m22()); matrixOut.set(2, 3, matrixInput.m32()); matrixOut.set(3, 0, matrixInput.m03()); matrixOut.set(3, 1, matrixInput.m13()); matrixOut.set(3, 2, matrixInput.m23()); matrixOut.set(3, 3, matrixInput.m33()); }