当我使用旋转矩阵绕着Z轴旋转我的正方形时,实际上是围绕它绕着小圆圈,实际上并没有在原地旋转。我希望它像轮子一样旋转。
我尝试过先旋转然后平移,然后反之亦然,但是它不起作用。我读过一些人在网上问的问题,但所有问题都是在旧的OpenGL中。
我的矩阵代码:
public static Matrix4f createTranslateMatrix(Vector3f t) {
Matrix4f tM = new Matrix4f();
Matrix4f.translate(t, tM, tM);
return tM;
}
public static Matrix4f createRotateMatrixZ(float z) {
Matrix4f zM = new Matrix4f();
Matrix4f.rotate((float) Math.toRadians(z), new Vector3f(0,0,1), zM, zM);
return zM;
}
渲染代码:
GL30.glBindVertexArray(cube_model.vaoID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
Matrix4f translateM = Maths.createTranslateMatrix(new Vector3f(0.0f, Display.getWidth() - 200f, 0.0f));
Matrix4f rotateM = Maths.createRotateMatrixZ(increaseRotation);
Matrix4f translateM2 = Maths.createTranslateMatrix(new Vector3f(0.0f, 0.0f, 0.0f));
Matrix4f modelM = new Matrix4f();
modelM.setIdentity();
Matrix4f.mul(translateM2, rotateM, modelM);
Matrix4f.mul(modelM, translateM, modelM);
shader.loadModelMatrix(modelM);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);
increaseRotation += 1f;
if(increaseRotation == 360.0f) {
increaseRotation = 0;
}
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
Quad:
private static final float vertices[] = {
-Display.getWidth() / 5, 0.0f, 0.0f, 1.0f,1.0f,1.0f,1.0f,
Display.getWidth() / 5, 0.0f, 0.0f, 0.0f,1.0f,1.0f,1.0f,
-Display.getWidth() / 5, Display.getHeight() / 5, 0.0f, 1.0f,1.0f,1.0f,1.0f,
Display.getWidth() / 5, 0.0f, 0.0f, 1.0f,1.0f,1.0f,1.0f,
Display.getWidth() / 5, Display.getHeight() /5, 0.0f, 0.0f,1.0f,1.0f,1.0f,
-Display.getWidth() / 5, Display.getHeight() / 5, 0.0f, 1.0f,1.0f,1.0f,1.0f
};
正交矩阵:
public static Matrix4f createOrthoMatrix(final float l, final float r, final float b, final float t, final float n, final float f ) {
Matrix4f orthoM = new Matrix4f();
orthoM.m00 = 2 / (r - l);
orthoM.m03 = -(r+l)/(r-l);
orthoM.m11 = 2/(t-b);
orthoM.m13 = -(t+b)/(t-b);
orthoM.m22 = -2/(f-n);
orthoM.m23 = -(f+n)/(f-n);
orthoM.m33 = 1;
return orthoM;
}
正交矩阵初始化:
loadProjectionMatrix(Maths.createOrthoMatrix(-Display.getWidth(), Display.getWidth(), -Display.getHeight(), Display.getHeight(), 0.1f, 1000));
我希望对象向内旋转而不是围绕其某些边缘旋转。
答案 0 :(得分:0)
旋转始终围绕坐标系的原点发生。因此,您首先必须将几何图形平移到原点,然后旋转然后平移回去。