0
votes

I'm trying to follow this tutorial to remove the background color of texture with a shader. https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/discard.php

Everything is running correctly, I copied the shader code directly from the tutorial, however the fragment shader is throwing several errors.

sampler2D myTexture;
           varying vec2 vTexCoord;
void main (void) 
{  
   vec4 color = texture2D(myTexture, vTexCoord); 

    if (color.rgb == vec3(1.0,0.0,0.0))
      discard; 

   gl_FragColor = color;
}

ERROR: 0:3: 'sampler2D' : samplers must be uniform
ERROR: 0:7: 'myTexture' : undeclared identifier
ERROR: 0:7: 'vTexCoord' : undeclared identifier
ERROR: 0:7: 'texture2D' : no matching overloaded function found (using implicit conversion)
ERROR: 0:7: '=' : cannot convert from 'const float' to '4-component vector of float'

I replaced the undeclared identifiers with my texture and Vec2, but they remain unidentified. I'm guessing they have to be loaded in somehow.

This is probably what is then throwing the "...no matching overloaded..." error. The other two I don't understand, the code is directly from the tutorial so there shouldn't be such problems, in theory.

My lwjgl version is 2.9.1

Here is the relevant source:

    shaderProgram = glCreateProgram();
    vertexShader  = glCreateShader(GL_VERTEX_SHADER);
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    StringBuilder vertexShaderSource   = new StringBuilder();
    StringBuilder fragmentShaderSource = new StringBuilder();

    try{
        BufferedReader reader = new BufferedReader(new FileReader("src/shader.vert"));
        String line;
        line = reader.readLine();

        while(line != null){
            line = reader.readLine();

            if(line != null){
                vertexShaderSource.append(line).append('\n');
            }
        }
        reader.close();


    } catch (IOException e) {
        System.err.println("Vertex no load.");
        Display.destroy();
        System.exit(1);
    }

    try{
        BufferedReader reader = new BufferedReader(new FileReader("src/shader.frag"));
        String line;
        //line = reader.readLine();

        while(true){
            line = reader.readLine();

            if(line == null){
                break;
            }

            fragmentShaderSource.append(line).append('\n');
        }
        reader.close();


    } catch (IOException e) {
        System.err.println("Frag no load.");
        Display.destroy();
        System.exit(1);
    }
    System.out.println(fragmentShaderSource);

    glShaderSource(vertexShader, vertexShaderSource);
    glCompileShader(vertexShader);
    if(glGetShader(vertexShader, GL_COMPILE_STATUS) == GL_FALSE){
        System.err.println("Vertex no compile");
    }
    glShaderSource(fragmentShader, fragmentShaderSource);
    glCompileShader(fragmentShader);
    if(glGetShader(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE){
        System.err.println("frag no compile");
    }

    System.out.println("Shader.frag Error Log\n" + glGetShaderInfoLog(fragmentShader, 1024));

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glValidateProgram(shaderProgram);
1
Which OpenGL version are you targeting? Basically one has to add a 'uniform' in front of the sampler2D.BDL
@BDL: That tutorial targets ARB_shading_language_100. Though even in ARB_shading_language_100 the uniform is required for samplers, perhaps the tutorial author was using an extremely permissive "GLSL" compiler.genpfault
Wow, thats an really ancient version :)BDL
@BDL: Yep. That one keyword fixed all of the errors. I don't understand /how/ but I'll take it for what it is. Put that in an answer and I'll accept.dunworray
One problem is that the shader appears to be applied to everything, and I can't 'stop using shader' by using glUseProgram(0), which is apparently what was supposed to work. Is there a way to stop the shader after its shaded the image?dunworray

1 Answers

1
votes

Uniform variables in GLSL have to start with the uniform keyword. So to fix everything, replace the first line with

uniform sampler2D myTexture;