我有一个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));
}
答案 0 :(得分:0)
好吧,事实证明我使用以下方法将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);
}