/** * Creates a camera with the specified projection matrix * @param projection */ public Camera(Matrix4f projection) { this.projection = projection; // Set the view to a default identity matrix view = new Matrix4f(); // Set the position to origin position = new Vector3f(); // Set the orientation to standard orientation = new Quaternion(); // Create the default local axes (right handed) up = new Vector3f(AXIS_Y); forward = new Vector3f(AXIS_Z).negate(null); right = new Vector3f(AXIS_X); }
public Camera(final Camera other) { this.projection = new Matrix4f(other.projection); this.view = new Matrix4f(other.view); this.position = new Vector3f(other.position); this.orientation = new Quaternion(other.orientation); this.up = new Vector3f(other.up); this.forward = new Vector3f(other.forward); this.right = new Vector3f(other.right); this.pitch = other.pitch; }
/** * Rotates the camera around the specified axis in the counter-clockwise direction * @param axis The axis to rotate around * @param v1 The first local camera vector to be updated * @param v2 The second local camera vector to be updated * @param angle The amount to rotate around the axis in degrees */ private void rotate(Vector3f axis, Vector3f v1, Vector3f v2, float angle) { Quaternion rotation = QuaternionUtil.createFromAxisAngle(axis, angle, null); Quaternion.mul(rotation, orientation, orientation); QuaternionUtil.rotate(v1, rotation, v1); QuaternionUtil.rotate(v2, rotation, v2); v1.normalise(); v2.normalise(); }
/** * Creates a copy of this camera object * @return */ public Camera copy() { Camera res = new Camera(new Matrix4f(this.getProjection())); res.setPosition(new Vector3f(this.getPosition())); res.setView(new Matrix4f(this.getView())); res.orientation = new Quaternion(this.orientation); res.up = new Vector3f(this.up); res.right = new Vector3f(this.right); res.forward = new Vector3f(this.forward); res.pitch = this.pitch; return res; }
private static Quaternion makeQuaternion(float p_188035_0_, float p_188035_1_, float p_188035_2_) { float f = p_188035_0_ * 0.017453292F; float f1 = p_188035_1_ * 0.017453292F; float f2 = p_188035_2_ * 0.017453292F; float f3 = MathHelper.sin(0.5F * f); float f4 = MathHelper.cos(0.5F * f); float f5 = MathHelper.sin(0.5F * f1); float f6 = MathHelper.cos(0.5F * f1); float f7 = MathHelper.sin(0.5F * f2); float f8 = MathHelper.cos(0.5F * f2); return new Quaternion(f3 * f6 * f8 + f4 * f5 * f7, f4 * f5 * f8 - f3 * f6 * f7, f3 * f5 * f8 + f4 * f6 * f7, f4 * f6 * f8 - f3 * f5 * f7); }
public static FloatBuffer quatToGlMatrix(FloatBuffer p_187418_0_, Quaternion p_187418_1_) { p_187418_0_.clear(); float f = p_187418_1_.x * p_187418_1_.x; float f1 = p_187418_1_.x * p_187418_1_.y; float f2 = p_187418_1_.x * p_187418_1_.z; float f3 = p_187418_1_.x * p_187418_1_.w; float f4 = p_187418_1_.y * p_187418_1_.y; float f5 = p_187418_1_.y * p_187418_1_.z; float f6 = p_187418_1_.y * p_187418_1_.w; float f7 = p_187418_1_.z * p_187418_1_.z; float f8 = p_187418_1_.z * p_187418_1_.w; p_187418_0_.put(1.0F - 2.0F * (f4 + f7)); p_187418_0_.put(2.0F * (f1 + f8)); p_187418_0_.put(2.0F * (f2 - f6)); p_187418_0_.put(0.0F); p_187418_0_.put(2.0F * (f1 - f8)); p_187418_0_.put(1.0F - 2.0F * (f + f7)); p_187418_0_.put(2.0F * (f5 + f3)); p_187418_0_.put(0.0F); p_187418_0_.put(2.0F * (f2 + f6)); p_187418_0_.put(2.0F * (f5 - f3)); p_187418_0_.put(1.0F - 2.0F * (f + f4)); p_187418_0_.put(0.0F); p_187418_0_.put(0.0F); p_187418_0_.put(0.0F); p_187418_0_.put(0.0F); p_187418_0_.put(1.0F); p_187418_0_.rewind(); return p_187418_0_; }
public static void rotate(Quaternion q, float pitch, float yaw, float roll) { // Assuming the angles are in radians. float c1 = (float)Math.cos(pitch/2); float s1 = (float)Math.sin(pitch/2); float c2 = (float)Math.cos(yaw/2); float s2 = (float)Math.sin(yaw/2); float c3 = (float)Math.cos(roll/2); float s3 = (float)Math.sin(roll/2); float c1c2 = c1*c2; float s1s2 = s1*s2; q.w =(c1c2*c3 - s1s2*s3); q.x =(c1c2*s3 + s1s2*c3); q.y =(s1*c2*c3 + c1*s2*s3); q.z =(c1*s2*c3 - s1*c2*s3); }
public static Matrix4f toMatrix4f(Quaternion q){ float x2 = q.x * q.x; float y2 = q.y * q.y; float z2 = q.z * q.z; float xy = q.x * q.y; float xz = q.x * q.z; float yz = q.y * q.z; float wx = q.w * q.x; float wy = q.w * q.y; float wz = q.w * q.z; Matrix4f rslt = new Matrix4f(); rslt.m00=1.0f - 2.0f * (y2 + z2); rslt.m01=2.0f * (xy - wz); rslt.m02=2.0f * (xz + wy); rslt.m03=0.0f; rslt.m10=2.0f * (xy + wz); rslt.m11=1.0f - 2.0f * (x2 + z2); rslt.m12=2.0f * (yz - wx); rslt.m13=0.0f; rslt.m20=2.0f * (xz - wy); rslt.m21=2.0f * (yz + wx); rslt.m22=1.0f - 2.0f * (x2 + y2); rslt.m23=0.0f; rslt.m30=0.0f; rslt.m31=0.0f; rslt.m32=0.0f; rslt.m33=1.0f; return rslt; }
@Override public void execute(RenderingManager renderingManager) { Quaternion rot = object.getRotation(); Quaternion n = new Quaternion(); QuaternionHelper.rotate(n, pitch, yaw, roll); rot = Quaternion.mul(rot, n, rot); object.setRotation(rot); }
@Override public void interpolate(Quaternion dest, Quaternion left, Quaternion right, float progress) { float rev = 1.0f - progress; dest.set(left.x * rev + right.x * progress, left.y * rev + right.y * progress, left.z * rev + right.z * progress, left.w * rev + right.w * progress); }
public G3DModelLoader(Logger logger) { this.logger = logger; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Color.class, new ColorDeserializer()); gsonBuilder.registerTypeAdapter(Quaternion.class, new QuaternionDeserializer()); gsonBuilder.registerTypeAdapter(Vector2f.class, new Vector2fDeserializer()); gsonBuilder.registerTypeAdapter(Vector3f.class, new Vector3fDeserializer()); gson = gsonBuilder.create(); }
private final void rotateWithQuaternion(final float angleDir, final float xSpeed, final float ySpeed, final float zSpeed) { Quaternion qRotation = new Quaternion(); Quaternion qView = new Quaternion(); Quaternion qNewView = new Quaternion(); // Create the rotation quaternion based on the axis we are rotating on. Vector4f quat = new Vector4f(xSpeed, ySpeed, zSpeed, angleDir); qRotation.setFromAxisAngle(quat); // Create the view quaternion. This will be the direction of the view and position. qView.x = m_vView.x - m_vPosition.x; // This gets the direction of the X. qView.y = m_vView.y - m_vPosition.y; // This gets the direction of the Y. qView.z = m_vView.z - m_vPosition.z; // This gets the direction of the Z. qView.w = 0; // Create the resulting quaternion by multiplying the rotation quat by the view quat // then multiplying that by the conjugate of the rotation quat. Quaternion q = new Quaternion(); Quaternion.mul(qRotation, qView, q); Quaternion.mul(q, qRotation.negate(qRotation), qNewView); //qNewView = Quaternion.multiply(Quaternion.multiply(qRotation,qView), qRotation.conjugate()); // Update the view information by adding the position to the resulting quaternion. m_vView.x = m_vPosition.x + qNewView.x; m_vView.y = m_vPosition.y + qNewView.y; m_vView.z = m_vPosition.z + qNewView.z; }
private final void rotateWithQuaternion(final float angleDir, final float xSpeed, final float ySpeed, final float zSpeed) { final Quaternion qRotation = new Quaternion(); final Quaternion qView = new Quaternion(); final Quaternion qNewView = new Quaternion(); // Create the rotation quaternion based on the axis we are rotating on. qRotation.setFromAxisAngle(new Vector4f(xSpeed, ySpeed, zSpeed, angleDir)); // Create the view quaternion. This will be the direction of the view // and position. qView.x = this.m_vView.x - this.m_vPosition.x; // This gets the // direction of the // X. qView.y = this.m_vView.y - this.m_vPosition.y; // This gets the // direction of the // Y. qView.z = this.m_vView.z - this.m_vPosition.z; // This gets the // direction of the // Z. qView.w = 0; // Create the resulting quaternion by multiplying the rotation quat by // the view quat // then multiplying that by the conjugate of the rotation quat. final Quaternion dv = new Quaternion(); final Quaternion nqRotation = new Quaternion(); Quaternion.mul(qRotation, qView, dv); qRotation.negate(nqRotation); Quaternion.mul(dv, nqRotation, qNewView); // Update the view information by adding the position to the resulting // quaternion. this.m_vView.x = this.m_vPosition.x + qNewView.x; this.m_vView.y = this.m_vPosition.y + qNewView.y; this.m_vView.z = this.m_vPosition.z + qNewView.z; }
public static FloatBuffer quarternionToMatrix(Quaternion q, FloatBuffer buffer) { //System.out.println(q); //if(q.length() != 0) // System.out.println("n:" + q.normalise()); // System.out.println("Matrix before:"); // System.out.println(String.format("%f %f %f %f", buffer.get(0), buffer.get(1), buffer.get(2), buffer.get(3))); // System.out.println(String.format("%f %f %f %f", buffer.get(4), buffer.get(5), buffer.get(6), buffer.get(7))); // System.out.println(String.format("%f %f %f %f", buffer.get(8), buffer.get(9), buffer.get(10), buffer.get(11))); // System.out.println(String.format("%f %f %f %f", buffer.get(12), buffer.get(13), buffer.get(14), buffer.get(15))); //System.out.println(q); // q = new Quaternion(0.5F, -0.1F, 0.4F, 0.8F); // q.normalise(); FloatBuffer buffer2; float[] rotationMatrixF = new float[]{1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; ByteBuffer vbb = ByteBuffer.allocateDirect(rotationMatrixF.length*4); vbb.order(ByteOrder.nativeOrder()); buffer2 = vbb.asFloatBuffer(); buffer2.put(buffer); buffer.position(0); buffer2.position(0); float xx2=q.x*q.x*2; float yy2=q.y*q.y*2; float zz2=q.z*q.z*2; float xy2 = q.x*q.y*2; float xz2 = q.x*q.z*2; float yz2 = q.y*q.z*2; float wx2 = q.w*q.x*2; float wy2 = q.w*q.y*2; float wz2 = q.w*q.z*2; // buffer2.position(0); // buffer2.put(1-yy2-zz2); // buffer2.put(xy2+wz2); // buffer2.put(xz2-wy2); // buffer2.put(0); // buffer2.put(xy2-wz2); // buffer2.put(1-xx2-zz2); // buffer2.put(yz2+wx2); // buffer2.put(0); // buffer2.put(xz2+wy2); // buffer2.put(yz2-wx2); // buffer2.put(1-xx2-yy2); // buffer2.put(0); // buffer2.position(0); buffer2.position(0); buffer2.put(1-yy2-zz2); buffer2.put(xy2-wz2); buffer2.put(xz2+wy2); buffer2.put(0); buffer2.put(xy2+wz2); buffer2.put(1-xx2-zz2); buffer2.put(yz2-wx2); buffer2.put(0); buffer2.put(xz2-wy2); buffer2.put(yz2+wx2); buffer2.put(1-xx2-yy2); buffer2.put(0); buffer2.position(0); // System.out.println("Matrix after:"); // System.out.println(String.format("%f %f %f %f", buffer2.get(0), buffer2.get(1), buffer2.get(2), buffer2.get(3))); // System.out.println(String.format("%f %f %f %f", buffer2.get(4), buffer2.get(5), buffer2.get(6), buffer2.get(7))); // System.out.println(String.format("%f %f %f %f", buffer2.get(8), buffer2.get(9), buffer2.get(10), buffer2.get(11))); // System.out.println(String.format("%f %f %f %f", buffer2.get(12), buffer2.get(13), buffer2.get(14), buffer2.get(15))); return buffer; }
public static void rotate(Quaternion p_187444_0_) { multMatrix(quatToGlMatrix(BUF_FLOAT_16, p_187444_0_)); }
public void setRotation(Quaternion q){ rotation=q; markDirty(); }
public Quaternion getRotation(){ return rotation; }
public NodeFieldAnimatorQuaternion() { super(new Quaternion()); }
public static Matrix4f setTRS(Matrix4f matrix, Vector3f translation, Quaternion quaternion, Vector3f scale) { quaternion = quaternion.normalise(new Quaternion()); return setTRS(matrix, translation.getX(), translation.getY(), translation.getZ(), quaternion.getX(), quaternion.getY(), quaternion.getZ(), quaternion.getW(), scale.getX(), scale.getY(), scale.getZ()); }
@Override public Quaternion deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { float[] array = context.deserialize(json, float[].class); return new Quaternion(array[0], array[1], array[2], array[3]); }