如何在 LWJGL 中将浮点数组传递给着色器
How to pass an array of float to Shader in LWJGL
我一直在关注 LWJGL 网站 (http://wiki.lwjgl.org/wiki/GLSL_Tutorial:_Communicating_with_Shaders.html) to try to send an array of random floats to my fragment shader, but every value in the shader was 0. I tried copy-pasting the "arrayOfInts" example to try and isolate the problem, changing everything to be ints, but I still only get zeros shader-side. According to the documentation ( https://javadoc.lwjgl.org/org/lwjgl/opengl/GL20.html#glUniform1iv(int,int%5B%5D) ) 上的教程,函数 glUniform1iv
存在并且可以满足我的需要,但是当我尝试它时 Eclipse 告诉我它不存在于class GL20,当使用 int[]
或 IntBuffer
时,同样适用于 glUniform1fv
。统一变量的位置为 0,因此已正确加载。存储在生成的 java 数组中的值是正确的。
顶点着色器:
#version 400 core
in vec3 position;
out vec2 uvPosition;
void main(void){
gl_Position = vec4(position, 1.0);
uvPosition = (position.xy+vec2(1.0, 1.0))/2;
}
片段着色器
#version 400 core
in vec2 uvPosition;
out vec4 outColor;
uniform int random[50];
int randomIterator = 0;
float randf(){
return random[randomIterator++];
}
void main(void){
outColor = vec4(random[0]/10, 1.0, 1.0, 1.0);
}
Java 我加载制服的代码:
final int RAND_AMOUNT = 50;
IntBuffer buffer = BufferUtils.createIntBuffer(RAND_AMOUNT);
int[] array = new int[RAND_AMOUNT];
for(int i = 0; i < RAND_AMOUNT; i++) {
buffer.put((int)(Math.random()*9.999f));
array[i] = (int)(Math.random()*9.999f);
}
buffer.rewind();
System.out.println(buffer.get(0));
GL20.glUniform1(glGetUniformLocation(programID, "random"), buffer); //In this line, using GL20.glUniform1iv tells me it doesn't exist. Same with
没有任何错误,显示为青色,这意味着红色分量为 0。任何帮助生成随机整数或直接发送随机浮点数的帮助都会有所帮助。欢迎提问。
glUniform
* sets a value of a uniform in the default uniform block of the currently installed program. Thus the program has to be installed by glUseProgram
之前。
int random_loc = GL20.glGetUniformLocation(programID, "random")
GL20.glUseProgram(programID);
GL20.glUniform1(random_loc, buffer);
我一直在关注 LWJGL 网站 (http://wiki.lwjgl.org/wiki/GLSL_Tutorial:_Communicating_with_Shaders.html) to try to send an array of random floats to my fragment shader, but every value in the shader was 0. I tried copy-pasting the "arrayOfInts" example to try and isolate the problem, changing everything to be ints, but I still only get zeros shader-side. According to the documentation ( https://javadoc.lwjgl.org/org/lwjgl/opengl/GL20.html#glUniform1iv(int,int%5B%5D) ) 上的教程,函数 glUniform1iv
存在并且可以满足我的需要,但是当我尝试它时 Eclipse 告诉我它不存在于class GL20,当使用 int[]
或 IntBuffer
时,同样适用于 glUniform1fv
。统一变量的位置为 0,因此已正确加载。存储在生成的 java 数组中的值是正确的。
顶点着色器:
#version 400 core
in vec3 position;
out vec2 uvPosition;
void main(void){
gl_Position = vec4(position, 1.0);
uvPosition = (position.xy+vec2(1.0, 1.0))/2;
}
片段着色器
#version 400 core
in vec2 uvPosition;
out vec4 outColor;
uniform int random[50];
int randomIterator = 0;
float randf(){
return random[randomIterator++];
}
void main(void){
outColor = vec4(random[0]/10, 1.0, 1.0, 1.0);
}
Java 我加载制服的代码:
final int RAND_AMOUNT = 50;
IntBuffer buffer = BufferUtils.createIntBuffer(RAND_AMOUNT);
int[] array = new int[RAND_AMOUNT];
for(int i = 0; i < RAND_AMOUNT; i++) {
buffer.put((int)(Math.random()*9.999f));
array[i] = (int)(Math.random()*9.999f);
}
buffer.rewind();
System.out.println(buffer.get(0));
GL20.glUniform1(glGetUniformLocation(programID, "random"), buffer); //In this line, using GL20.glUniform1iv tells me it doesn't exist. Same with
没有任何错误,显示为青色,这意味着红色分量为 0。任何帮助生成随机整数或直接发送随机浮点数的帮助都会有所帮助。欢迎提问。
glUniform
* sets a value of a uniform in the default uniform block of the currently installed program. Thus the program has to be installed by glUseProgram
之前。
int random_loc = GL20.glGetUniformLocation(programID, "random")
GL20.glUseProgram(programID);
GL20.glUniform1(random_loc, buffer);