GLSL Uniform Variable not Updating

TekkunDashi :

So I am trying to get the Color or my Triangle to change from a gradient to a solid color! I'm not quite sure whats wrong, I declared the uniform variable for gradientChange the same way I did size, and size works fine and increases the size of the triangle, but I attempt to change the gradientChange in the java code, the value changes, but the color doesn't change on the triangle. Also a question on the formatting of the GLSL code, On all the examples I see online they are able to have the declarations in a separate line for each one, but when I attempt to do so it breaks the code?


    public void display(GLAutoDrawable drawable) {
        GL4 gl = (GL4) GLContext.getCurrentGL();
        gl.glClear(GL_DEPTH_BUFFER_BIT);
        gl.glClear(GL_COLOR_BUFFER_BIT);
        gl.glUseProgram(renderingProgram);

        x+=inc;
        if(x>1.0f)inc = -0.01f;
        if(x<-1.0f)inc = 0.01f;
        int offsetLoc = gl.glGetUniformLocation(renderingProgram, "offset");
        gl.glProgramUniform1f(renderingProgram,  offsetLoc, x);

        int gradientloc = gl.glGetUniformLocation(renderingProgram, "gradientChange");
        gl.glProgramUniform1f(renderingProgram,  gradientloc, gradientChange);

        int sizeloc = gl.glGetUniformLocation(renderingProgram, "size");
        gl.glProgramUniform1f(renderingProgram,  sizeloc, size);

        gl.glDrawArrays(GL_TRIANGLES, 0, 3);



    }


this is my vertShader.glsl

#version 430
out vec4 varyingColor; uniform float offset; uniform int gradientChange; vec4 colorChange; uniform float size;
void main(void)
{   if (gl_VertexID == 0){ gl_Position = vec4(size * (0.25+offset), size * (-0.25), 0.0, 1.0); 
    colorChange = vec4( 1.0,0.0,0.0,1.0);}
    if (gl_VertexID == 1){ gl_Position = vec4(size * (-0.25+offset), size * (-0.25), 0.0, 1.0); 
        colorChange = vec4( 0.0,1.0,0.0,1.0);}
    if (gl_VertexID == 2){ gl_Position = vec4(size * (0.0+offset), size * (0.25), 0.0, 1.0);    
        colorChange = vec4( 0.0,0.0,1.0,1.0);}
    if(gradientChange == 1){varyingColor = vec4( 1.0,0.0,0.0,1.0);}else{varyingColor = colorChange;}
    }

Rabbid76 :

The type of gradientChange is int, So you have to use glProgramUniform1i rather than glProgramUniform1f to set the value of the variable:
(See glProgramUniform)

gl.glProgramUniform1f(renderingProgram, gradientloc, gradientChange);

gl.glProgramUniform1i(renderingProgram,  gradientloc, gradientChange);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=4422&siteId=1