gpt4 book ai didi

java - 如何修复在 OpenGL 中沿 x 或 y 轴旋转时奇怪的对象拉伸(stretch)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:25 24 4
gpt4 key购买 nike

当我在 openGL 中使用旋转矩阵旋转我的正方形时,当​​我想围绕 X 或 Y 轴旋转它时,整个对象消失并拉伸(stretch)。Z 轴工作正常。可能是什么问题?

我试过在 DrawArrays 之前加载 uniformMatrix4f 但它不起作用。仍然是同样的问题。翻译工作正常并且可以缩放。

渲染方法

public void render(Player model, Cube cube_model, StaticShader shader) {

GL30.glBindVertexArray(model.vaoID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);

shader.loadModelMatrix(Maths.createModelMatrix(new Vector3f(move_X,-Display.getHeight(),0f), 0f,0f,tilt_Y,1f));

GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);

GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(0);

GL30.glBindVertexArray(0);

GL30.glBindVertexArray(cube_model.vaoID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
shader.loadModelMatrix(Maths.createModelMatrix(new Vector3f(0f,Display.getHeight() - 200f,0f), 0f,increaseRotation,0f,0.5f));

GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);

increaseRotation += 1f;
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
}

矩阵数学:

public static Matrix4f createModelMatrix(Vector3f t, float rx, float ry, float rz, float s) {
Matrix4f modelMatrix = new Matrix4f();
modelMatrix.setIdentity();

Matrix4f.translate(t, modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), modelMatrix, modelMatrix);
Matrix4f.scale(new Vector3f(s,s,s), modelMatrix, modelMatrix);

return modelMatrix;
}

投影矩阵设置:

public StaticShader(String vertexShaderFileName, String fragmentShaderFileName) {
super(vertexShaderFileName, fragmentShaderFileName);

enableProgram();
loadProjectionMatrix(Maths.createOrthoMatrix(-Display.getWidth(), Display.getWidth(), -Display.getHeight(), Display.getHeight(), 0.1f, 1000));
disableProgram();
}

立方体顶点:

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
};

我希望立方体在 Y 轴上正常旋转,但它实际上在旋转并消失,然后它的一条线在中间出现拉伸(stretch)并消失。

最佳答案

I want to rotate it around X or Y axis the whole object disapears and stretches.Z axis is working fine.

当然。

您的对象是二维对象。它只是一个具有 2 个维度 x 和 y 的平面。 x 和 y 轴平行于视口(viewport)。 Z 轴指向视口(viewport)外。
如果您围绕 z 轴(在 xy 平面中)进行旋转,对象将保持其大小并旋转。
但是,如果您绕 x 轴旋转,则对象似乎会沿 y 轴拉伸(stretch)并分别以 90° 和 270° 的角度消失。如果您绕 y 轴旋转,则对象似乎会沿 x 轴拉伸(stretch)。

由于 Orthographic projection,围绕 x 和 y 轴的旋转看起来是“平的” .实际上你没有投影矩阵,所以场景是平行投影的(正交投影)。
如果你想“看到”3 维旋转,那么你必须使用 Perspective projection

关于java - 如何修复在 OpenGL 中沿 x 或 y 轴旋转时奇怪的对象拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com