Ich habe schon lange nach dem Fehler gesucht und ihn nicht gefunden. Wenn ich das Objekt mit einer Rotationsmatrix drehe, wird es dabei bei der Rotation schmaler und dann wieder breiter und zeigt die Rückseite gar nicht.
Matrix4f:
Draw:
Matrix4f:
Java:
public Matrix4f negate() {
return multiply(-1);
}
public Matrix4f substract(Matrix4f other) {
return add(other.negate());
}
public Matrix4f identity() {
this.setIdentity();
return this;
}
public Matrix4f multiply(float f) {
Matrix4f res = new Matrix4f();
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
res.matrix[i][j] = this.matrix[i][j] * f;
}
}
return res;
}
public Matrix4f multiply(Matrix4f other) {
Matrix4f res = new Matrix4f();
for(int row = 0; row < 4; row++) {
for(int column = 0; column < 4; column++) {
for(int otherColumn = 0; otherColumn < 4; otherColumn++) {
res.matrix[row][otherColumn]
+= this.matrix[row][column] * other.matrix[column][otherColumn];
}
}
}
return res;
}
public Matrix4f transpose() {
Matrix4f res = new Matrix4f();
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
res.matrix[j][i] = this.matrix[i][j];
}
}
return res;
}
public Matrix4f perspective(float fov, float aspect, float zNear, float zFar) {
return multiply(getPerspectiveMatrix(fov, aspect, zNear, zFar));
}
public Matrix4f translate(Vector3f offset) {
return multiply(getTranslateMatrix(offset.x, offset.y, offset.z));
}
public Matrix4f rotate(float angle, Vector3f pos) {
return multiply(getRotationMatrix(angle, pos.x, pos.y, pos.z));
}
public Matrix4f rotate(Vector3f rotation) {
return multiply(getRotationMatrixX(rotation.x)).
multiply(getRotationMatrixY(rotation.y)).
multiply(getRotationMatrixZ(rotation.z));
}
public Matrix4f scale(float scale) {
return multiply(getScaleMatrix(scale, scale, scale));
}
public Matrix4f scale(Vector3f scale) {
return multiply(getScaleMatrix(scale.x, scale.y, scale.z));
}
public static Matrix4f getWorldMatrix(Vector3f offset, Vector3f rotation, float scale) {
return new Matrix4f().translate(offset).rotate(rotation).scale(scale);
}
public static Matrix4f getPerspectiveMatrix(float fov, float aspect, float zNear, float zFar) {
Matrix4f ret = new Matrix4f();
float f = (float) (1f/(Math.tan(Math.toRadians(fov)/2)));
ret.matrix[0][0] = f/aspect;
ret.matrix[1][1] = f;
ret.matrix[2][2] = (zNear+zFar)/(zNear-zFar);
ret.matrix[3][2] = -1f;
ret.matrix[2][3] = -(2f*zNear*zFar)/(zFar-zNear);
ret.matrix[3][3] = 0;
return ret;
}
public static Matrix4f getTranslateMatrix(float x, float y, float z) {
Matrix4f ret = new Matrix4f();
ret.matrix[0][3] = x;
ret.matrix[1][3] = y;
ret.matrix[2][3] = z;
System.out.println("translate");
System.out.println(ret);
return ret;
}
public static Matrix4f getRotationMatrix(float angle, float x, float y, float z) {
Matrix4f rotation = new Matrix4f();
float c = (float) Math.cos(Math.toRadians(angle));
float s = (float) Math.sin(Math.toRadians(angle));
Vector3f vec = new Vector3f(x, y, z);
if (vec.length() != 1f) {
vec.normalize();
x = vec.x;
y = vec.y;
z = vec.z;
}
rotation.matrix[0][0] = x * x * (1f - c) + c;
rotation.matrix[1][0] = y * x * (1f - c) + z * s;
rotation.matrix[2][0] = x * z * (1f - c) - y * s;
rotation.matrix[0][1] = x * y * (1f - c) - z * s;
rotation.matrix[1][1] = y * y * (1f - c) + c;
rotation.matrix[2][1] = y * z * (1f - c) + x * s;
rotation.matrix[0][2] = x * z * (1f - c) + y * s;
rotation.matrix[1][2] = y * z * (1f - c) - x * s;
rotation.matrix[2][2] = z * z * (1f - c) + c;
return rotation;
}
public static Matrix4f getRotationMatrixX(float rotation) {
Matrix4f ret = new Matrix4f();
rotation = (float) Math.toRadians(rotation);
float sin = (float) Math.sin(rotation);
float cos = (float) Math.cos(rotation);
ret.matrix[1][1] = cos;
ret.matrix[1][2] = -sin;
ret.matrix[2][1] = sin;
ret.matrix[2][2] = cos;
return ret;
}
public static Matrix4f getRotationMatrixY(float rotation) {
Matrix4f ret = new Matrix4f();
rotation = (float) Math.toRadians(rotation);
float sin = (float) Math.sin(rotation);
float cos = (float) Math.cos(rotation);
ret.matrix[0][0] = cos;
ret.matrix[0][2] = sin;
ret.matrix[2][0] = -sin;
ret.matrix[2][2] = cos;
return ret;
}
public static Matrix4f getRotationMatrixZ(float rotation) {
Matrix4f ret = new Matrix4f();
rotation = (float) Math.toRadians(rotation);
float sin = (float) Math.sin(rotation);
float cos = (float) Math.cos(rotation);
ret.matrix[0][0] = cos;
ret.matrix[0][1] = -sin;
ret.matrix[1][0] = sin;
ret.matrix[1][1] = cos;
return ret;
}
public static Matrix4f getScaleMatrix(float x, float y, float z) {
Matrix4f ret = new Matrix4f();
ret.matrix[0][0] = x;
ret.matrix[1][1] = y;
ret.matrix[2][2] = z;
return ret;
}
public static Matrix4f getViewMatrix(Vector3f pos, Vector3f rot) {
Matrix4f viewMatrix = new Matrix4f();
viewMatrix = viewMatrix.rotate((float) Math.toRadians(rot.x), new Vector3f(1, 0, 0))
.rotate((float) Math.toRadians(rot.y), new Vector3f(0, 1, 0));
return viewMatrix.translate(new Vector3f(-pos.x, -pos.y, -pos.z));
}
public static Matrix4f getModelViewMatrix(Vector3f pos, Vector3f rot, float scale, Matrix4f viewMatrix) {
Matrix4f modelViewMatrix = new Matrix4f();
rot = rot.negate();
modelViewMatrix = modelViewMatrix.translate(pos).rotate(rot).scale(scale);
Matrix4f viewCurr = new Matrix4f(viewMatrix);
System.out.println("modelViewMatrix");
System.out.println(modelViewMatrix);
System.out.println("rot");
System.out.println(rot);
return viewCurr.multiply(modelViewMatrix);
//return modelViewMatrix;
}
Code:
public void draw() {
clearWindow();
shader.start();
shader.setUniform("projectionMatrix", projectionMatrix);
shader.setUniform("textureSampler", 0);
Matrix4f viewMatrix = Matrix4f.getViewMatrix(camera.getPos(), camera.getRotation());
for(GLObject object: objects) {
shader.setUniform("modelViewMatrix", Matrix4f.getModelViewMatrix(object.getPosition(), object.getRotation(), object.getScale(), viewMatrix));
object.getMesh().render(object);
}
shader.stop();
}