【问题标题】:GLSL Matrix Translation Leaves Blank Screen?GLSL 矩阵翻译留下空白屏幕?
【发布时间】:2014-09-03 15:24:05
【问题描述】:

我有一个 matrix4f,我使用统一变量从我的 ShaderProgram 类传递到我的顶点着色器类。该矩阵应该充当顶点的平移。下面是矩阵的样子

1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1

当我将该变量(称为“测试”)乘以顶点(称为 gl_Vertex)时,什么都看不到,它只会留下一个空白屏幕。这只发生在我将它乘以统一变量“test”时,如果我将它乘以具有相同值的新 matrix4f,它会正常工作。如果我使用向量统一变量而不是矩阵,它会按预期工作。

我是否正确地将变量传递给 GLSL 顶点着色器类?如果是这样,为什么我的四边形没有出现在屏幕上?

这是我的顶点着色器

#version 400 core

uniform vec4 translation;
uniform vec4 size;
uniform vec4 rotation;
uniform mat4 test;

in vec2 textureCoords;
in vec3 position;
out vec2 pass_textureCoords;

void main(void){
    //pass texture cords
    pass_textureCoords = textureCoords;

    //This works by multiplying by identity matrix
    //gl_Position = mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) * gl_Vertex;

    //This works by passing vec4's not matrix4
    /*gl_Position = vec4(((gl_Vertex.x + translation.x)*size.x),
                        ((gl_Vertex.y + translation.y)*size.y),
                        ((gl_Vertex.z + translation.z)*size.z),
                        ((gl_Vertex.w + translation.w)*size.w)
                        );*/

    //this leaves a blank window
    gl_Position = test * gl_Vertex;

}

这就是我声明统一变量位置的方式:

translationLocation = GL20.glGetUniformLocation(programID, "translation");
sizeLocation = GL20.glGetUniformLocation(programID, "size");
rotationLocation = GL20.glGetUniformLocation(programID, "rotation");
textureLocation = GL20.glGetUniformLocation(programID, "textureSampler");
testMat = GL20.glGetUniformLocation(programID, "test");

这就是我渲染统一变量的方式

public void start(){
    GL20.glUseProgram(programID);

    Vector4f translation = offset.getTranslation();
    Vector4f size = offset.getSize();
    Vector4f rotation = offset.getRotation();

    GL20.glUniform4f(translationLocation, translation.x, translation.y, translation.z,      translation.w);
    GL20.glUniform4f(sizeLocation, size.x, size.y, size.z, size.w);
    GL20.glUniform4f(rotationLocation, rotation.x, rotation.y, rotation.z, rotation.w);

    FloatBuffer buff = BufferUtils.createFloatBuffer(16);
    offset.getTestTranslation().storeTranspose(buff);
    GL20.glUniformMatrix4(testMat, false, buff);

    GL20.glUniform1i(textureLocation, 0);
}

这就是我在将变量传递给 GLSL 之前声明变量的方式

Vector4f translation;
Vector4f size;
Vector4f rotation;
Matrix4f testTranslation;

public Offset(){
    translation = new Vector4f(0, 0, 0, 0);
    size = new Vector4f(1, 1, 1, 1);
    rotation = new Vector4f(0, 0 , 0, 0);
    testTranslation = new Matrix4f();
    testTranslation.translate(new Vector3f(0,0,0));
}

【问题讨论】:

  • 请不要链接到 Pastebin。那里的资源不会持续存在,所以有一天,当资源不可用时,SO 用户将无法看到您附加的内容。直接在问题中发布代码中最相关的部分。
  • 我很抱歉,但它已被删除,我在以正确格式粘贴它时遇到了一些问题。已修复,谢谢

标签: java opengl glsl lwjgl matrix-multiplication


【解决方案1】:

嗯,原来我是用下面的方法把matrix4f转成floatBuffer

matrix4f.storeTranspose(buff)

当显然没有正确地将矩阵存储到浮点缓冲区中时。我现在使用这种方法在渲染着色器程序时将矩阵发送到顶点着色器

public void setMatrixArray(boolean transposed, Matrix4f[] matrices){

    FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16*matrices.length);
    for(int i = 0; i<matrices.length; i++)  {
        matrices[i].store(matrixBuffer);
    }
    matrixBuffer.flip();
    GL20.glUniformMatrix4(testMat,transposed,matrixBuffer);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多