OpenGL 方形拉伸而不是移动

OpenGL square stretches instead of moving

我正在尝试在 Java 中使用 LWJGL3(Open GL 4.1,NVIDIA-10.8.14)创建游戏引擎,但我似乎 运行 遇到了一个*小*问题...

当我尝试平移正方形时,它并没有像预期的那样实际移动,而是拉伸了。

LWJGL 没有 math class,所以我不得不创建自己的 Matrix 和 TransformationMatrix classes。我已将它们合并为一个片段以减小此 post.

的大小

我的假设是错误出在 TransformationMatrix 的 translate() 函数中的某处

public TransformationMatrix translate(Vector3f vector){
    super.m30 += ((super.m00 * vector.getX()) + (super.m10 * vector.getY()) + (super.m20 * vector.getZ()));
    super.m31 += ((super.m01 * vector.getX()) + (super.m11 * vector.getY()) + (super.m21 * vector.getZ()));
    super.m32 += ((super.m02 * vector.getX()) + (super.m12 * vector.getY()) + (super.m22 * vector.getZ()));
    super.m33 += ((super.m03 * vector.getX()) + (super.m13 * vector.getY()) + (super.m23 * vector.getZ()));

    return this;
}

我试过它,但我没能从中得到任何积极的结果。

public class Matrix4f {

    public float 
        m00, m01, m02, m03,
        m10, m11, m12, m13,
        m20, m21, m22, m23,
        m30, m31, m32, m33;

    public FloatBuffer store(FloatBuffer buffer){
        buffer.put(this.m00);
        buffer.put(this.m10);
        buffer.put(this.m20);
        buffer.put(this.m30);
        buffer.put(this.m01);
        buffer.put(this.m11);
        buffer.put(this.m21);
        buffer.put(this.m31);
        buffer.put(this.m02);
        buffer.put(this.m12);
        buffer.put(this.m22);
        buffer.put(this.m32);
        buffer.put(this.m03);
        buffer.put(this.m13);
        buffer.put(this.m23);
        buffer.put(this.m33);

        return buffer;
    }


    ////////////////////////////
    //                        //
    //  TRANSFORMATION MATRIX //
    //                        //
    ////////////////////////////   


    public class TransformationMatrix extends Matrix4f{

    public TransformationMatrix(Vector3f translation){
        this.setIdentity();
        this.translate(translation);
    }

    public TransformationMatrix setIdentity(){
        super.m00 = 1.0f;
        super.m01 = 0.0f;
        super.m02 = 0.0f;
        super.m03 = 0.0f;
        super.m10 = 0.0f;
        super.m11 = 1.0f;
        super.m12 = 0.0f;
        super.m13 = 0.0f;
        super.m20 = 0.0f;
        super.m21 = 0.0f;
        super.m22 = 1.0f;
        super.m23 = 0.0f;
        super.m30 = 0.0f;
        super.m31 = 0.0f;
        super.m32 = 0.0f;
        super.m33 = 1.0f;

        return this;
    }

    public TransformationMatrix translate(Vector3f vector){
            super.m30 += ((super.m00 * vector.getX()) + (super.m10 * vector.getY()) + (super.m20 * vector.getZ()));
            super.m31 += ((super.m01 * vector.getX()) + (super.m11 * vector.getY()) + (super.m21 * vector.getZ()));
            super.m32 += ((super.m02 * vector.getX()) + (super.m12 * vector.getY()) + (super.m22 * vector.getZ()));
            super.m33 += ((super.m03 * vector.getX()) + (super.m13 * vector.getY()) + (super.m23 * vector.getZ()));

            return this;
    }
}

我的顶点着色器包含

#version 400 core

in vec3 position;
in vec2 texture;
out vec2 texture_coords;
uniform mat4 transformationMatrix;

void main(void){
    gl_Position = transformationMatrix * vec4(position, 1.0);
    texture_coords = texture;
}

片段着色器包含

#version 400 core

//Variables...

void main(void){
    out_color = texture(textureSampler, texture_coords);
} 

使用以下点和索引渲染正方形

//stored in the vertex shader's "position"
float[] positions = {
    -0.5f,0.5f,0.0f,    
    -0.5f,-0.5f,0.0f,   
    0.5f,-0.5f,0.0f,    
    0.5f,0.5f,0.0f,     
};

//bound using
//
//int id = GL15.glGenBuffers();
//GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, id);
//GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, toIntBuffer(indices), GL15.GL_STATIC_DRAW);
int[] indices = {
    0,1,3,  
    3,1,2,
};

//stored in the vertex shader's "texture"
float[] textureCoords = {
    0,0,
    0,1,
    1,1,
    1,0,
};

然后使用 TransformationMatrix.translate() 翻译(在上面的 gif 中,它由 <0.0f, 0.01f, 0.0f> 翻译)。正方形使用

渲染
public void render(){
    GL30.glBindVertexArray(modelID);
    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);


    //position begins at <0, 0, 0>, and is incremented by <0, 0.01f, 0>
    //every frame in the above gif
    TransformationMatrix matrix = new TransformationMatrix(
            position
    );

    //load the transformation matrix to the vertex shader
    FloatBuffer buffer = matrix.store(BufferUtils.createFloatBuffer(16));
    buffer.flip();
    //location being the location of the "transformationMatrix" in the
    //vertex shader
    GL20.glUniformMatrix4fv(location, false, buffer);

    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID));
    //vertexCount is indices.length (6)
    GL11.glDrawElements(GL11.GL_TRIANGLES, vertexCount, GL11.GL_UNSIGNED_INT, 0); 

    GL20.glDisableVertexAttribArray(1);
    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
}

到目前为止,我已经尝试使用 TransformationMatrix.translate(),并且我已经双重、三重和四重检查了我是否拥有 correct code in these classes.

我注意到的一件事是更改 translate() 方法以添加到 m03m13m23 而不是 m30m31m32分别使正方形开始向上平移,缩小,然后开始向下平移

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

在渲染每一帧之前调用

更多的是与你的矩阵有关。请记住,3D 中的平移应该是其 x、y 和 z 值中第一行的第 4 列。您不止一次地输入每个值,这当然会改变翻译。您使转换过于复杂;您需要做的就是获取第 4 列、第 1 行、第 2 行和第 3 行矩阵的位置,然后分别根据 x、y 和 z 值递增它们的值。使用你的矩阵:

    m00, m01, m02, m03,
    m10, m11, m12, m13,
    m20, m21, m22, m23,
    m30, m31, m32, m33;

m03m13m23 将分别是您希望翻译 xyz 值的位置。要在 x 中翻译 100,在 y 中翻译 200,在 z 中翻译 300,您可以这样做:

    1.0, 0.0, 0.0, 100,
    0.0, 1.0, 0.0, 200,
    0.0, 0.0, 1.0, 300,
    0.0, 0.0, 0.0, 1.0;

你的translation功能是做什么的,我不知道。看起来你正在创建一个翻译矩阵并将其乘以当前,这使得你的代码非常难以阅读。事实上,这不是必需的,因为您的代码已经 returns 一个矩阵,这意味着这种用法:

playerPosition = translate(playerPosition);

我没有发现您的渲染代码或着色器有任何问题。因此,它必须与您的矩阵有关。这只是建立你的主要地位的一个例子。请记住,OpenGL 会读取所有列 major。所以,这意味着 OpenGL 将读取 m00 然后 m10 然后 m20 等等。它会根据这些值创建自己的矩阵。