user2316718
user2316718

Reputation: 21

GLSL Uniforms will not be set

I'm trying to understand how GLSL works, so I've been following a person on youtube: thebennnybox. I'm stuck on setting uniforms for my program. First off, here are the 2 shaders I'm currently using.

vertex shader:

#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); 
}

fragment shader:

#version 430
in vec4 color;
out vec4 fragColor;
void main(){

    fragColor = color;
}

They are both very simple, but I can't seem to set the uniformFloat variable to anything. After linking and validating both of these shaders to the shader program. I immediately try and set a value of 1.0f to uniformFloat(just for testing purposes)... and when I do this, I just get a black screen with nothing drawn on it. If I replace the uniformFloat with a 1.0f (in the shader itself), I will get exactly what I need.

The code for adding and setting the uniforms is listed below:

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);
}

(uniforms is a HashMap that holds a location value for each different uniform value names) I literally, put the addUniform and setUniformf methods right after validating the if the program has been validated. Is there anything I'm missing here? Below is the code for adding the creating the shaders, linking them, and then adding the uniforms.

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));
    }
}

There is an error that appears, it's an "invalid operation". Is there any way I can pinpoint what exactly is happening?

Upvotes: 0

Views: 2599

Answers (1)

user2316718
user2316718

Reputation: 21

Well... I got it to work, but I still don't know why it works. I kept the code for adding uniforms where it was (right after linking/validating the shader program). However, I moved the code for setting the uniform value after glUseProgram() and the the correct triangle was drawn to the screen. I didn't think uniform variables were allowed to be changed/set while the shader program was running, though. Guess I have some more reading to do.

Thanks for the help nonetheless!

Upvotes: 0

Related Questions