我正在尝试理解GLSL是如何工作的,所以我一直关注youtube上的一个人:thebennnybox。我坚持为我的程序设置制服。首先,这是我正在使用的2个着色器。
顶点着色器:
#version 430
layout (location = 0) in vec3 position;
out vec4 color;
uniform float uniformFloat;
void main(){
color = vec4(clamp(position, 0.0, uniformFloat), 1.0);
gl_Position = vec4(0.25 * position, 1.0);
}
片段着色器:
#version 430
in vec4 color;
out vec4 fragColor;
void main(){
fragColor = color;
}
它们都很简单,但我似乎无法将uniformFloat变量设置为任何东西。 将这两个着色器链接并验证到着色器程序后。我立即尝试将1.0f的值设置为uniformFloat(仅用于测试目的)......当我这样做时,我只是得到一个黑色的屏幕上没有任何东西。如果我用1.0f替换uniformFloat(在着色器本身中),我将得到我所需要的。
下面列出了添加和设置制服的代码:
public void addUniform(String uniform) {
int uniformLocation = glGetUniformLocation(shaderProgram, uniform);
if(uniformLocation == -1)
{
System.err.println("Error: Could not find uniform: " + uniform);
new Exception().printStackTrace();
System.exit(1);
}
uniforms.put(uniform, uniformLocation);
}
public void setUniformf(String uniformName, float value){
glUniform1f(uniforms.get(uniformName), value);
}
(制服是一个HashMap,它包含每个不同统一值名称的位置值) 我确实在验证程序是否已经过验证后立即放置了addUniform和setUniformf方法。这里有什么我想念的吗?下面是添加创建着色器,链接它们,然后添加制服的代码。
public ProgramShader(String vertexShaderLocation, String fragmentShaderLocation) {
shaderProgram = glCreateProgram();
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
vertexShaderSource = new StringBuilder();
fragmentShaderSource = new StringBuilder();
uniforms = new HashMap<String, Integer>();
for (int i = 0; i < 2; i++) {
try {
BufferedReader reader;
if (i == 0) {
reader = new BufferedReader(new FileReader(
vertexShaderLocation));
} else {
reader = new BufferedReader(new FileReader(
fragmentShaderLocation));
}
String line;
while ((line = reader.readLine()) != null) {
if (i == 0) {
vertexShaderSource.append(line + "\n");
} else {
fragmentShaderSource.append(line + "\n");
}
}
reader.close();
} catch (IOException e) {
if (i == 0) {
System.err
.println("Vertex Shader Source code was not found.");
} else {
System.err
.println("Fragment Shader Source code was not found.");
}
}
}
glShaderSource(vertexShader, vertexShaderSource);
glCompileShader(vertexShader);
if (glGetShaderi(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Vertex Shader was not compiled correctly. Error log: ");
System.err.println(glGetShaderInfoLog(vertexShader, 1024));
}
glShaderSource(fragmentShader, fragmentShaderSource);
glCompileShader(fragmentShader);
if (glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Fragment Shader was not compiled correctly. Error log: ");
System.err.println(glGetShaderInfoLog(fragmentShader, 1024));
}
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
if (glGetProgrami(shaderProgram, GL_LINK_STATUS) == GL_FALSE) {
System.err.println("Shader program wasn't linked correctly.");
System.err.println(glGetProgramInfoLog(shaderProgram, 1024));
}
glValidateProgram(shaderProgram);
addUniform("uniformFloat");
setUniformf("uniformFloat", 1.0f);
int error = glGetError();
if(error != GL_NO_ERROR){
System.out.println("ERROR: ");
System.err.println(gluErrorString(error));
}
}
出现错误,这是“无效操作”。有什么方法可以确定究竟发生了什么?
答案 0 :(得分:0)
嗯......我得到了它的工作,但我仍然不知道为什么它的工作原理。我保留了用于添加制服的代码(在链接/验证着色器程序之后)。但是,我移动了用于在glUseProgram()之后设置统一值的代码,并且正确的三角形被绘制到屏幕上。但是,我不认为在着色器程序运行时允许更改/设置统一变量。猜猜我还有更多的阅读材料。
感谢您的帮助!