gpt4 book ai didi

java - opengl : loading array types to uniform buffer objects

转载 作者:行者123 更新时间:2023-12-04 08:28:13 24 4
gpt4 key购买 nike

这是我在片段着色器中的统一块

layout (std140)   uniform Material
{
float array[2];
};
这是统一块对象。我正在使用 LWJGL 2.9.3
  private UBO(int programID,String blockName)
{
blockID=GL31.glGetUniformBlockIndex(programID,blockName); //Get index of uniform block

IntBuffer indices=BufferUtils.createIntBuffer(16);
GL31.glGetActiveUniformBlock(programID,blockID,GL31.GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES,indices); //query all the indices of every variable in the uniform block
indices.flip().limit(GL31.glGetActiveUniformBlocki(programID,blockID,GL31.GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS)); //the above method requires an int buffer of 16 even if i dont have 16 variables, but after retriving indices limit the buffer to read only actual number of variables
while(indices.hasRemaining())
{
int variableIndex=indices.get();

String name=GL31.glGetActiveUniformName(programID
,variableIndex
,GL31.glGetActiveUniformsi(programID,variableIndex,GL31.GL_UNIFORM_NAME_LENGTH)); //get the name of the variable

int offset=GL31.glGetActiveUniformsi(programID,variableIndex,GL31.GL_UNIFORM_OFFSET); //query the offset[will use it in buffer sub data]

offsets.put(name,offset);
}


GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER,bufferID=GL15.glGenBuffers());

GL15.glBufferData(GL31.GL_UNIFORM_BUFFER
,GL31.glGetActiveUniformBlocki(programID,blockID,GL31.GL_UNIFORM_BLOCK_DATA_SIZE)
,GL15.GL_DYNAMIC_DRAW); //allocate buffer to the actual size of the uniform block

GL15.glBindBuffer(GL31.GL_UNIFORM_BUFFER,0);
}

private void bufferData(String name,Buffer value)
{
int offset=offsets.get(name); //update float or int arrays
if(value instanceof FloatBuffer){GL15.glBufferSubData(GL31.GL_UNIFORM_BUFFER,offset,(FloatBuffer)value);}
else{GL15.glBufferSubData(GL31.GL_UNIFORM_BUFFER,offset,(IntBuffer)value);}
}
对于常规的非数组变量,它工作正常,但对于数组,浮点数和每个数组类型的值都加载不正确。我已经阅读了文档,指定数组中的每个元素都有 16[vec4 size] 对齐,但我不知道如何在我的代码中指定
更新浮点数组我只是使用
bufferData("array[0]",BufferUtils.createFloatBuffer(2).put(new float[]{10.5f,3.0f}).flip());
//the array[0] dosen't actually refer to only the first element of the array it is the name of the array returned by GL31.glGetActiveUniformName()
但同样,浮点数都加载不正确。

最佳答案

I have read the docs specifying that every element in the array has an 16[vec4 size] alignment but i don't know how to specify that in my code [...]


你是对的,你必须为每个元素添加 3 个浮点数来填充:
BufferUtils.createFloatBuffer(8).put(new float[]{10.5f,0.0f,0.0f,0.0f,3.0f,0.0f,0.0f,0.0f}).flip()
但是,我建议使用单个 vec2而不是 float array[2] :
layout (std140)   uniform Material
{
vec2 array;
};
注意,在 GLSL 中,可以使用 Swizzling 访问 vector 数据类型的组件。或索引运算符(例如 array[0]array[1] )。

关于java - opengl : loading array types to uniform buffer objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65159329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com