【问题标题】:trouble with entity coordinates with lwjgllwjgl的实体坐标问题
【发布时间】:2019-05-03 12:34:14
【问题描述】:

我在 OpenGL 上下文中移动实体时遇到了问题: 当我尝试放置一个实体时,位置似乎是正确的,但是当实体开始移动时,一切都出错了,并且碰撞不起作用。我是 OpenGL 新手,我怀疑我的世界矩阵或模型矩阵有误。

这是顶点着色器的代码:

#version 330 core

layout (location=0) in vec3 position;

out vec3 extColor;

uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;
uniform vec3 inColor;
void main()
{
    gl_Position = projectionMatrix * modelMatrix  * vec4(position, 1.0);
    extColor = inColor;
}

这是计算大部分矩阵的类:

public class Transformations {
    private Matrix4f projectionMatrix;
    private  Matrix4f modelMatrix;

    public Transformations() {
        projectionMatrix = new Matrix4f();
        modelMatrix = new Matrix4f();
    }

    public final Matrix4f getOrthoMatrix(float width, float height, float zNear, float zFar) {
        projectionMatrix.identity();
        projectionMatrix.ortho(0.0f, width, 0.0f, height, zNear, zFar);
        return projectionMatrix;
    }

    public Matrix4f getModelMatrix(Vector3f offset, float angleZ, float scale) {
        modelMatrix.identity().translation(offset).rotate(angleZ, 0, 0, 0).scale(scale);
        return modelMatrix;
    }
}

这是碰撞测试:

 public boolean isIn(Pos p) {
        return (p.getX() >= this.pos.getX() &&
                p.getX() <= this.pos.getX() + DIMENSION)
                && (p.getY() >= this.pos.getY() &&
                p.getY() <= this.pos.getY() + DIMENSION);
}

此外,还有一个指向 github 项目的链接:https://github.com/ShiroUsagi-san/opengl-engine。

我真的是 OpenGL 3 的新手,所以我可能会犯一些非常大的错误。

我也将 i3 作为 WM 运行,我真的不知道这是否会导致此类问题。

【问题讨论】:

  • 确实是错字,已修正,谢谢。
  • 旋转轴必须为 (0, 0, 1)。换成.rotate(angleZ, 0, 0, 1)后,问题解决了吗?
  • 绝对不是。我不认为这是相关的。

标签: java lwjgl opengl-3


【解决方案1】:

在考虑了 openGL 和 VBO 的工作原理后,我解决了这些问题:确实,我为每个实体设置了一个新的引用,所以我不得不改变这一行

Mesh fourmiMesh = MeshBuilder.buildRect(this.position.getX(), this.position.getY(), 10, 10);

到

Mesh fourmiMesh = MeshBuilder.buildRect(0, 0, 10, 10);

我在 VBO 中的顶点位置和我的世界中的位置之间造成了混淆。 希望误解能帮助大家理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多