【发布时间】:2019-09-09 17:22:18
【问题描述】:
当我使用旋转矩阵围绕 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);
四:
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));
我希望对象在原地旋转而不是围绕它的某些边缘旋转。
【问题讨论】:
-
我已经更新了帖子中Quad的代码。
-
好的。您的三角形以 z 轴为中心。那你为什么要使用那些翻译矩阵呢?它们基本上将旋转原点从世界中心移开。
-
我正在尝试一些东西。我不太明白你的意思?谢谢你帮助我,你能告诉我我该怎么做才能理解吗?这不是我为学习 OpenGL 而创建的我自己的项目的作业。刚才我尝试只使用 createRotationMatrix 并且它也在做同样的事情。它绕着某个点旋转,它不像轮子一样在原地旋转。