java - OpenGL正方形拉伸(stretch)而不是移动

标签 java opengl lwjgl opengl-4

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

当我尝试平移正方形时,它最终没有按预期移动,而是拉伸(stretch)了。

enter image description here

LWJGL 没有math类,所以我必须创建自己的 Matrix 和 TransformationMatrix 类。我将它们组合成一个片段,以减少这篇文章的大小。

我的假设是错误位于 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()添加到m03的方法, m13 ,和m23而不是m30 , m31 ,和m32分别使正方形开始向上平移、缩小、然后开始向下平移

enter image description here

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

在渲染每一帧之前调用

最佳答案

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

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

m03m13m23 将是您想要翻译 xy 的位置分别为 z 值。要平移 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;

我不知道你的翻译函数在做什么。看起来您正在创建一个平移矩阵并将其乘以当前矩阵,这使得您的代码极其不可读。事实上,这不是必需的,因为您的代码已经返回一个矩阵,这意味着这种用法:

playerPosition = translate(playerPosition);

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

关于java - OpenGL正方形拉伸(stretch)而不是移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476457/

相关文章:

java - 是什么导致我的 LWJGL 程序帧速率低?

java - 如何防止 maven- assembly-plugin 以疯狂的方式从 dependencySet 复制 natives-* 分类器

java - 在java中绘制图像的奇怪行为

java - 在复杂对象中收集数据的良好设计模式

java - 在泛型类中使用 equals

c - 将 3d 模型文件(3ds/obj/任何开放格式)转换为 C 数组的工具

java - Hibernate HQL Count Distinct 不起作用?

c++ - 在 OpenGL 中绘制对象数组

opengl - 我想使用 openGL 制作一个简单的 3D 游戏,我应该从哪里开始?

java - 使用 NetBeans 编译 Java LWJGL 项目(org.lwjgl 包不存在)