use of org.joml.Vector3f in project chunkstories by Hugobros3.
the class Camera method vadd.
// Convinience methods, why wouldn't java allow operator overloading is beyond
// me.
private Vector3f vadd(Vector3f a, Vector3f b) {
Vector3f out = new Vector3f(a);
out.add(b);
return out;
}
use of org.joml.Vector3f in project chunkstories by Hugobros3.
the class Camera method transform3DCoordinate.
public Vector3f transform3DCoordinate(Vector4fc in) {
Matrix4f mvm = this.modelViewMatrix4f;
Matrix4f pm = this.projectionMatrix4f;
Vector4f transormed = new Vector4f();
mvm.transform(in, transormed);
pm.transform(transormed, transormed);
Vector3f posOnScreen = new Vector3f((float) in.x(), (float) in.y(), 0f);
float scale = 1 / in.z();
posOnScreen.mul(scale);
posOnScreen.x = ((posOnScreen.x() * 0.5f + 0.5f) * viewportWidth);
posOnScreen.y = (((posOnScreen.y() * 0.5f + 0.5f)) * viewportHeight);
posOnScreen.z = (scale);
return posOnScreen;
}
use of org.joml.Vector3f in project chunkstories by Hugobros3.
the class Camera method setupUsingScreenSize.
public void setupUsingScreenSize(int width, int height) {
this.viewportWidth = width;
this.viewportHeight = height;
// Frustrum values
float fovRad = (float) toRad(fov / 2.0);
float aspect = (float) width / (float) height;
float top = (float) Math.tan(fovRad) * 0.1f;
float bottom = -top;
float left = aspect * bottom;
float right = aspect * top;
float near = 0.1f;
float far = 3000f;
// Generate the projection matrix
projectionMatrix4f.identity();
projectionMatrix4f.m00((near * 2) / (right - left));
projectionMatrix4f.m11((near * 2) / (top - bottom));
float A = (right + left) / (right - left);
float B = (top + bottom) / (top - bottom);
float C = -(far + near) / (far - near);
float D = -(2 * far * near) / (far - near);
projectionMatrix4f.m20(A);
projectionMatrix4f.m21(B);
projectionMatrix4f.m22(C);
projectionMatrix4f.m32(D);
projectionMatrix4f.m23(-1);
projectionMatrix4f.m33(0);
// Grab the generated matrix
modelViewMatrix4f.identity();
modelViewMatrix4f.rotate((float) (rotationZ / 180 * Math.PI), new Vector3f(0.0f, 0.0f, 1.0f));
modelViewMatrix4f.rotate((float) (rotationX / 180 * Math.PI), new Vector3f(1.0f, 0.0f, 0.0f));
modelViewMatrix4f.rotate((float) (rotationY / 180 * Math.PI), new Vector3f(0.0f, 1.0f, 0.0f));
Vector3f position = new Vector3f((float) this.position.x, (float) this.position.y, (float) this.position.z);
float rotH = rotationY;
float rotV = rotationX;
float a = (float) ((180 - rotH) / 180f * Math.PI);
float b = (float) ((-rotV) / 180f * Math.PI);
Vector3f lookAt = new Vector3f((float) (Math.sin(a) * Math.cos(b)), (float) (Math.sin(b)), (float) (Math.cos(a) * Math.cos(b)));
up.set(0.0f, 1.0f, 0.0f);
lookAt.cross(up, up);
up.cross(lookAt, up);
if (up.x == lookAt.x && lookAt.y == -1.0f && up.z == lookAt.z) {
// System.out.println("straight down");
up.set((float) (Math.sin(a)), 0, (float) (Math.cos(a)));
} else if (up.x == -lookAt.x && lookAt.y == 1.0f && up.z == -lookAt.z) {
// System.out.println("straight up");
up.set(-(float) (Math.sin(a)), 0, -(float) (Math.cos(a)));
}
// System.out.println(up + " : " + lookAt);
modelViewMatrix4f.identity();
position.mul(0.0f);
modelViewMatrix4f.lookAt(position, lookAt, up);
lookAt.add(position);
// TODO ?
position.mul(0.0f);
computeFrustrumPlanes();
updateMatricesForShaderUniforms();
translateCamera();
alUpdate();
}
use of org.joml.Vector3f in project chunkstories by Hugobros3.
the class Camera method smult.
private Vector3f smult(Vector3f in, float scale) {
Vector3f out = new Vector3f(in);
out.mul(scale);
return out;
}
use of org.joml.Vector3f in project lwjgl3-demos by LWJGL.
the class WavefrontMeshLoader method loadMesh.
public Mesh loadMesh(String resource) throws IOException {
byte[] arr = readSingleFileZip(resource);
WavefrontInfo info = getInfo(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(arr))));
// Allocate buffers for all vertices/normal
ByteBuffer positionByteBuffer = BufferUtils.createByteBuffer(3 * info.numberOfVertices * 4);
ByteBuffer normalByteBuffer = BufferUtils.createByteBuffer(3 * info.numberOfNormals * 4);
FloatBuffer positions = positionByteBuffer.asFloatBuffer();
FloatBuffer normals = normalByteBuffer.asFloatBuffer();
// Allocate buffers for the actual face vertices/normals
ByteBuffer positionDataByteBuffer = BufferUtils.createByteBuffer((fourComponentPosition ? 4 : 3) * 3 * info.numberOfFaces * 4);
ByteBuffer normalDataByteBuffer = BufferUtils.createByteBuffer(3 * 3 * info.numberOfFaces * 4);
FloatBuffer positionData = positionDataByteBuffer.asFloatBuffer();
FloatBuffer normalData = normalDataByteBuffer.asFloatBuffer();
Mesh mesh = new Mesh();
MeshObject object = null;
float minX = 1E38f, minY = 1E38f, minZ = 1E38f;
float maxX = -1E38f, maxY = -1E38f, maxZ = -1E38f;
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(arr)));
String line;
int faceIndex = 0;
Vector3f tmp = new Vector3f();
while ((line = reader.readLine()) != null) {
if (line.startsWith("o ")) {
String name = line.substring(2);
object = new MeshObject();
object.name = name;
object.first = faceIndex;
mesh.objects.add(object);
} else if (line.startsWith("vn ")) {
String[] ns = line.split(" +");
float x = Float.parseFloat(ns[1]);
float y = Float.parseFloat(ns[2]);
float z = Float.parseFloat(ns[3]);
normals.put(x).put(y).put(z);
} else if (line.startsWith("v ")) {
String[] vs = line.split(" +");
float x = Float.parseFloat(vs[1]);
float y = Float.parseFloat(vs[2]);
float z = Float.parseFloat(vs[3]);
positions.put(x).put(y).put(z);
} else if (line.startsWith("f")) {
String[] fs = line.split(" +");
String[] f1 = fs[1].split("/");
String[] f2 = fs[2].split("/");
String[] f3 = fs[3].split("/");
int v1 = Integer.parseInt(f1[0]);
int v2 = Integer.parseInt(f2[0]);
int v3 = Integer.parseInt(f3[0]);
int n1 = Integer.parseInt(f1[2]);
int n2 = Integer.parseInt(f2[2]);
int n3 = Integer.parseInt(f3[2]);
float ver1X = positions.get(3 * (v1 - 1) + 0);
float ver1Y = positions.get(3 * (v1 - 1) + 1);
float ver1Z = positions.get(3 * (v1 - 1) + 2);
minX = minX < ver1X ? minX : ver1X;
minY = minY < ver1Y ? minY : ver1Y;
minZ = minZ < ver1Z ? minZ : ver1Z;
maxX = maxX > ver1X ? maxX : ver1X;
maxY = maxY > ver1Y ? maxY : ver1Y;
maxZ = maxZ > ver1Z ? maxZ : ver1Z;
tmp.set(ver1X, ver1Y, ver1Z);
if (object != null) {
object.min.min(tmp);
object.max.max(tmp);
}
float ver2X = positions.get(3 * (v2 - 1) + 0);
float ver2Y = positions.get(3 * (v2 - 1) + 1);
float ver2Z = positions.get(3 * (v2 - 1) + 2);
minX = minX < ver2X ? minX : ver2X;
minY = minY < ver2Y ? minY : ver2Y;
minZ = minZ < ver2Z ? minZ : ver2Z;
maxX = maxX > ver2X ? maxX : ver2X;
maxY = maxY > ver2Y ? maxY : ver2Y;
maxZ = maxZ > ver2Z ? maxZ : ver2Z;
tmp.set(ver2X, ver2Y, ver2Z);
if (object != null) {
object.min.min(tmp);
object.max.max(tmp);
}
float ver3X = positions.get(3 * (v3 - 1) + 0);
float ver3Y = positions.get(3 * (v3 - 1) + 1);
float ver3Z = positions.get(3 * (v3 - 1) + 2);
minX = minX < ver3X ? minX : ver3X;
minY = minY < ver3Y ? minY : ver3Y;
minZ = minZ < ver3Z ? minZ : ver3Z;
maxX = maxX > ver3X ? maxX : ver3X;
maxY = maxY > ver3Y ? maxY : ver3Y;
maxZ = maxZ > ver3Z ? maxZ : ver3Z;
tmp.set(ver3X, ver3Y, ver3Z);
if (object != null) {
object.min.min(tmp);
object.max.max(tmp);
}
positionData.put(ver1X).put(ver1Y).put(ver1Z);
if (fourComponentPosition) {
positionData.put(1.0f);
}
positionData.put(ver2X).put(ver2Y).put(ver2Z);
if (fourComponentPosition) {
positionData.put(1.0f);
}
positionData.put(ver3X).put(ver3Y).put(ver3Z);
if (fourComponentPosition) {
positionData.put(1.0f);
}
float norm1X = normals.get(3 * (n1 - 1) + 0);
float norm1Y = normals.get(3 * (n1 - 1) + 1);
float norm1Z = normals.get(3 * (n1 - 1) + 2);
float norm2X = normals.get(3 * (n2 - 1) + 0);
float norm2Y = normals.get(3 * (n2 - 1) + 1);
float norm2Z = normals.get(3 * (n2 - 1) + 2);
float norm3X = normals.get(3 * (n3 - 1) + 0);
float norm3Y = normals.get(3 * (n3 - 1) + 1);
float norm3Z = normals.get(3 * (n3 - 1) + 2);
normalData.put(norm1X).put(norm1Y).put(norm1Z);
normalData.put(norm2X).put(norm2Y).put(norm2Z);
normalData.put(norm3X).put(norm3Y).put(norm3Z);
faceIndex++;
if (object != null) {
object.count++;
}
}
}
if (mesh.objects.isEmpty()) {
object = new MeshObject();
object.count = faceIndex;
mesh.objects.add(object);
}
positionData.flip();
normalData.flip();
mesh.boundingSphereRadius = Math.max(maxX - minX, Math.max(maxY - minY, maxZ - minZ)) * 0.5f;
mesh.positions = positionData;
mesh.normals = normalData;
mesh.numVertices = positionData.limit() / (fourComponentPosition ? 4 : 3);
return mesh;
}
Aggregations