问题
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:
Uniform variables in GLSL have to start with the uniform
keyword. So to fix everything, replace the first line with
uniform sampler2D myTexture;
来源:https://stackoverflow.com/questions/31617503/java-opengl-discarding-texture-background-with-shaders