use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class ColorRangeGeneratorComponent method copyFrom.
@Override
public void copyFrom(ColorRangeGeneratorComponent other) {
this.minColorComponents = new Vector4f(other.minColorComponents);
this.maxColorComponents = new Vector4f(other.maxColorComponents);
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class SphereBuilder method build.
// refrence: https://github.com/caosdoar/spheres/blob/master/src/spheres.cpp
public StandardMeshData build() {
StandardMeshData meshData = new StandardMeshData();
Matrix4f mat = new Matrix4f().setRotationXYZ(0, 0, (float) (Math.PI / 2.0f));
Vector4f pos = new Vector4f();
Vector3f normal = new Vector3f();
Vector2f uv0 = new Vector2f();
Vector3f loc = new Vector3f();
float s = 0.0f;
float t = 1.0f;
float ds = 1.0f / verticalCuts;
float dt = 1.0f / horizontalCuts;
for (int j = 0; j <= horizontalCuts; ++j) {
double polar = (Math.PI * j) / horizontalCuts;
double sp = Math.sin(polar);
double cp = Math.cos(polar);
s = 0.0f;
for (int i = 0; i <= verticalCuts; ++i) {
double azimuth = (2.0 * Math.PI * i) / verticalCuts;
double sa = Math.sin(azimuth);
double ca = Math.cos(azimuth);
if (this.normals) {
normal.set((float) (sp * ca), (float) cp, (float) (sp * sa));
meshData.normal.put(normal);
}
if (this.textured) {
uv0.set(s, t);
meshData.uv0.put(uv0);
}
s += ds;
pos.set((float) ((sp * ca) * radius), (float) (cp * radius), (float) ((sp * sa) * radius), 1.0f);
mat.transform(pos);
loc.set(pos.x, pos.y, pos.z);
meshData.position.put(loc);
}
t -= dt;
}
for (int j = 0; j < horizontalCuts; ++j) {
int aStart = (j * (verticalCuts + 1));
int bStart = (j + 1) * (verticalCuts + 1);
for (int i = 0; i < verticalCuts; ++i) {
int a = aStart + i;
int a1 = aStart + ((i + 1) % (verticalCuts + 1));
int b = bStart + i;
int b1 = bStart + ((i + 1) % (verticalCuts + 1));
meshData.indices.putAll(a, b1, b);
meshData.indices.putAll(a1, b1, a);
}
}
return meshData;
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class GLTFAttributeMapping method readColor4FBuffer.
public static void readColor4FBuffer(byte[] buffer, GLTFAccessor accessor, GLTFBufferView bufferView, VertexAttributeBinding<Colorc, Color> mapping) {
if (accessor.getComponentType() != GLTFComponentType.FLOAT) {
return;
}
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, bufferView.getByteOffset() + accessor.getByteOffset(), bufferView.getByteLength() - accessor.getByteOffset());
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
int gap = 0;
if (bufferView.getByteStride() > 0) {
gap = bufferView.getByteStride() - accessor.getComponentType().getByteLength() * accessor.getType().getDimension();
}
Vector4f value = new Vector4f();
Color c = new Color();
if (byteBuffer.position() < byteBuffer.limit()) {
for (int i = 0; i < accessor.getType().getDimension(); i++) {
value.setComponent(i, byteBuffer.getFloat());
}
c.set(value);
mapping.put(c);
}
while (byteBuffer.position() < byteBuffer.limit() - gap) {
byteBuffer.position(byteBuffer.position() + gap);
for (int i = 0; i < accessor.getType().getDimension(); i++) {
value.setComponent(i, byteBuffer.getFloat());
}
c.set(value);
mapping.put(c);
}
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class OpenVRStereoCamera method updateMatrices.
@Override
public void updateMatrices(float fov) {
prevViewProjectionMatrix.set(viewProjectionMatrix);
Matrix4f leftEyeProjection = vrProvider.getState().getEyeProjectionMatrix(0);
Matrix4f rightEyeProjection = vrProvider.getState().getEyeProjectionMatrix(1);
Matrix4f leftEyePose = vrProvider.getState().getEyePose(0);
Matrix4f rightEyePose = vrProvider.getState().getEyePose(1);
float halfIPD = (float) Math.sqrt(Math.pow(leftEyePose.m30() - rightEyePose.m30(), 2) + Math.pow(leftEyePose.m31() - rightEyePose.m31(), 2) + Math.pow(leftEyePose.m32() - rightEyePose.m32(), 2)) / 2.0f;
// set camera orientation
Vector4f vecQuaternion = OpenVRUtil.convertToQuaternion(leftEyePose);
Quaternionf quaternion = new Quaternionf(vecQuaternion.x, vecQuaternion.y, vecQuaternion.z, vecQuaternion.w);
setOrientation(quaternion);
// view matrix is inverse of pose matrix
leftEyePose = leftEyePose.invert();
rightEyePose = rightEyePose.invert();
if (Math.sqrt(Math.pow(leftEyePose.m30(), 2) + Math.pow(leftEyePose.m31(), 2) + Math.pow(leftEyePose.m32(), 2)) < 0.25) {
return;
}
projectionMatrixLeftEye.set(leftEyeProjection);
projectionMatrixRightEye.set(rightEyeProjection);
projectionMatrix = projectionMatrixLeftEye;
viewMatrixLeftEye.set(leftEyePose);
viewMatrixRightEye.set(rightEyePose);
viewMatrix = viewMatrixLeftEye;
normViewMatrix = viewMatrixLeftEye;
reflectionMatrix.setRow(0, new Vector4f(1.0f, 0.0f, 0.0f, 0.0f));
reflectionMatrix.setRow(1, new Vector4f(0.0f, -1.0f, 0.0f, 2f * (-position.y + 32f)));
reflectionMatrix.setRow(2, new Vector4f(0.0f, 0.0f, 1.0f, 0.0f));
reflectionMatrix.setRow(3, new Vector4f(0.0f, 0.0f, 0.0f, 1.0f));
viewMatrix.mul(reflectionMatrix, viewMatrixReflected);
reflectionMatrix.setRow(1, new Vector4f(0.0f, -1.0f, 0.0f, 0.0f));
normViewMatrix.mul(reflectionMatrix, normViewMatrixReflected);
viewTranslationLeftEye.identity();
viewTranslationLeftEye.setTranslation(halfIPD, 0.0f, 0.0f);
viewTranslationRightEye.identity();
viewTranslationRightEye.setTranslation(-halfIPD, 0.0f, 0.0f);
viewTranslationLeftEye.mul(viewMatrixReflected, viewMatrixReflectedLeftEye);
viewTranslationRightEye.mul(viewMatrixReflected, viewMatrixReflectedRightEye);
projectionMatrixLeftEye.mul(viewMatrixLeftEye, viewProjectionMatrixLeftEye);
projectionMatrixRightEye.mul(viewMatrixRightEye, viewProjectionMatrixRightEye);
viewProjectionMatrixLeftEye.invert(inverseViewProjectionMatrixLeftEye);
viewProjectionMatrixRightEye.invert(inverseViewProjectionMatrixRightEye);
projectionMatrixLeftEye.invert(inverseProjectionMatrixLeftEye);
projectionMatrixRightEye.invert(inverseProjectionMatrixRightEye);
updateFrustum();
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class PerspectiveCamera method updateMatrices.
@Override
public void updateMatrices(float fov) {
// Nothing to do...
if (cachedPosition.equals(getPosition()) && cachedViewigDirection.equals(viewingDirection) && cachedBobbingRotationOffsetFactor == bobbingRotationOffsetFactor && cachedBobbingVerticalOffsetFactor == bobbingVerticalOffsetFactor && cachedFov == fov && cachedZFar == getzFar() && cachedZNear == getzNear() && cachedReflectionHeight == getReflectionHeight()) {
return;
}
viewingDirection.cross(up, tempRightVector);
tempRightVector.mul(bobbingRotationOffsetFactor);
float aspectRatio = (float) displayDevice.getWidth() / displayDevice.getHeight();
float fovY = (float) (2 * Math.atan2(Math.tan(0.5 * fov * TeraMath.DEG_TO_RAD), aspectRatio));
projectionMatrix.setPerspective(fovY, aspectRatio, getzNear(), getzFar());
viewMatrix.setLookAt(0f, bobbingVerticalOffsetFactor * 2.0f, 0f, viewingDirection.x, viewingDirection.y + bobbingVerticalOffsetFactor * 2.0f, viewingDirection.z, up.x + tempRightVector.x, up.y + tempRightVector.y, up.z + tempRightVector.z);
normViewMatrix.setLookAt(0f, bobbingVerticalOffsetFactor * 2.0f, 0f, viewingDirection.x, viewingDirection.y + bobbingVerticalOffsetFactor * 2.0f, viewingDirection.z, up.x + tempRightVector.x, up.y + tempRightVector.y, up.z + tempRightVector.z);
reflectionMatrix.setRow(0, new Vector4f(1.0f, 0.0f, 0.0f, 0.0f));
reflectionMatrix.setRow(1, new Vector4f(0.0f, -1.0f, 0.0f, 2f * (-position.y + getReflectionHeight())));
reflectionMatrix.setRow(2, new Vector4f(0.0f, 0.0f, 1.0f, 0.0f));
reflectionMatrix.setRow(3, new Vector4f(0.0f, 0.0f, 0.0f, 1.0f));
viewMatrix.mul(reflectionMatrix, viewMatrixReflected);
reflectionMatrix.setRow(1, new Vector4f(0.0f, -1.0f, 0.0f, 0.0f));
normViewMatrix.mul(reflectionMatrix, normViewMatrixReflected);
projectionMatrix.mul(viewMatrix, viewProjectionMatrix);
projectionMatrix.invert(inverseProjectionMatrix);
viewProjectionMatrix.invert(inverseViewProjectionMatrix);
// Used for dirty checks
cachedPosition.set(getPosition());
cachedViewigDirection.set(viewingDirection);
cachedBobbingVerticalOffsetFactor = bobbingVerticalOffsetFactor;
cachedBobbingRotationOffsetFactor = bobbingRotationOffsetFactor;
cachedFov = fov;
cachedZNear = getzNear();
cachedZFar = getzFar();
cachedReflectionHeight = getReflectionHeight();
updateFrustum();
}
Aggregations